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