CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/wl/include/bcmendian.h
Views: 3959
1
/*
2
* Byte order utilities
3
*
4
* $Copyright Open Broadcom Corporation$
5
*
6
* $Id: bcmendian.h 370210 2012-11-21 05:35:27Z nisar $
7
*
8
* This file by default provides proper behavior on little-endian architectures.
9
* On big-endian architectures, IL_BIGENDIAN should be defined.
10
*/
11
12
#ifndef _BCMENDIAN_H_
13
#define _BCMENDIAN_H_
14
15
#include <typedefs.h>
16
17
/* Reverse the bytes in a 16-bit value */
18
#define BCMSWAP16(val) \
19
((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
20
(((uint16)(val) & (uint16)0xff00U) >> 8)))
21
22
/* Reverse the bytes in a 32-bit value */
23
#define BCMSWAP32(val) \
24
((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
25
(((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
26
(((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
27
(((uint32)(val) & (uint32)0xff000000U) >> 24)))
28
29
/* Reverse the two 16-bit halves of a 32-bit value */
30
#define BCMSWAP32BY16(val) \
31
((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
32
(((uint32)(val) & (uint32)0xffff0000U) >> 16)))
33
34
/* Reverse the bytes in a 64-bit value */
35
#define BCMSWAP64(val) \
36
((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \
37
(((uint64)(val) & 0x000000000000ff00ULL) << 40) | \
38
(((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \
39
(((uint64)(val) & 0x00000000ff000000ULL) << 8) | \
40
(((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \
41
(((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \
42
(((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \
43
(((uint64)(val) & 0xff00000000000000ULL) >> 56)))
44
45
/* Reverse the two 32-bit halves of a 64-bit value */
46
#define BCMSWAP64BY32(val) \
47
((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \
48
(((uint64)(val) & 0xffffffff00000000ULL) >> 32)))
49
50
51
/* Byte swapping macros
52
* Host <=> Network (Big Endian) for 16- and 32-bit values
53
* Host <=> Little-Endian for 16- and 32-bit values
54
*/
55
#ifndef hton16
56
#ifndef IL_BIGENDIAN
57
#define HTON16(i) BCMSWAP16(i)
58
#define hton16(i) bcmswap16(i)
59
#define HTON32(i) BCMSWAP32(i)
60
#define hton32(i) bcmswap32(i)
61
#define NTOH16(i) BCMSWAP16(i)
62
#define ntoh16(i) bcmswap16(i)
63
#define NTOH32(i) BCMSWAP32(i)
64
#define ntoh32(i) bcmswap32(i)
65
#define LTOH16(i) (i)
66
#define ltoh16(i) (i)
67
#define LTOH32(i) (i)
68
#define ltoh32(i) (i)
69
#define HTOL16(i) (i)
70
#define htol16(i) (i)
71
#define HTOL32(i) (i)
72
#define htol32(i) (i)
73
#define HTOL64(i) (i)
74
#define htol64(i) (i)
75
#else /* IL_BIGENDIAN */
76
#define HTON16(i) (i)
77
#define hton16(i) (i)
78
#define HTON32(i) (i)
79
#define hton32(i) (i)
80
#define NTOH16(i) (i)
81
#define ntoh16(i) (i)
82
#define NTOH32(i) (i)
83
#define ntoh32(i) (i)
84
#define LTOH16(i) BCMSWAP16(i)
85
#define ltoh16(i) bcmswap16(i)
86
#define LTOH32(i) BCMSWAP32(i)
87
#define ltoh32(i) bcmswap32(i)
88
#define HTOL16(i) BCMSWAP16(i)
89
#define htol16(i) bcmswap16(i)
90
#define HTOL32(i) BCMSWAP32(i)
91
#define htol32(i) bcmswap32(i)
92
#define HTOL64(i) BCMSWAP64(i)
93
#define htol64(i) bcmswap64(i)
94
#endif /* IL_BIGENDIAN */
95
#endif /* hton16 */
96
97
#ifndef IL_BIGENDIAN
98
#define ltoh16_buf(buf, i)
99
#define htol16_buf(buf, i)
100
#else
101
#define ltoh16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
102
#define htol16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
103
#endif /* IL_BIGENDIAN */
104
105
/* Unaligned loads and stores in host byte order */
106
#ifndef IL_BIGENDIAN
107
#define load32_ua(a) ltoh32_ua(a)
108
#define store32_ua(a, v) htol32_ua_store(v, a)
109
#define load16_ua(a) ltoh16_ua(a)
110
#define store16_ua(a, v) htol16_ua_store(v, a)
111
#else
112
#define load32_ua(a) ntoh32_ua(a)
113
#define store32_ua(a, v) hton32_ua_store(v, a)
114
#define load16_ua(a) ntoh16_ua(a)
115
#define store16_ua(a, v) hton16_ua_store(v, a)
116
#endif /* IL_BIGENDIAN */
117
118
#define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
119
#define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
120
#define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
121
#define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
122
123
#define ltoh_ua(ptr) \
124
(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
125
sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
126
sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
127
*(uint8 *)0)
128
129
#define ntoh_ua(ptr) \
130
(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
131
sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
132
sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
133
*(uint8 *)0)
134
135
#ifdef __GNUC__
136
137
/* GNU macro versions avoid referencing the argument multiple times, while also
138
* avoiding the -fno-inline used in ROM builds.
139
*/
140
141
#define bcmswap16(val) ({ \
142
uint16 _val = (val); \
143
BCMSWAP16(_val); \
144
})
145
146
#define bcmswap32(val) ({ \
147
uint32 _val = (val); \
148
BCMSWAP32(_val); \
149
})
150
151
#define bcmswap64(val) ({ \
152
uint64 _val = (val); \
153
BCMSWAP64(_val); \
154
})
155
156
#define bcmswap32by16(val) ({ \
157
uint32 _val = (val); \
158
BCMSWAP32BY16(_val); \
159
})
160
161
#define bcmswap16_buf(buf, len) ({ \
162
uint16 *_buf = (uint16 *)(buf); \
163
uint _wds = (len) / 2; \
164
while (_wds--) { \
165
*_buf = bcmswap16(*_buf); \
166
_buf++; \
167
} \
168
})
169
170
#define htol16_ua_store(val, bytes) ({ \
171
uint16 _val = (val); \
172
uint8 *_bytes = (uint8 *)(bytes); \
173
_bytes[0] = _val & 0xff; \
174
_bytes[1] = _val >> 8; \
175
})
176
177
#define htol32_ua_store(val, bytes) ({ \
178
uint32 _val = (val); \
179
uint8 *_bytes = (uint8 *)(bytes); \
180
_bytes[0] = _val & 0xff; \
181
_bytes[1] = (_val >> 8) & 0xff; \
182
_bytes[2] = (_val >> 16) & 0xff; \
183
_bytes[3] = _val >> 24; \
184
})
185
186
#define hton16_ua_store(val, bytes) ({ \
187
uint16 _val = (val); \
188
uint8 *_bytes = (uint8 *)(bytes); \
189
_bytes[0] = _val >> 8; \
190
_bytes[1] = _val & 0xff; \
191
})
192
193
#define hton32_ua_store(val, bytes) ({ \
194
uint32 _val = (val); \
195
uint8 *_bytes = (uint8 *)(bytes); \
196
_bytes[0] = _val >> 24; \
197
_bytes[1] = (_val >> 16) & 0xff; \
198
_bytes[2] = (_val >> 8) & 0xff; \
199
_bytes[3] = _val & 0xff; \
200
})
201
202
#define ltoh16_ua(bytes) ({ \
203
const uint8 *_bytes = (const uint8 *)(bytes); \
204
_LTOH16_UA(_bytes); \
205
})
206
207
#define ltoh32_ua(bytes) ({ \
208
const uint8 *_bytes = (const uint8 *)(bytes); \
209
_LTOH32_UA(_bytes); \
210
})
211
212
#define ntoh16_ua(bytes) ({ \
213
const uint8 *_bytes = (const uint8 *)(bytes); \
214
_NTOH16_UA(_bytes); \
215
})
216
217
#define ntoh32_ua(bytes) ({ \
218
const uint8 *_bytes = (const uint8 *)(bytes); \
219
_NTOH32_UA(_bytes); \
220
})
221
222
#else /* !__GNUC__ */
223
224
/* Inline versions avoid referencing the argument multiple times */
225
static INLINE uint16
226
bcmswap16(uint16 val)
227
{
228
return BCMSWAP16(val);
229
}
230
231
static INLINE uint32
232
bcmswap32(uint32 val)
233
{
234
return BCMSWAP32(val);
235
}
236
237
static INLINE uint64
238
bcmswap64(uint64 val)
239
{
240
return BCMSWAP64(val);
241
}
242
243
static INLINE uint32
244
bcmswap32by16(uint32 val)
245
{
246
return BCMSWAP32BY16(val);
247
}
248
249
/* Reverse pairs of bytes in a buffer (not for high-performance use) */
250
/* buf - start of buffer of shorts to swap */
251
/* len - byte length of buffer */
252
static INLINE void
253
bcmswap16_buf(uint16 *buf, uint len)
254
{
255
len = len / 2;
256
257
while (len--) {
258
*buf = bcmswap16(*buf);
259
buf++;
260
}
261
}
262
263
/*
264
* Store 16-bit value to unaligned little-endian byte array.
265
*/
266
static INLINE void
267
htol16_ua_store(uint16 val, uint8 *bytes)
268
{
269
bytes[0] = val & 0xff;
270
bytes[1] = val >> 8;
271
}
272
273
/*
274
* Store 32-bit value to unaligned little-endian byte array.
275
*/
276
static INLINE void
277
htol32_ua_store(uint32 val, uint8 *bytes)
278
{
279
bytes[0] = val & 0xff;
280
bytes[1] = (val >> 8) & 0xff;
281
bytes[2] = (val >> 16) & 0xff;
282
bytes[3] = val >> 24;
283
}
284
285
/*
286
* Store 16-bit value to unaligned network-(big-)endian byte array.
287
*/
288
static INLINE void
289
hton16_ua_store(uint16 val, uint8 *bytes)
290
{
291
bytes[0] = val >> 8;
292
bytes[1] = val & 0xff;
293
}
294
295
/*
296
* Store 32-bit value to unaligned network-(big-)endian byte array.
297
*/
298
static INLINE void
299
hton32_ua_store(uint32 val, uint8 *bytes)
300
{
301
bytes[0] = val >> 24;
302
bytes[1] = (val >> 16) & 0xff;
303
bytes[2] = (val >> 8) & 0xff;
304
bytes[3] = val & 0xff;
305
}
306
307
/*
308
* Load 16-bit value from unaligned little-endian byte array.
309
*/
310
static INLINE uint16
311
ltoh16_ua(const void *bytes)
312
{
313
return _LTOH16_UA((const uint8 *)bytes);
314
}
315
316
/*
317
* Load 32-bit value from unaligned little-endian byte array.
318
*/
319
static INLINE uint32
320
ltoh32_ua(const void *bytes)
321
{
322
return _LTOH32_UA((const uint8 *)bytes);
323
}
324
325
/*
326
* Load 16-bit value from unaligned big-(network-)endian byte array.
327
*/
328
static INLINE uint16
329
ntoh16_ua(const void *bytes)
330
{
331
return _NTOH16_UA((const uint8 *)bytes);
332
}
333
334
/*
335
* Load 32-bit value from unaligned big-(network-)endian byte array.
336
*/
337
static INLINE uint32
338
ntoh32_ua(const void *bytes)
339
{
340
return _NTOH32_UA((const uint8 *)bytes);
341
}
342
343
#endif /* !__GNUC__ */
344
#endif /* !_BCMENDIAN_H_ */
345
346