endian.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright Rene Rivera 2013-2015
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_PREDEF_ENDIAN_H
  8. #define BOOST_PREDEF_ENDIAN_H
  9. #include <boost/predef/version_number.h>
  10. #include <boost/predef/make.h>
  11. #include <boost/predef/library/c/gnu.h>
  12. #include <boost/predef/os/macos.h>
  13. #include <boost/predef/os/bsd.h>
  14. #include <boost/predef/platform/android.h>
  15. /* tag::reference[]
  16. = `BOOST_ENDIAN_*`
  17. Detection of endian memory ordering. There are four defined macros
  18. in this header that define the various generally possible endian
  19. memory orderings:
  20. * `BOOST_ENDIAN_BIG_BYTE`, byte-swapped big-endian.
  21. * `BOOST_ENDIAN_BIG_WORD`, word-swapped big-endian.
  22. * `BOOST_ENDIAN_LITTLE_BYTE`, byte-swapped little-endian.
  23. * `BOOST_ENDIAN_LITTLE_WORD`, word-swapped little-endian.
  24. The detection is conservative in that it only identifies endianness
  25. that it knows for certain. In particular bi-endianness is not
  26. indicated as is it not practically possible to determine the
  27. endianness from anything but an operating system provided
  28. header. And the currently known headers do not define that
  29. programatic bi-endianness is available.
  30. This implementation is a compilation of various publicly available
  31. information and acquired knowledge:
  32. . The indispensable documentation of "Pre-defined Compiler Macros"
  33. http://sourceforge.net/p/predef/wiki/Endianness[Endianness].
  34. . The various endian specifications available in the
  35. http://wikipedia.org/[Wikipedia] computer architecture pages.
  36. . Generally available searches for headers that define endianness.
  37. */ // end::reference[]
  38. #define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_NOT_AVAILABLE
  39. #define BOOST_ENDIAN_BIG_WORD BOOST_VERSION_NUMBER_NOT_AVAILABLE
  40. #define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_NOT_AVAILABLE
  41. #define BOOST_ENDIAN_LITTLE_WORD BOOST_VERSION_NUMBER_NOT_AVAILABLE
  42. /* GNU libc provides a header defining __BYTE_ORDER, or _BYTE_ORDER.
  43. * And some OSs provide some for of endian header also.
  44. */
  45. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  46. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  47. # if BOOST_LIB_C_GNU || BOOST_PLAT_ANDROID || BOOST_OS_BSD_OPEN
  48. # include <endian.h>
  49. # else
  50. # if BOOST_OS_MACOS
  51. # include <machine/endian.h>
  52. # else
  53. # if BOOST_OS_BSD
  54. # include <sys/endian.h>
  55. # endif
  56. # endif
  57. # endif
  58. # if defined(__BYTE_ORDER)
  59. # if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
  60. # undef BOOST_ENDIAN_BIG_BYTE
  61. # define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  62. # endif
  63. # if defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
  64. # undef BOOST_ENDIAN_LITTLE_BYTE
  65. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  66. # endif
  67. # if defined(__PDP_ENDIAN) && (__BYTE_ORDER == __PDP_ENDIAN)
  68. # undef BOOST_ENDIAN_LITTLE_WORD
  69. # define BOOST_ENDIAN_LITTLE_WORD BOOST_VERSION_NUMBER_AVAILABLE
  70. # endif
  71. # endif
  72. # if !defined(__BYTE_ORDER) && defined(_BYTE_ORDER)
  73. # if defined(_BIG_ENDIAN) && (_BYTE_ORDER == _BIG_ENDIAN)
  74. # undef BOOST_ENDIAN_BIG_BYTE
  75. # define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  76. # endif
  77. # if defined(_LITTLE_ENDIAN) && (_BYTE_ORDER == _LITTLE_ENDIAN)
  78. # undef BOOST_ENDIAN_LITTLE_BYTE
  79. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  80. # endif
  81. # if defined(_PDP_ENDIAN) && (_BYTE_ORDER == _PDP_ENDIAN)
  82. # undef BOOST_ENDIAN_LITTLE_WORD
  83. # define BOOST_ENDIAN_LITTLE_WORD BOOST_VERSION_NUMBER_AVAILABLE
  84. # endif
  85. # endif
  86. #endif
  87. /* Built-in byte-swapped big-endian macros.
  88. */
  89. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  90. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  91. # if (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
  92. (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || \
  93. defined(__ARMEB__) || \
  94. defined(__THUMBEB__) || \
  95. defined(__AARCH64EB__) || \
  96. defined(_MIPSEB) || \
  97. defined(__MIPSEB) || \
  98. defined(__MIPSEB__)
  99. # undef BOOST_ENDIAN_BIG_BYTE
  100. # define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  101. # endif
  102. #endif
  103. /* Built-in byte-swapped little-endian macros.
  104. */
  105. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  106. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  107. # if (defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
  108. (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \
  109. defined(__ARMEL__) || \
  110. defined(__THUMBEL__) || \
  111. defined(__AARCH64EL__) || \
  112. defined(__loongarch__) || \
  113. defined(_MIPSEL) || \
  114. defined(__MIPSEL) || \
  115. defined(__MIPSEL__) || \
  116. defined(__riscv) || \
  117. defined(__e2k__)
  118. # undef BOOST_ENDIAN_LITTLE_BYTE
  119. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  120. # endif
  121. #endif
  122. /* Some architectures are strictly one endianess (as opposed
  123. * the current common bi-endianess).
  124. */
  125. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  126. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  127. # include <boost/predef/architecture.h>
  128. # if BOOST_ARCH_M68K || \
  129. BOOST_ARCH_PARISC || \
  130. BOOST_ARCH_SPARC || \
  131. BOOST_ARCH_SYS370 || \
  132. BOOST_ARCH_SYS390 || \
  133. BOOST_ARCH_Z
  134. # undef BOOST_ENDIAN_BIG_BYTE
  135. # define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  136. # endif
  137. # if BOOST_ARCH_IA64 || \
  138. BOOST_ARCH_X86 || \
  139. BOOST_ARCH_BLACKFIN
  140. # undef BOOST_ENDIAN_LITTLE_BYTE
  141. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  142. # endif
  143. #endif
  144. /* Windows on ARM, if not otherwise detected/specified, is always
  145. * byte-swapped little-endian.
  146. */
  147. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  148. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  149. # if BOOST_ARCH_ARM
  150. # include <boost/predef/os/windows.h>
  151. # if BOOST_OS_WINDOWS
  152. # undef BOOST_ENDIAN_LITTLE_BYTE
  153. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  154. # endif
  155. # endif
  156. #endif
  157. #if BOOST_ENDIAN_BIG_BYTE
  158. # define BOOST_ENDIAN_BIG_BYTE_AVAILABLE
  159. #endif
  160. #if BOOST_ENDIAN_BIG_WORD
  161. # define BOOST_ENDIAN_BIG_WORD_BYTE_AVAILABLE
  162. #endif
  163. #if BOOST_ENDIAN_LITTLE_BYTE
  164. # define BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE
  165. #endif
  166. #if BOOST_ENDIAN_LITTLE_WORD
  167. # define BOOST_ENDIAN_LITTLE_WORD_BYTE_AVAILABLE
  168. #endif
  169. #define BOOST_ENDIAN_BIG_BYTE_NAME "Byte-Swapped Big-Endian"
  170. #define BOOST_ENDIAN_BIG_WORD_NAME "Word-Swapped Big-Endian"
  171. #define BOOST_ENDIAN_LITTLE_BYTE_NAME "Byte-Swapped Little-Endian"
  172. #define BOOST_ENDIAN_LITTLE_WORD_NAME "Word-Swapped Little-Endian"
  173. #endif
  174. #include <boost/predef/detail/test.h>
  175. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_BIG_BYTE,BOOST_ENDIAN_BIG_BYTE_NAME)
  176. #include <boost/predef/detail/test.h>
  177. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_BIG_WORD,BOOST_ENDIAN_BIG_WORD_NAME)
  178. #include <boost/predef/detail/test.h>
  179. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_LITTLE_BYTE,BOOST_ENDIAN_LITTLE_BYTE_NAME)
  180. #include <boost/predef/detail/test.h>
  181. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_LITTLE_WORD,BOOST_ENDIAN_LITTLE_WORD_NAME)