Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

149 linhas
4.7KB

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