endian.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Otto-von-Guericke-Universität Magdeburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
9 #pragma once
10 
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #ifdef DOXYGEN
35 #define LITTLE_ENDIAN magic-number
36 
40 #define BIG_ENDIAN magic-number
41 
45 #define PDP_ENDIAN magic-number
46 
55 #define BYTE_ORDER <LITTLE_ENDIAN or BIG_ENDIAN>
56 
57 uint16_t htobe16(uint16_t host_16bits);
58 uint16_t htole16(uint16_t host_16bits);
59 uint16_t be16toh(uint16_t big_endian_16bits);
60 uint16_t le16toh(uint16_t little_endian_16bits);
62 uint32_t htobe32(uint32_t host_32bits);
63 uint32_t htole32(uint32_t host_32bits);
64 uint32_t be32toh(uint32_t big_endian_32bits);
65 uint32_t le32toh(uint32_t little_endian_32bits);
67 uint64_t htobe64(uint64_t host_64bits);
68 uint64_t htole64(uint64_t host_64bits);
69 uint64_t be64toh(uint64_t big_endian_64bits);
70 uint64_t le64toh(uint64_t little_endian_64bits);
72 #else /* DOXYGEN */
73 
74 /* Depending on the version of newlib used, newlib may provide them indirectly
75  * as well. We don't want to redefine them in this case */
76 #ifndef LITTLE_ENDIAN
77 # define LITTLE_ENDIAN 1234
78 #endif
79 #ifndef BIG_ENDIAN
80 # define BIG_ENDIAN 4321
81 #endif
82 #ifndef PDP_ENDIAN
83 # define PDP_ENDIAN 3412
84 #endif
85 #ifndef BYTE_ORDER
86 # define BYTE_ORDER __BYTE_ORDER__
87 #endif
88 
89 /* But to avoid lots of pain in the ass: Let's at least make sure everyone
90  * agrees on what magic number is what */
91 #if (LITTLE_ENDIAN != 1234) || (BIG_ENDIAN != 4321) || (PDP_ENDIAN != 3412)
92 # error "Mismatching magic numbers to refer to endianness"
93 #endif
94 
95 #if BYTE_ORDER == LITTLE_ENDIAN
96 # ifndef htobe16
97 # define htobe16(_x) __builtin_bswap16(_x)
98 # endif
99 # ifndef htole16
100 # define htole16(_x) ((uint16_t)(_x))
101 # endif
102 # ifndef be16toh
103 # define be16toh(_x) __builtin_bswap16(_x)
104 # endif
105 # ifndef le16toh
106 # define le16toh(_x) ((uint16_t)(_x))
107 # endif
108 # ifndef htobe32
109 # define htobe32(_x) __builtin_bswap32(_x)
110 # endif
111 # ifndef htole32
112 # define htole32(_x) ((uint32_t)(_x))
113 # endif
114 # ifndef be32toh
115 # define be32toh(_x) __builtin_bswap32(_x)
116 # endif
117 # ifndef le32toh
118 # define le32toh(_x) ((uint32_t)(_x))
119 # endif
120 # ifndef htobe64
121 # define htobe64(_x) __builtin_bswap64(_x)
122 # endif
123 # ifndef htole64
124 # define htole64(_x) ((uint64_t)(_x))
125 # endif
126 # ifndef be64toh
127 # define be64toh(_x) __builtin_bswap64(_x)
128 # endif
129 # ifndef le64toh
130 # define le64toh(_x) ((uint64_t)(_x))
131 # endif
132 #elif BYTE_ORDER == BIG_ENDIAN
133 # ifndef htole16
134 # define htole16(_x) __builtin_bswap16(_x)
135 # endif
136 # ifndef htobe16
137 # define htobe16(_x) ((uint16_t)(_x))
138 # endif
139 # ifndef le16toh
140 # define le16toh(_x) __builtin_bswap16(_x)
141 # endif
142 # ifndef be16toh
143 # define be16toh(_x) ((uint16_t)(_x))
144 # endif
145 # ifndef htole32
146 # define htole32(_x) __builtin_bswap32(_x)
147 # endif
148 # ifndef htobe32
149 # define htobe32(_x) ((uint32_t)(_x))
150 # endif
151 # ifndef le32toh
152 # define le32toh(_x) __builtin_bswap32(_x)
153 # endif
154 # ifndef be32toh
155 # define be32toh(_x) ((uint32_t)(_x))
156 # endif
157 # ifndef htole64
158 # define htole64(_x) __builtin_bswap64(_x)
159 # endif
160 # ifndef htobe64
161 # define htobe64(_x) ((uint64_t)(_x))
162 # endif
163 # ifndef le64toh
164 # define le64toh(_x) __builtin_bswap64(_x)
165 # endif
166 # ifndef be64toh
167 # define be64toh(_x) ((uint64_t)(_x))
168 # endif
169 #else
170 # error "Byte order not supported"
171 #endif
172 
173 #endif /* DOXYGEN */
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
uint16_t be16toh(uint16_t big_endian_16bits)
big endian to host, 16 bit
uint16_t htobe16(uint16_t host_16bits)
host to big endian, 16 bit
uint32_t le32toh(uint32_t little_endian_32bits)
little endian to host, 32 bit
uint32_t htobe32(uint32_t host_32bits)
host to big endian, 32 bit
uint16_t le16toh(uint16_t little_endian_16bits)
little endian to host, 16 bit
uint64_t htobe64(uint64_t host_64bits)
host to big endian, 64 bit
uint64_t be64toh(uint64_t big_endian_64bits)
big endian to host, 64 bit
uint32_t be32toh(uint32_t big_endian_32bits)
big endian to host, 32 bit
uint64_t htole64(uint64_t host_64bits)
host to little endian, 64 bit
uint32_t htole32(uint32_t host_32bits)
host to little endian, 32 bit
uint16_t htole16(uint16_t host_16bits)
host to little endian, 16 bit
uint64_t le64toh(uint64_t little_endian_64bits)
little endian to host, 64 bit