numeric_traits.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2020 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 cannot be used for this.
  33. #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
  34. #define __glibcxx_digits(_Tp) \
  35. (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
  36. #define __glibcxx_min(_Tp) \
  37. (__glibcxx_signed(_Tp) ? -__glibcxx_max(_Tp) - 1 : (_Tp)0)
  38. #define __glibcxx_max(_Tp) \
  39. (__glibcxx_signed(_Tp) ? \
  40. (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
  41. template<typename _Value>
  42. struct __numeric_traits_integer
  43. {
  44. // Only integers for initialization of member constant.
  45. static const _Value __min = __glibcxx_min(_Value);
  46. static const _Value __max = __glibcxx_max(_Value);
  47. // NB: these two also available in std::numeric_limits as compile
  48. // time constants, but <limits> is big and we avoid including it.
  49. static const bool __is_signed = __glibcxx_signed(_Value);
  50. static const int __digits = __glibcxx_digits(_Value);
  51. };
  52. template<typename _Value>
  53. const _Value __numeric_traits_integer<_Value>::__min;
  54. template<typename _Value>
  55. const _Value __numeric_traits_integer<_Value>::__max;
  56. template<typename _Value>
  57. const bool __numeric_traits_integer<_Value>::__is_signed;
  58. template<typename _Value>
  59. const int __numeric_traits_integer<_Value>::__digits;
  60. #undef __glibcxx_signed
  61. #undef __glibcxx_digits
  62. #undef __glibcxx_min
  63. #undef __glibcxx_max
  64. #define __glibcxx_floating(_Tp, _Fval, _Dval, _LDval) \
  65. (std::__are_same<_Tp, float>::__value ? _Fval \
  66. : std::__are_same<_Tp, double>::__value ? _Dval : _LDval)
  67. #define __glibcxx_max_digits10(_Tp) \
  68. (2 + __glibcxx_floating(_Tp, __FLT_MANT_DIG__, __DBL_MANT_DIG__, \
  69. __LDBL_MANT_DIG__) * 643L / 2136)
  70. #define __glibcxx_digits10(_Tp) \
  71. __glibcxx_floating(_Tp, __FLT_DIG__, __DBL_DIG__, __LDBL_DIG__)
  72. #define __glibcxx_max_exponent10(_Tp) \
  73. __glibcxx_floating(_Tp, __FLT_MAX_10_EXP__, __DBL_MAX_10_EXP__, \
  74. __LDBL_MAX_10_EXP__)
  75. template<typename _Value>
  76. struct __numeric_traits_floating
  77. {
  78. // Only floating point types. See N1822.
  79. static const int __max_digits10 = __glibcxx_max_digits10(_Value);
  80. // See above comment...
  81. static const bool __is_signed = true;
  82. static const int __digits10 = __glibcxx_digits10(_Value);
  83. static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
  84. };
  85. template<typename _Value>
  86. const int __numeric_traits_floating<_Value>::__max_digits10;
  87. template<typename _Value>
  88. const bool __numeric_traits_floating<_Value>::__is_signed;
  89. template<typename _Value>
  90. const int __numeric_traits_floating<_Value>::__digits10;
  91. template<typename _Value>
  92. const int __numeric_traits_floating<_Value>::__max_exponent10;
  93. template<typename _Value>
  94. struct __numeric_traits
  95. : public __conditional_type<std::__is_integer<_Value>::__value,
  96. __numeric_traits_integer<_Value>,
  97. __numeric_traits_floating<_Value> >::__type
  98. { };
  99. _GLIBCXX_END_NAMESPACE_VERSION
  100. } // namespace
  101. #undef __glibcxx_floating
  102. #undef __glibcxx_max_digits10
  103. #undef __glibcxx_digits10
  104. #undef __glibcxx_max_exponent10
  105. #endif