2 * SPDX-License-Identifier: MIT
4 * Copyright 2012 (c) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * endian.h compatibility layer.
9 #ifndef _BABELTRACE_ENDIAN_H
10 #define _BABELTRACE_ENDIAN_H
13 #include <machine/endian.h>
15 #elif defined(__sun__)
16 #include <sys/byteorder.h>
19 #define __BIG_ENDIAN 4321
21 #ifndef __LITTLE_ENDIAN
22 #define __LITTLE_ENDIAN 1234
26 #define __BYTE_ORDER __LITTLE_ENDIAN
30 #define __BYTE_ORDER __BIG_ENDIAN
33 #define LITTLE_ENDIAN __LITTLE_ENDIAN
34 #define BIG_ENDIAN __BIG_ENDIAN
35 #define BYTE_ORDER __BYTE_ORDER
37 #define betoh16(x) BE_16(x)
38 #define letoh16(x) LE_16(x)
39 #define betoh32(x) BE_32(x)
40 #define letoh32(x) LE_32(x)
41 #define betoh64(x) BE_64(x)
42 #define letoh64(x) LE_64(x)
43 #define htobe16(x) BE_16(x)
44 #define be16toh(x) BE_16(x)
45 #define htobe32(x) BE_32(x)
46 #define be32toh(x) BE_32(x)
47 #define htobe64(x) BE_64(x)
48 #define be64toh(x) BE_64(x)
50 #elif defined(__MINGW32__)
54 #define __BIG_ENDIAN 4321
56 #ifndef __LITTLE_ENDIAN
57 #define __LITTLE_ENDIAN 1234
61 #define __BYTE_ORDER __LITTLE_ENDIAN
64 #define LITTLE_ENDIAN __LITTLE_ENDIAN
65 #define BIG_ENDIAN __BIG_ENDIAN
66 #define PDP_ENDIAN __PDP_ENDIAN
67 #define BYTE_ORDER __BYTE_ORDER
69 #define htobe16(x) (uint16_t) _byteswap_ushort(x)
70 #define htole16(x) (x)
71 #define be16toh(x) (uint16_t) _byteswap_ushort(x)
72 #define le16toh(x) (x)
74 #define htobe32(x) (uint32_t) _byteswap_ulong(x)
75 #define htole32(x) (x)
76 #define be32toh(x) (uint32_t) _byteswap_ulong(x)
77 #define le32toh(x) (x)
79 #define htobe64(x) (uint64_t) _byteswap_uint64(x)
80 #define htole64(x) (x)
81 #define be64toh(x) (uint64_t) _byteswap_uint64(x)
82 #define le64toh(x) (x)
84 #elif defined(__APPLE__)
85 # include <machine/endian.h>
86 # include <libkern/OSByteOrder.h>
88 # if BYTE_ORDER == LITTLE_ENDIAN
89 # define htobe16(x) OSSwapConstInt16(x)
90 # define htole16(x) (x)
91 # define be16toh(x) OSSwapConstInt16(x)
92 # define le16toh(x) (x)
94 # define htobe32(x) OSSwapConstInt32(x)
95 # define htole32(x) (x)
96 # define be32toh(x) OSSwapConstInt32(x)
97 # define le32toh(x) (x)
99 # define htobe64(x) OSSwapConstInt64(x)
100 # define htole64(x) (x)
101 # define be64toh(x) OSSwapConstInt64(x)
102 # define le64toh(x) (x)
104 # else /* BYTE_ORDER == LITTLE_ENDIAN */
105 # define htobe16(x) (x)
106 # define htole16(x) OSSwapConstInt16(x)
107 # define be16toh(x) (x)
108 # define le16toh(x) OSSwapConstInt16(x)
110 # define htobe32(x) (x)
111 # define htole32(x) OSSwapConstInt32(x)
112 # define be32toh(x) (x)
113 # define le32toh(x) OSSwapConstInt32(x)
115 # define htobe64(x) (x)
116 # define htole64(x) OSSwapConstInt64(x)
117 # define be64toh(x) (x)
118 # define le64toh(x) OSSwapConstInt64(x)
125 * htobe/betoh are not defined for glibc < 2.9, so add them explicitly
126 * if they are missing.
129 /* Conversion interfaces. */
130 # include <byteswap.h>
132 # if __BYTE_ORDER == __LITTLE_ENDIAN
134 # define htobe16(x) __bswap_16(x)
137 # define htole16(x) (x)
140 # define be16toh(x) __bswap_16(x)
143 # define le16toh(x) (x)
147 # define htobe32(x) __bswap_32(x)
150 # define htole32(x) (x)
153 # define be32toh(x) __bswap_32(x)
156 # define le32toh(x) (x)
160 # define htobe64(x) __bswap_64(x)
163 # define htole64(x) (x)
166 # define be64toh(x) __bswap_64(x)
169 # define le64toh(x) (x)
172 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
174 # define htobe16(x) (x)
177 # define htole16(x) __bswap_16(x)
180 # define be16toh(x) (x)
183 # define le16toh(x) __bswap_16(x)
187 # define htobe32(x) (x)
190 # define htole32(x) __bswap_32(x)
193 # define be32toh(x) (x)
196 # define le32toh(x) __bswap_32(x)
200 # define htobe64(x) (x)
203 # define htole64(x) __bswap_64(x)
206 # define be64toh(x) (x)
209 # define le64toh(x) __bswap_64(x)
212 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
213 # endif /* __USE_BSD */
214 #endif /* else -- __FreeBSD__ */
216 #ifndef FLOAT_WORD_ORDER
217 #ifdef __FLOAT_WORD_ORDER
218 #define FLOAT_WORD_ORDER __FLOAT_WORD_ORDER
219 #else /* __FLOAT_WORD_ORDER */
220 #define FLOAT_WORD_ORDER BYTE_ORDER
221 #endif /* __FLOAT_WORD_ORDER */
222 #endif /* FLOAT_WORD_ORDER */
224 #endif /* _BABELTRACE_ENDIAN_H */
This page took 0.034087 seconds and 4 git commands to generate.