OpenDaVINCI  4.15.3
PortableEndian.h
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+, and NetBSD 6.1.5+, Christian Berger
8 
9 #ifndef PORTABLE_ENDIAN_H__
10 #define PORTABLE_ENDIAN_H__
11 
12 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
13 
14 # define __WINDOWS__
15 
16 #endif
17 
18 #if defined(__linux__) || defined(__CYGWIN__)
19 
20 # include <endian.h>
21 
22 #elif defined(__APPLE__)
23 
24 # include <libkern/OSByteOrder.h>
25 
26 # define htobe16(x) OSSwapHostToBigInt16(x)
27 # define htole16(x) OSSwapHostToLittleInt16(x)
28 # define be16toh(x) OSSwapBigToHostInt16(x)
29 # define le16toh(x) OSSwapLittleToHostInt16(x)
30 
31 # define htobe32(x) OSSwapHostToBigInt32(x)
32 # define htole32(x) OSSwapHostToLittleInt32(x)
33 # define be32toh(x) OSSwapBigToHostInt32(x)
34 # define le32toh(x) OSSwapLittleToHostInt32(x)
35 
36 # define htobe64(x) OSSwapHostToBigInt64(x)
37 # define htole64(x) OSSwapHostToLittleInt64(x)
38 # define be64toh(x) OSSwapBigToHostInt64(x)
39 # define le64toh(x) OSSwapLittleToHostInt64(x)
40 
41 # define __BYTE_ORDER BYTE_ORDER
42 # define __BIG_ENDIAN BIG_ENDIAN
43 # define __LITTLE_ENDIAN LITTLE_ENDIAN
44 # define __PDP_ENDIAN PDP_ENDIAN
45 
46 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
47 
48 # include <sys/endian.h>
49 
50 #elif defined(__WINDOWS__)
51 
52 # include <winsock2.h>
53 
54 # if BYTE_ORDER == LITTLE_ENDIAN
55 
56 # define htobe16(x) htons(x)
57 # define htole16(x) (x)
58 # define be16toh(x) ntohs(x)
59 # define le16toh(x) (x)
60 
61 # define htobe32(x) htonl(x)
62 # define htole32(x) (x)
63 # define be32toh(x) ntohl(x)
64 # define le32toh(x) (x)
65 
66 # define htobe64(x) htonll(x)
67 # define htole64(x) (x)
68 # define be64toh(x) ntohll(x)
69 # define le64toh(x) (x)
70 
71 # elif BYTE_ORDER == BIG_ENDIAN
72 
73  /* that would be xbox 360 */
74 # define htobe16(x) (x)
75 # define htole16(x) __builtin_bswap16(x)
76 # define be16toh(x) (x)
77 # define le16toh(x) __builtin_bswap16(x)
78 
79 # define htobe32(x) (x)
80 # define htole32(x) __builtin_bswap32(x)
81 # define be32toh(x) (x)
82 # define le32toh(x) __builtin_bswap32(x)
83 
84 # define htobe64(x) (x)
85 # define htole64(x) __builtin_bswap64(x)
86 # define be64toh(x) (x)
87 # define le64toh(x) __builtin_bswap64(x)
88 
89 # else
90 
91 # error byte order not supported
92 
93 # endif
94 
95 # define __BYTE_ORDER BYTE_ORDER
96 # define __BIG_ENDIAN BIG_ENDIAN
97 # define __LITTLE_ENDIAN LITTLE_ENDIAN
98 # define __PDP_ENDIAN PDP_ENDIAN
99 
100 #else
101 
102 # error platform not supported
103 
104 #endif
105 
106 #endif