summaryrefslogtreecommitdiffstats
path: root/include/endianness.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/endianness.h')
-rw-r--r--include/endianness.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/include/endianness.h b/include/endianness.h
new file mode 100644
index 0000000..972e51b
--- /dev/null
+++ b/include/endianness.h
@@ -0,0 +1,130 @@
1#ifndef ENDIANNESS_H
2#define ENDIANNESS_H
3
4#ifndef __LITTLE_ENDIAN
5#define __LITTLE_ENDIAN 1234
6#endif
7
8#ifndef __BIG_ENDIAN
9#define __BIG_ENDIAN 4321
10#endif
11
12#ifndef __BYTE_ORDER
13#ifdef __LITTLE_ENDIAN__
14#define __BYTE_ORDER __LITTLE_ENDIAN
15#else
16#ifdef __BIG_ENDIAN__
17#define __BYTE_ORDER __BIG_ENDIAN
18#endif
19#endif
20#endif
21
22#ifndef __BYTE_ORDER
23#ifndef _WIN32
24#warning __BYTE_ORDER is not defined, assuming little endian
25#endif
26#define __BYTE_ORDER __LITTLE_ENDIAN
27#endif
28
29#ifndef be16toh
30#if __BYTE_ORDER == __BIG_ENDIAN
31#define be16toh(x) (x)
32#else
33#define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
34#endif
35#endif
36
37#ifndef htobe16
38#define htobe16 be16toh
39#endif
40
41#ifndef le16toh
42#if __BYTE_ORDER == __BIG_ENDIAN
43#define le16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
44#else
45#define le16toh(x) (x)
46#endif
47#endif
48
49#ifndef htole16
50#define htole16 le16toh
51#endif
52
53#ifndef __bswap_32
54#define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \
55 | (((x) & 0x00FF0000) >> 8) \
56 | (((x) & 0x0000FF00) << 8) \
57 | (((x) & 0x000000FF) << 24))
58#endif
59
60#ifndef be32toh
61#if __BYTE_ORDER == __BIG_ENDIAN
62#define be32toh(x) (x)
63#else
64#define be32toh(x) __bswap_32(x)
65#endif
66#endif
67
68#ifndef htobe32
69#define htobe32 be32toh
70#endif
71
72#ifndef le32toh
73#if __BYTE_ORDER == __BIG_ENDIAN
74#define le32toh(x) __bswap_32(x)
75#else
76#define le32toh(x) (x)
77#endif
78#endif
79
80#ifndef htole32
81#define htole32 le32toh
82#endif
83
84#ifndef __bswap_64
85#define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \
86 | (((x) & 0x00FF000000000000ull) >> 40) \
87 | (((x) & 0x0000FF0000000000ull) >> 24) \
88 | (((x) & 0x000000FF00000000ull) >> 8) \
89 | (((x) & 0x00000000FF000000ull) << 8) \
90 | (((x) & 0x0000000000FF0000ull) << 24) \
91 | (((x) & 0x000000000000FF00ull) << 40) \
92 | (((x) & 0x00000000000000FFull) << 56))
93#endif
94
95#ifndef htobe64
96#if __BYTE_ORDER == __BIG_ENDIAN
97#define htobe64(x) (x)
98#else
99#define htobe64(x) __bswap_64(x)
100#endif
101#endif
102
103#ifndef be64toh
104#define be64toh htobe64
105#endif
106
107#ifndef le64toh
108#if __BYTE_ORDER == __LITTLE_ENDIAN
109#define le64toh(x) (x)
110#else
111#define le64toh(x) __bswap_64(x)
112#endif
113#endif
114
115#ifndef htole64
116#define htole64 le64toh
117#endif
118
119#if (defined(__BIG_ENDIAN__) \
120 && !defined(__FLOAT_WORD_ORDER__)) \
121 || (defined(__FLOAT_WORD_ORDER__) \
122 && __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
123#define float_bswap64(x) __bswap_64(x)
124#define float_bswap32(x) __bswap_32(x)
125#else
126#define float_bswap64(x) (x)
127#define float_bswap32(x) (x)
128#endif
129
130#endif /* ENDIANNESS_H */