debuggers.hg

view tools/blktap/drivers/bswap.h @ 16564:1b863ae2bf1e

blktap: cleanups for *BSD.
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 05 09:59:23 2007 +0000 (2007-12-05)
parents b1da8762f853
children
line source
1 #ifndef BSWAP_H
2 #define BSWAP_H
4 //#include "config-host.h"
6 #include <inttypes.h>
8 #if defined(__NetBSD__)
9 #include <sys/endian.h>
10 #include <sys/types.h>
11 #elif defined(__OpenBSD__)
12 #include <machine/endian.h>
13 #define bswap_16(x) swap16(x)
14 #define bswap_32(x) swap32(x)
15 #define bswap_64(x) swap64(x)
16 #else
18 #ifdef HAVE_BYTESWAP_H
19 #include <byteswap.h>
20 #else
22 #define bswap_16(x) \
23 ({ \
24 uint16_t __x = (x); \
25 ((uint16_t)( \
26 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
27 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
28 })
30 #define bswap_32(x) \
31 ({ \
32 uint32_t __x = (x); \
33 ((uint32_t)( \
34 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
35 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
36 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
37 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
38 })
40 #define bswap_64(x) \
41 ({ \
42 uint64_t __x = (x); \
43 ((uint64_t)( \
44 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
45 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
46 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
47 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
48 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
49 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
50 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
51 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
52 })
54 #endif /* !HAVE_BYTESWAP_H */
56 static inline uint16_t bswap16(uint16_t x)
57 {
58 return bswap_16(x);
59 }
61 static inline uint32_t bswap32(uint32_t x)
62 {
63 return bswap_32(x);
64 }
66 static inline uint64_t bswap64(uint64_t x)
67 {
68 return bswap_64(x);
69 }
71 static inline void bswap16s(uint16_t *s)
72 {
73 *s = bswap16(*s);
74 }
76 static inline void bswap32s(uint32_t *s)
77 {
78 *s = bswap32(*s);
79 }
81 static inline void bswap64s(uint64_t *s)
82 {
83 *s = bswap64(*s);
84 }
86 #endif
88 #if defined(WORDS_BIGENDIAN)
89 #define be_bswap(v, size) (v)
90 #define le_bswap(v, size) bswap ## size(v)
91 #define be_bswaps(v, size)
92 #define le_bswaps(p, size) *p = bswap ## size(*p);
93 #else
94 #define le_bswap(v, size) (v)
95 #define be_bswap(v, size) bswap ## size(v)
96 #define le_bswaps(v, size)
97 #define be_bswaps(p, size) *p = bswap ## size(*p);
98 #endif
100 #define CPU_CONVERT(endian, size, type)\
101 static inline type endian ## size ## _to_cpu(type v)\
102 {\
103 return endian ## _bswap(v, size);\
104 }\
105 \
106 static inline type cpu_to_ ## endian ## size(type v)\
107 {\
108 return endian ## _bswap(v, size);\
109 }\
110 \
111 static inline void endian ## size ## _to_cpus(type *p)\
112 {\
113 endian ## _bswaps(p, size)\
114 }\
115 \
116 static inline void cpu_to_ ## endian ## size ## s(type *p)\
117 {\
118 endian ## _bswaps(p, size)\
119 }\
120 \
121 static inline type endian ## size ## _to_cpup(const type *p)\
122 {\
123 return endian ## size ## _to_cpu(*p);\
124 }\
125 \
126 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
127 {\
128 *p = cpu_to_ ## endian ## size(v);\
129 }
131 CPU_CONVERT(be, 16, uint16_t)
132 CPU_CONVERT(be, 32, uint32_t)
133 CPU_CONVERT(be, 64, uint64_t)
135 CPU_CONVERT(le, 16, uint16_t)
136 CPU_CONVERT(le, 32, uint32_t)
137 CPU_CONVERT(le, 64, uint64_t)
139 /* unaligned versions (optimized for frequent unaligned accesses)*/
141 #if defined(__i386__) || defined(__powerpc__)
143 #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
144 #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
145 #define le16_to_cpupu(p) le16_to_cpup(p)
146 #define le32_to_cpupu(p) le32_to_cpup(p)
148 #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
149 #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
151 #else
153 static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
154 {
155 uint8_t *p1 = (uint8_t *)p;
157 p1[0] = v;
158 p1[1] = v >> 8;
159 }
161 static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
162 {
163 uint8_t *p1 = (uint8_t *)p;
165 p1[0] = v;
166 p1[1] = v >> 8;
167 p1[2] = v >> 16;
168 p1[3] = v >> 24;
169 }
171 static inline uint16_t le16_to_cpupu(const uint16_t *p)
172 {
173 const uint8_t *p1 = (const uint8_t *)p;
174 return p1[0] | (p1[1] << 8);
175 }
177 static inline uint32_t le32_to_cpupu(const uint32_t *p)
178 {
179 const uint8_t *p1 = (const uint8_t *)p;
180 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
181 }
183 static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
184 {
185 uint8_t *p1 = (uint8_t *)p;
187 p1[0] = v >> 8;
188 p1[1] = v;
189 }
191 static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
192 {
193 uint8_t *p1 = (uint8_t *)p;
195 p1[0] = v >> 24;
196 p1[1] = v >> 16;
197 p1[2] = v >> 8;
198 p1[3] = v;
199 }
201 #endif
203 #ifdef WORDS_BIGENDIAN
204 #define cpu_to_32wu cpu_to_be32wu
205 #else
206 #define cpu_to_32wu cpu_to_le32wu
207 #endif
209 #undef le_bswap
210 #undef be_bswap
211 #undef le_bswaps
212 #undef be_bswaps
214 #endif /* BSWAP_H */