float_sizes.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2018-2023 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/float_sizes.hpp
  10. *
  11. * This header defines macros for testing buitin floating point type sizes
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_FLOAT_SIZES_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_FLOAT_SIZES_HPP_INCLUDED_
  15. #include <boost/atomic/detail/config.hpp>
  16. #ifdef BOOST_HAS_PRAGMA_ONCE
  17. #pragma once
  18. #endif
  19. // Detect value sizes of the different floating point types. The value sizes may be less than the corresponding type sizes
  20. // if the type contains padding bits. This is typical e.g. with x87 80-bit extended double types, which are often represented as 96 or 128-bit types.
  21. // See: https://en.wikipedia.org/wiki/IEEE_754
  22. // For Intel x87 extended double see: https://en.wikipedia.org/wiki/Extended_precision#x86_Architecture_Extended_Precision_Format
  23. // For IBM extended double (a.k.a. double-double) see: https://en.wikipedia.org/wiki/Long_double#Implementations, https://gcc.gnu.org/wiki/Ieee128PowerPC
  24. #if defined(__FLT_RADIX__) && defined(__FLT_MANT_DIG__) && defined(__FLT_MAX_EXP__) && \
  25. defined(__DBL_MANT_DIG__) && defined(__DBL_MAX_EXP__) && defined(__LDBL_MANT_DIG__) && defined(__LDBL_MAX_EXP__)
  26. #if (__FLT_RADIX__ == 2)
  27. #if (__FLT_MANT_DIG__ == 11) && (__FLT_MAX_EXP__ == 16) // IEEE 754 binary16
  28. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 2
  29. #elif (__FLT_MANT_DIG__ == 24) && (__FLT_MAX_EXP__ == 128) // IEEE 754 binary32
  30. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 4
  31. #elif (__FLT_MANT_DIG__ == 53) && (__FLT_MAX_EXP__ == 1024) // IEEE 754 binary64
  32. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 8
  33. #elif (__FLT_MANT_DIG__ == 64 || __FLT_MANT_DIG__ == 53 || __FLT_MANT_DIG__ == 24) && (__FLT_MAX_EXP__ == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  34. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 10
  35. #elif (__FLT_MANT_DIG__ == 106) && (__FLT_MAX_EXP__ == 1024) // IBM extended double
  36. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  37. #elif (__FLT_MANT_DIG__ == 113) && (__FLT_MAX_EXP__ == 16384) // IEEE 754 binary128
  38. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  39. #elif (__FLT_MANT_DIG__ == 237) && (__FLT_MAX_EXP__ == 262144) // IEEE 754 binary256
  40. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 32
  41. #endif
  42. #if (__DBL_MANT_DIG__ == 11) && (__DBL_MAX_EXP__ == 16) // IEEE 754 binary16
  43. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 2
  44. #elif (__DBL_MANT_DIG__ == 24) && (__DBL_MAX_EXP__ == 128) // IEEE 754 binary32
  45. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 4
  46. #elif (__DBL_MANT_DIG__ == 53) && (__DBL_MAX_EXP__ == 1024) // IEEE 754 binary64
  47. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 8
  48. #elif (__DBL_MANT_DIG__ == 64 || __DBL_MANT_DIG__ == 53 || __DBL_MANT_DIG__ == 24) && (__DBL_MAX_EXP__ == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  49. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 10
  50. #elif (__DBL_MANT_DIG__ == 106) && (__DBL_MAX_EXP__ == 1024) // IBM extended double
  51. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  52. #elif (__DBL_MANT_DIG__ == 113) && (__DBL_MAX_EXP__ == 16384) // IEEE 754 binary128
  53. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  54. #elif (__DBL_MANT_DIG__ == 237) && (__DBL_MAX_EXP__ == 262144) // IEEE 754 binary256
  55. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 32
  56. #endif
  57. #if (__LDBL_MANT_DIG__ == 11) && (__LDBL_MAX_EXP__ == 16) // IEEE 754 binary16
  58. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 2
  59. #elif (__LDBL_MANT_DIG__ == 24) && (__LDBL_MAX_EXP__ == 128) // IEEE 754 binary32
  60. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 4
  61. #elif (__LDBL_MANT_DIG__ == 53) && (__LDBL_MAX_EXP__ == 1024) // IEEE 754 binary64
  62. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 8
  63. #elif (__LDBL_MANT_DIG__ == 64 || __LDBL_MANT_DIG__ == 53 || __LDBL_MANT_DIG__ == 24) && (__LDBL_MAX_EXP__ == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  64. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 10
  65. #elif (__LDBL_MANT_DIG__ == 106) && (__LDBL_MAX_EXP__ == 1024) // IBM extended double
  66. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  67. #elif (__LDBL_MANT_DIG__ == 113) && (__LDBL_MAX_EXP__ == 16384) // IEEE 754 binary128
  68. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  69. #elif (__LDBL_MANT_DIG__ == 237) && (__LDBL_MAX_EXP__ == 262144) // IEEE 754 binary256
  70. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 32
  71. #endif
  72. #elif (__FLT_RADIX__ == 10)
  73. #if (__FLT_MANT_DIG__ == 7) && (__FLT_MAX_EXP__ == 97) // IEEE 754 decimal32
  74. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 4
  75. #elif (__FLT_MANT_DIG__ == 16) && (__FLT_MAX_EXP__ == 385) // IEEE 754 decimal64
  76. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 8
  77. #elif (__FLT_MANT_DIG__ == 34) && (__FLT_MAX_EXP__ == 6145) // IEEE 754 decimal128
  78. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  79. #endif
  80. #if (__DBL_MANT_DIG__ == 7) && (__DBL_MAX_EXP__ == 97) // IEEE 754 decimal32
  81. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 4
  82. #elif (__DBL_MANT_DIG__ == 16) && (__DBL_MAX_EXP__ == 385) // IEEE 754 decimal64
  83. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 8
  84. #elif (__DBL_MANT_DIG__ == 34) && (__DBL_MAX_EXP__ == 6145) // IEEE 754 decimal128
  85. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  86. #endif
  87. #if (__LDBL_MANT_DIG__ == 7) && (__LDBL_MAX_EXP__ == 97) // IEEE 754 decimal32
  88. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 4
  89. #elif (__LDBL_MANT_DIG__ == 16) && (__LDBL_MAX_EXP__ == 385) // IEEE 754 decimal64
  90. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 8
  91. #elif (__LDBL_MANT_DIG__ == 34) && (__LDBL_MAX_EXP__ == 6145) // IEEE 754 decimal128
  92. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  93. #endif
  94. #endif
  95. #else // defined(__FLT_RADIX__) ...
  96. #include <cfloat>
  97. #if (FLT_RADIX == 2)
  98. #if (FLT_MANT_DIG == 11) && (FLT_MAX_EXP == 16) // IEEE 754 binary16
  99. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 2
  100. #elif (FLT_MANT_DIG == 24) && (FLT_MAX_EXP == 128) // IEEE 754 binary32
  101. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 4
  102. #elif (FLT_MANT_DIG == 53) && (FLT_MAX_EXP == 1024) // IEEE 754 binary64
  103. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 8
  104. #elif (FLT_MANT_DIG == 64 || FLT_MANT_DIG == 53 || FLT_MANT_DIG == 24) && (FLT_MAX_EXP == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  105. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 10
  106. #elif (FLT_MANT_DIG == 106) && (FLT_MAX_EXP == 1024) // IBM extended double
  107. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  108. #elif (FLT_MANT_DIG == 113) && (FLT_MAX_EXP == 16384) // IEEE 754 binary128
  109. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  110. #elif (FLT_MANT_DIG == 237) && (FLT_MAX_EXP == 262144) // IEEE 754 binary256
  111. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 32
  112. #endif
  113. #if (DBL_MANT_DIG == 11) && (DBL_MAX_EXP == 16) // IEEE 754 binary16
  114. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 2
  115. #elif (DBL_MANT_DIG == 24) && (DBL_MAX_EXP == 128) // IEEE 754 binary32
  116. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 4
  117. #elif (DBL_MANT_DIG == 53) && (DBL_MAX_EXP == 1024) // IEEE 754 binary64
  118. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 8
  119. #elif (DBL_MANT_DIG == 64 || DBL_MANT_DIG == 53 || DBL_MANT_DIG == 24) && (DBL_MAX_EXP == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  120. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 10
  121. #elif (DBL_MANT_DIG == 106) && (DBL_MAX_EXP == 1024) // IBM extended double
  122. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  123. #elif (DBL_MANT_DIG == 113) && (DBL_MAX_EXP == 16384) // IEEE 754 binary128
  124. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  125. #elif (DBL_MANT_DIG == 237) && (DBL_MAX_EXP == 262144) // IEEE 754 binary256
  126. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 32
  127. #endif
  128. #if (LDBL_MANT_DIG == 11) && (LDBL_MAX_EXP == 16) // IEEE 754 binary16
  129. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 2
  130. #elif (LDBL_MANT_DIG == 24) && (LDBL_MAX_EXP == 128) // IEEE 754 binary32
  131. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 4
  132. #elif (LDBL_MANT_DIG == 53) && (LDBL_MAX_EXP == 1024) // IEEE 754 binary64
  133. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 8
  134. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 53 || LDBL_MANT_DIG == 24) && (LDBL_MAX_EXP == 16384) // x87 extended double, with full 64-bit significand or reduced to 53 or 24 bits
  135. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 10
  136. #elif (LDBL_MANT_DIG == 106) && (LDBL_MAX_EXP == 1024) // IBM extended double
  137. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  138. #elif (LDBL_MANT_DIG == 113) && (LDBL_MAX_EXP == 16384) // IEEE 754 binary128
  139. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  140. #elif (LDBL_MANT_DIG == 237) && (LDBL_MAX_EXP == 262144) // IEEE 754 binary256
  141. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 32
  142. #endif
  143. #elif (FLT_RADIX == 10)
  144. #if (FLT_MANT_DIG == 7) && (FLT_MAX_EXP == 97) // IEEE 754 decimal32
  145. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 4
  146. #elif (FLT_MANT_DIG == 16) && (FLT_MAX_EXP == 385) // IEEE 754 decimal64
  147. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 8
  148. #elif (FLT_MANT_DIG == 34) && (FLT_MAX_EXP == 6145) // IEEE 754 decimal128
  149. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE 16
  150. #endif
  151. #if (DBL_MANT_DIG == 7) && (DBL_MAX_EXP == 97) // IEEE 754 decimal32
  152. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 4
  153. #elif (DBL_MANT_DIG == 16) && (DBL_MAX_EXP == 385) // IEEE 754 decimal64
  154. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 8
  155. #elif (DBL_MANT_DIG == 34) && (DBL_MAX_EXP == 6145) // IEEE 754 decimal128
  156. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE 16
  157. #endif
  158. #if (LDBL_MANT_DIG == 7) && (LDBL_MAX_EXP == 97) // IEEE 754 decimal32
  159. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 4
  160. #elif (LDBL_MANT_DIG == 16) && (LDBL_MAX_EXP == 385) // IEEE 754 decimal64
  161. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 8
  162. #elif (LDBL_MANT_DIG == 34) && (LDBL_MAX_EXP == 6145) // IEEE 754 decimal128
  163. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE 16
  164. #endif
  165. #endif
  166. #endif // defined(__FLT_RADIX__) ...
  167. // GCC and compatible compilers define internal macros with builtin type traits
  168. #if defined(__SIZEOF_FLOAT__)
  169. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT __SIZEOF_FLOAT__
  170. #endif
  171. #if defined(__SIZEOF_DOUBLE__)
  172. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE __SIZEOF_DOUBLE__
  173. #endif
  174. #if defined(__SIZEOF_LONG_DOUBLE__)
  175. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE __SIZEOF_LONG_DOUBLE__
  176. #endif
  177. #if !defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE)
  178. #define BOOST_ATOMIC_DETAIL_ALIGN_SIZE_TO_POWER_OF_2(x)\
  179. ((x) == 1u ? 1u : ((x) == 2u ? 2u : ((x) <= 4u ? 4u : ((x) <= 8u ? 8u : ((x) <= 16u ? 16u : ((x) <= 32u ? 32u : (x)))))))
  180. // Make our best guess. These sizes may not be accurate, but they are good enough to estimate the size of the storage required to hold these types.
  181. #if !defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT) && defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE)
  182. #define BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT BOOST_ATOMIC_DETAIL_ALIGN_SIZE_TO_POWER_OF_2(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE)
  183. #endif
  184. #if !defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE) && defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE)
  185. #define BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE BOOST_ATOMIC_DETAIL_ALIGN_SIZE_TO_POWER_OF_2(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE)
  186. #endif
  187. #if !defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE) && defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE)
  188. #define BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE BOOST_ATOMIC_DETAIL_ALIGN_SIZE_TO_POWER_OF_2(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE)
  189. #endif
  190. #endif // !defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE)
  191. #if !defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT_VALUE) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_FLOAT) ||\
  192. !defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE_VALUE) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_DOUBLE) ||\
  193. !defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE_VALUE) || !defined(BOOST_ATOMIC_DETAIL_SIZEOF_LONG_DOUBLE)
  194. #error Boost.Atomic: Failed to determine builtin floating point type sizes, the target platform is not supported. Please, report to the developers (patches are welcome).
  195. #endif
  196. #endif // BOOST_ATOMIC_DETAIL_FLOAT_SIZES_HPP_INCLUDED_