Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 1 | //===-- int_endianness.h - configuration header for compiler-rt -----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is a configuration header for compiler-rt. |
| 10 | // This file is not part of the interface of this library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 13 | |
| 14 | #ifndef INT_ENDIANNESS_H |
| 15 | #define INT_ENDIANNESS_H |
| 16 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 17 | #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 18 | defined(__ORDER_LITTLE_ENDIAN__) |
| 19 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 20 | // Clang and GCC provide built-in endianness definitions. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 21 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 22 | #define _YUGA_LITTLE_ENDIAN 0 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 23 | #define _YUGA_BIG_ENDIAN 1 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 24 | #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 25 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 26 | #define _YUGA_BIG_ENDIAN 0 |
| 27 | #endif // __BYTE_ORDER__ |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 28 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 29 | #else // Compilers other than Clang or GCC. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 30 | |
| 31 | #if defined(__SVR4) && defined(__sun) |
| 32 | #include <sys/byteorder.h> |
| 33 | |
| 34 | #if defined(_BIG_ENDIAN) |
| 35 | #define _YUGA_LITTLE_ENDIAN 0 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 36 | #define _YUGA_BIG_ENDIAN 1 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 37 | #elif defined(_LITTLE_ENDIAN) |
| 38 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 39 | #define _YUGA_BIG_ENDIAN 0 |
| 40 | #else // !_LITTLE_ENDIAN |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 41 | #error "unknown endianness" |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 42 | #endif // !_LITTLE_ENDIAN |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 43 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 44 | #endif // Solaris |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 45 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 46 | // .. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 47 | |
| 48 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \ |
| 49 | defined(__minix) |
| 50 | #include <sys/endian.h> |
| 51 | |
| 52 | #if _BYTE_ORDER == _BIG_ENDIAN |
| 53 | #define _YUGA_LITTLE_ENDIAN 0 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 54 | #define _YUGA_BIG_ENDIAN 1 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 55 | #elif _BYTE_ORDER == _LITTLE_ENDIAN |
| 56 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 57 | #define _YUGA_BIG_ENDIAN 0 |
| 58 | #endif // _BYTE_ORDER |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 59 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 60 | #endif // *BSD |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 61 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 62 | #if defined(__OpenBSD__) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 63 | #include <machine/endian.h> |
| 64 | |
| 65 | #if _BYTE_ORDER == _BIG_ENDIAN |
| 66 | #define _YUGA_LITTLE_ENDIAN 0 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 67 | #define _YUGA_BIG_ENDIAN 1 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 68 | #elif _BYTE_ORDER == _LITTLE_ENDIAN |
| 69 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 70 | #define _YUGA_BIG_ENDIAN 0 |
| 71 | #endif // _BYTE_ORDER |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 72 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 73 | #endif // OpenBSD |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 74 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 75 | // .. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 76 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 77 | // Mac OSX has __BIG_ENDIAN__ or __LITTLE_ENDIAN__ automatically set by the |
| 78 | // compiler (at least with GCC) |
| 79 | #if defined(__APPLE__) || defined(__ellcc__) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 80 | |
| 81 | #ifdef __BIG_ENDIAN__ |
| 82 | #if __BIG_ENDIAN__ |
| 83 | #define _YUGA_LITTLE_ENDIAN 0 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 84 | #define _YUGA_BIG_ENDIAN 1 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 85 | #endif |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 86 | #endif // __BIG_ENDIAN__ |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 87 | |
| 88 | #ifdef __LITTLE_ENDIAN__ |
| 89 | #if __LITTLE_ENDIAN__ |
| 90 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 91 | #define _YUGA_BIG_ENDIAN 0 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 92 | #endif |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 93 | #endif // __LITTLE_ENDIAN__ |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 94 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 95 | #endif // Mac OSX |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 96 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 97 | // .. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 98 | |
| 99 | #if defined(_WIN32) |
| 100 | |
| 101 | #define _YUGA_LITTLE_ENDIAN 1 |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 102 | #define _YUGA_BIG_ENDIAN 0 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 103 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 104 | #endif // Windows |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 105 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 106 | #endif // Clang or GCC. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 107 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 108 | // . |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 109 | |
| 110 | #if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN) |
| 111 | #error Unable to determine endian |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 112 | #endif // Check we found an endianness correctly. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 113 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 114 | #endif // INT_ENDIANNESS_H |