| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- // Definition of numeric_limits replacement traits P1841R1 -*- C++ -*-
- // Copyright (C) 2020-2021 Free Software Foundation, Inc.
- //
- // This file is part of the GNU ISO C++ Library. This library is free
- // software; you can redistribute it and/or modify it under the
- // terms of the GNU General Public License as published by the
- // Free Software Foundation; either version 3, or (at your option)
- // any later version.
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // Under Section 7 of GPL version 3, you are granted additional
- // permissions described in the GCC Runtime Library Exception, version
- // 3.1, as published by the Free Software Foundation.
- // You should have received a copy of the GNU General Public License and
- // a copy of the GCC Runtime Library Exception along with this program;
- // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
- // <http://www.gnu.org/licenses/>.
- #include <type_traits>
- namespace std {
- template <template <typename> class _Trait, typename _Tp, typename = void>
- struct __value_exists_impl : false_type {};
- template <template <typename> class _Trait, typename _Tp>
- struct __value_exists_impl<_Trait, _Tp, void_t<decltype(_Trait<_Tp>::value)>>
- : true_type {};
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __digits_impl {};
- template <typename _Tp>
- struct __digits_impl<_Tp, true>
- {
- static inline constexpr int value
- = sizeof(_Tp) * __CHAR_BIT__ - is_signed_v<_Tp>;
- };
- template <>
- struct __digits_impl<float, true>
- { static inline constexpr int value = __FLT_MANT_DIG__; };
- template <>
- struct __digits_impl<double, true>
- { static inline constexpr int value = __DBL_MANT_DIG__; };
- template <>
- struct __digits_impl<long double, true>
- { static inline constexpr int value = __LDBL_MANT_DIG__; };
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __digits10_impl {};
- template <typename _Tp>
- struct __digits10_impl<_Tp, true>
- {
- // The fraction 643/2136 approximates log10(2) to 7 significant digits.
- static inline constexpr int value = __digits_impl<_Tp>::value * 643L / 2136;
- };
- template <>
- struct __digits10_impl<float, true>
- { static inline constexpr int value = __FLT_DIG__; };
- template <>
- struct __digits10_impl<double, true>
- { static inline constexpr int value = __DBL_DIG__; };
- template <>
- struct __digits10_impl<long double, true>
- { static inline constexpr int value = __LDBL_DIG__; };
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __max_digits10_impl {};
- template <typename _Tp>
- struct __max_digits10_impl<_Tp, true>
- {
- static inline constexpr int value
- = is_floating_point_v<_Tp> ? 2 + __digits_impl<_Tp>::value * 643L / 2136
- : __digits10_impl<_Tp>::value + 1;
- };
- template <typename _Tp>
- struct __max_exponent_impl {};
- template <>
- struct __max_exponent_impl<float>
- { static inline constexpr int value = __FLT_MAX_EXP__; };
- template <>
- struct __max_exponent_impl<double>
- { static inline constexpr int value = __DBL_MAX_EXP__; };
- template <>
- struct __max_exponent_impl<long double>
- { static inline constexpr int value = __LDBL_MAX_EXP__; };
- template <typename _Tp>
- struct __max_exponent10_impl {};
- template <>
- struct __max_exponent10_impl<float>
- { static inline constexpr int value = __FLT_MAX_10_EXP__; };
- template <>
- struct __max_exponent10_impl<double>
- { static inline constexpr int value = __DBL_MAX_10_EXP__; };
- template <>
- struct __max_exponent10_impl<long double>
- { static inline constexpr int value = __LDBL_MAX_10_EXP__; };
- template <typename _Tp>
- struct __min_exponent_impl {};
- template <>
- struct __min_exponent_impl<float>
- { static inline constexpr int value = __FLT_MIN_EXP__; };
- template <>
- struct __min_exponent_impl<double>
- { static inline constexpr int value = __DBL_MIN_EXP__; };
- template <>
- struct __min_exponent_impl<long double>
- { static inline constexpr int value = __LDBL_MIN_EXP__; };
- template <typename _Tp>
- struct __min_exponent10_impl {};
- template <>
- struct __min_exponent10_impl<float>
- { static inline constexpr int value = __FLT_MIN_10_EXP__; };
- template <>
- struct __min_exponent10_impl<double>
- { static inline constexpr int value = __DBL_MIN_10_EXP__; };
- template <>
- struct __min_exponent10_impl<long double>
- { static inline constexpr int value = __LDBL_MIN_10_EXP__; };
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __radix_impl {};
- template <typename _Tp>
- struct __radix_impl<_Tp, true>
- {
- static inline constexpr int value
- = is_floating_point_v<_Tp> ? __FLT_RADIX__ : 2;
- };
- // [num.traits.util], numeric utility traits
- template <template <typename> class _Trait, typename _Tp>
- struct __value_exists : __value_exists_impl<_Trait, _Tp> {};
- template <template <typename> class _Trait, typename _Tp>
- inline constexpr bool __value_exists_v = __value_exists<_Trait, _Tp>::value;
- template <template <typename> class _Trait, typename _Tp, typename _Up = _Tp>
- inline constexpr _Up
- __value_or(_Up __def = _Up()) noexcept
- {
- if constexpr (__value_exists_v<_Trait, _Tp>)
- return static_cast<_Up>(_Trait<_Tp>::value);
- else
- return __def;
- }
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __norm_min_impl {};
- template <typename _Tp>
- struct __norm_min_impl<_Tp, true>
- { static inline constexpr _Tp value = 1; };
- template <>
- struct __norm_min_impl<float, true>
- { static inline constexpr float value = __FLT_MIN__; };
- template <>
- struct __norm_min_impl<double, true>
- { static inline constexpr double value = __DBL_MIN__; };
- template <>
- struct __norm_min_impl<long double, true>
- { static inline constexpr long double value = __LDBL_MIN__; };
- template <typename _Tp>
- struct __denorm_min_impl : __norm_min_impl<_Tp> {};
- #if __FLT_HAS_DENORM__
- template <>
- struct __denorm_min_impl<float>
- { static inline constexpr float value = __FLT_DENORM_MIN__; };
- #endif
- #if __DBL_HAS_DENORM__
- template <>
- struct __denorm_min_impl<double>
- { static inline constexpr double value = __DBL_DENORM_MIN__; };
- #endif
- #if __LDBL_HAS_DENORM__
- template <>
- struct __denorm_min_impl<long double>
- { static inline constexpr long double value = __LDBL_DENORM_MIN__; };
- #endif
- template <typename _Tp>
- struct __epsilon_impl {};
- template <>
- struct __epsilon_impl<float>
- { static inline constexpr float value = __FLT_EPSILON__; };
- template <>
- struct __epsilon_impl<double>
- { static inline constexpr double value = __DBL_EPSILON__; };
- template <>
- struct __epsilon_impl<long double>
- { static inline constexpr long double value = __LDBL_EPSILON__; };
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __finite_min_impl {};
- template <typename _Tp>
- struct __finite_min_impl<_Tp, true>
- {
- static inline constexpr _Tp value
- = is_unsigned_v<_Tp> ? _Tp()
- : -2 * (_Tp(1) << __digits_impl<_Tp>::value - 1);
- };
- template <>
- struct __finite_min_impl<float, true>
- { static inline constexpr float value = -__FLT_MAX__; };
- template <>
- struct __finite_min_impl<double, true>
- { static inline constexpr double value = -__DBL_MAX__; };
- template <>
- struct __finite_min_impl<long double, true>
- { static inline constexpr long double value = -__LDBL_MAX__; };
- template <typename _Tp, bool = is_arithmetic_v<_Tp>>
- struct __finite_max_impl {};
- template <typename _Tp>
- struct __finite_max_impl<_Tp, true>
- { static inline constexpr _Tp value = ~__finite_min_impl<_Tp>::value; };
- template <>
- struct __finite_max_impl<float, true>
- { static inline constexpr float value = __FLT_MAX__; };
- template <>
- struct __finite_max_impl<double, true>
- { static inline constexpr double value = __DBL_MAX__; };
- template <>
- struct __finite_max_impl<long double, true>
- { static inline constexpr long double value = __LDBL_MAX__; };
- template <typename _Tp>
- struct __infinity_impl {};
- #if __FLT_HAS_INFINITY__
- template <>
- struct __infinity_impl<float>
- { static inline constexpr float value = __builtin_inff(); };
- #endif
- #if __DBL_HAS_INFINITY__
- template <>
- struct __infinity_impl<double>
- { static inline constexpr double value = __builtin_inf(); };
- #endif
- #if __LDBL_HAS_INFINITY__
- template <>
- struct __infinity_impl<long double>
- { static inline constexpr long double value = __builtin_infl(); };
- #endif
- template <typename _Tp>
- struct __quiet_NaN_impl {};
- #if __FLT_HAS_QUIET_NAN__
- template <>
- struct __quiet_NaN_impl<float>
- { static inline constexpr float value = __builtin_nanf(""); };
- #endif
- #if __DBL_HAS_QUIET_NAN__
- template <>
- struct __quiet_NaN_impl<double>
- { static inline constexpr double value = __builtin_nan(""); };
- #endif
- #if __LDBL_HAS_QUIET_NAN__
- template <>
- struct __quiet_NaN_impl<long double>
- { static inline constexpr long double value = __builtin_nanl(""); };
- #endif
- template <typename _Tp, bool = is_floating_point_v<_Tp>>
- struct __reciprocal_overflow_threshold_impl {};
- template <typename _Tp>
- struct __reciprocal_overflow_threshold_impl<_Tp, true>
- {
- // This typically yields a subnormal value. Is this incorrect for
- // flush-to-zero configurations?
- static constexpr _Tp _S_search(_Tp __ok, _Tp __overflows)
- {
- const _Tp __mid = (__ok + __overflows) / 2;
- // 1/__mid without -ffast-math is not a constant expression if it
- // overflows. Therefore divide 1 by the radix before division.
- // Consequently finite_max (the threshold) must be scaled by the
- // same value.
- if (__mid == __ok || __mid == __overflows)
- return __ok;
- else if (_Tp(1) / (__radix_impl<_Tp>::value * __mid)
- <= __finite_max_impl<_Tp>::value / __radix_impl<_Tp>::value)
- return _S_search(__mid, __overflows);
- else
- return _S_search(__ok, __mid);
- }
- static inline constexpr _Tp value
- = _S_search(_Tp(1.01) / __finite_max_impl<_Tp>::value,
- _Tp(0.99) / __finite_max_impl<_Tp>::value);
- };
- template <typename _Tp, bool = is_floating_point_v<_Tp>>
- struct __round_error_impl {};
- template <typename _Tp>
- struct __round_error_impl<_Tp, true>
- { static inline constexpr _Tp value = 0.5; };
- template <typename _Tp>
- struct __signaling_NaN_impl {};
- #if __FLT_HAS_QUIET_NAN__
- template <>
- struct __signaling_NaN_impl<float>
- { static inline constexpr float value = __builtin_nansf(""); };
- #endif
- #if __DBL_HAS_QUIET_NAN__
- template <>
- struct __signaling_NaN_impl<double>
- { static inline constexpr double value = __builtin_nans(""); };
- #endif
- #if __LDBL_HAS_QUIET_NAN__
- template <>
- struct __signaling_NaN_impl<long double>
- { static inline constexpr long double value = __builtin_nansl(""); };
- #endif
- // [num.traits.val], numeric distinguished value traits
- template <typename _Tp>
- struct __denorm_min : __denorm_min_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __epsilon : __epsilon_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __finite_max : __finite_max_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __finite_min : __finite_min_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __infinity : __infinity_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __norm_min : __norm_min_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __quiet_NaN : __quiet_NaN_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __reciprocal_overflow_threshold
- : __reciprocal_overflow_threshold_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __round_error : __round_error_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __signaling_NaN : __signaling_NaN_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- inline constexpr auto __denorm_min_v = __denorm_min<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __epsilon_v = __epsilon<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __finite_max_v = __finite_max<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __finite_min_v = __finite_min<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __infinity_v = __infinity<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __norm_min_v = __norm_min<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __quiet_NaN_v = __quiet_NaN<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __reciprocal_overflow_threshold_v
- = __reciprocal_overflow_threshold<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __round_error_v = __round_error<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __signaling_NaN_v = __signaling_NaN<_Tp>::value;
- // [num.traits.char], numeric characteristics traits
- template <typename _Tp>
- struct __digits : __digits_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __digits10 : __digits10_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __max_digits10 : __max_digits10_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __max_exponent : __max_exponent_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __max_exponent10 : __max_exponent10_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __min_exponent : __min_exponent_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __min_exponent10 : __min_exponent10_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- struct __radix : __radix_impl<remove_cv_t<_Tp>> {};
- template <typename _Tp>
- inline constexpr auto __digits_v = __digits<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __digits10_v = __digits10<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __max_digits10_v = __max_digits10<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __max_exponent_v = __max_exponent<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __max_exponent10_v = __max_exponent10<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __min_exponent_v = __min_exponent<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __min_exponent10_v = __min_exponent10<_Tp>::value;
- template <typename _Tp>
- inline constexpr auto __radix_v = __radix<_Tp>::value;
- // mkretz's extensions
- // TODO: does GCC tell me? __GCC_IEC_559 >= 2 is not the right answer
- template <typename _Tp>
- struct __has_iec559_storage_format : true_type {};
- template <typename _Tp>
- inline constexpr bool __has_iec559_storage_format_v
- = __has_iec559_storage_format<_Tp>::value;
- /* To propose:
- If __has_iec559_behavior<__quiet_NaN, T> is true the following holds:
- - nan == nan is false
- - isnan(nan) is true
- - isnan(nan + x) is true
- - isnan(inf/inf) is true
- - isnan(0/0) is true
- - isunordered(nan, x) is true
- If __has_iec559_behavior<__infinity, T> is true the following holds (x is
- neither nan nor inf):
- - isinf(inf) is true
- - isinf(inf + x) is true
- - isinf(1/0) is true
- */
- template <template <typename> class _Trait, typename _Tp>
- struct __has_iec559_behavior : false_type {};
- template <template <typename> class _Trait, typename _Tp>
- inline constexpr bool __has_iec559_behavior_v
- = __has_iec559_behavior<_Trait, _Tp>::value;
- #if !__FINITE_MATH_ONLY__
- #if __FLT_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__quiet_NaN, float> : true_type {};
- #endif
- #if __DBL_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__quiet_NaN, double> : true_type {};
- #endif
- #if __LDBL_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__quiet_NaN, long double> : true_type {};
- #endif
- #if __FLT_HAS_INFINITY__
- template <>
- struct __has_iec559_behavior<__infinity, float> : true_type {};
- #endif
- #if __DBL_HAS_INFINITY__
- template <>
- struct __has_iec559_behavior<__infinity, double> : true_type {};
- #endif
- #if __LDBL_HAS_INFINITY__
- template <>
- struct __has_iec559_behavior<__infinity, long double> : true_type {};
- #endif
- #ifdef __SUPPORT_SNAN__
- #if __FLT_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__signaling_NaN, float> : true_type {};
- #endif
- #if __DBL_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__signaling_NaN, double> : true_type {};
- #endif
- #if __LDBL_HAS_QUIET_NAN__
- template <>
- struct __has_iec559_behavior<__signaling_NaN, long double> : true_type {};
- #endif
- #endif
- #endif // __FINITE_MATH_ONLY__
- } // namespace std
|