11#ifndef ZEPHYR_INCLUDE_SYS_BYTEORDER_H_
12#define ZEPHYR_INCLUDE_SYS_BYTEORDER_H_
21#if HAS_BUILTIN(__builtin_bswap16)
22#define BSWAP_16(x) ((uint16_t) __builtin_bswap16(x))
24#define BSWAP_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
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))
31#define BSWAP_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \
33 (((x) & 0xff) << 16)))
35#define BSWAP_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
36 (((x) >> 8) & 0xff00) | \
37 (((x) & 0xff00) << 8) | \
38 (((x) & 0xff) << 24)))
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))
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)))
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)))
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)))
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)
266#define sys_uint16_to_array(val) { \
268 (((val) >> 8) & 0xff)}
270#define sys_uint32_to_array(val) { \
272 (((val) >> 8) & 0xff), \
273 (((val) >> 16) & 0xff), \
274 (((val) >> 24) & 0xff)}
276#define sys_uint64_to_array(val) { \
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)}
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)
312#define sys_uint16_to_array(val) { \
313 (((val) >> 8) & 0xff), \
316#define sys_uint32_to_array(val) { \
317 (((val) >> 24) & 0xff), \
318 (((val) >> 16) & 0xff), \
319 (((val) >> 8) & 0xff), \
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), \
525 return ((
uint16_t)src[0] << 8) | src[1];
615 return ((
uint16_t)src[1] << 8) | src[0];
711 __ASSERT(((psrc < pdst && (psrc + length) <= pdst) ||
712 (psrc > pdst && (pdst + length) <= psrc)),
713 "Source and destination buffers must not overlap");
717 for (; length > 0; length--) {
736 for (i = 0; i < (length/2); i++) {
740 ((
uint8_t *)buf)[length - 1 - i] = tmp;
806static inline void sys_put_le(
void *dst,
const void *src,
size_t length)
809 (void)
memcpy(dst, src, length);
825static inline void sys_put_be(
void *dst,
const void *src,
size_t length)
830 (void)
memcpy(dst, src, length);
844static inline void sys_get_le(
void *dst,
const void *src,
size_t length)
847 (void)
memcpy(dst, src, length);
863static inline void sys_get_be(
void *dst,
const void *src,
size_t length)
868 (void)
memcpy(dst, src, length);
#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