1 // Definition of numeric_limits replacement traits P1841R1 -*- C++ -*- 2 3 // Copyright (C) 2020-2022 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 #ifndef _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H 26 #define _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H 27 28 #include <type_traits> 29 30 namespace std { 31 32 template <template <typename> class _Trait, typename _Tp, typename = void> 33 struct __value_exists_impl : false_type {}; 34 35 template <template <typename> class _Trait, typename _Tp> 36 struct __value_exists_impl<_Trait, _Tp, void_t<decltype(_Trait<_Tp>::value)>> 37 : true_type {}; 38 39 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 40 struct __digits_impl {}; 41 42 template <typename _Tp> 43 struct __digits_impl<_Tp, true> 44 { 45 static inline constexpr int value 46 = sizeof(_Tp) * __CHAR_BIT__ - is_signed_v<_Tp>; 47 }; 48 49 template <> 50 struct __digits_impl<float, true> 51 { static inline constexpr int value = __FLT_MANT_DIG__; }; 52 53 template <> 54 struct __digits_impl<double, true> 55 { static inline constexpr int value = __DBL_MANT_DIG__; }; 56 57 template <> 58 struct __digits_impl<long double, true> 59 { static inline constexpr int value = __LDBL_MANT_DIG__; }; 60 61 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 62 struct __digits10_impl {}; 63 64 template <typename _Tp> 65 struct __digits10_impl<_Tp, true> 66 { 67 // The fraction 643/2136 approximates log10(2) to 7 significant digits. 68 static inline constexpr int value = __digits_impl<_Tp>::value * 643L / 2136; 69 }; 70 71 template <> 72 struct __digits10_impl<float, true> 73 { static inline constexpr int value = __FLT_DIG__; }; 74 75 template <> 76 struct __digits10_impl<double, true> 77 { static inline constexpr int value = __DBL_DIG__; }; 78 79 template <> 80 struct __digits10_impl<long double, true> 81 { static inline constexpr int value = __LDBL_DIG__; }; 82 83 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 84 struct __max_digits10_impl {}; 85 86 template <typename _Tp> 87 struct __max_digits10_impl<_Tp, true> 88 { 89 static inline constexpr int value 90 = is_floating_point_v<_Tp> ? 2 + __digits_impl<_Tp>::value * 643L / 2136 91 : __digits10_impl<_Tp>::value + 1; 92 }; 93 94 template <typename _Tp> 95 struct __max_exponent_impl {}; 96 97 template <> 98 struct __max_exponent_impl<float> 99 { static inline constexpr int value = __FLT_MAX_EXP__; }; 100 101 template <> 102 struct __max_exponent_impl<double> 103 { static inline constexpr int value = __DBL_MAX_EXP__; }; 104 105 template <> 106 struct __max_exponent_impl<long double> 107 { static inline constexpr int value = __LDBL_MAX_EXP__; }; 108 109 template <typename _Tp> 110 struct __max_exponent10_impl {}; 111 112 template <> 113 struct __max_exponent10_impl<float> 114 { static inline constexpr int value = __FLT_MAX_10_EXP__; }; 115 116 template <> 117 struct __max_exponent10_impl<double> 118 { static inline constexpr int value = __DBL_MAX_10_EXP__; }; 119 120 template <> 121 struct __max_exponent10_impl<long double> 122 { static inline constexpr int value = __LDBL_MAX_10_EXP__; }; 123 124 template <typename _Tp> 125 struct __min_exponent_impl {}; 126 127 template <> 128 struct __min_exponent_impl<float> 129 { static inline constexpr int value = __FLT_MIN_EXP__; }; 130 131 template <> 132 struct __min_exponent_impl<double> 133 { static inline constexpr int value = __DBL_MIN_EXP__; }; 134 135 template <> 136 struct __min_exponent_impl<long double> 137 { static inline constexpr int value = __LDBL_MIN_EXP__; }; 138 139 template <typename _Tp> 140 struct __min_exponent10_impl {}; 141 142 template <> 143 struct __min_exponent10_impl<float> 144 { static inline constexpr int value = __FLT_MIN_10_EXP__; }; 145 146 template <> 147 struct __min_exponent10_impl<double> 148 { static inline constexpr int value = __DBL_MIN_10_EXP__; }; 149 150 template <> 151 struct __min_exponent10_impl<long double> 152 { static inline constexpr int value = __LDBL_MIN_10_EXP__; }; 153 154 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 155 struct __radix_impl {}; 156 157 template <typename _Tp> 158 struct __radix_impl<_Tp, true> 159 { 160 static inline constexpr int value 161 = is_floating_point_v<_Tp> ? __FLT_RADIX__ : 2; 162 }; 163 164 // [num.traits.util], numeric utility traits 165 template <template <typename> class _Trait, typename _Tp> 166 struct __value_exists : __value_exists_impl<_Trait, _Tp> {}; 167 168 template <template <typename> class _Trait, typename _Tp> 169 inline constexpr bool __value_exists_v = __value_exists<_Trait, _Tp>::value; 170 171 template <template <typename> class _Trait, typename _Tp, typename _Up = _Tp> 172 inline constexpr _Up 173 __value_or(_Up __def = _Up()) noexcept 174 { 175 if constexpr (__value_exists_v<_Trait, _Tp>) 176 return static_cast<_Up>(_Trait<_Tp>::value); 177 else 178 return __def; 179 } 180 181 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 182 struct __norm_min_impl {}; 183 184 template <typename _Tp> 185 struct __norm_min_impl<_Tp, true> 186 { static inline constexpr _Tp value = 1; }; 187 188 template <> 189 struct __norm_min_impl<float, true> 190 { static inline constexpr float value = __FLT_MIN__; }; 191 192 template <> 193 struct __norm_min_impl<double, true> 194 { static inline constexpr double value = __DBL_MIN__; }; 195 196 template <> 197 struct __norm_min_impl<long double, true> 198 { static inline constexpr long double value = __LDBL_MIN__; }; 199 200 template <typename _Tp> 201 struct __denorm_min_impl : __norm_min_impl<_Tp> {}; 202 203 #if __FLT_HAS_DENORM__ 204 template <> 205 struct __denorm_min_impl<float> 206 { static inline constexpr float value = __FLT_DENORM_MIN__; }; 207 #endif 208 209 #if __DBL_HAS_DENORM__ 210 template <> 211 struct __denorm_min_impl<double> 212 { static inline constexpr double value = __DBL_DENORM_MIN__; }; 213 #endif 214 215 #if __LDBL_HAS_DENORM__ 216 template <> 217 struct __denorm_min_impl<long double> 218 { static inline constexpr long double value = __LDBL_DENORM_MIN__; }; 219 #endif 220 221 template <typename _Tp> 222 struct __epsilon_impl {}; 223 224 template <> 225 struct __epsilon_impl<float> 226 { static inline constexpr float value = __FLT_EPSILON__; }; 227 228 template <> 229 struct __epsilon_impl<double> 230 { static inline constexpr double value = __DBL_EPSILON__; }; 231 232 template <> 233 struct __epsilon_impl<long double> 234 { static inline constexpr long double value = __LDBL_EPSILON__; }; 235 236 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 237 struct __finite_min_impl {}; 238 239 template <typename _Tp> 240 struct __finite_min_impl<_Tp, true> 241 { 242 static inline constexpr _Tp value 243 = is_unsigned_v<_Tp> ? _Tp() 244 : -2 * (_Tp(1) << __digits_impl<_Tp>::value - 1); 245 }; 246 247 template <> 248 struct __finite_min_impl<float, true> 249 { static inline constexpr float value = -__FLT_MAX__; }; 250 251 template <> 252 struct __finite_min_impl<double, true> 253 { static inline constexpr double value = -__DBL_MAX__; }; 254 255 template <> 256 struct __finite_min_impl<long double, true> 257 { static inline constexpr long double value = -__LDBL_MAX__; }; 258 259 template <typename _Tp, bool = is_arithmetic_v<_Tp>> 260 struct __finite_max_impl {}; 261 262 template <typename _Tp> 263 struct __finite_max_impl<_Tp, true> 264 { static inline constexpr _Tp value = ~__finite_min_impl<_Tp>::value; }; 265 266 template <> 267 struct __finite_max_impl<float, true> 268 { static inline constexpr float value = __FLT_MAX__; }; 269 270 template <> 271 struct __finite_max_impl<double, true> 272 { static inline constexpr double value = __DBL_MAX__; }; 273 274 template <> 275 struct __finite_max_impl<long double, true> 276 { static inline constexpr long double value = __LDBL_MAX__; }; 277 278 template <typename _Tp> 279 struct __infinity_impl {}; 280 281 #if __FLT_HAS_INFINITY__ 282 template <> 283 struct __infinity_impl<float> 284 { static inline constexpr float value = __builtin_inff(); }; 285 #endif 286 287 #if __DBL_HAS_INFINITY__ 288 template <> 289 struct __infinity_impl<double> 290 { static inline constexpr double value = __builtin_inf(); }; 291 #endif 292 293 #if __LDBL_HAS_INFINITY__ 294 template <> 295 struct __infinity_impl<long double> 296 { static inline constexpr long double value = __builtin_infl(); }; 297 #endif 298 299 template <typename _Tp> 300 struct __quiet_NaN_impl {}; 301 302 #if __FLT_HAS_QUIET_NAN__ 303 template <> 304 struct __quiet_NaN_impl<float> 305 { static inline constexpr float value = __builtin_nanf(""); }; 306 #endif 307 308 #if __DBL_HAS_QUIET_NAN__ 309 template <> 310 struct __quiet_NaN_impl<double> 311 { static inline constexpr double value = __builtin_nan(""); }; 312 #endif 313 314 #if __LDBL_HAS_QUIET_NAN__ 315 template <> 316 struct __quiet_NaN_impl<long double> 317 { static inline constexpr long double value = __builtin_nanl(""); }; 318 #endif 319 320 template <typename _Tp, bool = is_floating_point_v<_Tp>> 321 struct __reciprocal_overflow_threshold_impl {}; 322 323 template <typename _Tp> 324 struct __reciprocal_overflow_threshold_impl<_Tp, true> 325 { 326 // This typically yields a subnormal value. Is this incorrect for 327 // flush-to-zero configurations? 328 static constexpr _Tp _S_search(_Tp __ok, _Tp __overflows) 329 { 330 const _Tp __mid = (__ok + __overflows) / 2; 331 // 1/__mid without -ffast-math is not a constant expression if it 332 // overflows. Therefore divide 1 by the radix before division. 333 // Consequently finite_max (the threshold) must be scaled by the 334 // same value. 335 if (__mid == __ok || __mid == __overflows) 336 return __ok; 337 else if (_Tp(1) / (__radix_impl<_Tp>::value * __mid) 338 <= __finite_max_impl<_Tp>::value / __radix_impl<_Tp>::value) 339 return _S_search(__mid, __overflows); 340 else 341 return _S_search(__ok, __mid); 342 } 343 344 static inline constexpr _Tp value 345 = _S_search(_Tp(1.01) / __finite_max_impl<_Tp>::value, 346 _Tp(0.99) / __finite_max_impl<_Tp>::value); 347 }; 348 349 template <typename _Tp, bool = is_floating_point_v<_Tp>> 350 struct __round_error_impl {}; 351 352 template <typename _Tp> 353 struct __round_error_impl<_Tp, true> 354 { static inline constexpr _Tp value = 0.5; }; 355 356 template <typename _Tp> 357 struct __signaling_NaN_impl {}; 358 359 #if __FLT_HAS_QUIET_NAN__ 360 template <> 361 struct __signaling_NaN_impl<float> 362 { static inline constexpr float value = __builtin_nansf(""); }; 363 #endif 364 365 #if __DBL_HAS_QUIET_NAN__ 366 template <> 367 struct __signaling_NaN_impl<double> 368 { static inline constexpr double value = __builtin_nans(""); }; 369 #endif 370 371 #if __LDBL_HAS_QUIET_NAN__ 372 template <> 373 struct __signaling_NaN_impl<long double> 374 { static inline constexpr long double value = __builtin_nansl(""); }; 375 #endif 376 377 // [num.traits.val], numeric distinguished value traits 378 template <typename _Tp> 379 struct __denorm_min : __denorm_min_impl<remove_cv_t<_Tp>> {}; 380 381 template <typename _Tp> 382 struct __epsilon : __epsilon_impl<remove_cv_t<_Tp>> {}; 383 384 template <typename _Tp> 385 struct __finite_max : __finite_max_impl<remove_cv_t<_Tp>> {}; 386 387 template <typename _Tp> 388 struct __finite_min : __finite_min_impl<remove_cv_t<_Tp>> {}; 389 390 template <typename _Tp> 391 struct __infinity : __infinity_impl<remove_cv_t<_Tp>> {}; 392 393 template <typename _Tp> 394 struct __norm_min : __norm_min_impl<remove_cv_t<_Tp>> {}; 395 396 template <typename _Tp> 397 struct __quiet_NaN : __quiet_NaN_impl<remove_cv_t<_Tp>> {}; 398 399 template <typename _Tp> 400 struct __reciprocal_overflow_threshold 401 : __reciprocal_overflow_threshold_impl<remove_cv_t<_Tp>> {}; 402 403 template <typename _Tp> 404 struct __round_error : __round_error_impl<remove_cv_t<_Tp>> {}; 405 406 template <typename _Tp> 407 struct __signaling_NaN : __signaling_NaN_impl<remove_cv_t<_Tp>> {}; 408 409 template <typename _Tp> 410 inline constexpr auto __denorm_min_v = __denorm_min<_Tp>::value; 411 412 template <typename _Tp> 413 inline constexpr auto __epsilon_v = __epsilon<_Tp>::value; 414 415 template <typename _Tp> 416 inline constexpr auto __finite_max_v = __finite_max<_Tp>::value; 417 418 template <typename _Tp> 419 inline constexpr auto __finite_min_v = __finite_min<_Tp>::value; 420 421 template <typename _Tp> 422 inline constexpr auto __infinity_v = __infinity<_Tp>::value; 423 424 template <typename _Tp> 425 inline constexpr auto __norm_min_v = __norm_min<_Tp>::value; 426 427 template <typename _Tp> 428 inline constexpr auto __quiet_NaN_v = __quiet_NaN<_Tp>::value; 429 430 template <typename _Tp> 431 inline constexpr auto __reciprocal_overflow_threshold_v 432 = __reciprocal_overflow_threshold<_Tp>::value; 433 434 template <typename _Tp> 435 inline constexpr auto __round_error_v = __round_error<_Tp>::value; 436 437 template <typename _Tp> 438 inline constexpr auto __signaling_NaN_v = __signaling_NaN<_Tp>::value; 439 440 // [num.traits.char], numeric characteristics traits 441 template <typename _Tp> 442 struct __digits : __digits_impl<remove_cv_t<_Tp>> {}; 443 444 template <typename _Tp> 445 struct __digits10 : __digits10_impl<remove_cv_t<_Tp>> {}; 446 447 template <typename _Tp> 448 struct __max_digits10 : __max_digits10_impl<remove_cv_t<_Tp>> {}; 449 450 template <typename _Tp> 451 struct __max_exponent : __max_exponent_impl<remove_cv_t<_Tp>> {}; 452 453 template <typename _Tp> 454 struct __max_exponent10 : __max_exponent10_impl<remove_cv_t<_Tp>> {}; 455 456 template <typename _Tp> 457 struct __min_exponent : __min_exponent_impl<remove_cv_t<_Tp>> {}; 458 459 template <typename _Tp> 460 struct __min_exponent10 : __min_exponent10_impl<remove_cv_t<_Tp>> {}; 461 462 template <typename _Tp> 463 struct __radix : __radix_impl<remove_cv_t<_Tp>> {}; 464 465 template <typename _Tp> 466 inline constexpr auto __digits_v = __digits<_Tp>::value; 467 468 template <typename _Tp> 469 inline constexpr auto __digits10_v = __digits10<_Tp>::value; 470 471 template <typename _Tp> 472 inline constexpr auto __max_digits10_v = __max_digits10<_Tp>::value; 473 474 template <typename _Tp> 475 inline constexpr auto __max_exponent_v = __max_exponent<_Tp>::value; 476 477 template <typename _Tp> 478 inline constexpr auto __max_exponent10_v = __max_exponent10<_Tp>::value; 479 480 template <typename _Tp> 481 inline constexpr auto __min_exponent_v = __min_exponent<_Tp>::value; 482 483 template <typename _Tp> 484 inline constexpr auto __min_exponent10_v = __min_exponent10<_Tp>::value; 485 486 template <typename _Tp> 487 inline constexpr auto __radix_v = __radix<_Tp>::value; 488 489 // mkretz's extensions 490 // TODO: does GCC tell me? __GCC_IEC_559 >= 2 is not the right answer 491 template <typename _Tp> 492 struct __has_iec559_storage_format : true_type {}; 493 494 template <typename _Tp> 495 inline constexpr bool __has_iec559_storage_format_v 496 = __has_iec559_storage_format<_Tp>::value; 497 498 /* To propose: 499 If __has_iec559_behavior<__quiet_NaN, T> is true the following holds: 500 - nan == nan is false 501 - isnan(nan) is true 502 - isnan(nan + x) is true 503 - isnan(inf/inf) is true 504 - isnan(0/0) is true 505 - isunordered(nan, x) is true 506 507 If __has_iec559_behavior<__infinity, T> is true the following holds (x is 508 neither nan nor inf): 509 - isinf(inf) is true 510 - isinf(inf + x) is true 511 - isinf(1/0) is true 512 */ 513 template <template <typename> class _Trait, typename _Tp> 514 struct __has_iec559_behavior : false_type {}; 515 516 template <template <typename> class _Trait, typename _Tp> 517 inline constexpr bool __has_iec559_behavior_v 518 = __has_iec559_behavior<_Trait, _Tp>::value; 519 520 #if !__FINITE_MATH_ONLY__ 521 #if __FLT_HAS_QUIET_NAN__ 522 template <> 523 struct __has_iec559_behavior<__quiet_NaN, float> : true_type {}; 524 #endif 525 526 #if __DBL_HAS_QUIET_NAN__ 527 template <> 528 struct __has_iec559_behavior<__quiet_NaN, double> : true_type {}; 529 #endif 530 531 #if __LDBL_HAS_QUIET_NAN__ 532 template <> 533 struct __has_iec559_behavior<__quiet_NaN, long double> : true_type {}; 534 #endif 535 536 #if __FLT_HAS_INFINITY__ 537 template <> 538 struct __has_iec559_behavior<__infinity, float> : true_type {}; 539 #endif 540 541 #if __DBL_HAS_INFINITY__ 542 template <> 543 struct __has_iec559_behavior<__infinity, double> : true_type {}; 544 #endif 545 546 #if __LDBL_HAS_INFINITY__ 547 template <> 548 struct __has_iec559_behavior<__infinity, long double> : true_type {}; 549 #endif 550 551 #ifdef __SUPPORT_SNAN__ 552 #if __FLT_HAS_QUIET_NAN__ 553 template <> 554 struct __has_iec559_behavior<__signaling_NaN, float> : true_type {}; 555 #endif 556 557 #if __DBL_HAS_QUIET_NAN__ 558 template <> 559 struct __has_iec559_behavior<__signaling_NaN, double> : true_type {}; 560 #endif 561 562 #if __LDBL_HAS_QUIET_NAN__ 563 template <> 564 struct __has_iec559_behavior<__signaling_NaN, long double> : true_type {}; 565 #endif 566 567 #endif 568 #endif // __FINITE_MATH_ONLY__ 569 570 } // namespace std 571 #endif // _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H 572