32 namespace json_details {
33 inline constexpr double dpow10_tbl[] = {
34 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
35 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
36 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
37 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
38 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
39 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
40 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
41 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
42 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
43 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
44 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
45 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
46 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
47 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
48 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
49 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
50 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169,
51 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179,
52 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189,
53 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199,
54 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209,
55 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219,
56 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229,
57 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239,
58 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249,
59 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259,
60 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269,
61 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279,
62 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289,
63 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299,
64 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308 };
69 inline constexpr long double ldpow10_tbl[] = {
70 1e0L, 1e1L, 1e2L, 1e3L, 1e4L, 1e5L, 1e6L,
71 1e7L, 1e8L, 1e9L, 1e10L, 1e11L, 1e12L, 1e13L,
72 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L, 1e20L,
73 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L,
74 1e28L, 1e29L, 1e30L, 1e31L, 1e32L, 1e33L, 1e34L,
75 1e35L, 1e36L, 1e37L, 1e38L, 1e39L, 1e40L, 1e41L,
76 1e42L, 1e43L, 1e44L, 1e45L, 1e46L, 1e47L, 1e48L,
78 inline constexpr int max_ld_table_exp = 48;
80 inline constexpr int max_dbl_exp =
81 std::numeric_limits<double>::max_exponent10;
84 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr T
const &
85 Min( T
const &l, T
const &r )
noexcept {
92 template<
typename Result>
93 inline constexpr int max_exp =
94 std::is_same_v<Result, float>
95 ? Min( max_dbl_exp, std::numeric_limits<float>::max_exponent10 )
98 template<
typename Result,
typename Un
signed>
99 [[nodiscard]] DAW_ATTRIB_FLATINLINE
constexpr Result
100 power10_constexpr( Result result, Unsigned p ) {
103 if constexpr( is_double_sized_long_double_v<Result> ==
false and
104 std::is_same_v<Result, long double> ) {
105 DAW_CPP23_STATIC_LOCAL
constexpr int max_exact_exp =
106 std::numeric_limits<long double>::digits == 113
109 if( p >= -max_exact_exp and p <= max_exact_exp ) {
112 ldpow10_tbl[
static_cast<std::size_t
>( -p )];
114 return result * ldpow10_tbl[
static_cast<std::size_t
>( p )];
118 DAW_CPP23_STATIC_LOCAL
constexpr auto max_v =
119 static_cast<Result
>( dpow10_tbl[max_exp<Result>] );
121 if( DAW_UNLIKELY( p > max_exp<Result> ) ) {
122 if constexpr( std::is_floating_point_v<Result> ) {
123 if( DAW_UNLIKELY( p >
124 daw::numeric_limits<Result>::max_exponent ) ) {
125 return std::numeric_limits<Result>::infinity( );
129 p -= max_exp<Result>;
131 for( ; p > max_exp<Result>; p -= max_exp<Result> ) {
134 return static_cast<Result
>( result ) *
135 ( exp2 *
static_cast<Result
>(
136 dpow10_tbl[
static_cast<std::size_t
>( p )] ) );
138 if( DAW_UNLIKELY( p < -max_exp<Result> ) ) {
139 if constexpr( std::is_floating_point_v<Result> ) {
140 if( DAW_UNLIKELY( p <
141 daw::numeric_limits<Result>::min_exponent ) ) {
146 p += max_exp<Result>;
148 for( ; p < -max_exp<Result>; p += max_exp<Result> ) {
151 return (
static_cast<Result
>( result ) /
153 dpow10_tbl[
static_cast<std::size_t
>( -p )] ) ) /
157 return static_cast<Result
>( result ) /
159 dpow10_tbl[
static_cast<std::size_t
>( -p )] );
161 return static_cast<Result
>( result ) *
162 static_cast<Result
>( dpow10_tbl[
static_cast<std::size_t
>( p )] );
165 namespace power10_details {
167 inline constexpr std::uint32_t max_exact_double_power10 = 22;
176 template<
typename Result>
177 [[nodiscard]]
constexpr Result power10_value( std::uint32_t n ) {
179 static_cast<Result
>( dpow10_tbl[n % max_exact_double_power10] );
180 auto q = n / max_exact_double_power10;
183 static_cast<Result
>( dpow10_tbl[max_exact_double_power10] );
185 if( ( q & 1U ) != 0 ) {
186 power = power * base;
207 template<
typename Result>
208 [[nodiscard]]
constexpr Result power10_by_squaring( Result result,
210 using limits = std::numeric_limits<Result>;
213 DAW_CPP23_STATIC_LOCAL
constexpr std::uint32_t chunk =
214 limits::max_exponent10 > 0
215 ?
static_cast<std::uint32_t
>( limits::max_exponent10 )
216 : power10_details::max_exact_double_power10;
218 DAW_CPP23_STATIC_LOCAL
constexpr std::uint64_t max_chunks =
219 static_cast<std::uint64_t
>(
220 ( limits::max_exponent10 - limits::min_exponent10 ) > 0
221 ? ( limits::max_exponent10 - limits::min_exponent10 )
226 bool const negative = p < 0;
227 auto n = negative ? std::uint64_t{ 0 } -
static_cast<std::uint64_t
>( p )
228 :
static_cast<std::uint64_t
>( p );
231 bool overflowed =
false;
232 auto const scale = [&]( Result value, Result
const &power ) -> Result {
234 return value / power;
236 if( DAW_UNLIKELY( value > limits::max( ) / power ) ) {
238 if constexpr( limits::has_infinity ) {
239 return limits::infinity( );
241 return limits::max( );
244 return value * power;
246 if( DAW_UNLIKELY( n > chunk ) ) {
247 auto const chunk_power =
248 power10_details::power10_value<Result>( chunk );
249 std::uint64_t chunks = 0;
250 while( n > chunk and chunks < max_chunks ) {
251 result = scale( result, chunk_power );
262 return scale( result,
263 power10_details::power10_value<Result>(
264 static_cast<std::uint32_t
>( n ) ) );
267#if defined( DAW_JSON_HAS_FLOAT128 )
268 namespace power10_details {
269 inline constexpr int float128_max_exponent = 16384;
270 inline constexpr int float128_min_exponent = -16381;
271#if defined( __STDCPP_FLOAT128_T__ )
272 using float128_t = std::float128_t;
274 using float128_t = __float128;
276 inline constexpr int float128_digits = 113;
281 [[nodiscard]]
constexpr float128_t make_float128(
bool negative,
282 std::uint64_t sig_hi,
283 std::uint64_t sig_lo,
285 auto value =
static_cast<float128_t
>( sig_hi ) *
286 static_cast<float128_t
>( 18446744073709551616.0 ) +
287 static_cast<float128_t
>( sig_lo );
288 auto base =
static_cast<float128_t
>( exp2 < 0 ? 0.5 : 2.0 );
289 auto n =
static_cast<unsigned>( exp2 < 0 ? -exp2 : exp2 );
291 if( ( n & 1U ) != 0 ) {
292 value = value * base;
299 return negative ? -value : value;
303 struct float128_pair {
310 [[nodiscard]]
constexpr float128_t
311 product_error( float128_t a, float128_t b,
312 [[maybe_unused]] float128_t p ) {
313#if defined( __FP_FAST_FMAF128 )
316 if( not DAW_IS_CONSTANT_EVALUATED_COMPAT( ) ) {
317#if defined( __STDCPP_FLOAT128_T__ )
318 return std::fma( a, b, -p );
319#elif DAW_HAS_BUILTIN( __builtin_fmaf128 )
320 return __builtin_fmaf128( a, b, -p );
327 DAW_CPP23_STATIC_LOCAL
constexpr auto splitter =
328 static_cast<float128_t
>( std::uint64_t{ 144115188075855873ULL } );
329 DAW_CPP23_STATIC_LOCAL
constexpr float128_t big =
330 make_float128(
false, 0, 1, 16000 );
331 DAW_CPP23_STATIC_LOCAL
constexpr float128_t down =
332 make_float128(
false, 0, 1, -80 );
333 DAW_CPP23_STATIC_LOCAL
constexpr float128_t up =
334 make_float128(
false, 0, 1, 80 );
335 auto scale =
static_cast<float128_t
>( 1 );
336 if( a > big or a < -big ) {
340 if( b > big or b < -big ) {
344 auto const split = []( float128_t x ) {
345 auto const c = splitter * x;
346 auto const hi = c - ( c - x );
347 return float128_pair{ hi, x - hi };
349 auto const sa = split( a );
350 auto const sb = split( b );
351 auto const scaled_p = a * b;
352 return ( ( ( ( sa.hi * sb.hi - scaled_p ) + sa.hi * sb.lo ) +
358 [[nodiscard]]
constexpr float128_pair multiply( float128_pair a,
360 auto const p = a.hi * b.hi;
362 product_error( a.hi, b.hi, p ) + ( a.hi * b.lo + a.lo * b.hi );
363 auto const s = p + e;
364 return { s, e - ( s - p ) };
367 [[nodiscard]]
constexpr float128_pair divide( float128_pair a,
369 auto const q1 = a.hi / b.hi;
370 auto const p = q1 * b.hi;
372 ( ( a.hi - p ) - product_error( q1, b.hi, p ) + a.lo ) - q1 * b.lo;
373 auto const q2 = r / b.hi;
374 auto const s = q1 + q2;
375 return { s, q2 - ( s - q1 ) };
380 inline constexpr float128_t power10_hi[] = {
381 static_cast<float128_t
>( 1e16 ),
382 static_cast<float128_t
>( 1e16 ) *
static_cast<float128_t
>( 1e16 ),
383 make_float128(
false, 0x184f03e93ff9f, 0x4daa797ed6e38ed6, 100 ),
384 make_float128(
false, 0x127748f9301d3, 0x19bf8cde66d86d62, 313 ),
385 make_float128(
false, 0x154fdd7f73bf3, 0xbd1bbb77203731fe, 738 ),
386 make_float128(
false, 0x1c633415d4c1d, 0x238d98cab8a978a1, 1588 ),
387 make_float128(
false, 0x192eceb0d02ea, 0x182eca1a7a51e317, 3289 ),
388 make_float128(
false, 0x13d1676bb8a7a, 0xbbc94e9a519c6536, 6691 ),
389 make_float128(
false, 0x188c0a4051441, 0x2f3592982a7f0095, 13494 ) };
390 inline constexpr float128_t power10_lo[] = {
391 static_cast<float128_t
>( 0 ),
392 static_cast<float128_t
>( 0 ),
393 make_float128(
false, 0x12fda87c04000, 0x0000000000000000, -14 ),
394 make_float128(
false, 0x1370234f3fd7b, 0x08dd390bc3c54e3f, 194 ),
395 make_float128(
true, 0x15487ae1b340c, 0x21d06755187f3801, 623 ),
396 make_float128(
true, 0x1383b1cd3e33f, 0x17659a2dee9638ae, 1474 ),
397 make_float128(
true, 0x1798d6ca33bec, 0x56db2781485a496c, 3175 ),
398 make_float128(
true, 0x18d6cae734220, 0xa0e7e584475598ea, 6577 ),
399 make_float128(
true, 0x1ab6cd44f026d, 0xdd8eecc1151841b6, 13380 ) };
402 [[nodiscard]]
constexpr float128_pair power10_pair( std::uint32_t n ) {
404 float128_pair{
static_cast<float128_t
>( dpow10_tbl[n & 15U] ),
405 static_cast<float128_t
>( 0 ) };
407 for( std::size_t k = 0; bits != 0; ++k, bits >>= 1U ) {
408 if( ( bits & 1U ) != 0 ) {
409 power = multiply( power, { power10_hi[k], power10_lo[k] } );
418 [[nodiscard]]
constexpr bool scale_float128( float128_t &value,
421 DAW_CPP23_STATIC_LOCAL
constexpr std::uint32_t chunk = 4932;
422 DAW_CPP23_STATIC_LOCAL
constexpr float128_t max_value = make_float128(
423 false, 0x1ffffffffffff, 0xffffffffffffffff, 16383 - 112 );
424 bool const negative = p < 0;
426 ? std::uint64_t{ 0 } -
static_cast<std::uint64_t
>( p )
427 :
static_cast<std::uint64_t
>( p );
433 n <= 22 ? static_cast<float128_t>( dpow10_tbl[n] )
434 :
static_cast<float128_t
>( dpow10_tbl[22] ) *
435 static_cast<float128_t
>( dpow10_tbl[n - 22] );
437 value = value / power;
440 if( value > max_value / power ) {
443 value = value * power;
446 auto v = float128_pair{ value,
static_cast<float128_t
>( 0 ) };
447 auto const apply = [&]( float128_pair
const &power ) {
449 v = divide( v, power );
452 if( v.hi > max_value / power.hi ) {
455 v = multiply( v, power );
459 for(
int chunks = 0; n > chunk and chunks < 3; ++chunks ) {
460 if( not apply( power10_pair( chunk ) ) ) {
468 if( not apply( power10_pair(
static_cast<std::uint32_t
>( n ) ) ) ) {
475#if defined( __STDCPP_FLOAT128_T__ ) and defined( __SIZEOF_FLOAT128__ )
479 using other_float128_t = __float128;
481 using other_float128_t = float128_t;
484#if defined( __STDCPP_FLOAT128_T__ )
486 inline constexpr bool is_std_float128_v =
487 std::is_same_v<F, std::float128_t>;
490 inline constexpr bool is_std_float128_v =
false;
498 template<
typename Result,
typename F>
499 inline constexpr bool converts_from_v =
500 std::is_floating_point_v<F> and std::is_constructible_v<Result, F> and
501 ( std::is_floating_point_v<Result> or is_std_float128_v<F> );
503 template<
typename Result,
typename F>
504 inline constexpr bool converts_to_v =
505 std::is_floating_point_v<F> and std::is_constructible_v<F, Result> and
506 ( std::is_floating_point_v<Result> or is_std_float128_v<F> );
508 template<
typename Result>
509 inline constexpr bool converts_from_float128_v =
510 converts_from_v<Result, float128_t> or
511 converts_from_v<Result, other_float128_t>;
513 template<
typename Result>
514 inline constexpr bool converts_to_float128_v =
515 converts_to_v<Result, float128_t> or
516 converts_to_v<Result, other_float128_t>;
523 template<
typename Result>
524 inline constexpr bool uses_float128_v =
525 ( std::numeric_limits<Result>::radix == 2 ) and
526 ( std::numeric_limits<Result>::digits >
527 std::numeric_limits<double>::digits ) and
528 ( std::numeric_limits<Result>::digits <= float128_digits ) and
529 ( std::numeric_limits<Result>::max_exponent <=
530 float128_max_exponent ) and
531 ( std::numeric_limits<Result>::min_exponent >=
532 float128_min_exponent - 1 ) and
533 converts_from_float128_v<Result> and converts_to_float128_v<Result>;
535 template<
typename Result>
536 [[nodiscard]]
constexpr float128_t to_float128( Result
const &value ) {
537 if constexpr( converts_to_v<Result, float128_t> ) {
538 return static_cast<float128_t
>( value );
540 return static_cast<float128_t
>(
541 static_cast<other_float128_t
>( value ) );
545 template<
typename Result>
546 [[nodiscard]]
constexpr Result from_float128( float128_t value ) {
547 if constexpr( converts_from_v<Result, float128_t> ) {
548 return static_cast<Result
>( value );
550 return static_cast<Result
>(
551 static_cast<other_float128_t
>( value ) );
561 template<
typename Result>
562 [[nodiscard]]
constexpr Result power10_wide( Result result,
564#if defined( DAW_JSON_HAS_FLOAT128 )
565 if constexpr( power10_details::uses_float128_v<Result> ) {
566 auto value = power10_details::to_float128( result );
567 if( DAW_LIKELY( power10_details::scale_float128( value, p ) ) ) {
568 return power10_details::from_float128<Result>( value );
570 using limits = std::numeric_limits<Result>;
571 if constexpr( limits::has_infinity ) {
572 return limits::infinity( );
574 return limits::max( );
577 return power10_by_squaring( result, p );
580 return power10_by_squaring( result, p );
584 template<
typename Result,
typename Un
signed>
585 [[nodiscard]] DAW_ATTRIB_FLATINLINE
constexpr Result
586 power10_runtime( Result result, Unsigned p ) {
587 if constexpr( std::is_same_v<Result, double> or
588 std::is_same_v<Result, float> ) {
589 return power10_constexpr( result,
static_cast<std::int32_t
>( p ) );
590 }
else if constexpr( is_double_sized_long_double_v<Result> ) {
593 return static_cast<Result
>( power10_constexpr(
594 static_cast<double>( result ),
static_cast<std::int32_t
>( p ) ) );
598 return power10_constexpr( result,
static_cast<std::int32_t
>( p ) );
602 template<
typename Result,
typename ExecTag,
typename Un
signed>
603 [[nodiscard]] DAW_ATTRIB_FLATINLINE
constexpr Result
604 power10( ExecTag, Result result, Unsigned p ) {
605 if( use_constexpr_exec_mode<ExecTag>( ) ) {
606 return power10_constexpr<Result>( result, p );
608 return power10_runtime<Result>( result, p );