endian.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(_MIPSEL) || \
  113. defined(__MIPSEL) || \
  114. defined(__MIPSEL__) || \
  115. defined(__riscv) || \
  116. defined(__e2k__)
  117. # undef BOOST_ENDIAN_LITTLE_BYTE
  118. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  119. # endif
  120. #endif
  121. /* Some architectures are strictly one endianess (as opposed
  122. * the current common bi-endianess).
  123. */
  124. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  125. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  126. # include <boost/predef/architecture.h>
  127. # if BOOST_ARCH_M68K || \
  128. BOOST_ARCH_PARISC || \
  129. BOOST_ARCH_SPARC || \
  130. BOOST_ARCH_SYS370 || \
  131. BOOST_ARCH_SYS390 || \
  132. BOOST_ARCH_Z
  133. # undef BOOST_ENDIAN_BIG_BYTE
  134. # define BOOST_ENDIAN_BIG_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  135. # endif
  136. # if BOOST_ARCH_IA64 || \
  137. BOOST_ARCH_X86 || \
  138. BOOST_ARCH_BLACKFIN
  139. # undef BOOST_ENDIAN_LITTLE_BYTE
  140. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  141. # endif
  142. #endif
  143. /* Windows on ARM, if not otherwise detected/specified, is always
  144. * byte-swapped little-endian.
  145. */
  146. #if !BOOST_ENDIAN_BIG_BYTE && !BOOST_ENDIAN_BIG_WORD && \
  147. !BOOST_ENDIAN_LITTLE_BYTE && !BOOST_ENDIAN_LITTLE_WORD
  148. # if BOOST_ARCH_ARM
  149. # include <boost/predef/os/windows.h>
  150. # if BOOST_OS_WINDOWS
  151. # undef BOOST_ENDIAN_LITTLE_BYTE
  152. # define BOOST_ENDIAN_LITTLE_BYTE BOOST_VERSION_NUMBER_AVAILABLE
  153. # endif
  154. # endif
  155. #endif
  156. #if BOOST_ENDIAN_BIG_BYTE
  157. # define BOOST_ENDIAN_BIG_BYTE_AVAILABLE
  158. #endif
  159. #if BOOST_ENDIAN_BIG_WORD
  160. # define BOOST_ENDIAN_BIG_WORD_BYTE_AVAILABLE
  161. #endif
  162. #if BOOST_ENDIAN_LITTLE_BYTE
  163. # define BOOST_ENDIAN_LITTLE_BYTE_AVAILABLE
  164. #endif
  165. #if BOOST_ENDIAN_LITTLE_WORD
  166. # define BOOST_ENDIAN_LITTLE_WORD_BYTE_AVAILABLE
  167. #endif
  168. #define BOOST_ENDIAN_BIG_BYTE_NAME "Byte-Swapped Big-Endian"
  169. #define BOOST_ENDIAN_BIG_WORD_NAME "Word-Swapped Big-Endian"
  170. #define BOOST_ENDIAN_LITTLE_BYTE_NAME "Byte-Swapped Little-Endian"
  171. #define BOOST_ENDIAN_LITTLE_WORD_NAME "Word-Swapped Little-Endian"
  172. #endif
  173. #include <boost/predef/detail/test.h>
  174. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_BIG_BYTE,BOOST_ENDIAN_BIG_BYTE_NAME)
  175. #include <boost/predef/detail/test.h>
  176. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_BIG_WORD,BOOST_ENDIAN_BIG_WORD_NAME)
  177. #include <boost/predef/detail/test.h>
  178. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_LITTLE_BYTE,BOOST_ENDIAN_LITTLE_BYTE_NAME)
  179. #include <boost/predef/detail/test.h>
  180. BOOST_PREDEF_DECLARE_TEST(BOOST_ENDIAN_LITTLE_WORD,BOOST_ENDIAN_LITTLE_WORD_NAME)