| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2005 Jakub Jermar
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libc
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /** @file
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #ifndef _LIBC_BYTEORDER_H_
|
|---|
| 14 | #define _LIBC_BYTEORDER_H_
|
|---|
| 15 |
|
|---|
| 16 | #include <stdint.h>
|
|---|
| 17 |
|
|---|
| 18 | #if !(defined(__BE__) ^ defined(__LE__))
|
|---|
| 19 | #error The architecture must be either big-endian or little-endian.
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 | #ifdef __BE__
|
|---|
| 23 |
|
|---|
| 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))
|
|---|
| 27 |
|
|---|
| 28 | #define uint16_t_be2host(n) (n)
|
|---|
| 29 | #define uint32_t_be2host(n) (n)
|
|---|
| 30 | #define uint64_t_be2host(n) (n)
|
|---|
| 31 |
|
|---|
| 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))
|
|---|
| 35 |
|
|---|
| 36 | #define host2uint16_t_be(n) (n)
|
|---|
| 37 | #define host2uint32_t_be(n) (n)
|
|---|
| 38 | #define host2uint64_t_be(n) (n)
|
|---|
| 39 |
|
|---|
| 40 | #else
|
|---|
| 41 |
|
|---|
| 42 | #define uint16_t_le2host(n) (n)
|
|---|
| 43 | #define uint32_t_le2host(n) (n)
|
|---|
| 44 | #define uint64_t_le2host(n) (n)
|
|---|
| 45 |
|
|---|
| 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))
|
|---|
| 49 |
|
|---|
| 50 | #define host2uint16_t_le(n) (n)
|
|---|
| 51 | #define host2uint32_t_le(n) (n)
|
|---|
| 52 | #define host2uint64_t_le(n) (n)
|
|---|
| 53 |
|
|---|
| 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))
|
|---|
| 57 |
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 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))
|
|---|
| 64 |
|
|---|
| 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)
|
|---|
| 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)
|
|---|
| 90 |
|
|---|
| 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 |
|
|---|
| 117 | #endif
|
|---|
| 118 |
|
|---|
| 119 | /** @}
|
|---|
| 120 | */
|
|---|