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__)
44 * htobe/betoh are not defined for glibc <2.9, so add them
45 * explicitly if they are missing.
48 /* Conversion interfaces. */
49 # include <byteswap.h>
51 # if __BYTE_ORDER == __LITTLE_ENDIAN
53 # define htobe16(x) __bswap_16(x)
56 # define htole16(x) (x)
59 # define be16toh(x) __bswap_16(x)
62 # define le16toh(x) (x)
66 # define htobe32(x) __bswap_32(x)
69 # define htole32(x) (x)
72 # define be32toh(x) __bswap_32(x)
75 # define le32toh(x) (x)
79 # define htobe64(x) __bswap_64(x)
82 # define htole64(x) (x)
85 # define be64toh(x) __bswap_64(x)
88 # define le64toh(x) (x)
91 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
93 # define htobe16(x) (x)
96 # define htole16(x) __bswap_16(x)
99 # define be16toh(x) (x)
102 # define le16toh(x) __bswap_16(x)
106 # define htobe32(x) (x)
109 # define htole32(x) __bswap_32(x)
112 # define be32toh(x) (x)
115 # define le32toh(x) __bswap_32(x)
119 # define htobe64(x) (x)
122 # define htole64(x) __bswap_64(x)
125 # define be64toh(x) (x)
128 # define le64toh(x) __bswap_64(x)
131 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
132 #endif /* __USE_BSD */
134 #elif defined(__FreeBSD__)
135 #include <machine/endian.h>
137 #elif defined(__sun__)
138 #include <sys/byteorder.h>
140 #define __BIG_ENDIAN 4321
141 #endif /* __BIG_ENDIAN */
142 #ifndef __LITTLE_ENDIAN
143 #define __LITTLE_ENDIAN 1234
144 #endif /* __LITTLE_ENDIAN */
146 #ifdef _LITTLE_ENDIAN
147 #define __BYTE_ORDER __LITTLE_ENDIAN
148 #endif /* _LITTLE_ENDIAN */
150 #define __BYTE_ORDER __BIG_ENDIAN
151 #endif /* _BIG_ENDIAN */
153 #define LITTLE_ENDIAN __LITTLE_ENDIAN
154 #define BIG_ENDIAN __BIG_ENDIAN
155 #define BYTE_ORDER __BYTE_ORDER
157 #define betoh16(x) BE_16(x)
158 #define letoh16(x) LE_16(x)
159 #define betoh32(x) BE_32(x)
160 #define letoh32(x) LE_32(x)
161 #define betoh64(x) BE_64(x)
162 #define letoh64(x) LE_64(x)
163 #define htobe16(x) BE_16(x)
164 #define be16toh(x) BE_16(x)
165 #define htobe32(x) BE_32(x)
166 #define be32toh(x) BE_32(x)
167 #define htobe64(x) BE_64(x)
168 #define be64toh(x) BE_64(x)
170 #elif defined(__APPLE__)
171 # include <machine/endian.h>
172 # include <libkern/OSByteOrder.h>
174 # if BYTE_ORDER == LITTLE_ENDIAN
175 # define htobe16(x) OSSwapConstInt16(x)
176 # define htole16(x) (x)
177 # define be16toh(x) OSSwapConstInt16(x)
178 # define le16toh(x) (x)
180 # define htobe32(x) OSSwapConstInt32(x)
181 # define htole32(x) (x)
182 # define be32toh(x) OSSwapConstInt32(x)
183 # define le32toh(x) (x)
185 # define htobe64(x) OSSwapConstInt64(x)
186 # define htole64(x) (x)
187 # define be64toh(x) OSSwapConstInt64(x)
188 # define le64toh(x) (x)
190 # else /* BYTE_ORDER == LITTLE_ENDIAN */
191 # define htobe16(x) (x)
192 # define htole16(x) OSSwapConstInt16(x)
193 # define be16toh(x) (x)
194 # define le16toh(x) OSSwapConstInt16(x)
196 # define htobe32(x) (x)
197 # define htole32(x) OSSwapConstInt32(x)
198 # define be32toh(x) (x)
199 # define le32toh(x) OSSwapConstInt32(x)
201 # define htobe64(x) (x)
202 # define htole64(x) OSSwapConstInt64(x)
203 # define be64toh(x) (x)
204 # define le64toh(x) OSSwapConstInt64(x)
208 #error "Please add support for your OS."
211 #endif /* _COMPAT_ENDIAN_H */
This page took 0.03818 seconds and 6 git commands to generate.