1 /**
2 * @file bswap.h
3 * byte swap.
4 */
5
6 #ifndef __BSWAP_H__
7 #define __BSWAP_H__
8
9 #ifdef __FreeBSD__
10 #include <sys/endian.h>
11 #else
12 #include <endian.h>
13 #endif
14 #if __BYTE_ORDER == __BIG_ENDIAN
15 #define WORDS_BIGENDIAN
16 #endif
17
18 #ifdef HAVE_BYTESWAP_H
19 #include <byteswap.h>
20 #else
21
22 #ifdef ARCH_X86
ByteSwap16(unsigned short x)23 static inline unsigned short ByteSwap16(unsigned short x)
24 {
25 __asm("xchgb %b0,%h0" :
26 "=q" (x) :
27 "0" (x));
28 return x;
29 }
30 #define bswap_16(x) ByteSwap16(x)
31
ByteSwap32(unsigned int x)32 static inline unsigned int ByteSwap32(unsigned int x)
33 {
34 #if __CPU__ > 386
35 __asm("bswap %0":
36 "=r" (x) :
37 #else
38 __asm("xchgb %b0,%h0\n"
39 " rorl $16,%0\n"
40 " xchgb %b0,%h0":
41 "=q" (x) :
42 #endif
43 "0" (x));
44 return x;
45 }
46 #define bswap_32(x) ByteSwap32(x)
47
ByteSwap64(unsigned long long int x)48 static inline unsigned long long int ByteSwap64(unsigned long long int x)
49 {
50 register union { __extension__ uint64_t __ll;
51 uint32_t __l[2]; } __x;
52 asm("xchgl %0,%1":
53 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
54 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
55 return __x.__ll;
56 }
57 #define bswap_64(x) ByteSwap64(x)
58
59 #elif defined(ARCH_SH4)
60
ByteSwap16(uint16_t x)61 static inline uint16_t ByteSwap16(uint16_t x) {
62 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
63 return x;
64 }
65
ByteSwap32(uint32_t x)66 static inline uint32_t ByteSwap32(uint32_t x) {
67 __asm__(
68 "swap.b %0,%0\n"
69 "swap.w %0,%0\n"
70 "swap.b %0,%0\n"
71 :"=r"(x):"0"(x));
72 return x;
73 }
74
75 #define bswap_16(x) ByteSwap16(x)
76 #define bswap_32(x) ByteSwap32(x)
77
ByteSwap64(uint64_t x)78 static inline uint64_t ByteSwap64(uint64_t x)
79 {
80 union {
81 uint64_t ll;
82 struct {
83 uint32_t l,h;
84 } l;
85 } r;
86 r.l.l = bswap_32 (x);
87 r.l.h = bswap_32 (x>>32);
88 return r.ll;
89 }
90 #define bswap_64(x) ByteSwap64(x)
91
92 #else
93
94 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
95
96
97 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
98 #define bswap_32(x) \
99 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
100 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
101
ByteSwap64(uint64_t x)102 static inline uint64_t ByteSwap64(uint64_t x)
103 {
104 union {
105 uint64_t ll;
106 uint32_t l[2];
107 } w, r;
108 w.ll = x;
109 r.l[0] = bswap_32 (w.l[1]);
110 r.l[1] = bswap_32 (w.l[0]);
111 return r.ll;
112 }
113 #define bswap_64(x) ByteSwap64(x)
114
115 #endif /* !ARCH_X86 */
116
117 #endif /* !HAVE_BYTESWAP_H */
118
119 // be2me ... BigEndian to MachineEndian
120 // le2me ... LittleEndian to MachineEndian
121
122 #ifdef WORDS_BIGENDIAN
123 #define be2me_16(x) (x)
124 #define be2me_32(x) (x)
125 #define be2me_64(x) (x)
126 #define le2me_16(x) bswap_16(x)
127 #define le2me_32(x) bswap_32(x)
128 #define le2me_64(x) bswap_64(x)
129 #else
130 #define be2me_16(x) bswap_16(x)
131 #define be2me_32(x) bswap_32(x)
132 #define be2me_64(x) bswap_64(x)
133 #define le2me_16(x) (x)
134 #define le2me_32(x) (x)
135 #define le2me_64(x) (x)
136 #endif
137
138 #endif /* __BSWAP_H__ */
139