Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / common / compat / endian.h
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _COMPAT_ENDIAN_H
9 #define _COMPAT_ENDIAN_H
10
11 #if defined(__linux__) || defined(__CYGWIN__)
12 #include <endian.h>
13
14 /*
15 * htobe/betoh are not defined for glibc <2.9, so add them
16 * explicitly if they are missing.
17 */
18 #ifdef __USE_BSD
19 /* Conversion interfaces. */
20 # include <byteswap.h>
21
22 # if __BYTE_ORDER == __LITTLE_ENDIAN
23 # ifndef htobe16
24 # define htobe16(x) __bswap_16(x)
25 # endif
26 # ifndef htole16
27 # define htole16(x) (x)
28 # endif
29 # ifndef be16toh
30 # define be16toh(x) __bswap_16(x)
31 # endif
32 # ifndef le16toh
33 # define le16toh(x) (x)
34 # endif
35
36 # ifndef htobe32
37 # define htobe32(x) __bswap_32(x)
38 # endif
39 # ifndef htole32
40 # define htole32(x) (x)
41 # endif
42 # ifndef be32toh
43 # define be32toh(x) __bswap_32(x)
44 # endif
45 # ifndef le32toh
46 # define le32toh(x) (x)
47 # endif
48
49 # ifndef htobe64
50 # define htobe64(x) __bswap_64(x)
51 # endif
52 # ifndef htole64
53 # define htole64(x) (x)
54 # endif
55 # ifndef be64toh
56 # define be64toh(x) __bswap_64(x)
57 # endif
58 # ifndef le64toh
59 # define le64toh(x) (x)
60 # endif
61
62 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
63 # ifndef htobe16
64 # define htobe16(x) (x)
65 # endif
66 # ifndef htole16
67 # define htole16(x) __bswap_16(x)
68 # endif
69 # ifndef be16toh
70 # define be16toh(x) (x)
71 # endif
72 # ifndef le16toh
73 # define le16toh(x) __bswap_16(x)
74 # endif
75
76 # ifndef htobe32
77 # define htobe32(x) (x)
78 # endif
79 # ifndef htole32
80 # define htole32(x) __bswap_32(x)
81 # endif
82 # ifndef be32toh
83 # define be32toh(x) (x)
84 # endif
85 # ifndef le32toh
86 # define le32toh(x) __bswap_32(x)
87 # endif
88
89 # ifndef htobe64
90 # define htobe64(x) (x)
91 # endif
92 # ifndef htole64
93 # define htole64(x) __bswap_64(x)
94 # endif
95 # ifndef be64toh
96 # define be64toh(x) (x)
97 # endif
98 # ifndef le64toh
99 # define le64toh(x) __bswap_64(x)
100 # endif
101
102 # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
103 #endif /* __USE_BSD */
104
105 #elif defined(__FreeBSD__)
106 #include <machine/endian.h>
107
108 #elif defined(__sun__)
109 #include <sys/byteorder.h>
110 #ifndef __BIG_ENDIAN
111 #define __BIG_ENDIAN 4321
112 #endif /* __BIG_ENDIAN */
113 #ifndef __LITTLE_ENDIAN
114 #define __LITTLE_ENDIAN 1234
115 #endif /* __LITTLE_ENDIAN */
116
117 #ifdef _LITTLE_ENDIAN
118 #define __BYTE_ORDER __LITTLE_ENDIAN
119 #endif /* _LITTLE_ENDIAN */
120 #ifdef _BIG_ENDIAN
121 #define __BYTE_ORDER __BIG_ENDIAN
122 #endif /* _BIG_ENDIAN */
123
124 #define LITTLE_ENDIAN __LITTLE_ENDIAN
125 #define BIG_ENDIAN __BIG_ENDIAN
126 #define PDP_ENDIAN __PDP_ENDIAN
127 #define BYTE_ORDER __BYTE_ORDER
128
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)
141
142 #elif defined(__APPLE__)
143 # include <machine/endian.h>
144 # include <libkern/OSByteOrder.h>
145
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)
151
152 # define htobe32(x) OSSwapConstInt32(x)
153 # define htole32(x) (x)
154 # define be32toh(x) OSSwapConstInt32(x)
155 # define le32toh(x) (x)
156
157 # define htobe64(x) OSSwapConstInt64(x)
158 # define htole64(x) (x)
159 # define be64toh(x) OSSwapConstInt64(x)
160 # define le64toh(x) (x)
161
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)
167
168 # define htobe32(x) (x)
169 # define htole32(x) OSSwapConstInt32(x)
170 # define be32toh(x) (x)
171 # define le32toh(x) OSSwapConstInt32(x)
172
173 # define htobe64(x) (x)
174 # define htole64(x) OSSwapConstInt64(x)
175 # define be64toh(x) (x)
176 # define le64toh(x) OSSwapConstInt64(x)
177 # endif
178
179 #else
180 #error "Please add support for your OS."
181 #endif
182
183 #endif /* _COMPAT_ENDIAN_H */
This page took 0.032064 seconds and 4 git commands to generate.