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