Skip to content

Commit c07adbb

Browse files
committed
Added fuji_endian.h
1 parent b9ce9a7 commit c07adbb

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

include/fuji_endian.h

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
#ifndef PORTABLE_ENDIAN_H__
8+
#define PORTABLE_ENDIAN_H__
9+
10+
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
11+
12+
# define __WINDOWS__
13+
14+
#endif
15+
16+
#if defined(__linux__) || defined(__CYGWIN__)
17+
18+
# include <endian.h>
19+
20+
#elif defined(__APPLE__)
21+
22+
# include <libkern/OSByteOrder.h>
23+
24+
# define htobe16(x) OSSwapHostToBigInt16(x)
25+
# define htole16(x) OSSwapHostToLittleInt16(x)
26+
# define be16toh(x) OSSwapBigToHostInt16(x)
27+
# define le16toh(x) OSSwapLittleToHostInt16(x)
28+
29+
# define htobe32(x) OSSwapHostToBigInt32(x)
30+
# define htole32(x) OSSwapHostToLittleInt32(x)
31+
# define be32toh(x) OSSwapBigToHostInt32(x)
32+
# define le32toh(x) OSSwapLittleToHostInt32(x)
33+
34+
# define htobe64(x) OSSwapHostToBigInt64(x)
35+
# define htole64(x) OSSwapHostToLittleInt64(x)
36+
# define be64toh(x) OSSwapBigToHostInt64(x)
37+
# define le64toh(x) OSSwapLittleToHostInt64(x)
38+
39+
# define __BYTE_ORDER BYTE_ORDER
40+
# define __BIG_ENDIAN BIG_ENDIAN
41+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
42+
# define __PDP_ENDIAN PDP_ENDIAN
43+
44+
#elif defined(__OpenBSD__)
45+
46+
# include <sys/endian.h>
47+
48+
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
49+
50+
# include <sys/endian.h>
51+
52+
# define be16toh(x) betoh16(x)
53+
# define le16toh(x) letoh16(x)
54+
55+
# define be32toh(x) betoh32(x)
56+
# define le32toh(x) letoh32(x)
57+
58+
# define be64toh(x) betoh64(x)
59+
# define le64toh(x) letoh64(x)
60+
61+
#elif defined(__WINDOWS__)
62+
63+
# include <winsock2.h>
64+
# include <sys/param.h>
65+
66+
# if BYTE_ORDER == LITTLE_ENDIAN
67+
68+
# define htobe16(x) htons(x)
69+
# define htole16(x) (x)
70+
# define be16toh(x) ntohs(x)
71+
# define le16toh(x) (x)
72+
73+
# define htobe32(x) htonl(x)
74+
# define htole32(x) (x)
75+
# define be32toh(x) ntohl(x)
76+
# define le32toh(x) (x)
77+
78+
# define htobe64(x) htonll(x)
79+
# define htole64(x) (x)
80+
# define be64toh(x) ntohll(x)
81+
# define le64toh(x) (x)
82+
83+
# elif BYTE_ORDER == BIG_ENDIAN
84+
85+
/* that would be xbox 360 */
86+
# define htobe16(x) (x)
87+
# define htole16(x) __builtin_bswap16(x)
88+
# define be16toh(x) (x)
89+
# define le16toh(x) __builtin_bswap16(x)
90+
91+
# define htobe32(x) (x)
92+
# define htole32(x) __builtin_bswap32(x)
93+
# define be32toh(x) (x)
94+
# define le32toh(x) __builtin_bswap32(x)
95+
96+
# define htobe64(x) (x)
97+
# define htole64(x) __builtin_bswap64(x)
98+
# define be64toh(x) (x)
99+
# define le64toh(x) __builtin_bswap64(x)
100+
101+
# else
102+
103+
# error byte order not supported
104+
105+
# endif
106+
107+
# define __BYTE_ORDER BYTE_ORDER
108+
# define __BIG_ENDIAN BIG_ENDIAN
109+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
110+
# define __PDP_ENDIAN PDP_ENDIAN
111+
112+
#elif defined(_NEWLIB_VERSION)
113+
114+
/*
115+
* GNU ARM toolchain, and possibly other bare-metal toolchains
116+
* built on newlib. Tested with
117+
* (GNU Tools for ARM Embedded Processors 6-2017-q2-update
118+
*/
119+
120+
# include <machine/endian.h>
121+
122+
# if BYTE_ORDER == LITTLE_ENDIAN
123+
124+
# define htobe16(x) __bswap16(x)
125+
# define htole16(x) (x)
126+
# define be16toh(x) __bswap16(x)
127+
# define le16toh(x) (x)
128+
129+
# define htobe32(x) __bswap32(x)
130+
# define htole32(x) (x)
131+
# define be32toh(x) __bswap32(x)
132+
# define le32toh(x) (x)
133+
134+
# define htobe64(x) __bswap64(x)
135+
# define htole64(x) (x)
136+
# define be64toh(x) __bswap64(x)
137+
# define le64toh(x) (x)
138+
139+
# elif BYTE_ORDER == BIG_ENDIAN
140+
141+
# define htobe16(x) (x)
142+
# define htole16(x) __bswap16(x)
143+
# define be16toh(x) (x)
144+
# define le16toh(x) __bswap16(x)
145+
146+
# define htobe32(x) (x)
147+
# define htole32(x) __bswap32(x)
148+
# define be32toh(x) (x)
149+
# define le32toh(x) __bswap32(x)
150+
151+
# define htobe64(x) (x)
152+
# define htole64(x) __bswap64(x)
153+
# define be64toh(x) (x)
154+
# define le64toh(x) __bswap64(x)
155+
156+
# else
157+
158+
# error byte order not supported
159+
160+
# endif
161+
162+
#else
163+
164+
# error platform not supported
165+
166+
#endif
167+
168+
#endif

0 commit comments

Comments
 (0)