libcluon  0.0.148
PortableEndian.hpp
Go to the documentation of this file.
1 // "License": Public Domain
2 // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
3 // In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
4 // be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
5 // an example on how to get the endian conversion functions on different platforms.
6 
7 // Updated for FreeBSD 10.1+, DragonFly 4.2+, NetBSD 6.1.5+, fixes for Win32,
8 // and support for emscripten; Christian Berger.
9 
10 #ifndef CLUON_PORTABLEENDIAN_HPP
11 #define CLUON_PORTABLEENDIAN_HPP
12 
13 // clang-format off
14 #if defined(__linux__) || defined(__CYGWIN__)
15  #include <endian.h>
16 #elif defined(__APPLE__)
17  #include <libkern/OSByteOrder.h>
18  #define htobe16(x) OSSwapHostToBigInt16(x)
19  #define htole16(x) OSSwapHostToLittleInt16(x)
20  #define be16toh(x) OSSwapBigToHostInt16(x)
21  #define le16toh(x) OSSwapLittleToHostInt16(x)
22 
23  #define htobe32(x) OSSwapHostToBigInt32(x)
24  #define htole32(x) OSSwapHostToLittleInt32(x)
25  #define be32toh(x) OSSwapBigToHostInt32(x)
26  #define le32toh(x) OSSwapLittleToHostInt32(x)
27 
28  #define htobe64(x) OSSwapHostToBigInt64(x)
29  #define htole64(x) OSSwapHostToLittleInt64(x)
30  #define be64toh(x) OSSwapBigToHostInt64(x)
31  #define le64toh(x) OSSwapLittleToHostInt64(x)
32 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
33  #include <sys/endian.h>
34 #elif (defined(_WIN16) || defined(_WIN32) || defined(_WIN64))
35  #if BYTE_ORDER == LITTLE_ENDIAN
36  // Add missing definitions for MinGW.
37  #ifndef htonll
38  #define htonll(x) ((1==htonl(1)) ? (x) : (((uint64_t)htonl((x) & 0xFFFFFFFFUL)) << 32) | htonl((uint32_t)((x) >> 32)))
39  #endif
40  #ifndef ntohll
41  #define ntohll(x) ((1==ntohl(1)) ? (x) : (((uint64_t)ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t)((x) >> 32)))
42  #endif
43 
44  #define htobe16(x) htons(x)
45  #define htole16(x) (x)
46  #define be16toh(x) ntohs(x)
47  #define le16toh(x) (x)
48 
49  #define htobe32(x) htonl(x)
50  #define htole32(x) (x)
51  #define be32toh(x) ntohl(x)
52  #define le32toh(x) (x)
53 
54  #define htobe64(x) htonll(x)
55  #define htole64(x) (x)
56  #define be64toh(x) ntohll(x)
57  #define le64toh(x) (x)
58  #elif BYTE_ORDER == BIG_ENDIAN
59  /* that would be xbox 360 */
60  #define htobe16(x) (x)
61  #define htole16(x) __builtin_bswap16(x)
62  #define be16toh(x) (x)
63  #define le16toh(x) __builtin_bswap16(x)
64 
65  #define htobe32(x) (x)
66  #define htole32(x) __builtin_bswap32(x)
67  #define be32toh(x) (x)
68  #define le32toh(x) __builtin_bswap32(x)
69 
70  #define htobe64(x) (x)
71  #define htole64(x) __builtin_bswap64(x)
72  #define be64toh(x) (x)
73  #define le64toh(x) __builtin_bswap64(x)
74  #else
75  #error byte order not supported
76  #endif
77 #else
78  #ifdef __EMSCRIPTEN__
79  #include <endian.h>
80  #else
81  #warning platform not supported
82  #endif
83 #endif
84 // clang-format on
85 #endif