blob: 45587042907ed4bc7160474a121d11210c979e67 [file] [log] [blame]
bellardab93bbe2003-08-10 21:35:13 +00001#ifndef BSWAP_H
2#define BSWAP_H
3
4#include "config-host.h"
5
6#include <inttypes.h>
7
Juan Quintela57351472009-07-27 16:13:01 +02008#ifdef CONFIG_MACHINE_BSWAP_H
blueswir113606772008-12-05 17:54:09 +00009#include <sys/endian.h>
10#include <sys/types.h>
11#include <machine/bswap.h>
12#else
13
Juan Quintela936dfb82009-07-27 16:13:03 +020014#ifdef CONFIG_BYTESWAP_H
bellardab93bbe2003-08-10 21:35:13 +000015#include <byteswap.h>
16#else
17
18#define bswap_16(x) \
19({ \
20 uint16_t __x = (x); \
21 ((uint16_t)( \
22 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
23 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
24})
25
26#define bswap_32(x) \
27({ \
28 uint32_t __x = (x); \
29 ((uint32_t)( \
30 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
31 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
32 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
33 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
34})
35
36#define bswap_64(x) \
37({ \
38 uint64_t __x = (x); \
39 ((uint64_t)( \
40 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
41 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
42 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
43 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
44 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
45 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
46 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
47 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
48})
49
Juan Quintela936dfb82009-07-27 16:13:03 +020050#endif /* !CONFIG_BYTESWAP_H */
bellardab93bbe2003-08-10 21:35:13 +000051
bellardab93bbe2003-08-10 21:35:13 +000052static inline uint16_t bswap16(uint16_t x)
53{
54 return bswap_16(x);
55}
56
ths5fafdf22007-09-16 21:08:06 +000057static inline uint32_t bswap32(uint32_t x)
bellardab93bbe2003-08-10 21:35:13 +000058{
59 return bswap_32(x);
60}
61
ths5fafdf22007-09-16 21:08:06 +000062static inline uint64_t bswap64(uint64_t x)
bellardab93bbe2003-08-10 21:35:13 +000063{
64 return bswap_64(x);
65}
66
Juan Quintela57351472009-07-27 16:13:01 +020067#endif /* ! CONFIG_MACHINE_BSWAP_H */
blueswir113606772008-12-05 17:54:09 +000068
bellardab93bbe2003-08-10 21:35:13 +000069static inline void bswap16s(uint16_t *s)
70{
71 *s = bswap16(*s);
72}
73
74static inline void bswap32s(uint32_t *s)
75{
76 *s = bswap32(*s);
77}
78
79static inline void bswap64s(uint64_t *s)
80{
81 *s = bswap64(*s);
82}
83
Juan Quintelae2542fe2009-07-27 16:13:06 +020084#if defined(HOST_WORDS_BIGENDIAN)
bellardaf8ffdf2004-08-01 21:48:12 +000085#define be_bswap(v, size) (v)
86#define le_bswap(v, size) bswap ## size(v)
87#define be_bswaps(v, size)
88#define le_bswaps(p, size) *p = bswap ## size(*p);
89#else
90#define le_bswap(v, size) (v)
91#define be_bswap(v, size) bswap ## size(v)
92#define le_bswaps(v, size)
93#define be_bswaps(p, size) *p = bswap ## size(*p);
94#endif
95
96#define CPU_CONVERT(endian, size, type)\
97static inline type endian ## size ## _to_cpu(type v)\
98{\
99 return endian ## _bswap(v, size);\
100}\
101\
102static inline type cpu_to_ ## endian ## size(type v)\
103{\
104 return endian ## _bswap(v, size);\
105}\
106\
107static inline void endian ## size ## _to_cpus(type *p)\
108{\
109 endian ## _bswaps(p, size)\
110}\
111\
112static inline void cpu_to_ ## endian ## size ## s(type *p)\
113{\
114 endian ## _bswaps(p, size)\
115}\
116\
117static inline type endian ## size ## _to_cpup(const type *p)\
118{\
119 return endian ## size ## _to_cpu(*p);\
120}\
121\
122static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
123{\
124 *p = cpu_to_ ## endian ## size(v);\
125}
126
127CPU_CONVERT(be, 16, uint16_t)
128CPU_CONVERT(be, 32, uint32_t)
129CPU_CONVERT(be, 64, uint64_t)
130
131CPU_CONVERT(le, 16, uint16_t)
132CPU_CONVERT(le, 32, uint32_t)
133CPU_CONVERT(le, 64, uint64_t)
134
135/* unaligned versions (optimized for frequent unaligned accesses)*/
136
malce58ffeb2009-01-14 18:39:49 +0000137#if defined(__i386__) || defined(_ARCH_PPC)
bellardaf8ffdf2004-08-01 21:48:12 +0000138
139#define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
140#define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
141#define le16_to_cpupu(p) le16_to_cpup(p)
142#define le32_to_cpupu(p) le32_to_cpup(p)
aurel3288738c02008-03-28 22:30:48 +0000143#define be32_to_cpupu(p) be32_to_cpup(p)
bellardaf8ffdf2004-08-01 21:48:12 +0000144
bellard188d8572004-10-10 15:44:19 +0000145#define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
146#define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
147
bellardaf8ffdf2004-08-01 21:48:12 +0000148#else
149
150static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
151{
152 uint8_t *p1 = (uint8_t *)p;
153
blueswir19e622b12009-03-07 15:46:23 +0000154 p1[0] = v & 0xff;
bellardaf8ffdf2004-08-01 21:48:12 +0000155 p1[1] = v >> 8;
156}
157
158static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
159{
160 uint8_t *p1 = (uint8_t *)p;
161
blueswir19e622b12009-03-07 15:46:23 +0000162 p1[0] = v & 0xff;
bellardaf8ffdf2004-08-01 21:48:12 +0000163 p1[1] = v >> 8;
164 p1[2] = v >> 16;
165 p1[3] = v >> 24;
166}
167
168static inline uint16_t le16_to_cpupu(const uint16_t *p)
169{
170 const uint8_t *p1 = (const uint8_t *)p;
171 return p1[0] | (p1[1] << 8);
172}
173
174static inline uint32_t le32_to_cpupu(const uint32_t *p)
175{
176 const uint8_t *p1 = (const uint8_t *)p;
177 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
178}
179
aurel3288738c02008-03-28 22:30:48 +0000180static inline uint32_t be32_to_cpupu(const uint32_t *p)
181{
182 const uint8_t *p1 = (const uint8_t *)p;
183 return p1[3] | (p1[2] << 8) | (p1[1] << 16) | (p1[0] << 24);
184}
185
bellard188d8572004-10-10 15:44:19 +0000186static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
187{
188 uint8_t *p1 = (uint8_t *)p;
189
190 p1[0] = v >> 8;
blueswir19e622b12009-03-07 15:46:23 +0000191 p1[1] = v & 0xff;
bellard188d8572004-10-10 15:44:19 +0000192}
193
194static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
195{
196 uint8_t *p1 = (uint8_t *)p;
197
198 p1[0] = v >> 24;
199 p1[1] = v >> 16;
200 p1[2] = v >> 8;
blueswir19e622b12009-03-07 15:46:23 +0000201 p1[3] = v & 0xff;
bellard188d8572004-10-10 15:44:19 +0000202}
203
204#endif
205
Juan Quintelae2542fe2009-07-27 16:13:06 +0200206#ifdef HOST_WORDS_BIGENDIAN
bellard188d8572004-10-10 15:44:19 +0000207#define cpu_to_32wu cpu_to_be32wu
208#else
209#define cpu_to_32wu cpu_to_le32wu
bellardaf8ffdf2004-08-01 21:48:12 +0000210#endif
211
212#undef le_bswap
213#undef be_bswap
214#undef le_bswaps
215#undef be_bswaps
216
bellardab93bbe2003-08-10 21:35:13 +0000217#endif /* BSWAP_H */