Zephyr API Documentation 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#define BSWAP_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \
27 (((x)) & 0xff00) | \
28 (((x) & 0xff) << 16)))
29#if HAS_BUILTIN(__builtin_bswap32)
30#define BSWAP_32(x) ((uint32_t) __builtin_bswap32(x))
31#else
32#define BSWAP_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
33 (((x) >> 8) & 0xff00) | \
34 (((x) & 0xff00) << 8) | \
35 (((x) & 0xff) << 24)))
36#endif
37#define BSWAP_40(x) ((uint64_t) ((((x) >> 32) & 0xff) | \
38 (((x) >> 16) & 0xff00) | \
39 (((x)) & 0xff0000) | \
40 (((x) & 0xff00) << 16) | \
41 (((x) & 0xff) << 32)))
42#define BSWAP_48(x) ((uint64_t) ((((x) >> 40) & 0xff) | \
43 (((x) >> 24) & 0xff00) | \
44 (((x) >> 8) & 0xff0000) | \
45 (((x) & 0xff0000) << 8) | \
46 (((x) & 0xff00) << 24) | \
47 (((x) & 0xff) << 40)))
48#if HAS_BUILTIN(__builtin_bswap64)
49#define BSWAP_64(x) ((uint64_t) __builtin_bswap64(x))
50#else
51#define BSWAP_64(x) ((uint64_t) ((((x) >> 56) & 0xff) | \
52 (((x) >> 40) & 0xff00) | \
53 (((x) >> 24) & 0xff0000) | \
54 (((x) >> 8) & 0xff000000) | \
55 (((x) & 0xff000000) << 8) | \
56 (((x) & 0xff0000) << 24) | \
57 (((x) & 0xff00) << 40) | \
58 (((x) & 0xff) << 56)))
59#endif
60
68
76
84
92
100
108
116
124
132
140
148
156
164
172
180
188
201
215
231
232#ifdef CONFIG_LITTLE_ENDIAN
233#define sys_le16_to_cpu(val) (val)
234#define sys_cpu_to_le16(val) (val)
235#define sys_le24_to_cpu(val) (val)
236#define sys_cpu_to_le24(val) (val)
237#define sys_le32_to_cpu(val) (val)
238#define sys_cpu_to_le32(val) (val)
239#define sys_le40_to_cpu(val) (val)
240#define sys_cpu_to_le40(val) (val)
241#define sys_le48_to_cpu(val) (val)
242#define sys_cpu_to_le48(val) (val)
243#define sys_le64_to_cpu(val) (val)
244#define sys_cpu_to_le64(val) (val)
245#define sys_be16_to_cpu(val) BSWAP_16(val)
246#define sys_cpu_to_be16(val) BSWAP_16(val)
247#define sys_be24_to_cpu(val) BSWAP_24(val)
248#define sys_cpu_to_be24(val) BSWAP_24(val)
249#define sys_be32_to_cpu(val) BSWAP_32(val)
250#define sys_cpu_to_be32(val) BSWAP_32(val)
251#define sys_be40_to_cpu(val) BSWAP_40(val)
252#define sys_cpu_to_be40(val) BSWAP_40(val)
253#define sys_be48_to_cpu(val) BSWAP_48(val)
254#define sys_cpu_to_be48(val) BSWAP_48(val)
255#define sys_be64_to_cpu(val) BSWAP_64(val)
256#define sys_cpu_to_be64(val) BSWAP_64(val)
257
258#define sys_uint16_to_array(val) { \
259 ((val) & 0xff), \
260 (((val) >> 8) & 0xff)}
261
262#define sys_uint32_to_array(val) { \
263 ((val) & 0xff), \
264 (((val) >> 8) & 0xff), \
265 (((val) >> 16) & 0xff), \
266 (((val) >> 24) & 0xff)}
267
268#define sys_uint64_to_array(val) { \
269 ((val) & 0xff), \
270 (((val) >> 8) & 0xff), \
271 (((val) >> 16) & 0xff), \
272 (((val) >> 24) & 0xff), \
273 (((val) >> 32) & 0xff), \
274 (((val) >> 40) & 0xff), \
275 (((val) >> 48) & 0xff), \
276 (((val) >> 56) & 0xff)}
277
278#else
279#define sys_le16_to_cpu(val) BSWAP_16(val)
280#define sys_cpu_to_le16(val) BSWAP_16(val)
281#define sys_le24_to_cpu(val) BSWAP_24(val)
282#define sys_cpu_to_le24(val) BSWAP_24(val)
283#define sys_le32_to_cpu(val) BSWAP_32(val)
284#define sys_cpu_to_le32(val) BSWAP_32(val)
285#define sys_le40_to_cpu(val) BSWAP_40(val)
286#define sys_cpu_to_le40(val) BSWAP_40(val)
287#define sys_le48_to_cpu(val) BSWAP_48(val)
288#define sys_cpu_to_le48(val) BSWAP_48(val)
289#define sys_le64_to_cpu(val) BSWAP_64(val)
290#define sys_cpu_to_le64(val) BSWAP_64(val)
291#define sys_be16_to_cpu(val) (val)
292#define sys_cpu_to_be16(val) (val)
293#define sys_be24_to_cpu(val) (val)
294#define sys_cpu_to_be24(val) (val)
295#define sys_be32_to_cpu(val) (val)
296#define sys_cpu_to_be32(val) (val)
297#define sys_be40_to_cpu(val) (val)
298#define sys_cpu_to_be40(val) (val)
299#define sys_be48_to_cpu(val) (val)
300#define sys_cpu_to_be48(val) (val)
301#define sys_be64_to_cpu(val) (val)
302#define sys_cpu_to_be64(val) (val)
303
304#define sys_uint16_to_array(val) { \
305 (((val) >> 8) & 0xff), \
306 ((val) & 0xff)}
307
308#define sys_uint32_to_array(val) { \
309 (((val) >> 24) & 0xff), \
310 (((val) >> 16) & 0xff), \
311 (((val) >> 8) & 0xff), \
312 ((val) & 0xff)}
313
314#define sys_uint64_to_array(val) { \
315 (((val) >> 56) & 0xff), \
316 (((val) >> 48) & 0xff), \
317 (((val) >> 40) & 0xff), \
318 (((val) >> 32) & 0xff), \
319 (((val) >> 24) & 0xff), \
320 (((val) >> 16) & 0xff), \
321 (((val) >> 8) & 0xff), \
322 ((val) & 0xff)}
323
324#endif
325
335static inline void sys_put_be16(uint16_t val, uint8_t dst[2])
336{
337 dst[0] = val >> 8;
338 dst[1] = val;
339}
340
350static inline void sys_put_be24(uint32_t val, uint8_t dst[3])
351{
352 dst[0] = val >> 16;
353 sys_put_be16(val, &dst[1]);
354}
355
365static inline void sys_put_be32(uint32_t val, uint8_t dst[4])
366{
367 sys_put_be16(val >> 16, dst);
368 sys_put_be16(val, &dst[2]);
369}
370
379static inline void sys_put_be40(uint64_t val, uint8_t dst[5])
380{
381 dst[0] = val >> 32;
382 sys_put_be32(val, &dst[1]);
383}
384
394static inline void sys_put_be48(uint64_t val, uint8_t dst[6])
395{
396 sys_put_be16(val >> 32, dst);
397 sys_put_be32(val, &dst[2]);
398}
399
409static inline void sys_put_be64(uint64_t val, uint8_t dst[8])
410{
411 sys_put_be32(val >> 32, dst);
412 sys_put_be32(val, &dst[4]);
413}
414
424static inline void sys_put_le16(uint16_t val, uint8_t dst[2])
425{
426 dst[0] = val;
427 dst[1] = val >> 8;
428}
429
439static inline void sys_put_le24(uint32_t val, uint8_t dst[3])
440{
441 sys_put_le16(val, dst);
442 dst[2] = val >> 16;
443}
444
454static inline void sys_put_le32(uint32_t val, uint8_t dst[4])
455{
456 sys_put_le16(val, dst);
457 sys_put_le16(val >> 16, &dst[2]);
458}
459
469static inline void sys_put_le40(uint64_t val, uint8_t dst[5])
470{
471 sys_put_le32(val, dst);
472 dst[4] = val >> 32;
473}
474
484static inline void sys_put_le48(uint64_t val, uint8_t dst[6])
485{
486 sys_put_le32(val, dst);
487 sys_put_le16(val >> 32, &dst[4]);
488}
489
499static inline void sys_put_le64(uint64_t val, uint8_t dst[8])
500{
501 sys_put_le32(val, dst);
502 sys_put_le32(val >> 32, &dst[4]);
503}
504
515static inline uint16_t sys_get_be16(const uint8_t src[2])
516{
517 return ((uint16_t)src[0] << 8) | src[1];
518}
519
530static inline uint32_t sys_get_be24(const uint8_t src[3])
531{
532 return ((uint32_t)src[0] << 16) | sys_get_be16(&src[1]);
533}
534
545static inline uint32_t sys_get_be32(const uint8_t src[4])
546{
547 return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
548}
549
560static inline uint64_t sys_get_be40(const uint8_t src[5])
561{
562 return ((uint64_t)sys_get_be32(&src[0]) << 8) | src[4];
563}
564
575static inline uint64_t sys_get_be48(const uint8_t src[6])
576{
577 return ((uint64_t)sys_get_be32(&src[0]) << 16) | sys_get_be16(&src[4]);
578}
579
590static inline uint64_t sys_get_be64(const uint8_t src[8])
591{
592 return ((uint64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]);
593}
594
605static inline uint16_t sys_get_le16(const uint8_t src[2])
606{
607 return ((uint16_t)src[1] << 8) | src[0];
608}
609
620static inline uint32_t sys_get_le24(const uint8_t src[3])
621{
622 return ((uint32_t)src[2] << 16) | sys_get_le16(&src[0]);
623}
624
635static inline uint32_t sys_get_le32(const uint8_t src[4])
636{
637 return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
638}
639
650static inline uint64_t sys_get_le40(const uint8_t src[5])
651{
652 return ((uint64_t)sys_get_le32(&src[1]) << 8) | src[0];
653}
654
665static inline uint64_t sys_get_le48(const uint8_t src[6])
666{
667 return ((uint64_t)sys_get_le32(&src[2]) << 16) | sys_get_le16(&src[0]);
668}
669
680static inline uint64_t sys_get_le64(const uint8_t src[8])
681{
682 return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]);
683}
684
698static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
699{
700 uint8_t *pdst = (uint8_t *)dst;
701 const uint8_t *psrc = (const uint8_t *)src;
702
703 __ASSERT(((psrc < pdst && (psrc + length) <= pdst) ||
704 (psrc > pdst && (pdst + length) <= psrc)),
705 "Source and destination buffers must not overlap");
706
707 psrc += length - 1;
708
709 for (; length > 0; length--) {
710 *pdst++ = *psrc--;
711 }
712}
713
724static inline void sys_mem_swap(void *buf, size_t length)
725{
726 size_t i;
727
728 for (i = 0; i < (length/2); i++) {
729 uint8_t tmp = ((uint8_t *)buf)[i];
730
731 ((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i];
732 ((uint8_t *)buf)[length - 1 - i] = tmp;
733 }
734}
735
742static inline void sys_le_to_cpu(void *buf, size_t length)
743{
744 if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
745 sys_mem_swap(buf, length);
746 }
747}
748
755static inline void sys_cpu_to_le(void *buf, size_t length)
756{
757 if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
758 sys_mem_swap(buf, length);
759 }
760}
761
768static inline void sys_be_to_cpu(void *buf, size_t length)
769{
770 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
771 sys_mem_swap(buf, length);
772 }
773}
774
781static inline void sys_cpu_to_be(void *buf, size_t length)
782{
783 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
784 sys_mem_swap(buf, length);
785 }
786}
787
798static inline void sys_put_le(void *dst, const void *src, size_t length)
799{
800 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
801 (void)memcpy(dst, src, length);
802 } else {
803 sys_memcpy_swap(dst, src, length);
804 }
805}
806
817static inline void sys_put_be(void *dst, const void *src, size_t length)
818{
819 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
820 sys_memcpy_swap(dst, src, length);
821 } else {
822 (void)memcpy(dst, src, length);
823 }
824}
825
836static inline void sys_get_le(void *dst, const void *src, size_t length)
837{
838 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
839 (void)memcpy(dst, src, length);
840 } else {
841 sys_memcpy_swap(dst, src, length);
842 }
843}
844
855static inline void sys_get_be(void *dst, const void *src, size_t length)
856{
857 if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
858 sys_memcpy_swap(dst, src, length);
859 } else {
860 (void)memcpy(dst, src, length);
861 }
862}
863
864#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:698
static void sys_get_be(void *dst, const void *src, size_t length)
Get a buffer stored in big-endian format.
Definition byteorder.h:855
static uint64_t sys_get_le40(const uint8_t src[5])
Get a 40-bit integer stored in little-endian format.
Definition byteorder.h:650
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:439
static void sys_cpu_to_le(void *buf, size_t length)
Convert buffer from host endianness to little-endian.
Definition byteorder.h:755
static void sys_le_to_cpu(void *buf, size_t length)
Convert buffer from little-endian to host endianness.
Definition byteorder.h:742
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:365
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:409
static void sys_cpu_to_be(void *buf, size_t length)
Convert buffer from host endianness to big-endian.
Definition byteorder.h:781
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition byteorder.h:545
static void sys_be_to_cpu(void *buf, size_t length)
Convert buffer from big-endian to host endianness.
Definition byteorder.h:768
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition byteorder.h:605
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition byteorder.h:665
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition byteorder.h:680
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:335
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:798
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:350
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:499
static uint64_t sys_get_be40(const uint8_t src[5])
Get a 40-bit integer stored in big-endian format.
Definition byteorder.h:560
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition byteorder.h:515
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:484
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition byteorder.h:635
static uint32_t sys_get_le24(const uint8_t src[3])
Get a 24-bit integer stored in little-endian format.
Definition byteorder.h:620
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:469
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:394
static uint32_t sys_get_be24(const uint8_t src[3])
Get a 24-bit integer stored in big-endian format.
Definition byteorder.h:530
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition byteorder.h:575
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition byteorder.h:590
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:379
static void sys_mem_swap(void *buf, size_t length)
Swap buffer content.
Definition byteorder.h:724
static void sys_get_le(void *dst, const void *src, size_t length)
Get a buffer stored in little-endian format.
Definition byteorder.h:836
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:817
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:424
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:454
Macros to abstract toolchain specific capabilities.
Macro utilities.