E2SAR 0.2.0
Loading...
Searching...
No Matches
portable_endian.h
1//
2// endian.h
3//
4// https://gist.github.com/panzi/6856583
5//
6// I, Mathias Panzenböck, place this file hereby into the public domain. Use
7// it at your own risk for whatever you like. In case there are
8// jurisdictions that don't support putting things in the public domain you
9// can also consider it to be "dual licensed" under the BSD, MIT and Apache
10// licenses, if you want to. This code is trivial anyway. Consider it an
11// example on how to get the endian conversion functions on different
12// platforms.
13
14#ifndef PORTABLE_ENDIAN_H__
15#define PORTABLE_ENDIAN_H__
16
17#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
18
19# define __WINDOWS__
20
21#endif
22
23#if defined(__linux__) || defined(__CYGWIN__)
24
25# include <endian.h>
26
27#elif defined(__APPLE__)
28
29# include <libkern/OSByteOrder.h>
30
31# define htobe16(x) OSSwapHostToBigInt16(x)
32# define htole16(x) OSSwapHostToLittleInt16(x)
33# define be16toh(x) OSSwapBigToHostInt16(x)
34# define le16toh(x) OSSwapLittleToHostInt16(x)
35
36# define htobe32(x) OSSwapHostToBigInt32(x)
37# define htole32(x) OSSwapHostToLittleInt32(x)
38# define be32toh(x) OSSwapBigToHostInt32(x)
39# define le32toh(x) OSSwapLittleToHostInt32(x)
40
41# define htobe64(x) OSSwapHostToBigInt64(x)
42# define htole64(x) OSSwapHostToLittleInt64(x)
43# define be64toh(x) OSSwapBigToHostInt64(x)
44# define le64toh(x) OSSwapLittleToHostInt64(x)
45
46# define __BYTE_ORDER BYTE_ORDER
47# define __BIG_ENDIAN BIG_ENDIAN
48# define __LITTLE_ENDIAN LITTLE_ENDIAN
49# define __PDP_ENDIAN PDP_ENDIAN
50
51#elif defined(__OpenBSD__)
52
53# include <sys/endian.h>
54
55#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
56
57# include <sys/endian.h>
58
59# define be16toh(x) betoh16(x)
60# define le16toh(x) letoh16(x)
61
62# define be32toh(x) betoh32(x)
63# define le32toh(x) letoh32(x)
64
65# define be64toh(x) betoh64(x)
66# define le64toh(x) letoh64(x)
67
68#elif defined(__WINDOWS__)
69
70# include <winsock2.h>
71# include <sys/param.h>
72
73# if BYTE_ORDER == LITTLE_ENDIAN
74
75# define htobe16(x) htons(x)
76# define htole16(x) (x)
77# define be16toh(x) ntohs(x)
78# define le16toh(x) (x)
79
80# define htobe32(x) htonl(x)
81# define htole32(x) (x)
82# define be32toh(x) ntohl(x)
83# define le32toh(x) (x)
84
85# define htobe64(x) htonll(x)
86# define htole64(x) (x)
87# define be64toh(x) ntohll(x)
88# define le64toh(x) (x)
89
90# elif BYTE_ORDER == BIG_ENDIAN
91
92 /* that would be xbox 360 */
93# define htobe16(x) (x)
94# define htole16(x) __builtin_bswap16(x)
95# define be16toh(x) (x)
96# define le16toh(x) __builtin_bswap16(x)
97
98# define htobe32(x) (x)
99# define htole32(x) __builtin_bswap32(x)
100# define be32toh(x) (x)
101# define le32toh(x) __builtin_bswap32(x)
102
103# define htobe64(x) (x)
104# define htole64(x) __builtin_bswap64(x)
105# define be64toh(x) (x)
106# define le64toh(x) __builtin_bswap64(x)
107
108# else
109
110# error byte order not supported
111
112# endif
113
114# define __BYTE_ORDER BYTE_ORDER
115# define __BIG_ENDIAN BIG_ENDIAN
116# define __LITTLE_ENDIAN LITTLE_ENDIAN
117# define __PDP_ENDIAN PDP_ENDIAN
118
119#else
120
121# error platform not supported
122
123#endif
124
125#endif