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__)
16 * htobe/betoh are not defined for glibc <2.9, so add them
17 * explicitly if they are missing.
20 /* Conversion interfaces. */
21 # include <byteswap.h>
23 # if __BYTE_ORDER == __LITTLE_ENDIAN
25 # define htobe16(x) __bswap_16(x)
28 # define htole16(x) (x)
31 # define be16toh(x) __bswap_16(x)
34 # define le16toh(x) (x)
38 # define htobe32(x) __bswap_32(x)
41 # define htole32(x) (x)
44 # define be32toh(x) __bswap_32(x)
47 # define le32toh(x) (x)
51 # define htobe64(x) __bswap_64(x)
54 # define htole64(x) (x)
57 # define be64toh(x) __bswap_64(x)
60 # define le64toh(x) (x)
63 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
65 # define htobe16(x) (x)
68 # define htole16(x) __bswap_16(x)
71 # define be16toh(x) (x)
74 # define le16toh(x) __bswap_16(x)
78 # define htobe32(x) (x)
81 # define htole32(x) __bswap_32(x)
84 # define be32toh(x) (x)
87 # define le32toh(x) __bswap_32(x)
91 # define htobe64(x) (x)
94 # define htole64(x) __bswap_64(x)
97 # define be64toh(x) (x)
100 # define le64toh(x) __bswap_64(x)
103 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
104 #endif /* __USE_BSD */
106 #elif defined(__FreeBSD__)
107 #include <sys/endian.h>
109 #define bswap_16(x) bswap16(x)
110 #define bswap_32(x) bswap32(x)
111 #define bswap_64(x) bswap64(x)
113 #elif defined(__sun__)
114 #include <sys/byteorder.h>
116 #define __BIG_ENDIAN 4321
117 #endif /* __BIG_ENDIAN */
118 #ifndef __LITTLE_ENDIAN
119 #define __LITTLE_ENDIAN 1234
120 #endif /* __LITTLE_ENDIAN */
122 #ifdef _LITTLE_ENDIAN
123 #define __BYTE_ORDER __LITTLE_ENDIAN
124 #endif /* _LITTLE_ENDIAN */
126 #define __BYTE_ORDER __BIG_ENDIAN
127 #endif /* _BIG_ENDIAN */
129 #define LITTLE_ENDIAN __LITTLE_ENDIAN
130 #define BIG_ENDIAN __BIG_ENDIAN
131 #define PDP_ENDIAN __PDP_ENDIAN
132 #define BYTE_ORDER __BYTE_ORDER
134 #define betoh16(x) BE_16(x)
135 #define letoh16(x) LE_16(x)
136 #define betoh32(x) BE_32(x)
137 #define letoh32(x) LE_32(x)
138 #define betoh64(x) BE_64(x)
139 #define letoh64(x) LE_64(x)
140 #define htobe16(x) BE_16(x)
141 #define be16toh(x) BE_16(x)
142 #define htobe32(x) BE_32(x)
143 #define be32toh(x) BE_32(x)
144 #define htobe64(x) BE_64(x)
145 #define be64toh(x) BE_64(x)
147 #elif defined(__APPLE__)
148 # include <machine/endian.h>
149 # include <libkern/OSByteOrder.h>
151 # if BYTE_ORDER == LITTLE_ENDIAN
152 # define htobe16(x) OSSwapConstInt16(x)
153 # define htole16(x) (x)
154 # define be16toh(x) OSSwapConstInt16(x)
155 # define le16toh(x) (x)
157 # define htobe32(x) OSSwapConstInt32(x)
158 # define htole32(x) (x)
159 # define be32toh(x) OSSwapConstInt32(x)
160 # define le32toh(x) (x)
162 # define htobe64(x) OSSwapConstInt64(x)
163 # define htole64(x) (x)
164 # define be64toh(x) OSSwapConstInt64(x)
165 # define le64toh(x) (x)
167 # else /* BYTE_ORDER == LITTLE_ENDIAN */
168 # define htobe16(x) (x)
169 # define htole16(x) OSSwapConstInt16(x)
170 # define be16toh(x) (x)
171 # define le16toh(x) OSSwapConstInt16(x)
173 # define htobe32(x) (x)
174 # define htole32(x) OSSwapConstInt32(x)
175 # define be32toh(x) (x)
176 # define le32toh(x) OSSwapConstInt32(x)
178 # define htobe64(x) (x)
179 # define htole64(x) OSSwapConstInt64(x)
180 # define be64toh(x) (x)
181 # define le64toh(x) OSSwapConstInt64(x)
185 #error "Please add support for your OS."
188 #endif /* _COMPAT_ENDIAN_H */
This page took 0.045052 seconds and 4 git commands to generate.