1423
|
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
|
10481
|
11 static inline unsigned short ByteSwap16(unsigned short x)
|
1423
|
12 {
|
|
13 __asm("xchgb %b0,%h0" :
|
|
14 "=q" (x) :
|
|
15 "0" (x));
|
|
16 return x;
|
|
17 }
|
|
18 #define bswap_16(x) ByteSwap16(x)
|
|
19
|
10481
|
20 static inline unsigned int ByteSwap32(unsigned int x)
|
1423
|
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
|
10481
|
36 static inline unsigned long long int ByteSwap64(unsigned long long int x)
|
1423
|
37 {
|
10481
|
38 register union { __extension__ uint64_t __ll;
|
|
39 uint32_t __l[2]; } __x;
|
1423
|
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
|
10481
|
47 #elif defined(ARCH_SH4)
|
|
48
|
|
49 static inline uint16_t ByteSwap16(uint16_t x) {
|
|
50 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
|
|
51 return x;
|
|
52 }
|
|
53
|
|
54 static inline uint32_t ByteSwap32(uint32_t x) {
|
|
55 __asm__(
|
|
56 "swap.b %0,%0\n"
|
|
57 "swap.w %0,%0\n"
|
|
58 "swap.b %0,%0\n"
|
|
59 :"=r"(x):"0"(x));
|
|
60 return x;
|
|
61 }
|
|
62
|
|
63 #define bswap_16(x) ByteSwap16(x)
|
|
64 #define bswap_32(x) ByteSwap32(x)
|
|
65
|
|
66 static inline uint64_t ByteSwap64(uint64_t x)
|
|
67 {
|
|
68 union {
|
|
69 uint64_t ll;
|
|
70 struct {
|
|
71 uint32_t l,h;
|
|
72 } l;
|
|
73 } r;
|
|
74 r.l.l = bswap_32 (x);
|
|
75 r.l.h = bswap_32 (x>>32);
|
|
76 return r.ll;
|
|
77 }
|
|
78 #define bswap_64(x) ByteSwap64(x)
|
|
79
|
1423
|
80 #else
|
|
81
|
|
82 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
|
|
83
|
|
84
|
|
85 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
|
|
86 #define bswap_32(x) \
|
|
87 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
|
|
88 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
|
|
89
|
10481
|
90 static inline uint64_t ByteSwap64(uint64_t x)
|
|
91 {
|
|
92 union {
|
|
93 uint64_t ll;
|
|
94 uint32_t l[2];
|
|
95 } w, r;
|
|
96 w.ll = x;
|
|
97 r.l[0] = bswap_32 (w.l[1]);
|
|
98 r.l[1] = bswap_32 (w.l[0]);
|
|
99 return r.ll;
|
|
100 }
|
|
101 #define bswap_64(x) ByteSwap64(x)
|
|
102
|
1423
|
103 #endif /* !ARCH_X86 */
|
|
104
|
|
105 #endif /* !HAVE_BYTESWAP_H */
|
|
106
|
|
107 // be2me ... BigEndian to MachineEndian
|
|
108 // le2me ... LittleEndian to MachineEndian
|
|
109
|
|
110 #ifdef WORDS_BIGENDIAN
|
|
111 #define be2me_16(x) (x)
|
|
112 #define be2me_32(x) (x)
|
|
113 #define be2me_64(x) (x)
|
|
114 #define le2me_16(x) bswap_16(x)
|
|
115 #define le2me_32(x) bswap_32(x)
|
|
116 #define le2me_64(x) bswap_64(x)
|
|
117 #else
|
|
118 #define be2me_16(x) bswap_16(x)
|
|
119 #define be2me_32(x) bswap_32(x)
|
|
120 #define be2me_64(x) bswap_64(x)
|
|
121 #define le2me_16(x) (x)
|
|
122 #define le2me_32(x) (x)
|
|
123 #define le2me_64(x) (x)
|
|
124 #endif
|
|
125
|
10481
|
126 #endif /* __BSWAP_H__ */
|