untested generic bswap64
[libav.git] / libavcodec / bswap.h
1 #ifndef __BSWAP_H__
2 #define __BSWAP_H__
3
4 #ifdef HAVE_BYTESWAP_H
5 #include <byteswap.h>
6 #else
7
8 #include <inttypes.h> /* for __WORDSIZE */
9 #ifdef ARCH_X86
10 inline static unsigned short ByteSwap16(unsigned short x)
11 {
12 __asm("xchgb %b0,%h0" :
13 "=q" (x) :
14 "0" (x));
15 return x;
16 }
17 #define bswap_16(x) ByteSwap16(x)
18
19 inline static unsigned int ByteSwap32(unsigned int x)
20 {
21 #if __CPU__ > 386
22 __asm("bswap %0":
23 "=r" (x) :
24 #else
25 __asm("xchgb %b0,%h0\n"
26 " rorl $16,%0\n"
27 " xchgb %b0,%h0":
28 "=q" (x) :
29 #endif
30 "0" (x));
31 return x;
32 }
33 #define bswap_32(x) ByteSwap32(x)
34
35 inline static unsigned long long int ByteSwap64(unsigned long long int x)
36 {
37 register union { __extension__ unsigned long long int __ll;
38 unsigned long int __l[2]; } __x;
39 asm("xchgl %0,%1":
40 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
41 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
42 return __x.__ll;
43 }
44 #define bswap_64(x) ByteSwap64(x)
45
46 #else
47
48 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
49
50
51 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
52 #define bswap_32(x) \
53 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
54 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
55
56 #if __WORDSIZE >= 64
57 # define bswap_64(x) \
58 ((((x) & 0xff00000000000000ull) >> 56) \
59 | (((x) & 0x00ff000000000000ull) >> 40) \
60 | (((x) & 0x0000ff0000000000ull) >> 24) \
61 | (((x) & 0x000000ff00000000ull) >> 8) \
62 | (((x) & 0x00000000ff000000ull) << 8) \
63 | (((x) & 0x0000000000ff0000ull) << 24) \
64 | (((x) & 0x000000000000ff00ull) << 40) \
65 | (((x) & 0x00000000000000ffull) << 56))
66 #else
67 #define bswap_64(x) \
68 (__extension__ \
69 ({ union { __extension__ unsigned long long int __ll; \
70 unsigned long int __l[2]; } __w, __r; \
71 __w.__ll = (x); \
72 __r.__l[0] = bswap_32 (__w.__l[1]); \
73 __r.__l[1] = bswap_32 (__w.__l[0]); \
74 __r.__ll; }))
75 #endif
76 #endif /* !ARCH_X86 */
77
78 #endif /* !HAVE_BYTESWAP_H */
79
80 // be2me ... BigEndian to MachineEndian
81 // le2me ... LittleEndian to MachineEndian
82
83 #ifdef WORDS_BIGENDIAN
84 #define be2me_16(x) (x)
85 #define be2me_32(x) (x)
86 #define be2me_64(x) (x)
87 #define le2me_16(x) bswap_16(x)
88 #define le2me_32(x) bswap_32(x)
89 #define le2me_64(x) bswap_64(x)
90 #else
91 #define be2me_16(x) bswap_16(x)
92 #define be2me_32(x) bswap_32(x)
93 #define be2me_64(x) bswap_64(x)
94 #define le2me_16(x) (x)
95 #define le2me_32(x) (x)
96 #define le2me_64(x) (x)
97 #endif
98
99 #endif /* __BSWAP_H__ */