int_endianness.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //===----------------------------------------------------------------------===//
  13. #ifndef INT_ENDIANNESS_H
  14. #define INT_ENDIANNESS_H
  15. #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  16. defined(__ORDER_LITTLE_ENDIAN__)
  17. // Clang and GCC provide built-in endianness definitions.
  18. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  19. #define _YUGA_LITTLE_ENDIAN 0
  20. #define _YUGA_BIG_ENDIAN 1
  21. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  22. #define _YUGA_LITTLE_ENDIAN 1
  23. #define _YUGA_BIG_ENDIAN 0
  24. #endif // __BYTE_ORDER__
  25. #else // Compilers other than Clang or GCC.
  26. #if defined(__SVR4) && defined(__sun)
  27. #include <sys/byteorder.h>
  28. #if defined(_BIG_ENDIAN)
  29. #define _YUGA_LITTLE_ENDIAN 0
  30. #define _YUGA_BIG_ENDIAN 1
  31. #elif defined(_LITTLE_ENDIAN)
  32. #define _YUGA_LITTLE_ENDIAN 1
  33. #define _YUGA_BIG_ENDIAN 0
  34. #else // !_LITTLE_ENDIAN
  35. #error "unknown endianness"
  36. #endif // !_LITTLE_ENDIAN
  37. #endif // Solaris
  38. // ..
  39. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
  40. defined(__minix)
  41. #include <sys/endian.h>
  42. #if _BYTE_ORDER == _BIG_ENDIAN
  43. #define _YUGA_LITTLE_ENDIAN 0
  44. #define _YUGA_BIG_ENDIAN 1
  45. #elif _BYTE_ORDER == _LITTLE_ENDIAN
  46. #define _YUGA_LITTLE_ENDIAN 1
  47. #define _YUGA_BIG_ENDIAN 0
  48. #endif // _BYTE_ORDER
  49. #endif // *BSD
  50. #if defined(__OpenBSD__)
  51. #include <machine/endian.h>
  52. #if _BYTE_ORDER == _BIG_ENDIAN
  53. #define _YUGA_LITTLE_ENDIAN 0
  54. #define _YUGA_BIG_ENDIAN 1
  55. #elif _BYTE_ORDER == _LITTLE_ENDIAN
  56. #define _YUGA_LITTLE_ENDIAN 1
  57. #define _YUGA_BIG_ENDIAN 0
  58. #endif // _BYTE_ORDER
  59. #endif // OpenBSD
  60. // ..
  61. // Mac OSX has __BIG_ENDIAN__ or __LITTLE_ENDIAN__ automatically set by the
  62. // compiler (at least with GCC)
  63. #if defined(__APPLE__) || defined(__ellcc__)
  64. #ifdef __BIG_ENDIAN__
  65. #if __BIG_ENDIAN__
  66. #define _YUGA_LITTLE_ENDIAN 0
  67. #define _YUGA_BIG_ENDIAN 1
  68. #endif
  69. #endif // __BIG_ENDIAN__
  70. #ifdef __LITTLE_ENDIAN__
  71. #if __LITTLE_ENDIAN__
  72. #define _YUGA_LITTLE_ENDIAN 1
  73. #define _YUGA_BIG_ENDIAN 0
  74. #endif
  75. #endif // __LITTLE_ENDIAN__
  76. #endif // Mac OSX
  77. // ..
  78. #if defined(_WIN32)
  79. #define _YUGA_LITTLE_ENDIAN 1
  80. #define _YUGA_BIG_ENDIAN 0
  81. #endif // Windows
  82. #endif // Clang or GCC.
  83. // .
  84. #if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN)
  85. #error Unable to determine endian
  86. #endif // Check we found an endianness correctly.
  87. #endif // INT_ENDIANNESS_H