numeric_traits.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/numeric_traits.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _EXT_NUMERIC_TRAITS
  24. #define _EXT_NUMERIC_TRAITS 1
  25. #pragma GCC system_header
  26. #include <bits/cpp_type_traits.h>
  27. #include <ext/type_traits.h>
  28. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. // Compile time constants for builtin types.
  32. // In C++98 std::numeric_limits member functions are not constant expressions
  33. // (that changed in C++11 with the addition of 'constexpr').
  34. // Even for C++11, this header is smaller than <limits> and can be used
  35. // when only is_signed, digits, min, or max values are needed for integers,
  36. // or is_signed, digits10, max_digits10, or max_exponent10 for floats.
  37. // Unlike __is_integer (and std::is_integral) this trait is true for
  38. // non-standard built-in integer types such as __int128 and __int20.
  39. template<typename _Tp>
  40. struct __is_integer_nonstrict
  41. : public std::__is_integer<_Tp>
  42. {
  43. using std::__is_integer<_Tp>::__value;
  44. // The number of bits in the value representation.
  45. enum { __width = __value ? sizeof(_Tp) * __CHAR_BIT__ : 0 };
  46. };
  47. template<typename _Value>
  48. struct __numeric_traits_integer
  49. {
  50. #if __cplusplus >= 201103L
  51. static_assert(__is_integer_nonstrict<_Value>::__value,
  52. "invalid specialization");
  53. #endif
  54. // NB: these two are also available in std::numeric_limits as compile
  55. // time constants, but <limits> is big and we can avoid including it.
  56. static const bool __is_signed = (_Value)(-1) < 0;
  57. static const int __digits
  58. = __is_integer_nonstrict<_Value>::__width - __is_signed;
  59. // The initializers must be constants so that __max and __min are too.
  60. static const _Value __max = __is_signed
  61. ? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
  62. : ~(_Value)0;
  63. static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
  64. };
  65. template<typename _Value>
  66. const _Value __numeric_traits_integer<_Value>::__min;
  67. template<typename _Value>
  68. const _Value __numeric_traits_integer<_Value>::__max;
  69. template<typename _Value>
  70. const bool __numeric_traits_integer<_Value>::__is_signed;
  71. template<typename _Value>
  72. const int __numeric_traits_integer<_Value>::__digits;
  73. // Enable __numeric_traits_integer for types where the __is_integer_nonstrict
  74. // primary template doesn't give the right answer.
  75. #define _GLIBCXX_INT_N_TRAITS(T, WIDTH) \
  76. __extension__ \
  77. template<> struct __is_integer_nonstrict<T> \
  78. { \
  79. enum { __value = 1 }; \
  80. typedef std::__true_type __type; \
  81. enum { __width = WIDTH }; \
  82. }; \
  83. __extension__ \
  84. template<> struct __is_integer_nonstrict<unsigned T> \
  85. { \
  86. enum { __value = 1 }; \
  87. typedef std::__true_type __type; \
  88. enum { __width = WIDTH }; \
  89. };
  90. // We need to specify the width for some __intNN types because they
  91. // have padding bits, e.g. the object representation of __int20 has 32 bits,
  92. // but its width (number of bits in the value representation) is only 20.
  93. #if defined __GLIBCXX_TYPE_INT_N_0 && __GLIBCXX_BITSIZE_INT_N_0 % __CHAR_BIT__
  94. _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_0, __GLIBCXX_BITSIZE_INT_N_0)
  95. #endif
  96. #if defined __GLIBCXX_TYPE_INT_N_1 && __GLIBCXX_BITSIZE_INT_N_1 % __CHAR_BIT__
  97. _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_1, __GLIBCXX_BITSIZE_INT_N_1)
  98. #endif
  99. #if defined __GLIBCXX_TYPE_INT_N_2 && __GLIBCXX_BITSIZE_INT_N_2 % __CHAR_BIT__
  100. _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_2, __GLIBCXX_BITSIZE_INT_N_2)
  101. #endif
  102. #if defined __GLIBCXX_TYPE_INT_N_3 && __GLIBCXX_BITSIZE_INT_N_3 % __CHAR_BIT__
  103. _GLIBCXX_INT_N_TRAITS(__GLIBCXX_TYPE_INT_N_3, __GLIBCXX_BITSIZE_INT_N_3)
  104. #endif
  105. #if defined __STRICT_ANSI__ && defined __SIZEOF_INT128__
  106. // In strict modes __is_integer<__int128> is false,
  107. // but we still want to define __numeric_traits_integer<__int128>.
  108. _GLIBCXX_INT_N_TRAITS(__int128, 128)
  109. #endif
  110. #undef _GLIBCXX_INT_N_TRAITS
  111. #if __cplusplus >= 201103L
  112. /// Convenience alias for __numeric_traits<integer-type>.
  113. template<typename _Tp>
  114. using __int_traits = __numeric_traits_integer<_Tp>;
  115. #endif
  116. #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
  117. (std::__are_same<_Tp, float>::__value ? _Fval \
  118. : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
  119. #define __glibcxx_max_digits10(_Tp) \
  120. (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
  121. __LDBL_MANT_DIG__) * 643L / 2136)
  122. #define __glibcxx_digits10(_Tp) \
  123. __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
  124. #define __glibcxx_max_exponent10(_Tp) \
  125. __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
  126. __LDBL_MAX_10_EXP__)
  127. // N.B. this only supports float, double and long double (no __float128 etc.)
  128. template<typename _Value>
  129. struct __numeric_traits_floating
  130. {
  131. // Only floating point types. See N1822.
  132. static const int __max_digits10 = __glibcxx_max_digits10(_Value);
  133. // See above comment...
  134. static const bool __is_signed = true;
  135. static const int __digits10 = __glibcxx_digits10(_Value);
  136. static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
  137. };
  138. template<typename _Value>
  139. const int __numeric_traits_floating<_Value>::__max_digits10;
  140. template<typename _Value>
  141. const bool __numeric_traits_floating<_Value>::__is_signed;
  142. template<typename _Value>
  143. const int __numeric_traits_floating<_Value>::__digits10;
  144. template<typename _Value>
  145. const int __numeric_traits_floating<_Value>::__max_exponent10;
  146. #undef __glibcxx_floating
  147. #undef __glibcxx_max_digits10
  148. #undef __glibcxx_digits10
  149. #undef __glibcxx_max_exponent10
  150. template<typename _Value>
  151. struct __numeric_traits
  152. : public __numeric_traits_integer<_Value>
  153. { };
  154. template<>
  155. struct __numeric_traits<float>
  156. : public __numeric_traits_floating<float>
  157. { };
  158. template<>
  159. struct __numeric_traits<double>
  160. : public __numeric_traits_floating<double>
  161. { };
  162. template<>
  163. struct __numeric_traits<long double>
  164. : public __numeric_traits_floating<long double>
  165. { };
  166. #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
  167. # if defined __LONG_DOUBLE_IEEE128__
  168. // long double is __ieee128, define traits for __ibm128
  169. template<>
  170. struct __numeric_traits_floating<__ibm128>
  171. {
  172. static const int __max_digits10 = 33;
  173. static const bool __is_signed = true;
  174. static const int __digits10 = 31;
  175. static const int __max_exponent10 = 308;
  176. };
  177. template<>
  178. struct __numeric_traits<__ibm128>
  179. : public __numeric_traits_floating<__ibm128>
  180. { };
  181. # elif defined __LONG_DOUBLE_IBM128__
  182. // long double is __ibm128, define traits for __ieee128
  183. template<>
  184. struct __numeric_traits_floating<__ieee128>
  185. {
  186. static const int __max_digits10 = 36;
  187. static const bool __is_signed = true;
  188. static const int __digits10 = 33;
  189. static const int __max_exponent10 = 4932;
  190. };
  191. template<>
  192. struct __numeric_traits<__ieee128>
  193. : public __numeric_traits_floating<__ieee128>
  194. { };
  195. # endif
  196. #endif
  197. _GLIBCXX_END_NAMESPACE_VERSION
  198. } // namespace
  199. #endif