Beam-lib  2.16.3
This is the Beam C++ class library.
BEndian.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BEndian.h Byte swap fubctions
3  * T.Barnaby, BEAM Ltd, 2009-12-18
4  * Copyright (c) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  */
7 #ifndef BEndian_H
8 #define BEndian_H 1
9 
10 #include <BTypes.h>
11 
12 #if TARGET_win32 || TARGET_win64
13 #define __BYTE_ORDER __LITTLE_ENDIAN
14 inline uint16_t __bswap_16(uint16_t v){
15  uint16_t r;
16  const char* sp = (const char*)&v;
17  char* dp = (char*)&r;
18 
19  dp[1] = sp[0];
20  dp[0] = sp[1];
21 
22  return r;
23 }
24 inline uint32_t __bswap_32(uint32_t v){
25  uint32_t r;
26  const char* sp = (const char*)&v;
27  char* dp = (char*)&r;
28 
29  dp[3] = sp[0];
30  dp[2] = sp[1];
31  dp[1] = sp[2];
32  dp[0] = sp[3];
33 
34  return r;
35 }
36 inline uint64_t __bswap_64(uint64_t v){
37  uint64_t r;
38  const char* sp = (const char*)&v;
39  char* dp = (char*)&r;
40 
41  dp[7] = sp[0];
42  dp[6] = sp[1];
43  dp[5] = sp[2];
44  dp[4] = sp[3];
45  dp[3] = sp[4];
46  dp[2] = sp[5];
47  dp[1] = sp[6];
48  dp[0] = sp[7];
49 
50  return r;
51 }
52 #else
53 #include <byteswap.h>
54 #endif
55 
56 
57 #ifndef htobe16
58 # if __BYTE_ORDER == __LITTLE_ENDIAN
59 # define htobe16(x) __bswap_16 (x)
60 # define htole16(x) (x)
61 # define be16toh(x) __bswap_16 (x)
62 # define le16toh(x) (x)
63 
64 # define htobe32(x) __bswap_32 (x)
65 # define htole32(x) (x)
66 # define be32toh(x) __bswap_32 (x)
67 # define le32toh(x) (x)
68 
69 # define htobe64(x) __bswap_64 (x)
70 # define htole64(x) (x)
71 # define be64toh(x) __bswap_64 (x)
72 # define le64toh(x) (x)
73 # else
74 # define htobe16(x) (x)
75 # define htole16(x) __bswap_16 (x)
76 # define be16toh(x) (x)
77 # define le16toh(x) __bswap_16 (x)
78 
79 # define htobe32(x) (x)
80 # define htole32(x) __bswap_32 (x)
81 # define be32toh(x) (x)
82 # define le32toh(x) __bswap_32 (x)
83 
84 # define htobe64(x) (x)
85 # define htole64(x) __bswap_64 (x)
86 # define be64toh(x) (x)
87 # define le64toh(x) __bswap_64 (x)
88 # endif
89 #endif
90 
91 inline void bswap_p8(const void* s, void* d){
92  const char* sp = (const char*)s;
93  char* dp = (char*)d;
94 
95  *dp = *sp;
96 }
97 
98 inline void bswap_p16(const void* s, void* d){
99  const char* sp = (const char*)s;
100  char* dp = (char*)d;
101 
102  dp[1] = sp[0];
103  dp[0] = sp[1];
104 }
105 
106 inline void bswap_p32(const void* s, void* d){
107  const char* sp = (const char*)s;
108  char* dp = (char*)d;
109 
110  dp[3] = sp[0];
111  dp[2] = sp[1];
112  dp[1] = sp[2];
113  dp[0] = sp[3];
114 }
115 
116 inline void bswap_p64(const void* s, void* d){
117  const char* sp = (const char*)s;
118  char* dp = (char*)d;
119 
120  dp[7] = sp[0];
121  dp[6] = sp[1];
122  dp[5] = sp[2];
123  dp[4] = sp[3];
124  dp[3] = sp[4];
125  dp[2] = sp[5];
126  dp[1] = sp[6];
127  dp[0] = sp[7];
128 }
129 
130 void bswap_copy(int swap, const void* src, void* dst, BUInt32 nBytes, const char* swapType);
131 
132 
133 inline uint16_t htole(uint16_t v){
134  return htole16(v);
135 }
136 
137 inline int16_t htole(int16_t v){
138  return htole16(v);
139 }
140 
141 inline uint32_t htole(uint32_t v){
142  return htole32(v);
143 }
144 
145 inline int32_t htole(int32_t v){
146  return htole32(v);
147 }
148 
149 inline uint64_t htole(uint64_t v){
150  return htole64(v);
151 }
152 
153 inline int64_t htole(int64_t v){
154  return htole64(v);
155 }
156 
157 inline double htole(double v){
158  union {
159  int64_t i;
160  double d;
161  } t;
162 
163  t.d = v;
164  t.i = htole64(t.i);
165 
166  return t.d;
167 }
168 
169 inline float htole(float v){
170  union {
171  int32_t i;
172  float d;
173  } t;
174 
175  t.d = v;
176  t.i = htole32(t.i);
177 
178  return t.d;
179 }
180 
181 inline uint16_t htobe(uint16_t v){
182  return htobe16(v);
183 }
184 
185 inline int16_t htobe(int16_t v){
186  return htobe16(v);
187 }
188 
189 inline uint32_t htobe(uint32_t v){
190  return htobe32(v);
191 }
192 
193 inline int32_t htobe(int32_t v){
194  return htobe32(v);
195 }
196 
197 inline uint64_t htobe(uint64_t v){
198  return htobe64(v);
199 }
200 
201 inline int64_t htobe(int64_t v){
202  return htobe64(v);
203 }
204 
205 inline double htobe(double v){
206  union {
207  int64_t i;
208  double d;
209  } t;
210 
211  t.d = v;
212  t.i = htobe64(t.i);
213 
214  return t.d;
215 }
216 
217 inline float htobe(float v){
218  union {
219  int32_t i;
220  float d;
221  } t;
222 
223  t.d = v;
224  t.i = htobe32(t.i);
225 
226  return t.d;
227 }
228 
229 
230 inline uint16_t letoh(uint16_t v){
231  return le16toh(v);
232 }
233 
234 inline int16_t letoh(int16_t v){
235  return le16toh(v);
236 }
237 
238 inline uint32_t letoh(uint32_t v){
239  return le32toh(v);
240 }
241 
242 inline int32_t letoh(int32_t v){
243  return le32toh(v);
244 }
245 
246 inline uint64_t letoh(uint64_t v){
247  return le64toh(v);
248 }
249 
250 inline int64_t letoh(int64_t v){
251  return le64toh(v);
252 }
253 
254 inline double letoh(double v){
255  union {
256  int64_t i;
257  double d;
258  } t;
259 
260  t.d = v;
261  t.i = le64toh(t.i);
262 
263  return t.d;
264 }
265 
266 inline float letoh(float v){
267  union {
268  int32_t i;
269  float d;
270  } t;
271 
272  t.d = v;
273  t.i = le32toh(t.i);
274 
275  return t.d;
276 }
277 
278 
279 inline uint16_t betoh(uint16_t v){
280  return be16toh(v);
281 }
282 
283 inline int16_t betoh(int16_t v){
284  return be16toh(v);
285 }
286 
287 inline uint32_t betoh(uint32_t v){
288  return be32toh(v);
289 }
290 
291 inline int32_t betoh(int32_t v){
292  return be32toh(v);
293 }
294 
295 inline uint64_t betoh(uint64_t v){
296  return be64toh(v);
297 }
298 
299 inline int64_t betoh(int64_t v){
300  return be64toh(v);
301 }
302 
303 inline double betoh(double v){
304  union {
305  int64_t i;
306  double d;
307  } t;
308 
309  t.d = v;
310  t.i = be64toh(t.i);
311 
312  return t.d;
313 }
314 
315 inline float betoh(float v){
316  union {
317  int32_t i;
318  float d;
319  } t;
320 
321  t.d = v;
322  t.i = be32toh(t.i);
323 
324  return t.d;
325 }
326 
327 #endif
#define htobe16(x)
Definition: BEndian.h:59
uint16_t htole(uint16_t v)
Definition: BEndian.h:133
void bswap_p8(const void *s, void *d)
Definition: BEndian.h:91
uint16_t betoh(uint16_t v)
Definition: BEndian.h:279
#define le16toh(x)
Definition: BEndian.h:62
#define le64toh(x)
Definition: BEndian.h:72
#define le32toh(x)
Definition: BEndian.h:67
void bswap_copy(int swap, const void *src, void *dst, BUInt32 nBytes, const char *swapType)
Definition: BEndian.cpp:9
#define htole64(x)
Definition: BEndian.h:70
uint32_t BUInt32
Definition: BTypes.h:21
#define htobe64(x)
Definition: BEndian.h:69
#define be32toh(x)
Definition: BEndian.h:66
#define be64toh(x)
Definition: BEndian.h:71
#define htobe32(x)
Definition: BEndian.h:64
void bswap_p32(const void *s, void *d)
Definition: BEndian.h:106
#define htole32(x)
Definition: BEndian.h:65
#define be16toh(x)
Definition: BEndian.h:61
void bswap_p16(const void *s, void *d)
Definition: BEndian.h:98
uint16_t htobe(uint16_t v)
Definition: BEndian.h:181
void bswap_p64(const void *s, void *d)
Definition: BEndian.h:116
#define htole16(x)
Definition: BEndian.h:60
uint16_t letoh(uint16_t v)
Definition: BEndian.h:230