Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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/* Internal helpers only used by the sys_* APIs further below */
20#define __bswap_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
21#define __bswap_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \
22 (((x)) & 0xff00) | \
23 (((x) & 0xff) << 16)))
24#define __bswap_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \
25 (((x) >> 8) & 0xff00) | \
26 (((x) & 0xff00) << 8) | \
27 (((x) & 0xff) << 24)))
28#define __bswap_48(x) ((uint64_t) ((((x) >> 40) & 0xff) | \
29 (((x) >> 24) & 0xff00) | \
30 (((x) >> 8) & 0xff0000) | \
31 (((x) & 0xff0000) << 8) | \
32 (((x) & 0xff00) << 24) | \
33 (((x) & 0xff) << 40)))
34#define __bswap_64(x) ((uint64_t) ((((x) >> 56) & 0xff) | \
35 (((x) >> 40) & 0xff00) | \
36 (((x) >> 24) & 0xff0000) | \
37 (((x) >> 8) & 0xff000000) | \
38 (((x) & 0xff000000) << 8) | \
39 (((x) & 0xff0000) << 24) | \
40 (((x) & 0xff00) << 40) | \
41 (((x) & 0xff) << 56)))
42
171#ifdef CONFIG_LITTLE_ENDIAN
172#define sys_le16_to_cpu(val) (val)
173#define sys_cpu_to_le16(val) (val)
174#define sys_le24_to_cpu(val) (val)
175#define sys_cpu_to_le24(val) (val)
176#define sys_le32_to_cpu(val) (val)
177#define sys_cpu_to_le32(val) (val)
178#define sys_le48_to_cpu(val) (val)
179#define sys_cpu_to_le48(val) (val)
180#define sys_le64_to_cpu(val) (val)
181#define sys_cpu_to_le64(val) (val)
182#define sys_be16_to_cpu(val) __bswap_16(val)
183#define sys_cpu_to_be16(val) __bswap_16(val)
184#define sys_be24_to_cpu(val) __bswap_24(val)
185#define sys_cpu_to_be24(val) __bswap_24(val)
186#define sys_be32_to_cpu(val) __bswap_32(val)
187#define sys_cpu_to_be32(val) __bswap_32(val)
188#define sys_be48_to_cpu(val) __bswap_48(val)
189#define sys_cpu_to_be48(val) __bswap_48(val)
190#define sys_be64_to_cpu(val) __bswap_64(val)
191#define sys_cpu_to_be64(val) __bswap_64(val)
192#else
193#define sys_le16_to_cpu(val) __bswap_16(val)
194#define sys_cpu_to_le16(val) __bswap_16(val)
195#define sys_le24_to_cpu(val) __bswap_24(val)
196#define sys_cpu_to_le24(val) __bswap_24(val)
197#define sys_le32_to_cpu(val) __bswap_32(val)
198#define sys_cpu_to_le32(val) __bswap_32(val)
199#define sys_le48_to_cpu(val) __bswap_48(val)
200#define sys_cpu_to_le48(val) __bswap_48(val)
201#define sys_le64_to_cpu(val) __bswap_64(val)
202#define sys_cpu_to_le64(val) __bswap_64(val)
203#define sys_be16_to_cpu(val) (val)
204#define sys_cpu_to_be16(val) (val)
205#define sys_be24_to_cpu(val) (val)
206#define sys_cpu_to_be24(val) (val)
207#define sys_be32_to_cpu(val) (val)
208#define sys_cpu_to_be32(val) (val)
209#define sys_be48_to_cpu(val) (val)
210#define sys_cpu_to_be48(val) (val)
211#define sys_be64_to_cpu(val) (val)
212#define sys_cpu_to_be64(val) (val)
213#endif
214
224static inline void sys_put_be16(uint16_t val, uint8_t dst[2])
225{
226 dst[0] = val >> 8;
227 dst[1] = val;
228}
229
239static inline void sys_put_be24(uint32_t val, uint8_t dst[3])
240{
241 dst[0] = val >> 16;
242 sys_put_be16(val, &dst[1]);
243}
244
254static inline void sys_put_be32(uint32_t val, uint8_t dst[4])
255{
256 sys_put_be16(val >> 16, dst);
257 sys_put_be16(val, &dst[2]);
258}
259
269static inline void sys_put_be48(uint64_t val, uint8_t dst[6])
270{
271 sys_put_be16(val >> 32, dst);
272 sys_put_be32(val, &dst[2]);
273}
274
284static inline void sys_put_be64(uint64_t val, uint8_t dst[8])
285{
286 sys_put_be32(val >> 32, dst);
287 sys_put_be32(val, &dst[4]);
288}
289
299static inline void sys_put_le16(uint16_t val, uint8_t dst[2])
300{
301 dst[0] = val;
302 dst[1] = val >> 8;
303}
304
314static inline void sys_put_le24(uint32_t val, uint8_t dst[3])
315{
316 sys_put_le16(val, dst);
317 dst[2] = val >> 16;
318}
319
329static inline void sys_put_le32(uint32_t val, uint8_t dst[4])
330{
331 sys_put_le16(val, dst);
332 sys_put_le16(val >> 16, &dst[2]);
333}
334
344static inline void sys_put_le48(uint64_t val, uint8_t dst[6])
345{
346 sys_put_le32(val, dst);
347 sys_put_le16(val >> 32, &dst[4]);
348}
349
359static inline void sys_put_le64(uint64_t val, uint8_t dst[8])
360{
361 sys_put_le32(val, dst);
362 sys_put_le32(val >> 32, &dst[4]);
363}
364
375static inline uint16_t sys_get_be16(const uint8_t src[2])
376{
377 return ((uint16_t)src[0] << 8) | src[1];
378}
379
390static inline uint32_t sys_get_be24(const uint8_t src[3])
391{
392 return ((uint32_t)src[0] << 16) | sys_get_be16(&src[1]);
393}
394
405static inline uint32_t sys_get_be32(const uint8_t src[4])
406{
407 return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
408}
409
420static inline uint64_t sys_get_be48(const uint8_t src[6])
421{
422 return ((uint64_t)sys_get_be32(&src[0]) << 16) | sys_get_be16(&src[4]);
423}
424
435static inline uint64_t sys_get_be64(const uint8_t src[8])
436{
437 return ((uint64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]);
438}
439
450static inline uint16_t sys_get_le16(const uint8_t src[2])
451{
452 return ((uint16_t)src[1] << 8) | src[0];
453}
454
465static inline uint32_t sys_get_le24(const uint8_t src[3])
466{
467 return ((uint32_t)src[2] << 16) | sys_get_le16(&src[0]);
468}
469
480static inline uint32_t sys_get_le32(const uint8_t src[4])
481{
482 return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
483}
484
495static inline uint64_t sys_get_le48(const uint8_t src[6])
496{
497 return ((uint64_t)sys_get_le32(&src[2]) << 16) | sys_get_le16(&src[0]);
498}
499
510static inline uint64_t sys_get_le64(const uint8_t src[8])
511{
512 return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]);
513}
514
528static inline void sys_memcpy_swap(void *dst, const void *src, size_t length)
529{
530 uint8_t *pdst = (uint8_t *)dst;
531 const uint8_t *psrc = (const uint8_t *)src;
532
533 __ASSERT(((psrc < pdst && (psrc + length) <= pdst) ||
534 (psrc > pdst && (pdst + length) <= psrc)),
535 "Source and destination buffers must not overlap");
536
537 psrc += length - 1;
538
539 for (; length > 0; length--) {
540 *pdst++ = *psrc--;
541 }
542}
543
554static inline void sys_mem_swap(void *buf, size_t length)
555{
556 size_t i;
557
558 for (i = 0; i < (length/2); i++) {
559 uint8_t tmp = ((uint8_t *)buf)[i];
560
561 ((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i];
562 ((uint8_t *)buf)[length - 1 - i] = tmp;
563 }
564}
565
566#endif /* ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ */
static void sys_memcpy_swap(void *dst, const void *src, size_t length)
Swap one buffer content into another.
Definition: byteorder.h:528
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:314
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:254
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:284
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition: byteorder.h:405
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition: byteorder.h:450
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition: byteorder.h:495
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition: byteorder.h:510
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:224
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:239
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:359
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition: byteorder.h:375
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:344
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition: byteorder.h:480
static uint32_t sys_get_le24(const uint8_t src[3])
Get a 24-bit integer stored in big-endian format.
Definition: byteorder.h:465
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:269
static uint32_t sys_get_be24(const uint8_t src[3])
Get a 24-bit integer stored in big-endian format.
Definition: byteorder.h:390
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition: byteorder.h:420
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition: byteorder.h:435
static void sys_mem_swap(void *buf, size_t length)
Swap buffer content.
Definition: byteorder.h:554
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:299
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:329
__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
Macros to abstract toolchain specific capabilities.