Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
byteorder.h
Go to the documentation of this file.
1
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 <zephyr/sys/__assert.h>
17#include <zephyr/toolchain.h>
18
19#define BSWAP_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
20#define BSWAP_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \
21 (((x)) & 0xff00) | \
22 (((x) & 0xff) << 16)))
23#define BSWAP_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
24 (((x) >> 8) & 0xff00) | \
25 (((x) & 0xff00) << 8) | \
26 (((x) & 0xff) << 24)))
27#define BSWAP_48(x) ((uint64_t) ((((x) >> 40) & 0xff) | \
28 (((x) >> 24) & 0xff00) | \
29 (((x) >> 8) & 0xff0000) | \
30 (((x) & 0xff0000) << 8) | \
31 (((x) & 0xff00) << 24) | \
32 (((x) & 0xff) << 40)))
33#define BSWAP_64(x) ((uint64_t) ((((x) >> 56) & 0xff) | \
34 (((x) >> 40) & 0xff00) | \
35 (((x) >> 24) & 0xff0000) | \
36 (((x) >> 8) & 0xff000000) | \
37 (((x) & 0xff000000) << 8) | \
38 (((x) & 0xff0000) << 24) | \
39 (((x) & 0xff00) << 40) | \
40 (((x) & 0xff) << 56)))
41
213#ifdef CONFIG_LITTLE_ENDIAN
214#define sys_le16_to_cpu(val) (val)
215#define sys_cpu_to_le16(val) (val)
216#define sys_le24_to_cpu(val) (val)
217#define sys_cpu_to_le24(val) (val)
218#define sys_le32_to_cpu(val) (val)
219#define sys_cpu_to_le32(val) (val)
220#define sys_le48_to_cpu(val) (val)
221#define sys_cpu_to_le48(val) (val)
222#define sys_le64_to_cpu(val) (val)
223#define sys_cpu_to_le64(val) (val)
224#define sys_be16_to_cpu(val) BSWAP_16(val)
225#define sys_cpu_to_be16(val) BSWAP_16(val)
226#define sys_be24_to_cpu(val) BSWAP_24(val)
227#define sys_cpu_to_be24(val) BSWAP_24(val)
228#define sys_be32_to_cpu(val) BSWAP_32(val)
229#define sys_cpu_to_be32(val) BSWAP_32(val)
230#define sys_be48_to_cpu(val) BSWAP_48(val)
231#define sys_cpu_to_be48(val) BSWAP_48(val)
232#define sys_be64_to_cpu(val) BSWAP_64(val)
233#define sys_cpu_to_be64(val) BSWAP_64(val)
234
235#define sys_uint16_to_array(val) { \
236 ((val) & 0xff), \
237 (((val) >> 8) & 0xff)}
238
239#define sys_uint32_to_array(val) { \
240 ((val) & 0xff), \
241 (((val) >> 8) & 0xff), \
242 (((val) >> 16) & 0xff), \
243 (((val) >> 24) & 0xff)}
244
245#define sys_uint64_to_array(val) { \
246 ((val) & 0xff), \
247 (((val) >> 8) & 0xff), \
248 (((val) >> 16) & 0xff), \
249 (((val) >> 24) & 0xff), \
250 (((val) >> 32) & 0xff), \
251 (((val) >> 40) & 0xff), \
252 (((val) >> 48) & 0xff), \
253 (((val) >> 56) & 0xff)}
254
255#else
256#define sys_le16_to_cpu(val) BSWAP_16(val)
257#define sys_cpu_to_le16(val) BSWAP_16(val)
258#define sys_le24_to_cpu(val) BSWAP_24(val)
259#define sys_cpu_to_le24(val) BSWAP_24(val)
260#define sys_le32_to_cpu(val) BSWAP_32(val)
261#define sys_cpu_to_le32(val) BSWAP_32(val)
262#define sys_le48_to_cpu(val) BSWAP_48(val)
263#define sys_cpu_to_le48(val) BSWAP_48(val)
264#define sys_le64_to_cpu(val) BSWAP_64(val)
265#define sys_cpu_to_le64(val) BSWAP_64(val)
266#define sys_be16_to_cpu(val) (val)
267#define sys_cpu_to_be16(val) (val)
268#define sys_be24_to_cpu(val) (val)
269#define sys_cpu_to_be24(val) (val)
270#define sys_be32_to_cpu(val) (val)
271#define sys_cpu_to_be32(val) (val)
272#define sys_be48_to_cpu(val) (val)
273#define sys_cpu_to_be48(val) (val)
274#define sys_be64_to_cpu(val) (val)
275#define sys_cpu_to_be64(val) (val)
276
277#define sys_uint16_to_array(val) { \
278 (((val) >> 8) & 0xff), \
279 ((val) & 0xff)}
280
281#define sys_uint32_to_array(val) { \
282 (((val) >> 24) & 0xff), \
283 (((val) >> 16) & 0xff), \
284 (((val) >> 8) & 0xff), \
285 ((val) & 0xff)}
286
287#define sys_uint64_to_array(val) { \
288 (((val) >> 56) & 0xff), \
289 (((val) >> 48) & 0xff), \
290 (((val) >> 40) & 0xff), \
291 (((val) >> 32) & 0xff), \
292 (((val) >> 24) & 0xff), \
293 (((val) >> 16) & 0xff), \
294 (((val) >> 8) & 0xff), \
295 ((val) & 0xff)}
296
297#endif
298
308static inline void sys_put_be16(uint16_t val, uint8_t dst[2])
309{
310 dst[0] = val >> 8;
311 dst[1] = val;
312}
313
323static inline void sys_put_be24(uint32_t val, uint8_t dst[3])
324{
325 dst[0] = val >> 16;
326 sys_put_be16(val, &dst[1]);
327}
328
338static inline void sys_put_be32(uint32_t val, uint8_t dst[4])
339{
340 sys_put_be16(val >> 16, dst);
341 sys_put_be16(val, &dst[2]);
342}
343
353static inline void sys_put_be48(uint64_t val, uint8_t dst[6])
354{
355 sys_put_be16(val >> 32, dst);
356 sys_put_be32(val, &dst[2]);
357}
358
368static inline void sys_put_be64(uint64_t val, uint8_t dst[8])
369{
370 sys_put_be32(val >> 32, dst);
371 sys_put_be32(val, &dst[4]);
372}
373
383static inline void sys_put_le16(uint16_t val, uint8_t dst[2])
384{
385 dst[0] = val;
386 dst[1] = val >> 8;
387}
388
398static inline void sys_put_le24(uint32_t val, uint8_t dst[3])
399{
400 sys_put_le16(val, dst);
401 dst[2] = val >> 16;
402}
403
413static inline void sys_put_le32(uint32_t val, uint8_t dst[4])
414{
415 sys_put_le16(val, dst);
416 sys_put_le16(val >> 16, &dst[2]);
417}
418
428static inline void sys_put_le48(uint64_t val, uint8_t dst[6])
429{
430 sys_put_le32(val, dst);
431 sys_put_le16(val >> 32, &dst[4]);
432}
433
443static inline void sys_put_le64(uint64_t val, uint8_t dst[8])
444{
445 sys_put_le32(val, dst);
446 sys_put_le32(val >> 32, &dst[4]);
447}
448
459static inline uint16_t sys_get_be16(const uint8_t src[2])
460{
461 return ((uint16_t)src[0] << 8) | src[1];
462}
463
474static inline uint32_t sys_get_be24(const uint8_t src[3])
475{
476 return ((uint32_t)src[0] << 16) | sys_get_be16(&src[1]);
477}
478
489static inline uint32_t sys_get_be32(const uint8_t src[4])
490{
491 return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
492}
493
504static inline uint64_t sys_get_be48(const uint8_t src[6])
505{
506 return ((uint64_t)sys_get_be32(&src[0]) << 16) | sys_get_be16(&src[4]);
507}
508
519static inline uint64_t sys_get_be64(const uint8_t src[8])
520{
521 return ((uint64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]);
522}
523
534static inline uint16_t sys_get_le16(const uint8_t src[2])
535{
536 return ((uint16_t)src[1] << 8) | src[0];
537}
538
549static inline uint32_t sys_get_le24(const uint8_t src[3])
550{
551 return ((uint32_t)src[2] << 16) | sys_get_le16(&src[0]);
552}
553
564static inline uint32_t sys_get_le32(const uint8_t src[4])
565{
566 return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
567}
568
579static inline uint64_t sys_get_le48(const uint8_t src[6])
580{
581 return ((uint64_t)sys_get_le32(&src[2]) << 16) | sys_get_le16(&src[0]);
582}
583
594static inline uint64_t sys_get_le64(const uint8_t src[8])
595{
596 return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]);
597}
598
612static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
613{
614 uint8_t *pdst = (uint8_t *)dst;
615 const uint8_t *psrc = (const uint8_t *)src;
616
617 __ASSERT(((psrc < pdst && (psrc + length) <= pdst) ||
618 (psrc > pdst && (pdst + length) <= psrc)),
619 "Source and destination buffers must not overlap");
620
621 psrc += length - 1;
622
623 for (; length > 0; length--) {
624 *pdst++ = *psrc--;
625 }
626}
627
638static inline void sys_mem_swap(void *buf, size_t length)
639{
640 size_t i;
641
642 for (i = 0; i < (length/2); i++) {
643 uint8_t tmp = ((uint8_t *)buf)[i];
644
645 ((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i];
646 ((uint8_t *)buf)[length - 1 - i] = tmp;
647 }
648}
649
650#endif /* ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ */
__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
static void sys_memcpy_swap(void *dst, const void *src, size_t length)
Swap one buffer content into another.
Definition: byteorder.h:612
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:398
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:338
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:368
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition: byteorder.h:489
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition: byteorder.h:534
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition: byteorder.h:579
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition: byteorder.h:594
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:308
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:323
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:443
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition: byteorder.h:459
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:428
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition: byteorder.h:564
static uint32_t sys_get_le24(const uint8_t src[3])
Get a 24-bit integer stored in little-endian format.
Definition: byteorder.h:549
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:353
static uint32_t sys_get_be24(const uint8_t src[3])
Get a 24-bit integer stored in big-endian format.
Definition: byteorder.h:474
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition: byteorder.h:504
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition: byteorder.h:519
static void sys_mem_swap(void *buf, size_t length)
Swap buffer content.
Definition: byteorder.h:638
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:383
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:413
Macros to abstract toolchain specific capabilities.