Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1
4
5/*
6 * Copyright (c) 2015-2016, Intel Corporation.
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#ifndef ZEPHYR_INCLUDE_SYS_BYTEORDER_H_
12#define ZEPHYR_INCLUDE_SYS_BYTEORDER_H_
13
14#include <zephyr/types.h>
15#include <stddef.h>
16#include <string.h>
17#include <zephyr/sys/__assert.h>
19#include <zephyr/toolchain.h>
20
21#if HAS_BUILTIN(__builtin_bswap16)
22#define BSWAP_16(x) ((uint16_t) __builtin_bswap16(x))
23#else
24#define BSWAP_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
25#endif
26
27#if HAS_BUILTIN(__builtin_bswap32)
28#define BSWAP_24(x) ((uint32_t) __builtin_bswap32(x) >> 8)
29#define BSWAP_32(x) ((uint32_t) __builtin_bswap32(x))
30#else
31#define BSWAP_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \
32 (((x)) & 0xff00) | \
33 (((x) & 0xff) << 16)))
34
35#define BSWAP_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
36 (((x) >> 8) & 0xff00) | \
37 (((x) & 0xff00) << 8) | \
38 (((x) & 0xff) << 24)))
39#endif
40
41#if HAS_BUILTIN(__builtin_bswap64)
42#define BSWAP_40(x) ((uint64_t) __builtin_bswap64(x) >> 24)
43#define BSWAP_48(x) ((uint64_t) __builtin_bswap64(x) >> 16)
44#define BSWAP_64(x) ((uint64_t) __builtin_bswap64(x))
45#else
46#define BSWAP_40(x) ((uint64_t) ((((x) >> 32) & 0xff) | \
47 (((x) >> 16) & 0xff00) | \
48 (((x)) & 0xff0000) | \
49 (((x) & 0xff00) << 16) | \
50 (((x) & 0xff) << 32)))
51
52#define BSWAP_48(x) ((uint64_t) ((((x) >> 40) & 0xff) | \
53 (((x) >> 24) & 0xff00) | \
54 (((x) >> 8) & 0xff0000) | \
55 (((x) & 0xff0000) << 8) | \
56 (((x) & 0xff00) << 24) | \
57 (((x) & 0xff) << 40)))
58
59#define BSWAP_64(x) ((uint64_t) ((((x) >> 56) & 0xff) | \
60 (((x) >> 40) & 0xff00) | \
61 (((x) >> 24) & 0xff0000) | \
62 (((x) >> 8) & 0xff000000) | \
63 (((x) & 0xff000000) << 8) | \
64 (((x) & 0xff0000) << 24) | \
65 (((x) & 0xff00) << 40) | \
66 (((x) & 0xff) << 56)))
67#endif
68
76
84
92
100
108
116
124
132
140
148
156
164
172
180
188
196
209
223
239
240#ifdef CONFIG_LITTLE_ENDIAN
241#define sys_le16_to_cpu(val) (val)
242#define sys_cpu_to_le16(val) (val)
243#define sys_le24_to_cpu(val) (val)
244#define sys_cpu_to_le24(val) (val)
245#define sys_le32_to_cpu(val) (val)
246#define sys_cpu_to_le32(val) (val)
247#define sys_le40_to_cpu(val) (val)
248#define sys_cpu_to_le40(val) (val)
249#define sys_le48_to_cpu(val) (val)
250#define sys_cpu_to_le48(val) (val)
251#define sys_le64_to_cpu(val) (val)
252#define sys_cpu_to_le64(val) (val)
253#define sys_be16_to_cpu(val) BSWAP_16(val)
254#define sys_cpu_to_be16(val) BSWAP_16(val)
255#define sys_be24_to_cpu(val) BSWAP_24(val)
256#define sys_cpu_to_be24(val) BSWAP_24(val)
257#define sys_be32_to_cpu(val) BSWAP_32(val)
258#define sys_cpu_to_be32(val) BSWAP_32(val)
259#define sys_be40_to_cpu(val) BSWAP_40(val)
260#define sys_cpu_to_be40(val) BSWAP_40(val)
261#define sys_be48_to_cpu(val) BSWAP_48(val)
262#define sys_cpu_to_be48(val) BSWAP_48(val)
263#define sys_be64_to_cpu(val) BSWAP_64(val)
264#define sys_cpu_to_be64(val) BSWAP_64(val)
265
266#define sys_uint16_to_array(val) { \
267 ((val) & 0xff), \
268 (((val) >> 8) & 0xff)}
269
270#define sys_uint32_to_array(val) { \
271 ((val) & 0xff), \
272 (((val) >> 8) & 0xff), \
273 (((val) >> 16) & 0xff), \
274 (((val) >> 24) & 0xff)}
275
276#define sys_uint64_to_array(val) { \
277 ((val) & 0xff), \
278 (((val) >> 8) & 0xff), \
279 (((val) >> 16) & 0xff), \
280 (((val) >> 24) & 0xff), \
281 (((val) >> 32) & 0xff), \
282 (((val) >> 40) & 0xff), \
283 (((val) >> 48) & 0xff), \
284 (((val) >> 56) & 0xff)}
285
286#else
287#define sys_le16_to_cpu(val) BSWAP_16(val)
288#define sys_cpu_to_le16(val) BSWAP_16(val)
289#define sys_le24_to_cpu(val) BSWAP_24(val)
290#define sys_cpu_to_le24(val) BSWAP_24(val)
291#define sys_le32_to_cpu(val) BSWAP_32(val)
292#define sys_cpu_to_le32(val) BSWAP_32(val)
293#define sys_le40_to_cpu(val) BSWAP_40(val)
294#define sys_cpu_to_le40(val) BSWAP_40(val)
295#define sys_le48_to_cpu(val) BSWAP_48(val)
296#define sys_cpu_to_le48(val) BSWAP_48(val)
297#define sys_le64_to_cpu(val) BSWAP_64(val)
298#define sys_cpu_to_le64(val) BSWAP_64(val)
299#define sys_be16_to_cpu(val) (val)
300#define sys_cpu_to_be16(val) (val)
301#define sys_be24_to_cpu(val) (val)
302#define sys_cpu_to_be24(val) (val)
303#define sys_be32_to_cpu(val) (val)
304#define sys_cpu_to_be32(val) (val)
305#define sys_be40_to_cpu(val) (val)
306#define sys_cpu_to_be40(val) (val)
307#define sys_be48_to_cpu(val) (val)
308#define sys_cpu_to_be48(val) (val)
309#define sys_be64_to_cpu(val) (val)
310#define sys_cpu_to_be64(val) (val)
311
312#define sys_uint16_to_array(val) { \
313 (((val) >> 8) & 0xff), \
314 ((val) & 0xff)}
315
316#define sys_uint32_to_array(val) { \
317 (((val) >> 24) & 0xff), \
318 (((val) >> 16) & 0xff), \
319 (((val) >> 8) & 0xff), \
320 ((val) & 0xff)}
321
322#define sys_uint64_to_array(val) { \
323 (((val) >> 56) & 0xff), \
324 (((val) >> 48) & 0xff), \
325 (((val) >> 40) & 0xff), \
326 (((val) >> 32) & 0xff), \
327 (((val) >> 24) & 0xff), \
328 (((val) >> 16) & 0xff), \
329 (((val) >> 8) & 0xff), \
330 ((val) & 0xff)}
331
332#endif
333
343static inline void sys_put_be16(uint16_t val, uint8_t dst[2])
344{
345 dst[0] = val >> 8;
346 dst[1] = val;
347}
348
358static inline void sys_put_be24(uint32_t val, uint8_t dst[3])
359{
360 dst[0] = val >> 16;
361 sys_put_be16(val, &dst[1]);
362}
363
373static inline void sys_put_be32(uint32_t val, uint8_t dst[4])
374{
375 sys_put_be16(val >> 16, dst);
376 sys_put_be16(val, &dst[2]);
377}
378
387static inline void sys_put_be40(uint64_t val, uint8_t dst[5])
388{
389 dst[0] = val >> 32;
390 sys_put_be32(val, &dst[1]);
391}
392
402static inline void sys_put_be48(uint64_t val, uint8_t dst[6])
403{
404 sys_put_be16(val >> 32, dst);
405 sys_put_be32(val, &dst[2]);
406}
407
417static inline void sys_put_be64(uint64_t val, uint8_t dst[8])
418{
419 sys_put_be32(val >> 32, dst);
420 sys_put_be32(val, &dst[4]);
421}
422
432static inline void sys_put_le16(uint16_t val, uint8_t dst[2])
433{
434 dst[0] = val;
435 dst[1] = val >> 8;
436}
437
447static inline void sys_put_le24(uint32_t val, uint8_t dst[3])
448{
449 sys_put_le16(val, dst);
450 dst[2] = val >> 16;
451}
452
462static inline void sys_put_le32(uint32_t val, uint8_t dst[4])
463{
464 sys_put_le16(val, dst);
465 sys_put_le16(val >> 16, &dst[2]);
466}
467
477static inline void sys_put_le40(uint64_t val, uint8_t dst[5])
478{
479 sys_put_le32(val, dst);
480 dst[4] = val >> 32;
481}
482
492static inline void sys_put_le48(uint64_t val, uint8_t dst[6])
493{
494 sys_put_le32(val, dst);
495 sys_put_le16(val >> 32, &dst[4]);
496}
497
507static inline void sys_put_le64(uint64_t val, uint8_t dst[8])
508{
509 sys_put_le32(val, dst);
510 sys_put_le32(val >> 32, &dst[4]);
511}
512
523static inline uint16_t sys_get_be16(const uint8_t src[2])
524{
525 return ((uint16_t)src[0] << 8) | src[1];
526}
527
538static inline uint32_t sys_get_be24(const uint8_t src[3])
539{
540 return ((uint32_t)src[0] << 16) | sys_get_be16(&src[1]);
541}
542
553static inline uint32_t sys_get_be32(const uint8_t src[4])
554{
555 return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
556}
557
568static inline uint64_t sys_get_be40(const uint8_t src[5])
569{
570 return ((uint64_t)sys_get_be32(&src[0]) << 8) | src[4];
571}
572
583static inline uint64_t sys_get_be48(const uint8_t src[6])
584{
585 return ((uint64_t)sys_get_be32(&src[0]) << 16) | sys_get_be16(&src[4]);
586}
587
598static inline uint64_t sys_get_be64(const uint8_t src[8])
599{
600 return ((uint64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]);
601}
602
613static inline uint16_t sys_get_le16(const uint8_t src[2])
614{
615 return ((uint16_t)src[1] << 8) | src[0];
616}
617
628static inline uint32_t sys_get_le24(const uint8_t src[3])
629{
630 return ((uint32_t)src[2] << 16) | sys_get_le16(&src[0]);
631}
632
643static inline uint32_t sys_get_le32(const uint8_t src[4])
644{
645 return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
646}
647
658static inline uint64_t sys_get_le40(const uint8_t src[5])
659{
660 return ((uint64_t)sys_get_le32(&src[1]) << 8) | src[0];
661}
662
673static inline uint64_t sys_get_le48(const uint8_t src[6])
674{
675 return ((uint64_t)sys_get_le32(&src[2]) << 16) | sys_get_le16(&src[0]);
676}
677
688static inline uint64_t sys_get_le64(const uint8_t src[8])
689{
690 return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]);
691}
692
706static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
707{
708 uint8_t *pdst = (uint8_t *)dst;
709 const uint8_t *psrc = (const uint8_t *)src;
710
711 __ASSERT(((psrc < pdst && (psrc + length) <= pdst) ||
712 (psrc > pdst && (pdst + length) <= psrc)),
713 "Source and destination buffers must not overlap");
714
715 psrc += length - 1;
716
717 for (; length > 0; length--) {
718 *pdst++ = *psrc--;
719 }
720}
721
732static inline void sys_mem_swap(void *buf, size_t length)
733{
734 size_t i;
735
736 for (i = 0; i < (length/2); i++) {
737 uint8_t tmp = ((uint8_t *)buf)[i];
738
739 ((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i];
740 ((uint8_t *)buf)[length - 1 - i] = tmp;
741 }
742}
743
750static inline void sys_le_to_cpu(void *buf, size_t length)
751{
752 if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
753 sys_mem_swap(buf, length);
754 }
755}
756
763static inline void sys_cpu_to_le(void *buf, size_t length)
764{
765 if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
766 sys_mem_swap(buf, length);
767 }
768}
769
776static inline void sys_be_to_cpu(void *buf, size_t length)
777{
778 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
779 sys_mem_swap(buf, length);
780 }
781}
782
789static inline void sys_cpu_to_be(void *buf, size_t length)
790{
791 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
792 sys_mem_swap(buf, length);
793 }
794}
795
806static inline void sys_put_le(void *dst, const void *src, size_t length)
807{
808 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
809 (void)memcpy(dst, src, length);
810 } else {
811 sys_memcpy_swap(dst, src, length);
812 }
813}
814
825static inline void sys_put_be(void *dst, const void *src, size_t length)
826{
827 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
828 sys_memcpy_swap(dst, src, length);
829 } else {
830 (void)memcpy(dst, src, length);
831 }
832}
833
844static inline void sys_get_le(void *dst, const void *src, size_t length)
845{
846 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
847 (void)memcpy(dst, src, length);
848 } else {
849 sys_memcpy_swap(dst, src, length);
850 }
851}
852
863static inline void sys_get_be(void *dst, const void *src, size_t length)
864{
865 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
866 sys_memcpy_swap(dst, src, length);
867 } else {
868 (void)memcpy(dst, src, length);
869 }
870}
871
872#endif /* ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ */
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:154
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
static void sys_memcpy_swap(void *dst, const void *src, size_t length)
Swap one buffer content into another.
Definition byteorder.h:706
static void sys_get_be(void *dst, const void *src, size_t length)
Get a buffer stored in big-endian format.
Definition byteorder.h:863
static uint64_t sys_get_le40(const uint8_t src[5])
Get a 40-bit integer stored in little-endian format.
Definition byteorder.h:658
static void sys_put_le24(uint32_t val, uint8_t dst[3])
Put a 24-bit integer as little-endian to arbitrary location.
Definition byteorder.h:447
static void sys_cpu_to_le(void *buf, size_t length)
Convert buffer from host endianness to little-endian.
Definition byteorder.h:763
static void sys_le_to_cpu(void *buf, size_t length)
Convert buffer from little-endian to host endianness.
Definition byteorder.h:750
static void sys_put_be32(uint32_t val, uint8_t dst[4])
Put a 32-bit integer as big-endian to arbitrary location.
Definition byteorder.h:373
static void sys_put_be64(uint64_t val, uint8_t dst[8])
Put a 64-bit integer as big-endian to arbitrary location.
Definition byteorder.h:417
static void sys_cpu_to_be(void *buf, size_t length)
Convert buffer from host endianness to big-endian.
Definition byteorder.h:789
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition byteorder.h:553
static void sys_be_to_cpu(void *buf, size_t length)
Convert buffer from big-endian to host endianness.
Definition byteorder.h:776
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition byteorder.h:613
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition byteorder.h:673
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition byteorder.h:688
static void sys_put_be16(uint16_t val, uint8_t dst[2])
Put a 16-bit integer as big-endian to arbitrary location.
Definition byteorder.h:343
static void sys_put_le(void *dst, const void *src, size_t length)
Put a buffer as little-endian to arbitrary location.
Definition byteorder.h:806
static void sys_put_be24(uint32_t val, uint8_t dst[3])
Put a 24-bit integer as big-endian to arbitrary location.
Definition byteorder.h:358
static void sys_put_le64(uint64_t val, uint8_t dst[8])
Put a 64-bit integer as little-endian to arbitrary location.
Definition byteorder.h:507
static uint64_t sys_get_be40(const uint8_t src[5])
Get a 40-bit integer stored in big-endian format.
Definition byteorder.h:568
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition byteorder.h:523
static void sys_put_le48(uint64_t val, uint8_t dst[6])
Put a 48-bit integer as little-endian to arbitrary location.
Definition byteorder.h:492
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition byteorder.h:643
static uint32_t sys_get_le24(const uint8_t src[3])
Get a 24-bit integer stored in little-endian format.
Definition byteorder.h:628
static void sys_put_le40(uint64_t val, uint8_t dst[5])
Put a 40-bit integer as little-endian to arbitrary location.
Definition byteorder.h:477
static void sys_put_be48(uint64_t val, uint8_t dst[6])
Put a 48-bit integer as big-endian to arbitrary location.
Definition byteorder.h:402
static uint32_t sys_get_be24(const uint8_t src[3])
Get a 24-bit integer stored in big-endian format.
Definition byteorder.h:538
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition byteorder.h:583
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition byteorder.h:598
static void sys_put_be40(uint64_t val, uint8_t dst[5])
Put a 40-bit integer as big-endian to arbitrary location.
Definition byteorder.h:387
static void sys_mem_swap(void *buf, size_t length)
Swap buffer content.
Definition byteorder.h:732
static void sys_get_le(void *dst, const void *src, size_t length)
Get a buffer stored in little-endian format.
Definition byteorder.h:844
static void sys_put_be(void *dst, const void *src, size_t length)
Put a buffer as big-endian to arbitrary location.
Definition byteorder.h:825
static void sys_put_le16(uint16_t val, uint8_t dst[2])
Put a 16-bit integer as little-endian to arbitrary location.
Definition byteorder.h:432
static void sys_put_le32(uint32_t val, uint8_t dst[4])
Put a 32-bit integer as little-endian to arbitrary location.
Definition byteorder.h:462
Macros to abstract toolchain specific capabilities.
Macro utilities.