DAW JSON Link
Loading...
Searching...
No Matches
daw_json_parse_real_power10.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
12
15
16#include <daw/daw_arith_traits.h>
17#include <daw/daw_likely.h>
18
19#include <cmath>
20#include <cstddef>
21#include <limits>
22#include <type_traits>
23
24namespace daw::json {
25 inline namespace DAW_JSON_VER {
26 namespace json_details {
27 inline constexpr double dpow10_tbl[] = {
28 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
29 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
30 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
31 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
32 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
33 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
34 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
35 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
36 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
37 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
38 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
39 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
40 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
41 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
42 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
43 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
44 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169,
45 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179,
46 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189,
47 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199,
48 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209,
49 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219,
50 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229,
51 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239,
52 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249,
53 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259,
54 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269,
55 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279,
56 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289,
57 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299,
58 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308 };
59
60 // 10^0 .. 10^27 are exactly representable in 80-bit long double
61 // (5^27 < 2^64), and 10^0 .. 10^48 in binary128 (5^48 < 2^113).
62 // Entries above 22 must not be sourced from the double table.
63 inline constexpr long double ldpow10_tbl[] = {
64 1e0L, 1e1L, 1e2L, 1e3L, 1e4L, 1e5L, 1e6L,
65 1e7L, 1e8L, 1e9L, 1e10L, 1e11L, 1e12L, 1e13L,
66 1e14L, 1e15L, 1e16L, 1e17L, 1e18L, 1e19L, 1e20L,
67 1e21L, 1e22L, 1e23L, 1e24L, 1e25L, 1e26L, 1e27L,
68 1e28L, 1e29L, 1e30L, 1e31L, 1e32L, 1e33L, 1e34L,
69 1e35L, 1e36L, 1e37L, 1e38L, 1e39L, 1e40L, 1e41L,
70 1e42L, 1e43L, 1e44L, 1e45L, 1e46L, 1e47L, 1e48L,
71 };
72 inline constexpr int max_ld_table_exp = 48;
73
74 inline constexpr int max_dbl_exp =
75 std::numeric_limits<double>::max_exponent10;
76
77 template<typename T>
78 [[nodiscard]] DAW_ATTRIB_INLINE constexpr T const &
79 Min( T const &l, T const &r ) noexcept {
80 if( l <= r ) {
81 return l;
82 }
83 return r;
84 }
85
86 template<typename Result>
87 inline constexpr int max_exp =
88 std::is_same_v<Result, float>
89 ? Min( max_dbl_exp, std::numeric_limits<float>::max_exponent10 )
90 : max_dbl_exp;
91
92 template<typename Result, typename Unsigned>
93 [[nodiscard]] DAW_ATTRIB_FLATINLINE constexpr Result
94 power10_constexpr( Result result, Unsigned p ) {
95 // For extended long double use the exact LD table where available;
96 // the double table loses precision for p above 22.
97 if constexpr( is_double_sized_long_double_v<Result> == false and
98 std::is_same_v<Result, long double> ) {
99 DAW_CPP23_STATIC_LOCAL constexpr int max_exact_exp =
100 std::numeric_limits<long double>::digits == 113
101 ? max_ld_table_exp
102 : 27;
103 if( p >= -max_exact_exp and p <= max_exact_exp ) {
104 if( p < 0 ) {
105 return result /
106 ldpow10_tbl[static_cast<std::size_t>( -p )];
107 }
108 return result * ldpow10_tbl[static_cast<std::size_t>( p )];
109 }
110 }
111
112 DAW_CPP23_STATIC_LOCAL constexpr auto max_v =
113 static_cast<Result>( dpow10_tbl[max_exp<Result>] );
114
115 if( DAW_UNLIKELY( p > max_exp<Result> ) ) {
116 if constexpr( std::is_floating_point_v<Result> ) {
117 if( DAW_UNLIKELY( p >
118 daw::numeric_limits<Result>::max_exponent ) ) {
119 return std::numeric_limits<Result>::infinity( );
120 }
121 }
122 Result exp2 = max_v;
123 p -= max_exp<Result>;
124
125 for( ; p > max_exp<Result>; p -= max_exp<Result> ) {
126 exp2 *= max_v;
127 }
128 return static_cast<Result>( result ) *
129 ( exp2 * static_cast<Result>(
130 dpow10_tbl[static_cast<std::size_t>( p )] ) );
131 }
132 if( DAW_UNLIKELY( p < -max_exp<Result> ) ) {
133 if constexpr( std::is_floating_point_v<Result> ) {
134 if( DAW_UNLIKELY( p <
135 daw::numeric_limits<Result>::min_exponent ) ) {
136 return Result{ 0 };
137 }
138 }
139 Result exp2 = max_v;
140 p += max_exp<Result>;
141
142 for( ; p < -max_exp<Result>; p += max_exp<Result> ) {
143 result /= max_v;
144 }
145 return ( static_cast<Result>( result ) /
146 static_cast<Result>(
147 dpow10_tbl[static_cast<std::size_t>( -p )] ) ) /
148 exp2;
149 }
150 if( p < 0 ) {
151 return static_cast<Result>( result ) /
152 static_cast<Result>(
153 dpow10_tbl[static_cast<std::size_t>( -p )] );
154 }
155 return static_cast<Result>( result ) *
156 static_cast<Result>( dpow10_tbl[static_cast<std::size_t>( p )] );
157 }
158
159 template<typename Result, typename Unsigned>
160 [[nodiscard]] DAW_ATTRIB_FLATINLINE constexpr Result
161 power10_runtime( Result result, Unsigned p ) {
162 if constexpr( std::is_same_v<Result, double> or
163 std::is_same_v<Result, float> ) {
164 return power10_constexpr( result, static_cast<std::int32_t>( p ) );
165 } else if constexpr( is_double_sized_long_double_v<Result> ) {
166 // long double here has no more precision than double, so the
167 // double-precision table used by power10_constexpr loses nothing.
168 return static_cast<Result>( power10_constexpr(
169 static_cast<double>( result ), static_cast<std::int32_t>( p ) ) );
170 } else {
171 // Extended long double: power10_constexpr uses the exact LD table
172 // for all exponents the precise parser routes here.
173 return power10_constexpr( result, static_cast<std::int32_t>( p ) );
174 }
175 }
176
177 template<typename Result, typename ExecTag, typename Unsigned>
178 [[nodiscard]] DAW_ATTRIB_FLATINLINE constexpr Result
179 power10( ExecTag, Result result, Unsigned p ) {
180 if( use_constexpr_exec_mode<ExecTag>( ) ) {
181 return power10_constexpr<Result>( result, p );
182 }
183 return power10_runtime<Result>( result, p );
184 }
185 } // namespace json_details
186 } // namespace DAW_JSON_VER
187} // namespace daw::json
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20