1 #ifndef _BABELTRACE_ENDIAN_H
2 #define _BABELTRACE_ENDIAN_H
5 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * endian.h compatibility layer.
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <machine/endian.h>
31 #elif defined(__sun__)
32 #include <sys/byteorder.h>
35 #define __BIG_ENDIAN 4321
37 #ifndef __LITTLE_ENDIAN
38 #define __LITTLE_ENDIAN 1234
42 #define __BYTE_ORDER __LITTLE_ENDIAN
46 #define __BYTE_ORDER __BIG_ENDIAN
49 #define LITTLE_ENDIAN __LITTLE_ENDIAN
50 #define BIG_ENDIAN __BIG_ENDIAN
51 #define BYTE_ORDER __BYTE_ORDER
53 #define betoh16(x) BE_16(x)
54 #define letoh16(x) LE_16(x)
55 #define betoh32(x) BE_32(x)
56 #define letoh32(x) LE_32(x)
57 #define betoh64(x) BE_64(x)
58 #define letoh64(x) LE_64(x)
59 #define htobe16(x) BE_16(x)
60 #define be16toh(x) BE_16(x)
61 #define htobe32(x) BE_32(x)
62 #define be32toh(x) BE_32(x)
63 #define htobe64(x) BE_64(x)
64 #define be64toh(x) BE_64(x)
66 #elif defined(__MINGW32__)
70 #define __BIG_ENDIAN 4321
72 #ifndef __LITTLE_ENDIAN
73 #define __LITTLE_ENDIAN 1234
77 #define __BYTE_ORDER __LITTLE_ENDIAN
80 #define LITTLE_ENDIAN __LITTLE_ENDIAN
81 #define BIG_ENDIAN __BIG_ENDIAN
82 #define PDP_ENDIAN __PDP_ENDIAN
83 #define BYTE_ORDER __BYTE_ORDER
85 #define htobe16(x) (uint16_t) _byteswap_ushort(x)
86 #define htole16(x) (x)
87 #define be16toh(x) (uint16_t) _byteswap_ushort(x)
88 #define le16toh(x) (x)
90 #define htobe32(x) (uint32_t) _byteswap_ulong(x)
91 #define htole32(x) (x)
92 #define be32toh(x) (uint32_t) _byteswap_ulong(x)
93 #define le32toh(x) (x)
95 #define htobe64(x) (uint64_t) _byteswap_uint64(x)
96 #define htole64(x) (x)
97 #define be64toh(x) (uint64_t) _byteswap_uint64(x)
98 #define le64toh(x) (x)
100 #elif defined(__APPLE__)
101 # include <machine/endian.h>
102 # include <libkern/OSByteOrder.h>
104 # if BYTE_ORDER == LITTLE_ENDIAN
105 # define htobe16(x) OSSwapConstInt16(x)
106 # define htole16(x) (x)
107 # define be16toh(x) OSSwapConstInt16(x)
108 # define le16toh(x) (x)
110 # define htobe32(x) OSSwapConstInt32(x)
111 # define htole32(x) (x)
112 # define be32toh(x) OSSwapConstInt32(x)
113 # define le32toh(x) (x)
115 # define htobe64(x) OSSwapConstInt64(x)
116 # define htole64(x) (x)
117 # define be64toh(x) OSSwapConstInt64(x)
118 # define le64toh(x) (x)
120 # else /* BYTE_ORDER == LITTLE_ENDIAN */
121 # define htobe16(x) (x)
122 # define htole16(x) OSSwapConstInt16(x)
123 # define be16toh(x) (x)
124 # define le16toh(x) OSSwapConstInt16(x)
126 # define htobe32(x) (x)
127 # define htole32(x) OSSwapConstInt32(x)
128 # define be32toh(x) (x)
129 # define le32toh(x) OSSwapConstInt32(x)
131 # define htobe64(x) (x)
132 # define htole64(x) OSSwapConstInt64(x)
133 # define be64toh(x) (x)
134 # define le64toh(x) OSSwapConstInt64(x)
141 * htobe/betoh are not defined for glibc < 2.9, so add them explicitly
142 * if they are missing.
145 /* Conversion interfaces. */
146 # include <byteswap.h>
148 # if __BYTE_ORDER == __LITTLE_ENDIAN
150 # define htobe16(x) __bswap_16(x)
153 # define htole16(x) (x)
156 # define be16toh(x) __bswap_16(x)
159 # define le16toh(x) (x)
163 # define htobe32(x) __bswap_32(x)
166 # define htole32(x) (x)
169 # define be32toh(x) __bswap_32(x)
172 # define le32toh(x) (x)
176 # define htobe64(x) __bswap_64(x)
179 # define htole64(x) (x)
182 # define be64toh(x) __bswap_64(x)
185 # define le64toh(x) (x)
188 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
190 # define htobe16(x) (x)
193 # define htole16(x) __bswap_16(x)
196 # define be16toh(x) (x)
199 # define le16toh(x) __bswap_16(x)
203 # define htobe32(x) (x)
206 # define htole32(x) __bswap_32(x)
209 # define be32toh(x) (x)
212 # define le32toh(x) __bswap_32(x)
216 # define htobe64(x) (x)
219 # define htole64(x) __bswap_64(x)
222 # define be64toh(x) (x)
225 # define le64toh(x) __bswap_64(x)
228 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
229 # endif /* __USE_BSD */
230 #endif /* else -- __FreeBSD__ */
232 #ifndef FLOAT_WORD_ORDER
233 #ifdef __FLOAT_WORD_ORDER
234 #define FLOAT_WORD_ORDER __FLOAT_WORD_ORDER
235 #else /* __FLOAT_WORD_ORDER */
236 #define FLOAT_WORD_ORDER BYTE_ORDER
237 #endif /* __FLOAT_WORD_ORDER */
238 #endif /* FLOAT_WORD_ORDER */
240 #endif /* _BABELTRACE_ENDIAN_H */
This page took 0.03335 seconds and 4 git commands to generate.