1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file bswap.h
23  * byte swap.
24  */
25 
26 #ifndef __BSWAP_H__
27 #define __BSWAP_H__
28 
29 #ifdef HAVE_BYTESWAP_H
30 #include <byteswap.h>
31 #else
32 
33 #undef ROCKBOX
34 #ifdef ROCKBOX
35 
36 /* rockbox' optimised inline functions */
37 #define bswap_16(x) swap16(x)
38 #define bswap_32(x) swap32(x)
39 
ByteSwap64(uint64_t x)40 static inline uint64_t ByteSwap64(uint64_t x)
41 {
42     union {
43         uint64_t ll;
44         struct {
45            uint32_t l,h;
46         } l;
47     } r;
48     r.l.l = bswap_32 (x);
49     r.l.h = bswap_32 (x>>32);
50     return r.ll;
51 }
52 #define bswap_64(x) ByteSwap64(x)
53 
54 #elif defined(ARCH_X86)
ByteSwap16(unsigned short x)55 static inline unsigned short ByteSwap16(unsigned short x)
56 {
57   __asm("xchgb %b0,%h0"	:
58         "=q" (x)	:
59         "0" (x));
60     return x;
61 }
62 #define bswap_16(x) ByteSwap16(x)
63 
ByteSwap32(unsigned int x)64 static inline unsigned int ByteSwap32(unsigned int x)
65 {
66 #if __CPU__ > 386
67  __asm("bswap	%0":
68       "=r" (x)     :
69 #else
70  __asm("xchgb	%b0,%h0\n"
71       "	rorl	$16,%0\n"
72       "	xchgb	%b0,%h0":
73       "=q" (x)		:
74 #endif
75       "0" (x));
76   return x;
77 }
78 #define bswap_32(x) ByteSwap32(x)
79 
ByteSwap64(unsigned long long int x)80 static inline unsigned long long int ByteSwap64(unsigned long long int x)
81 {
82   register union { __extension__ uint64_t __ll;
83           uint32_t __l[2]; } __x;
84   asm("xchgl	%0,%1":
85       "=r"(__x.__l[0]),"=r"(__x.__l[1]):
86       "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
87   return __x.__ll;
88 }
89 #define bswap_64(x) ByteSwap64(x)
90 
91 #elif defined(ARCH_SH4)
92 
ByteSwap16(uint16_t x)93 static inline uint16_t ByteSwap16(uint16_t x) {
94 	__asm__("swap.b %0,%0":"=r"(x):"0"(x));
95 	return x;
96 }
97 
ByteSwap32(uint32_t x)98 static inline uint32_t ByteSwap32(uint32_t x) {
99 	__asm__(
100 	"swap.b %0,%0\n"
101 	"swap.w %0,%0\n"
102 	"swap.b %0,%0\n"
103 	:"=r"(x):"0"(x));
104 	return x;
105 }
106 
107 #define bswap_16(x) ByteSwap16(x)
108 #define bswap_32(x) ByteSwap32(x)
109 
ByteSwap64(uint64_t x)110 static inline uint64_t ByteSwap64(uint64_t x)
111 {
112     union {
113         uint64_t ll;
114         struct {
115            uint32_t l,h;
116         } l;
117     } r;
118     r.l.l = bswap_32 (x);
119     r.l.h = bswap_32 (x>>32);
120     return r.ll;
121 }
122 #define bswap_64(x) ByteSwap64(x)
123 
124 #else
125 
126 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
127 
128 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
129 #define bswap_32(x) \
130      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
131       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
132 
ByteSwap64(uint64_t x)133 static inline uint64_t ByteSwap64(uint64_t x)
134 {
135     union {
136         uint64_t ll;
137         uint32_t l[2];
138     } w, r;
139     w.ll = x;
140     r.l[0] = bswap_32 (w.l[1]);
141     r.l[1] = bswap_32 (w.l[0]);
142     return r.ll;
143 }
144 #define bswap_64(x) ByteSwap64(x)
145 
146 #endif	/* !ARCH_X86 */
147 
148 #endif	/* !HAVE_BYTESWAP_H */
149 
150 // be2me ... BigEndian to MachineEndian
151 // le2me ... LittleEndian to MachineEndian
152 
153 #ifdef WORDS_BIGENDIAN
154 #define be2me_16(x) (x)
155 #define be2me_32(x) (x)
156 #define be2me_64(x) (x)
157 #define le2me_16(x) bswap_16(x)
158 #define le2me_32(x) bswap_32(x)
159 #define le2me_64(x) bswap_64(x)
160 #else
161 #define be2me_16(x) bswap_16(x)
162 #define be2me_32(x) bswap_32(x)
163 #define be2me_64(x) bswap_64(x)
164 #define le2me_16(x) (x)
165 #define le2me_32(x) (x)
166 #define le2me_64(x) (x)
167 #endif
168 
169 #endif /* __BSWAP_H__ */
170