| [afffa1e] | 1 | /*
|
|---|
| [d7f7a4a] | 2 | * SPDX-FileCopyrightText: 2005 Jakub Jermar
|
|---|
| [afffa1e] | 3 | *
|
|---|
| [d7f7a4a] | 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| [afffa1e] | 5 | */
|
|---|
| 6 |
|
|---|
| [fadd381] | 7 | /** @addtogroup libc
|
|---|
| [b2951e2] | 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /** @file
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| [4805495] | 13 | #ifndef _LIBC_BYTEORDER_H_
|
|---|
| 14 | #define _LIBC_BYTEORDER_H_
|
|---|
| [aa59fa0] | 15 |
|
|---|
| [711e33fc] | 16 | #include <stdint.h>
|
|---|
| [afffa1e] | 17 |
|
|---|
| [ac47b7c2] | 18 | #if !(defined(__BE__) ^ defined(__LE__))
|
|---|
| [1433ecda] | 19 | #error The architecture must be either big-endian or little-endian.
|
|---|
| [776f2e6] | 20 | #endif
|
|---|
| 21 |
|
|---|
| [ac47b7c2] | 22 | #ifdef __BE__
|
|---|
| [711e33fc] | 23 |
|
|---|
| [ac47b7c2] | 24 | #define uint16_t_le2host(n) (uint16_t_byteorder_swap(n))
|
|---|
| 25 | #define uint32_t_le2host(n) (uint32_t_byteorder_swap(n))
|
|---|
| 26 | #define uint64_t_le2host(n) (uint64_t_byteorder_swap(n))
|
|---|
| [711e33fc] | 27 |
|
|---|
| [ac47b7c2] | 28 | #define uint16_t_be2host(n) (n)
|
|---|
| 29 | #define uint32_t_be2host(n) (n)
|
|---|
| 30 | #define uint64_t_be2host(n) (n)
|
|---|
| [711e33fc] | 31 |
|
|---|
| [ac47b7c2] | 32 | #define host2uint16_t_le(n) (uint16_t_byteorder_swap(n))
|
|---|
| 33 | #define host2uint32_t_le(n) (uint32_t_byteorder_swap(n))
|
|---|
| 34 | #define host2uint64_t_le(n) (uint64_t_byteorder_swap(n))
|
|---|
| [f772105e] | 35 |
|
|---|
| [ac47b7c2] | 36 | #define host2uint16_t_be(n) (n)
|
|---|
| 37 | #define host2uint32_t_be(n) (n)
|
|---|
| 38 | #define host2uint64_t_be(n) (n)
|
|---|
| [f772105e] | 39 |
|
|---|
| [711e33fc] | 40 | #else
|
|---|
| 41 |
|
|---|
| [ac47b7c2] | 42 | #define uint16_t_le2host(n) (n)
|
|---|
| 43 | #define uint32_t_le2host(n) (n)
|
|---|
| 44 | #define uint64_t_le2host(n) (n)
|
|---|
| [711e33fc] | 45 |
|
|---|
| [ac47b7c2] | 46 | #define uint16_t_be2host(n) (uint16_t_byteorder_swap(n))
|
|---|
| 47 | #define uint32_t_be2host(n) (uint32_t_byteorder_swap(n))
|
|---|
| 48 | #define uint64_t_be2host(n) (uint64_t_byteorder_swap(n))
|
|---|
| [711e33fc] | 49 |
|
|---|
| [ac47b7c2] | 50 | #define host2uint16_t_le(n) (n)
|
|---|
| 51 | #define host2uint32_t_le(n) (n)
|
|---|
| 52 | #define host2uint64_t_le(n) (n)
|
|---|
| [f772105e] | 53 |
|
|---|
| [ac47b7c2] | 54 | #define host2uint16_t_be(n) (uint16_t_byteorder_swap(n))
|
|---|
| 55 | #define host2uint32_t_be(n) (uint32_t_byteorder_swap(n))
|
|---|
| 56 | #define host2uint64_t_be(n) (uint64_t_byteorder_swap(n))
|
|---|
| [f772105e] | 57 |
|
|---|
| [711e33fc] | 58 | #endif
|
|---|
| 59 |
|
|---|
| [47b7006] | 60 | #define htons(n) host2uint16_t_be((n))
|
|---|
| 61 | #define htonl(n) host2uint32_t_be((n))
|
|---|
| 62 | #define ntohs(n) uint16_t_be2host((n))
|
|---|
| 63 | #define ntohl(n) uint32_t_be2host((n))
|
|---|
| [2687bdb] | 64 |
|
|---|
| [5cbccd4] | 65 | #define uint8_t_be2host(n) (n)
|
|---|
| 66 | #define uint8_t_le2host(n) (n)
|
|---|
| 67 | #define host2uint8_t_be(n) (n)
|
|---|
| 68 | #define host2uint8_t_le(n) (n)
|
|---|
| [39f1c86] | 69 | #define host2uint8_t_le(n) (n)
|
|---|
| 70 |
|
|---|
| 71 | #define int8_t_le2host(n) uint8_t_le2host(n)
|
|---|
| 72 | #define int16_t_le2host(n) uint16_t_le2host(n)
|
|---|
| 73 | #define int32_t_le2host(n) uint32_t_le2host(n)
|
|---|
| 74 | #define int64_t_le2host(n) uint64_t_le2host(n)
|
|---|
| 75 |
|
|---|
| 76 | #define int8_t_be2host(n) uint8_t_be2host(n)
|
|---|
| 77 | #define int16_t_be2host(n) uint16_t_be2host(n)
|
|---|
| 78 | #define int32_t_be2host(n) uint32_t_be2host(n)
|
|---|
| 79 | #define int64_t_be2host(n) uint64_t_be2host(n)
|
|---|
| 80 |
|
|---|
| 81 | #define host2int8_t_le(n) host2uint8_t_le(n)
|
|---|
| 82 | #define host2int16_t_le(n) host2uint16_t_le(n)
|
|---|
| 83 | #define host2int32_t_le(n) host2uint32_t_le(n)
|
|---|
| 84 | #define host2int64_t_le(n) host2uint64_t_le(n)
|
|---|
| 85 |
|
|---|
| 86 | #define host2int8_t_be(n) host2uint8_t_be(n)
|
|---|
| 87 | #define host2int16_t_be(n) host2uint16_t_be(n)
|
|---|
| 88 | #define host2int32_t_be(n) host2uint32_t_be(n)
|
|---|
| 89 | #define host2int64_t_be(n) host2uint64_t_be(n)
|
|---|
| [5cbccd4] | 90 |
|
|---|
| [711e33fc] | 91 | static inline uint64_t uint64_t_byteorder_swap(uint64_t n)
|
|---|
| 92 | {
|
|---|
| 93 | return ((n & 0xff) << 56) |
|
|---|
| 94 | ((n & 0xff00) << 40) |
|
|---|
| 95 | ((n & 0xff0000) << 24) |
|
|---|
| 96 | ((n & 0xff000000LL) << 8) |
|
|---|
| 97 | ((n & 0xff00000000LL) >> 8) |
|
|---|
| 98 | ((n & 0xff0000000000LL) >> 24) |
|
|---|
| 99 | ((n & 0xff000000000000LL) >> 40) |
|
|---|
| 100 | ((n & 0xff00000000000000LL) >> 56);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | static inline uint32_t uint32_t_byteorder_swap(uint32_t n)
|
|---|
| 104 | {
|
|---|
| 105 | return ((n & 0xff) << 24) |
|
|---|
| 106 | ((n & 0xff00) << 8) |
|
|---|
| 107 | ((n & 0xff0000) >> 8) |
|
|---|
| 108 | ((n & 0xff000000) >> 24);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static inline uint16_t uint16_t_byteorder_swap(uint16_t n)
|
|---|
| 112 | {
|
|---|
| 113 | return ((n & 0xff) << 8) |
|
|---|
| 114 | ((n & 0xff00) >> 8);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| [afffa1e] | 117 | #endif
|
|---|
| [85882fc] | 118 |
|
|---|
| [fadd381] | 119 | /** @}
|
|---|
| [b2951e2] | 120 | */
|
|---|