root / bswap.h @ 927e3a4e
History | View | Annotate | Download (4.7 kB)
1 | ab93bbe2 | bellard | #ifndef BSWAP_H
|
---|---|---|---|
2 | ab93bbe2 | bellard | #define BSWAP_H
|
3 | ab93bbe2 | bellard | |
4 | ab93bbe2 | bellard | #include "config-host.h" |
5 | ab93bbe2 | bellard | |
6 | ab93bbe2 | bellard | #include <inttypes.h> |
7 | ab93bbe2 | bellard | |
8 | 1360677c | blueswir1 | #ifdef HAVE_MACHINE_BSWAP_H
|
9 | 1360677c | blueswir1 | #include <sys/endian.h> |
10 | 1360677c | blueswir1 | #include <sys/types.h> |
11 | 1360677c | blueswir1 | #include <machine/bswap.h> |
12 | 1360677c | blueswir1 | #else
|
13 | 1360677c | blueswir1 | |
14 | ab93bbe2 | bellard | #ifdef HAVE_BYTESWAP_H
|
15 | ab93bbe2 | bellard | #include <byteswap.h> |
16 | ab93bbe2 | bellard | #else
|
17 | ab93bbe2 | bellard | |
18 | ab93bbe2 | bellard | #define bswap_16(x) \
|
19 | ab93bbe2 | bellard | ({ \ |
20 | ab93bbe2 | bellard | uint16_t __x = (x); \ |
21 | ab93bbe2 | bellard | ((uint16_t)( \ |
22 | ab93bbe2 | bellard | (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ |
23 | ab93bbe2 | bellard | (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ |
24 | ab93bbe2 | bellard | }) |
25 | ab93bbe2 | bellard | |
26 | ab93bbe2 | bellard | #define bswap_32(x) \
|
27 | ab93bbe2 | bellard | ({ \ |
28 | ab93bbe2 | bellard | uint32_t __x = (x); \ |
29 | ab93bbe2 | bellard | ((uint32_t)( \ |
30 | ab93bbe2 | bellard | (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ |
31 | ab93bbe2 | bellard | (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ |
32 | ab93bbe2 | bellard | (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
33 | ab93bbe2 | bellard | (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ |
34 | ab93bbe2 | bellard | }) |
35 | ab93bbe2 | bellard | |
36 | ab93bbe2 | bellard | #define bswap_64(x) \
|
37 | ab93bbe2 | bellard | ({ \ |
38 | ab93bbe2 | bellard | uint64_t __x = (x); \ |
39 | ab93bbe2 | bellard | ((uint64_t)( \ |
40 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
41 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
42 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
43 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
44 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
45 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
46 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
47 | ab93bbe2 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ |
48 | ab93bbe2 | bellard | }) |
49 | ab93bbe2 | bellard | |
50 | ab93bbe2 | bellard | #endif /* !HAVE_BYTESWAP_H */ |
51 | ab93bbe2 | bellard | |
52 | ab93bbe2 | bellard | static inline uint16_t bswap16(uint16_t x) |
53 | ab93bbe2 | bellard | { |
54 | ab93bbe2 | bellard | return bswap_16(x);
|
55 | ab93bbe2 | bellard | } |
56 | ab93bbe2 | bellard | |
57 | 5fafdf24 | ths | static inline uint32_t bswap32(uint32_t x) |
58 | ab93bbe2 | bellard | { |
59 | ab93bbe2 | bellard | return bswap_32(x);
|
60 | ab93bbe2 | bellard | } |
61 | ab93bbe2 | bellard | |
62 | 5fafdf24 | ths | static inline uint64_t bswap64(uint64_t x) |
63 | ab93bbe2 | bellard | { |
64 | ab93bbe2 | bellard | return bswap_64(x);
|
65 | ab93bbe2 | bellard | } |
66 | ab93bbe2 | bellard | |
67 | 1360677c | blueswir1 | #endif /* ! HAVE_MACHINE_BSWAP_H */ |
68 | 1360677c | blueswir1 | |
69 | ab93bbe2 | bellard | static inline void bswap16s(uint16_t *s) |
70 | ab93bbe2 | bellard | { |
71 | ab93bbe2 | bellard | *s = bswap16(*s); |
72 | ab93bbe2 | bellard | } |
73 | ab93bbe2 | bellard | |
74 | ab93bbe2 | bellard | static inline void bswap32s(uint32_t *s) |
75 | ab93bbe2 | bellard | { |
76 | ab93bbe2 | bellard | *s = bswap32(*s); |
77 | ab93bbe2 | bellard | } |
78 | ab93bbe2 | bellard | |
79 | ab93bbe2 | bellard | static inline void bswap64s(uint64_t *s) |
80 | ab93bbe2 | bellard | { |
81 | ab93bbe2 | bellard | *s = bswap64(*s); |
82 | ab93bbe2 | bellard | } |
83 | ab93bbe2 | bellard | |
84 | af8ffdfd | bellard | #if defined(WORDS_BIGENDIAN)
|
85 | af8ffdfd | bellard | #define be_bswap(v, size) (v)
|
86 | af8ffdfd | bellard | #define le_bswap(v, size) bswap ## size(v) |
87 | af8ffdfd | bellard | #define be_bswaps(v, size)
|
88 | af8ffdfd | bellard | #define le_bswaps(p, size) *p = bswap ## size(*p); |
89 | af8ffdfd | bellard | #else
|
90 | af8ffdfd | bellard | #define le_bswap(v, size) (v)
|
91 | af8ffdfd | bellard | #define be_bswap(v, size) bswap ## size(v) |
92 | af8ffdfd | bellard | #define le_bswaps(v, size)
|
93 | af8ffdfd | bellard | #define be_bswaps(p, size) *p = bswap ## size(*p); |
94 | af8ffdfd | bellard | #endif
|
95 | af8ffdfd | bellard | |
96 | af8ffdfd | bellard | #define CPU_CONVERT(endian, size, type)\
|
97 | af8ffdfd | bellard | static inline type endian ## size ## _to_cpu(type v)\ |
98 | af8ffdfd | bellard | {\ |
99 | af8ffdfd | bellard | return endian ## _bswap(v, size);\ |
100 | af8ffdfd | bellard | }\ |
101 | af8ffdfd | bellard | \ |
102 | af8ffdfd | bellard | static inline type cpu_to_ ## endian ## size(type v)\ |
103 | af8ffdfd | bellard | {\ |
104 | af8ffdfd | bellard | return endian ## _bswap(v, size);\ |
105 | af8ffdfd | bellard | }\ |
106 | af8ffdfd | bellard | \ |
107 | af8ffdfd | bellard | static inline void endian ## size ## _to_cpus(type *p)\ |
108 | af8ffdfd | bellard | {\ |
109 | af8ffdfd | bellard | endian ## _bswaps(p, size)\ |
110 | af8ffdfd | bellard | }\ |
111 | af8ffdfd | bellard | \ |
112 | af8ffdfd | bellard | static inline void cpu_to_ ## endian ## size ## s(type *p)\ |
113 | af8ffdfd | bellard | {\ |
114 | af8ffdfd | bellard | endian ## _bswaps(p, size)\ |
115 | af8ffdfd | bellard | }\ |
116 | af8ffdfd | bellard | \ |
117 | af8ffdfd | bellard | static inline type endian ## size ## _to_cpup(const type *p)\ |
118 | af8ffdfd | bellard | {\ |
119 | af8ffdfd | bellard | return endian ## size ## _to_cpu(*p);\ |
120 | af8ffdfd | bellard | }\ |
121 | af8ffdfd | bellard | \ |
122 | af8ffdfd | bellard | static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\ |
123 | af8ffdfd | bellard | {\ |
124 | af8ffdfd | bellard | *p = cpu_to_ ## endian ## size(v);\ |
125 | af8ffdfd | bellard | } |
126 | af8ffdfd | bellard | |
127 | af8ffdfd | bellard | CPU_CONVERT(be, 16, uint16_t)
|
128 | af8ffdfd | bellard | CPU_CONVERT(be, 32, uint32_t)
|
129 | af8ffdfd | bellard | CPU_CONVERT(be, 64, uint64_t)
|
130 | af8ffdfd | bellard | |
131 | af8ffdfd | bellard | CPU_CONVERT(le, 16, uint16_t)
|
132 | af8ffdfd | bellard | CPU_CONVERT(le, 32, uint32_t)
|
133 | af8ffdfd | bellard | CPU_CONVERT(le, 64, uint64_t)
|
134 | af8ffdfd | bellard | |
135 | af8ffdfd | bellard | /* unaligned versions (optimized for frequent unaligned accesses)*/
|
136 | af8ffdfd | bellard | |
137 | e58ffeb3 | malc | #if defined(__i386__) || defined(_ARCH_PPC)
|
138 | af8ffdfd | bellard | |
139 | af8ffdfd | bellard | #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
|
140 | af8ffdfd | bellard | #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
|
141 | af8ffdfd | bellard | #define le16_to_cpupu(p) le16_to_cpup(p)
|
142 | af8ffdfd | bellard | #define le32_to_cpupu(p) le32_to_cpup(p)
|
143 | 88738c09 | aurel32 | #define be32_to_cpupu(p) be32_to_cpup(p)
|
144 | af8ffdfd | bellard | |
145 | 188d8579 | bellard | #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
|
146 | 188d8579 | bellard | #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
|
147 | 188d8579 | bellard | |
148 | af8ffdfd | bellard | #else
|
149 | af8ffdfd | bellard | |
150 | af8ffdfd | bellard | static inline void cpu_to_le16wu(uint16_t *p, uint16_t v) |
151 | af8ffdfd | bellard | { |
152 | af8ffdfd | bellard | uint8_t *p1 = (uint8_t *)p; |
153 | af8ffdfd | bellard | |
154 | af8ffdfd | bellard | p1[0] = v;
|
155 | af8ffdfd | bellard | p1[1] = v >> 8; |
156 | af8ffdfd | bellard | } |
157 | af8ffdfd | bellard | |
158 | af8ffdfd | bellard | static inline void cpu_to_le32wu(uint32_t *p, uint32_t v) |
159 | af8ffdfd | bellard | { |
160 | af8ffdfd | bellard | uint8_t *p1 = (uint8_t *)p; |
161 | af8ffdfd | bellard | |
162 | af8ffdfd | bellard | p1[0] = v;
|
163 | af8ffdfd | bellard | p1[1] = v >> 8; |
164 | af8ffdfd | bellard | p1[2] = v >> 16; |
165 | af8ffdfd | bellard | p1[3] = v >> 24; |
166 | af8ffdfd | bellard | } |
167 | af8ffdfd | bellard | |
168 | af8ffdfd | bellard | static inline uint16_t le16_to_cpupu(const uint16_t *p) |
169 | af8ffdfd | bellard | { |
170 | af8ffdfd | bellard | const uint8_t *p1 = (const uint8_t *)p; |
171 | af8ffdfd | bellard | return p1[0] | (p1[1] << 8); |
172 | af8ffdfd | bellard | } |
173 | af8ffdfd | bellard | |
174 | af8ffdfd | bellard | static inline uint32_t le32_to_cpupu(const uint32_t *p) |
175 | af8ffdfd | bellard | { |
176 | af8ffdfd | bellard | const uint8_t *p1 = (const uint8_t *)p; |
177 | af8ffdfd | bellard | return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24); |
178 | af8ffdfd | bellard | } |
179 | af8ffdfd | bellard | |
180 | 88738c09 | aurel32 | static inline uint32_t be32_to_cpupu(const uint32_t *p) |
181 | 88738c09 | aurel32 | { |
182 | 88738c09 | aurel32 | const uint8_t *p1 = (const uint8_t *)p; |
183 | 88738c09 | aurel32 | return p1[3] | (p1[2] << 8) | (p1[1] << 16) | (p1[0] << 24); |
184 | 88738c09 | aurel32 | } |
185 | 88738c09 | aurel32 | |
186 | 188d8579 | bellard | static inline void cpu_to_be16wu(uint16_t *p, uint16_t v) |
187 | 188d8579 | bellard | { |
188 | 188d8579 | bellard | uint8_t *p1 = (uint8_t *)p; |
189 | 188d8579 | bellard | |
190 | 188d8579 | bellard | p1[0] = v >> 8; |
191 | 188d8579 | bellard | p1[1] = v;
|
192 | 188d8579 | bellard | } |
193 | 188d8579 | bellard | |
194 | 188d8579 | bellard | static inline void cpu_to_be32wu(uint32_t *p, uint32_t v) |
195 | 188d8579 | bellard | { |
196 | 188d8579 | bellard | uint8_t *p1 = (uint8_t *)p; |
197 | 188d8579 | bellard | |
198 | 188d8579 | bellard | p1[0] = v >> 24; |
199 | 188d8579 | bellard | p1[1] = v >> 16; |
200 | 188d8579 | bellard | p1[2] = v >> 8; |
201 | 188d8579 | bellard | p1[3] = v;
|
202 | 188d8579 | bellard | } |
203 | 188d8579 | bellard | |
204 | 188d8579 | bellard | #endif
|
205 | 188d8579 | bellard | |
206 | 188d8579 | bellard | #ifdef WORDS_BIGENDIAN
|
207 | 188d8579 | bellard | #define cpu_to_32wu cpu_to_be32wu
|
208 | 188d8579 | bellard | #else
|
209 | 188d8579 | bellard | #define cpu_to_32wu cpu_to_le32wu
|
210 | af8ffdfd | bellard | #endif
|
211 | af8ffdfd | bellard | |
212 | af8ffdfd | bellard | #undef le_bswap
|
213 | af8ffdfd | bellard | #undef be_bswap
|
214 | af8ffdfd | bellard | #undef le_bswaps
|
215 | af8ffdfd | bellard | #undef be_bswaps
|
216 | af8ffdfd | bellard | |
217 | ab93bbe2 | bellard | #endif /* BSWAP_H */ |