2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 * This compat header provides the following defines:
15 * And functions / macros :
37 #ifndef _COMPAT_ENDIAN_H
38 #define _COMPAT_ENDIAN_H
40 #if defined(__linux__) || defined(__CYGWIN__)
45 * htobe/betoh are not defined for glibc <2.9, so add them
46 * explicitly if they are missing.
49 /* Conversion interfaces. */
50 # include <byteswap.h>
52 # if __BYTE_ORDER == __LITTLE_ENDIAN
54 # define htobe16(x) __bswap_16(x)
57 # define htole16(x) (x)
60 # define be16toh(x) __bswap_16(x)
63 # define le16toh(x) (x)
67 # define htobe32(x) __bswap_32(x)
70 # define htole32(x) (x)
73 # define be32toh(x) __bswap_32(x)
76 # define le32toh(x) (x)
80 # define htobe64(x) __bswap_64(x)
83 # define htole64(x) (x)
86 # define be64toh(x) __bswap_64(x)
89 # define le64toh(x) (x)
92 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
94 # define htobe16(x) (x)
97 # define htole16(x) __bswap_16(x)
100 # define be16toh(x) (x)
103 # define le16toh(x) __bswap_16(x)
107 # define htobe32(x) (x)
110 # define htole32(x) __bswap_32(x)
113 # define be32toh(x) (x)
116 # define le32toh(x) __bswap_32(x)
120 # define htobe64(x) (x)
123 # define htole64(x) __bswap_64(x)
126 # define be64toh(x) (x)
129 # define le64toh(x) __bswap_64(x)
132 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
133 #endif /* __USE_BSD */
135 #elif defined(__FreeBSD__)
136 #include <sys/endian.h>
138 #define bswap_16(x) bswap16(x)
139 #define bswap_32(x) bswap32(x)
140 #define bswap_64(x) bswap64(x)
142 #elif defined(__sun__)
143 #include <sys/byteorder.h>
145 #define __BIG_ENDIAN 4321
146 #endif /* __BIG_ENDIAN */
147 #ifndef __LITTLE_ENDIAN
148 #define __LITTLE_ENDIAN 1234
149 #endif /* __LITTLE_ENDIAN */
151 #ifdef _LITTLE_ENDIAN
152 #define __BYTE_ORDER __LITTLE_ENDIAN
153 #endif /* _LITTLE_ENDIAN */
155 #define __BYTE_ORDER __BIG_ENDIAN
156 #endif /* _BIG_ENDIAN */
158 #define LITTLE_ENDIAN __LITTLE_ENDIAN
159 #define BIG_ENDIAN __BIG_ENDIAN
160 #define BYTE_ORDER __BYTE_ORDER
162 #define betoh16(x) BE_16(x)
163 #define letoh16(x) LE_16(x)
164 #define betoh32(x) BE_32(x)
165 #define letoh32(x) LE_32(x)
166 #define betoh64(x) BE_64(x)
167 #define letoh64(x) LE_64(x)
168 #define htobe16(x) BE_16(x)
169 #define be16toh(x) BE_16(x)
170 #define htobe32(x) BE_32(x)
171 #define be32toh(x) BE_32(x)
172 #define htobe64(x) BE_64(x)
173 #define be64toh(x) BE_64(x)
175 #elif defined(__APPLE__)
176 # include <machine/endian.h>
177 # include <libkern/OSByteOrder.h>
179 # if BYTE_ORDER == LITTLE_ENDIAN
180 # define htobe16(x) OSSwapConstInt16(x)
181 # define htole16(x) (x)
182 # define be16toh(x) OSSwapConstInt16(x)
183 # define le16toh(x) (x)
185 # define htobe32(x) OSSwapConstInt32(x)
186 # define htole32(x) (x)
187 # define be32toh(x) OSSwapConstInt32(x)
188 # define le32toh(x) (x)
190 # define htobe64(x) OSSwapConstInt64(x)
191 # define htole64(x) (x)
192 # define be64toh(x) OSSwapConstInt64(x)
193 # define le64toh(x) (x)
195 # else /* BYTE_ORDER == LITTLE_ENDIAN */
196 # define htobe16(x) (x)
197 # define htole16(x) OSSwapConstInt16(x)
198 # define be16toh(x) (x)
199 # define le16toh(x) OSSwapConstInt16(x)
201 # define htobe32(x) (x)
202 # define htole32(x) OSSwapConstInt32(x)
203 # define be32toh(x) (x)
204 # define le32toh(x) OSSwapConstInt32(x)
206 # define htobe64(x) (x)
207 # define htole64(x) OSSwapConstInt64(x)
208 # define be64toh(x) (x)
209 # define le64toh(x) OSSwapConstInt64(x)
213 #error "Please add support for your OS."
216 #endif /* _COMPAT_ENDIAN_H */