2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef _COMPAT_ENDIAN_H
9 #define _COMPAT_ENDIAN_H
11 #if defined(__linux__) || defined(__CYGWIN__)
15 * htobe/betoh are not defined for glibc <2.9, so add them
16 * explicitly if they are missing.
19 /* Conversion interfaces. */
20 # include <byteswap.h>
22 # if __BYTE_ORDER == __LITTLE_ENDIAN
24 # define htobe16(x) __bswap_16(x)
27 # define htole16(x) (x)
30 # define be16toh(x) __bswap_16(x)
33 # define le16toh(x) (x)
37 # define htobe32(x) __bswap_32(x)
40 # define htole32(x) (x)
43 # define be32toh(x) __bswap_32(x)
46 # define le32toh(x) (x)
50 # define htobe64(x) __bswap_64(x)
53 # define htole64(x) (x)
56 # define be64toh(x) __bswap_64(x)
59 # define le64toh(x) (x)
62 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
64 # define htobe16(x) (x)
67 # define htole16(x) __bswap_16(x)
70 # define be16toh(x) (x)
73 # define le16toh(x) __bswap_16(x)
77 # define htobe32(x) (x)
80 # define htole32(x) __bswap_32(x)
83 # define be32toh(x) (x)
86 # define le32toh(x) __bswap_32(x)
90 # define htobe64(x) (x)
93 # define htole64(x) __bswap_64(x)
96 # define be64toh(x) (x)
99 # define le64toh(x) __bswap_64(x)
102 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
103 #endif /* __USE_BSD */
105 #elif defined(__FreeBSD__)
106 #include <machine/endian.h>
108 #elif defined(__sun__)
109 #include <sys/byteorder.h>
111 #define __BIG_ENDIAN 4321
112 #endif /* __BIG_ENDIAN */
113 #ifndef __LITTLE_ENDIAN
114 #define __LITTLE_ENDIAN 1234
115 #endif /* __LITTLE_ENDIAN */
117 #ifdef _LITTLE_ENDIAN
118 #define __BYTE_ORDER __LITTLE_ENDIAN
119 #endif /* _LITTLE_ENDIAN */
121 #define __BYTE_ORDER __BIG_ENDIAN
122 #endif /* _BIG_ENDIAN */
124 #define LITTLE_ENDIAN __LITTLE_ENDIAN
125 #define BIG_ENDIAN __BIG_ENDIAN
126 #define PDP_ENDIAN __PDP_ENDIAN
127 #define BYTE_ORDER __BYTE_ORDER
129 #define betoh16(x) BE_16(x)
130 #define letoh16(x) LE_16(x)
131 #define betoh32(x) BE_32(x)
132 #define letoh32(x) LE_32(x)
133 #define betoh64(x) BE_64(x)
134 #define letoh64(x) LE_64(x)
135 #define htobe16(x) BE_16(x)
136 #define be16toh(x) BE_16(x)
137 #define htobe32(x) BE_32(x)
138 #define be32toh(x) BE_32(x)
139 #define htobe64(x) BE_64(x)
140 #define be64toh(x) BE_64(x)
142 #elif defined(__APPLE__)
143 # include <machine/endian.h>
144 # include <libkern/OSByteOrder.h>
146 # if BYTE_ORDER == LITTLE_ENDIAN
147 # define htobe16(x) OSSwapConstInt16(x)
148 # define htole16(x) (x)
149 # define be16toh(x) OSSwapConstInt16(x)
150 # define le16toh(x) (x)
152 # define htobe32(x) OSSwapConstInt32(x)
153 # define htole32(x) (x)
154 # define be32toh(x) OSSwapConstInt32(x)
155 # define le32toh(x) (x)
157 # define htobe64(x) OSSwapConstInt64(x)
158 # define htole64(x) (x)
159 # define be64toh(x) OSSwapConstInt64(x)
160 # define le64toh(x) (x)
162 # else /* BYTE_ORDER == LITTLE_ENDIAN */
163 # define htobe16(x) (x)
164 # define htole16(x) OSSwapConstInt16(x)
165 # define be16toh(x) (x)
166 # define le16toh(x) OSSwapConstInt16(x)
168 # define htobe32(x) (x)
169 # define htole32(x) OSSwapConstInt32(x)
170 # define be32toh(x) (x)
171 # define le32toh(x) OSSwapConstInt32(x)
173 # define htobe64(x) (x)
174 # define htole64(x) OSSwapConstInt64(x)
175 # define be64toh(x) (x)
176 # define le64toh(x) OSSwapConstInt64(x)
180 #error "Please add support for your OS."
183 #endif /* _COMPAT_ENDIAN_H */
This page took 0.036389 seconds and 4 git commands to generate.