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