aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/experimental/bits/numeric_traits.h
blob: 269ac08a70e6c64aa94ce044c5a219eb7c45f64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Definition of numeric_limits replacement traits P1841R1 -*- C++ -*-

// Copyright (C) 2020-2025 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/>.

#ifndef _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H
#define _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H

#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
#endif  // _GLIBCXX_EXPERIMENTAL_BITS_NUMERIC_TRAITS_H