@@ -117,14 +117,6 @@ namespace nall {
117117
118118}
119119
120- #if defined(PLATFORM_MACOS)
121- #include < machine/endian.h>
122- #elif defined(PLATFORM_LINUX)
123- #include < endian.h>
124- #elif defined(PLATFORM_BSD)
125- #include < sys/endian.h>
126- #endif
127-
128120/* Architecture detection */
129121
130122namespace nall {
@@ -157,12 +149,44 @@ namespace nall {
157149
158150/* Endian detection */
159151
152+ #if defined(PLATFORM_MACOS)
153+ #include < machine/endian.h>
154+ #elif defined(PLATFORM_LINUX)
155+ #include < endian.h>
156+ #elif defined(PLATFORM_BSD)
157+ #include < sys/endian.h>
158+ #endif
159+
160160namespace nall {
161161
162- #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(__amd64__) || defined(_M_IX86) || defined(_M_AMD64)
162+ // A note on endian constants: Traditional UNIX provides a header that defines
163+ // constants LITTLE_ENDIAN, BIG_ENDIAN, and BYTE_ORDER (set to LITTLE_ENDIAN or
164+ // BIG_ENDIAN as appropriate). However, C89 says that the compiler/libc should
165+ // not introduce any names unless they start with an underscore, so when you're
166+ // compiling in standards-compilant mode, those constants are named
167+ // __LITTLE_ENDIAN, or sometimes _LITTLE_ENDIAN, or sometimes even LITTLE_ENDIAN
168+ // on platforms that care more about tradition than standards. The platforms
169+ // that rename the constants usually provide some other name you can #define to
170+ // say, "forget C89, yes I really want traditional constant names", but *that*
171+ // name also differs from platform to platform, and it affects more than just
172+ // the endian header.
173+ //
174+ // Rather than wade into that mess, let's just test for all the constants we
175+ // know about.
176+
177+ #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) \
178+ || (defined ( _BYTE_ORDER) && defined ( _LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN) \
179+ || (defined ( BYTE_ORDER) && defined ( LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) \
180+ || defined (__LITTLE_ENDIAN__) \
181+ || defined (__i386__) || defined (__amd64__) \
182+ || defined (_M_IX86) || defined (_M_AMD64)
163183 #define ENDIAN_LSB
164184 constexpr auto endian () -> Endian { return Endian::LSB; }
165- #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || defined(__BIG_ENDIAN__) || defined(__powerpc__) || defined(_M_PPC)
185+ #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) \
186+ || (defined ( _BYTE_ORDER) && defined ( _BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN) \
187+ || (defined ( BYTE_ORDER) && defined ( BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) \
188+ || defined (__BIG_ENDIAN__) \
189+ || defined (__powerpc__) || defined (_M_PPC)
166190 #define ENDIAN_MSB
167191 constexpr auto endian () -> Endian { return Endian::MSB; }
168192#else
0 commit comments