Move to kernel style SPDX license identifiers
[babeltrace.git] / src / compat / endian.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2012 (c) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * endian.h compatibility layer.
7 */
8
9 #ifndef _BABELTRACE_ENDIAN_H
10 #define _BABELTRACE_ENDIAN_H
11
12 #ifdef __FreeBSD__
13 #include <machine/endian.h>
14
15 #elif defined(__sun__)
16 #include <sys/byteorder.h>
17
18 #ifndef __BIG_ENDIAN
19 #define __BIG_ENDIAN 4321
20 #endif
21 #ifndef __LITTLE_ENDIAN
22 #define __LITTLE_ENDIAN 1234
23 #endif
24
25 #ifdef _LITTLE_ENDIAN
26 #define __BYTE_ORDER __LITTLE_ENDIAN
27 #endif
28
29 #ifdef _BIG_ENDIAN
30 #define __BYTE_ORDER __BIG_ENDIAN
31 #endif
32
33 #define LITTLE_ENDIAN __LITTLE_ENDIAN
34 #define BIG_ENDIAN __BIG_ENDIAN
35 #define BYTE_ORDER __BYTE_ORDER
36
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)
49
50 #elif defined(__MINGW32__)
51 #include <stdint.h>
52
53 #ifndef __BIG_ENDIAN
54 #define __BIG_ENDIAN 4321
55 #endif
56 #ifndef __LITTLE_ENDIAN
57 #define __LITTLE_ENDIAN 1234
58 #endif
59
60 #ifndef __BYTE_ORDER
61 #define __BYTE_ORDER __LITTLE_ENDIAN
62 #endif
63
64 #define LITTLE_ENDIAN __LITTLE_ENDIAN
65 #define BIG_ENDIAN __BIG_ENDIAN
66 #define PDP_ENDIAN __PDP_ENDIAN
67 #define BYTE_ORDER __BYTE_ORDER
68
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)
73
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)
78
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)
83
84 #elif defined(__APPLE__)
85 # include <machine/endian.h>
86 # include <libkern/OSByteOrder.h>
87
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)
93
94 # define htobe32(x) OSSwapConstInt32(x)
95 # define htole32(x) (x)
96 # define be32toh(x) OSSwapConstInt32(x)
97 # define le32toh(x) (x)
98
99 # define htobe64(x) OSSwapConstInt64(x)
100 # define htole64(x) (x)
101 # define be64toh(x) OSSwapConstInt64(x)
102 # define le64toh(x) (x)
103
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)
109
110 # define htobe32(x) (x)
111 # define htole32(x) OSSwapConstInt32(x)
112 # define be32toh(x) (x)
113 # define le32toh(x) OSSwapConstInt32(x)
114
115 # define htobe64(x) (x)
116 # define htole64(x) OSSwapConstInt64(x)
117 # define be64toh(x) (x)
118 # define le64toh(x) OSSwapConstInt64(x)
119 # endif
120
121 #else
122 #include <endian.h>
123
124 /*
125 * htobe/betoh are not defined for glibc < 2.9, so add them explicitly
126 * if they are missing.
127 */
128 # ifdef __USE_BSD
129 /* Conversion interfaces. */
130 # include <byteswap.h>
131
132 # if __BYTE_ORDER == __LITTLE_ENDIAN
133 # ifndef htobe16
134 # define htobe16(x) __bswap_16(x)
135 # endif
136 # ifndef htole16
137 # define htole16(x) (x)
138 # endif
139 # ifndef be16toh
140 # define be16toh(x) __bswap_16(x)
141 # endif
142 # ifndef le16toh
143 # define le16toh(x) (x)
144 # endif
145
146 # ifndef htobe32
147 # define htobe32(x) __bswap_32(x)
148 # endif
149 # ifndef htole32
150 # define htole32(x) (x)
151 # endif
152 # ifndef be32toh
153 # define be32toh(x) __bswap_32(x)
154 # endif
155 # ifndef le32toh
156 # define le32toh(x) (x)
157 # endif
158
159 # ifndef htobe64
160 # define htobe64(x) __bswap_64(x)
161 # endif
162 # ifndef htole64
163 # define htole64(x) (x)
164 # endif
165 # ifndef be64toh
166 # define be64toh(x) __bswap_64(x)
167 # endif
168 # ifndef le64toh
169 # define le64toh(x) (x)
170 # endif
171
172 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
173 # ifndef htobe16
174 # define htobe16(x) (x)
175 # endif
176 # ifndef htole16
177 # define htole16(x) __bswap_16(x)
178 # endif
179 # ifndef be16toh
180 # define be16toh(x) (x)
181 # endif
182 # ifndef le16toh
183 # define le16toh(x) __bswap_16(x)
184 # endif
185
186 # ifndef htobe32
187 # define htobe32(x) (x)
188 # endif
189 # ifndef htole32
190 # define htole32(x) __bswap_32(x)
191 # endif
192 # ifndef be32toh
193 # define be32toh(x) (x)
194 # endif
195 # ifndef le32toh
196 # define le32toh(x) __bswap_32(x)
197 # endif
198
199 # ifndef htobe64
200 # define htobe64(x) (x)
201 # endif
202 # ifndef htole64
203 # define htole64(x) __bswap_64(x)
204 # endif
205 # ifndef be64toh
206 # define be64toh(x) (x)
207 # endif
208 # ifndef le64toh
209 # define le64toh(x) __bswap_64(x)
210 # endif
211
212 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
213 # endif /* __USE_BSD */
214 #endif /* else -- __FreeBSD__ */
215
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 */
223
224 #endif /* _BABELTRACE_ENDIAN_H */
This page took 0.034087 seconds and 4 git commands to generate.