Clean-up: fix '-Wundef' warnings on various platforms
[lttng-tools.git] / src / common / compat / endian.h
CommitLineData
eb71a0aa 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
eb71a0aa 3 *
c922647d 4 * SPDX-License-Identifier: LGPL-2.1-only
eb71a0aa 5 *
eb71a0aa
DG
6 */
7
6b5a354b
MJ
8/*
9 * This compat header provides the following defines:
10 *
11 * LITTLE_ENDIAN
12 * BIG_ENDIAN
13 * BYTE_ORDER
14 *
15 * And functions / macros :
16 *
17 * bswap_16()
18 * bswap_32()
19 * bswap_64()
20 *
21 * htobe16()
22 * htole16()
23 * be16toh()
24 * le16toh()
25 *
26 * htobe32()
27 * htole32()
28 * be32toh()
29 * le32toh()
30 *
31 * htobe64()
32 * htole64()
33 * be64toh()
34 * le64toh()
35 */
36
f263b7fd 37#ifndef _COMPAT_ENDIAN_H
eb71a0aa
DG
38#define _COMPAT_ENDIAN_H
39
07bb6d71 40#if defined(__linux__) || defined(__CYGWIN__)
eb71a0aa 41#include <endian.h>
4ff128af 42#include <byteswap.h>
f263b7fd
JD
43
44/*
45 * htobe/betoh are not defined for glibc <2.9, so add them
46 * explicitly if they are missing.
47 */
48#ifdef __USE_BSD
49/* Conversion interfaces. */
50# include <byteswap.h>
51
52# if __BYTE_ORDER == __LITTLE_ENDIAN
53# ifndef htobe16
54# define htobe16(x) __bswap_16(x)
55# endif
56# ifndef htole16
57# define htole16(x) (x)
58# endif
59# ifndef be16toh
60# define be16toh(x) __bswap_16(x)
61# endif
62# ifndef le16toh
63# define le16toh(x) (x)
64# endif
65
66# ifndef htobe32
67# define htobe32(x) __bswap_32(x)
68# endif
69# ifndef htole32
70# define htole32(x) (x)
71# endif
72# ifndef be32toh
73# define be32toh(x) __bswap_32(x)
74# endif
75# ifndef le32toh
76# define le32toh(x) (x)
77# endif
78
79# ifndef htobe64
80# define htobe64(x) __bswap_64(x)
81# endif
82# ifndef htole64
83# define htole64(x) (x)
84# endif
85# ifndef be64toh
86# define be64toh(x) __bswap_64(x)
87# endif
88# ifndef le64toh
89# define le64toh(x) (x)
90# endif
91
92# else /* __BYTE_ORDER == __LITTLE_ENDIAN */
93# ifndef htobe16
94# define htobe16(x) (x)
95# endif
96# ifndef htole16
97# define htole16(x) __bswap_16(x)
98# endif
99# ifndef be16toh
100# define be16toh(x) (x)
101# endif
102# ifndef le16toh
103# define le16toh(x) __bswap_16(x)
104# endif
105
106# ifndef htobe32
107# define htobe32(x) (x)
108# endif
109# ifndef htole32
110# define htole32(x) __bswap_32(x)
111# endif
112# ifndef be32toh
113# define be32toh(x) (x)
114# endif
115# ifndef le32toh
116# define le32toh(x) __bswap_32(x)
117# endif
118
119# ifndef htobe64
120# define htobe64(x) (x)
121# endif
122# ifndef htole64
123# define htole64(x) __bswap_64(x)
124# endif
125# ifndef be64toh
126# define be64toh(x) (x)
127# endif
128# ifndef le64toh
129# define le64toh(x) __bswap_64(x)
130# endif
131
132# endif /* __BYTE_ORDER == __LITTLE_ENDIAN */
133#endif /* __USE_BSD */
134
b2c3836f 135#elif defined(__FreeBSD__)
4ff128af
MJ
136#include <sys/endian.h>
137
138#define bswap_16(x) bswap16(x)
139#define bswap_32(x) bswap32(x)
140#define bswap_64(x) bswap64(x)
0c97bd7a
MJ
141
142#elif defined(__sun__)
143#include <sys/byteorder.h>
144#ifndef __BIG_ENDIAN
145#define __BIG_ENDIAN 4321
146#endif /* __BIG_ENDIAN */
147#ifndef __LITTLE_ENDIAN
148#define __LITTLE_ENDIAN 1234
149#endif /* __LITTLE_ENDIAN */
150
151#ifdef _LITTLE_ENDIAN
152#define __BYTE_ORDER __LITTLE_ENDIAN
153#endif /* _LITTLE_ENDIAN */
154#ifdef _BIG_ENDIAN
155#define __BYTE_ORDER __BIG_ENDIAN
156#endif /* _BIG_ENDIAN */
157
158#define LITTLE_ENDIAN __LITTLE_ENDIAN
159#define BIG_ENDIAN __BIG_ENDIAN
0c97bd7a
MJ
160#define BYTE_ORDER __BYTE_ORDER
161
162#define betoh16(x) BE_16(x)
163#define letoh16(x) LE_16(x)
164#define betoh32(x) BE_32(x)
165#define letoh32(x) LE_32(x)
166#define betoh64(x) BE_64(x)
167#define letoh64(x) LE_64(x)
168#define htobe16(x) BE_16(x)
169#define be16toh(x) BE_16(x)
170#define htobe32(x) BE_32(x)
171#define be32toh(x) BE_32(x)
172#define htobe64(x) BE_64(x)
173#define be64toh(x) BE_64(x)
174
50ec4d1a
MJ
175#elif defined(__APPLE__)
176# include <machine/endian.h>
177# include <libkern/OSByteOrder.h>
178
179# if BYTE_ORDER == LITTLE_ENDIAN
180# define htobe16(x) OSSwapConstInt16(x)
181# define htole16(x) (x)
182# define be16toh(x) OSSwapConstInt16(x)
183# define le16toh(x) (x)
184
185# define htobe32(x) OSSwapConstInt32(x)
186# define htole32(x) (x)
187# define be32toh(x) OSSwapConstInt32(x)
188# define le32toh(x) (x)
189
190# define htobe64(x) OSSwapConstInt64(x)
191# define htole64(x) (x)
192# define be64toh(x) OSSwapConstInt64(x)
193# define le64toh(x) (x)
194
195# else /* BYTE_ORDER == LITTLE_ENDIAN */
196# define htobe16(x) (x)
197# define htole16(x) OSSwapConstInt16(x)
198# define be16toh(x) (x)
199# define le16toh(x) OSSwapConstInt16(x)
200
201# define htobe32(x) (x)
202# define htole32(x) OSSwapConstInt32(x)
203# define be32toh(x) (x)
204# define le32toh(x) OSSwapConstInt32(x)
205
206# define htobe64(x) (x)
207# define htole64(x) OSSwapConstInt64(x)
208# define be64toh(x) (x)
209# define le64toh(x) OSSwapConstInt64(x)
210# endif
211
eb71a0aa 212#else
b2c3836f 213#error "Please add support for your OS."
eb71a0aa
DG
214#endif
215
216#endif /* _COMPAT_ENDIAN_H */
This page took 0.064949 seconds and 4 git commands to generate.