1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Byte order utilities
4 *
5 * Copyright (C) 1999-2019, Broadcom.
6 *
7 * Unless you and Broadcom execute a separate written software license
8 * agreement governing use of this software, this software is licensed to you
9 * under the terms of the GNU General Public License version 2 (the "GPL"),
10 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11 * following added to such license:
12 *
13 * As a special exception, the copyright holders of this software give you
14 * permission to link this software with independent modules, and to copy and
15 * distribute the resulting executable under terms of your choice, provided that
16 * you also meet, for each linked independent module, the terms and conditions of
17 * the license of that module. An independent module is a module which is not
18 * derived from this software. The special exception does not apply to any
19 * modifications of the software.
20 *
21 * Notwithstanding the above, under no circumstances may you combine this
22 * software in any way with any other Broadcom software provided under a license
23 * other than the GPL, without Broadcom's express prior written consent.
24 *
25 *
26 * <<Broadcom-WL-IPTag/Open:>>
27 *
28 * $Id: bcmendian.h 788572 2018-11-13 03:52:19Z $
29 *
30 * This file by default provides proper behavior on little-endian architectures.
31 * On big-endian architectures, IL_BIGENDIAN should be defined.
32 */
33
34 #ifndef _BCMENDIAN_H_
35 #define _BCMENDIAN_H_
36
37 #include <typedefs.h>
38
39 /* Reverse the bytes in a 16-bit value */
40 #define BCMSWAP16(val) \
41 ((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
42 (((uint16)(val) & (uint16)0xff00U) >> 8)))
43
44 /* Reverse the bytes in a 32-bit value */
45 #define BCMSWAP32(val) \
46 ((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
47 (((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
48 (((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
49 (((uint32)(val) & (uint32)0xff000000U) >> 24)))
50
51 /* Reverse the two 16-bit halves of a 32-bit value */
52 #define BCMSWAP32BY16(val) \
53 ((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
54 (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
55
56 /* Reverse the bytes in a 64-bit value */
57 #define BCMSWAP64(val) \
58 ((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \
59 (((uint64)(val) & 0x000000000000ff00ULL) << 40) | \
60 (((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \
61 (((uint64)(val) & 0x00000000ff000000ULL) << 8) | \
62 (((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \
63 (((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \
64 (((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \
65 (((uint64)(val) & 0xff00000000000000ULL) >> 56)))
66
67 /* Reverse the two 32-bit halves of a 64-bit value */
68 #define BCMSWAP64BY32(val) \
69 ((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \
70 (((uint64)(val) & 0xffffffff00000000ULL) >> 32)))
71
72 /* Byte swapping macros
73 * Host <=> Network (Big Endian) for 16- and 32-bit values
74 * Host <=> Little-Endian for 16- and 32-bit values
75 */
76 #ifndef hton16
77 #define HTON16(i) BCMSWAP16(i)
78 #define hton16(i) bcmswap16(i)
79 #define HTON32(i) BCMSWAP32(i)
80 #define hton32(i) bcmswap32(i)
81 #define NTOH16(i) BCMSWAP16(i)
82 #define ntoh16(i) bcmswap16(i)
83 #define NTOH32(i) BCMSWAP32(i)
84 #define ntoh32(i) bcmswap32(i)
85 #define LTOH16(i) (i)
86 #define ltoh16(i) (i)
87 #define LTOH32(i) (i)
88 #define ltoh32(i) (i)
89 #define HTOL16(i) (i)
90 #define htol16(i) (i)
91 #define HTOL32(i) (i)
92 #define htol32(i) (i)
93 #define HTOL64(i) (i)
94 #define htol64(i) (i)
95 #endif /* hton16 */
96
97 #define ltoh16_buf(buf, i)
98 #define htol16_buf(buf, i)
99 #define ltoh32_buf(buf, i)
100 #define htol32_buf(buf, i)
101 #define ltoh64_buf(buf, i)
102 #define htol64_buf(buf, i)
103
104 /* Unaligned loads and stores in host byte order */
105 #define load32_ua(a) ltoh32_ua(a)
106 #define store32_ua(a, v) htol32_ua_store(v, a)
107 #define load16_ua(a) ltoh16_ua(a)
108 #define store16_ua(a, v) htol16_ua_store(v, a)
109 #define load64_ua(a) ltoh64_ua(a)
110 #define store64_ua(a, v) htol64_ua_store(v, a)
111
112 #define _LTOH16_UA(cp) (uint16)((cp)[0] | ((cp)[1] << 8))
113 #define _LTOH32_UA(cp) (uint32)((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
114 #define _NTOH16_UA(cp) (uint16)(((cp)[0] << 8) | (cp)[1])
115 #define _NTOH32_UA(cp) (uint32)(((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
116
117 #define _LTOH64_UA(cp) ((uint64)(cp)[0] | ((uint64)(cp)[1] << 8) | \
118 ((uint64)(cp)[2] << 16) | ((uint64)(cp)[3] << 24) | \
119 ((uint64)(cp)[4] << 32) | ((uint64)(cp)[5] << 40) | \
120 ((uint64)(cp)[6] << 48) | ((uint64)(cp)[7] << 56))
121
122 #define _NTOH64_UA(cp) ((uint64)(cp)[7] | ((uint64)(cp)[6] << 8) | \
123 ((uint64)(cp)[5] << 16) | ((uint64)(cp)[4] << 24) | \
124 ((uint64)(cp)[3] << 32) | ((uint64)(cp)[2] << 40) | \
125 ((uint64)(cp)[1] << 48) | ((uint64)(cp)[0] << 56))
126
127 #define ltoh_ua(ptr) \
128 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
129 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
130 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
131 *(uint8 *)0)
132
133 #define ntoh_ua(ptr) \
134 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
135 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
136 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
137 *(uint8 *)0)
138
139 #ifdef __GNUC__
140
141 /* GNU macro versions avoid referencing the argument multiple times, while also
142 * avoiding the -fno-inline used in ROM builds.
143 */
144
145 #define bcmswap16(val) ({ \
146 uint16 _val = (val); \
147 BCMSWAP16(_val); \
148 })
149
150 #define bcmswap32(val) ({ \
151 uint32 _val = (val); \
152 BCMSWAP32(_val); \
153 })
154
155 #define bcmswap64(val) ({ \
156 uint64 _val = (val); \
157 BCMSWAP64(_val); \
158 })
159
160 #define bcmswap32by16(val) ({ \
161 uint32 _val = (val); \
162 BCMSWAP32BY16(_val); \
163 })
164
165 #define bcmswap16_buf(buf, len) ({ \
166 uint16 *_buf = (uint16 *)(buf); \
167 uint _wds = (len) / 2; \
168 while (_wds--) { \
169 *_buf = bcmswap16(*_buf); \
170 _buf++; \
171 } \
172 })
173
174 #define bcmswap32_buf(buf, len) ({ \
175 uint32 *_buf = (uint32 *)(buf); \
176 uint _wds = (len) / 4; \
177 while (_wds--) { \
178 *_buf = bcmswap32(*_buf); \
179 _buf++; \
180 } \
181 })
182
183 #define bcmswap64_buf(buf, len) ({ \
184 uint64 *_buf = (uint64 *)(buf); \
185 uint _wds = (len) / 8; \
186 while (_wds--) { \
187 *_buf = bcmswap64(*_buf); \
188 _buf++; \
189 } \
190 })
191
192 #define htol16_ua_store(val, bytes) ({ \
193 uint16 _val = (val); \
194 uint8 *_bytes = (uint8 *)(bytes); \
195 _bytes[0] = _val & 0xff; \
196 _bytes[1] = _val >> 8; \
197 })
198
199 #define htol32_ua_store(val, bytes) ({ \
200 uint32 _val = (val); \
201 uint8 *_bytes = (uint8 *)(bytes); \
202 _bytes[0] = _val & 0xff; \
203 _bytes[1] = (_val >> 8) & 0xff; \
204 _bytes[2] = (_val >> 16) & 0xff; \
205 _bytes[3] = _val >> 24; \
206 })
207
208 #define htol64_ua_store(val, bytes) ({ \
209 uint64 _val = (val); \
210 uint8 *_bytes = (uint8 *)(bytes); \
211 int i; \
212 for (i = 0; i < (int)sizeof(_val); ++i) { \
213 *_bytes++ = _val & 0xff; \
214 _val >>= 8; \
215 } \
216 })
217
218 #define hton16_ua_store(val, bytes) ({ \
219 uint16 _val = (val); \
220 uint8 *_bytes = (uint8 *)(bytes); \
221 _bytes[0] = _val >> 8; \
222 _bytes[1] = _val & 0xff; \
223 })
224
225 #define hton32_ua_store(val, bytes) ({ \
226 uint32 _val = (val); \
227 uint8 *_bytes = (uint8 *)(bytes); \
228 _bytes[0] = _val >> 24; \
229 _bytes[1] = (_val >> 16) & 0xff; \
230 _bytes[2] = (_val >> 8) & 0xff; \
231 _bytes[3] = _val & 0xff; \
232 })
233
234 #define ltoh16_ua(bytes) ({ \
235 const uint8 *_bytes = (const uint8 *)(bytes); \
236 _LTOH16_UA(_bytes); \
237 })
238
239 #define ltoh32_ua(bytes) ({ \
240 const uint8 *_bytes = (const uint8 *)(bytes); \
241 _LTOH32_UA(_bytes); \
242 })
243
244 #define ltoh64_ua(bytes) ({ \
245 const uint8 *_bytes = (const uint8 *)(bytes); \
246 _LTOH64_UA(_bytes); \
247 })
248
249 #define ntoh16_ua(bytes) ({ \
250 const uint8 *_bytes = (const uint8 *)(bytes); \
251 _NTOH16_UA(_bytes); \
252 })
253
254 #define ntoh32_ua(bytes) ({ \
255 const uint8 *_bytes = (const uint8 *)(bytes); \
256 _NTOH32_UA(_bytes); \
257 })
258
259 #define ntoh64_ua(bytes) ({ \
260 const uint8 *_bytes = (const uint8 *)(bytes); \
261 _NTOH64_UA(_bytes); \
262 })
263
264 #else /* !__GNUC__ */
265
266 /* Inline versions avoid referencing the argument multiple times */
267 static INLINE uint16
bcmswap16(uint16 val)268 bcmswap16(uint16 val)
269 {
270 return BCMSWAP16(val);
271 }
272
273 static INLINE uint32
bcmswap32(uint32 val)274 bcmswap32(uint32 val)
275 {
276 return BCMSWAP32(val);
277 }
278
279 static INLINE uint64
bcmswap64(uint64 val)280 bcmswap64(uint64 val)
281 {
282 return BCMSWAP64(val);
283 }
284
285 static INLINE uint32
bcmswap32by16(uint32 val)286 bcmswap32by16(uint32 val)
287 {
288 return BCMSWAP32BY16(val);
289 }
290
291 /* Reverse pairs of bytes in a buffer (not for high-performance use) */
292 /* buf - start of buffer of shorts to swap */
293 /* len - byte length of buffer */
294 static INLINE void
bcmswap16_buf(uint16 * buf,uint len)295 bcmswap16_buf(uint16 *buf, uint len)
296 {
297 len = len / 2;
298
299 while (len--) {
300 *buf = bcmswap16(*buf);
301 buf++;
302 }
303 }
304
305 /*
306 * Store 16-bit value to unaligned little-endian byte array.
307 */
308 static INLINE void
htol16_ua_store(uint16 val,uint8 * bytes)309 htol16_ua_store(uint16 val, uint8 *bytes)
310 {
311 bytes[0] = val & 0xff;
312 bytes[1] = val >> 8;
313 }
314
315 /*
316 * Store 32-bit value to unaligned little-endian byte array.
317 */
318 static INLINE void
htol32_ua_store(uint32 val,uint8 * bytes)319 htol32_ua_store(uint32 val, uint8 *bytes)
320 {
321 bytes[0] = val & 0xff;
322 bytes[1] = (val >> 8) & 0xff;
323 bytes[2] = (val >> 16) & 0xff;
324 bytes[3] = val >> 24;
325 }
326
327 /*
328 * Store 64-bit value to unaligned little-endian byte array.
329 */
330 static INLINE void
htol64_ua_store(uint64 val,uint8 * bytes)331 htol64_ua_store(uint64 val, uint8 *bytes)
332 {
333 int i;
334 for (i = 0; i < sizeof(val); ++i) {
335 *bytes++ = (uint8)(val & 0xff);
336 val >>= 8;
337 }
338 }
339
340 /*
341 * Store 16-bit value to unaligned network-(big-)endian byte array.
342 */
343 static INLINE void
hton16_ua_store(uint16 val,uint8 * bytes)344 hton16_ua_store(uint16 val, uint8 *bytes)
345 {
346 bytes[0] = val >> 8;
347 bytes[1] = val & 0xff;
348 }
349
350 /*
351 * Store 32-bit value to unaligned network-(big-)endian byte array.
352 */
353 static INLINE void
hton32_ua_store(uint32 val,uint8 * bytes)354 hton32_ua_store(uint32 val, uint8 *bytes)
355 {
356 bytes[0] = val >> 24;
357 bytes[1] = (val >> 16) & 0xff;
358 bytes[2] = (val >> 8) & 0xff;
359 bytes[3] = val & 0xff;
360 }
361
362 /*
363 * Load 16-bit value from unaligned little-endian byte array.
364 */
365 static INLINE uint16
ltoh16_ua(const void * bytes)366 ltoh16_ua(const void *bytes)
367 {
368 return _LTOH16_UA((const uint8 *)bytes);
369 }
370
371 /*
372 * Load 32-bit value from unaligned little-endian byte array.
373 */
374 static INLINE uint32
ltoh32_ua(const void * bytes)375 ltoh32_ua(const void *bytes)
376 {
377 return _LTOH32_UA((const uint8 *)bytes);
378 }
379
380 /*
381 * Load 64-bit value from unaligned little-endian byte array.
382 */
383 static INLINE uint64
ltoh64_ua(const void * bytes)384 ltoh64_ua(const void *bytes)
385 {
386 return _LTOH64_UA((const uint8 *)bytes);
387 }
388
389 /*
390 * Load 16-bit value from unaligned big-(network-)endian byte array.
391 */
392 static INLINE uint16
ntoh16_ua(const void * bytes)393 ntoh16_ua(const void *bytes)
394 {
395 return _NTOH16_UA((const uint8 *)bytes);
396 }
397
398 /*
399 * Load 32-bit value from unaligned big-(network-)endian byte array.
400 */
401 static INLINE uint32
ntoh32_ua(const void * bytes)402 ntoh32_ua(const void *bytes)
403 {
404 return _NTOH32_UA((const uint8 *)bytes);
405 }
406
407 /*
408 * Load 64-bit value from unaligned big-(network-)endian byte array.
409 */
410 static INLINE uint64
ntoh64_ua(const void * bytes)411 ntoh64_ua(const void *bytes)
412 {
413 return _NTOH64_UA((const uint8 *)bytes);
414 }
415
416 #endif /* !__GNUC__ */
417 #endif /* !_BCMENDIAN_H_ */
418