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