Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
strtod.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2// Tencent is pleased to support the open source community by making RapidJSON available.
3//
4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
5//
6// Licensed under the MIT License (the "License"); you may not use this file except
7// in compliance with the License. You may obtain a copy of the License at
8//
9// http://opensource.org/licenses/MIT
10//
11// Unless required by applicable law or agreed to in writing, software distributed
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the
14// specific language governing permissions and limitations under the License.
15
16#ifndef RAPIDJSON_STRTOD_
17#define RAPIDJSON_STRTOD_
18
19#include "ieee754.h"
20#include "biginteger.h"
21#include "diyfp.h"
22#include "pow10.h"
23#include <climits>
24#include <limits>
25
27namespace internal {
28
29inline double FastPath(double significand, int exp) {
30 if (exp < -308)
31 return 0.0;
32 else if (exp >= 0)
33 return significand * internal::Pow10(exp);
34 else
35 return significand / internal::Pow10(-exp);
36}
37
38inline double StrtodNormalPrecision(double d, int p) {
39 if (p < -308) {
40 // Prevent expSum < -308, making Pow10(p) = 0
41 d = FastPath(d, -308);
42 d = FastPath(d, p + 308);
43 }
44 else
45 d = FastPath(d, p);
46 return d;
47}
48
49template <typename T>
50inline T Min3(T a, T b, T c) {
51 T m = a;
52 if (m > b) m = b;
53 if (m > c) m = c;
54 return m;
55}
56
57inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
58 const Double db(b);
59 const uint64_t bInt = db.IntegerSignificand();
60 const int bExp = db.IntegerExponent();
61 const int hExp = bExp - 1;
62
63 int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
64
65 // Adjust for decimal exponent
66 if (dExp >= 0) {
67 dS_Exp2 += dExp;
68 dS_Exp5 += dExp;
69 }
70 else {
71 bS_Exp2 -= dExp;
72 bS_Exp5 -= dExp;
73 hS_Exp2 -= dExp;
74 hS_Exp5 -= dExp;
75 }
76
77 // Adjust for binary exponent
78 if (bExp >= 0)
79 bS_Exp2 += bExp;
80 else {
81 dS_Exp2 -= bExp;
82 hS_Exp2 -= bExp;
83 }
84
85 // Adjust for half ulp exponent
86 if (hExp >= 0)
87 hS_Exp2 += hExp;
88 else {
89 dS_Exp2 -= hExp;
90 bS_Exp2 -= hExp;
91 }
92
93 // Remove common power of two factor from all three scaled values
94 int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
95 dS_Exp2 -= common_Exp2;
96 bS_Exp2 -= common_Exp2;
97 hS_Exp2 -= common_Exp2;
98
99 BigInteger dS = d;
100 dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<= static_cast<unsigned>(dS_Exp2);
101
102 BigInteger bS(bInt);
103 bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<= static_cast<unsigned>(bS_Exp2);
104
105 BigInteger hS(1);
106 hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<= static_cast<unsigned>(hS_Exp2);
107
108 BigInteger delta(0);
109 dS.Difference(bS, &delta);
110
111 return delta.Compare(hS);
112}
113
114inline bool StrtodFast(double d, int p, double* result) {
115 // Use fast path for string-to-double conversion if possible
116 // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
117 if (p > 22 && p < 22 + 16) {
118 // Fast Path Cases In Disguise
119 d *= internal::Pow10(p - 22);
120 p = 22;
121 }
122
123 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1
124 *result = FastPath(d, p);
125 return true;
126 }
127 else
128 return false;
129}
130
131// Compute an approximation and see if it is within 1/2 ULP
132template<typename Ch>
133inline bool StrtodDiyFp(const Ch* decimals, int dLen, int dExp, double* result) {
134 uint64_t significand = 0;
135 int i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
136 for (; i < dLen; i++) {
137 if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
138 (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] >= Ch('5')))
139 break;
140 significand = significand * 10u + static_cast<unsigned>(decimals[i] - Ch('0'));
141 }
142
143 if (i < dLen && decimals[i] >= Ch('5')) // Rounding
144 significand++;
145
146 int remaining = dLen - i;
147 const int kUlpShift = 3;
148 const int kUlp = 1 << kUlpShift;
149 int64_t error = (remaining == 0) ? 0 : kUlp / 2;
150
151 DiyFp v(significand, 0);
152 v = v.Normalize();
153 error <<= -v.e;
154
155 dExp += remaining;
156
157 int actualExp;
158 DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
159 if (actualExp != dExp) {
160 static const DiyFp kPow10[] = {
161 DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 0x00000000), -60), // 10^1
162 DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 0x00000000), -57), // 10^2
163 DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 0x00000000), -54), // 10^3
164 DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), -50), // 10^4
165 DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 0x00000000), -47), // 10^5
166 DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 0x00000000), -44), // 10^6
167 DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 0x00000000), -40) // 10^7
168 };
169 int adjustment = dExp - actualExp;
170 RAPIDJSON_ASSERT(adjustment >= 1 && adjustment < 8);
171 v = v * kPow10[adjustment - 1];
172 if (dLen + adjustment > 19) // has more digits than decimal digits in 64-bit
173 error += kUlp / 2;
174 }
175
176 v = v * cachedPower;
177
178 error += kUlp + (error == 0 ? 0 : 1);
179
180 const int oldExp = v.e;
181 v = v.Normalize();
182 error <<= oldExp - v.e;
183
184 const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
185 int precisionSize = 64 - effectiveSignificandSize;
186 if (precisionSize + kUlpShift >= 64) {
187 int scaleExp = (precisionSize + kUlpShift) - 63;
188 v.f >>= scaleExp;
189 v.e += scaleExp;
190 error = (error >> scaleExp) + 1 + kUlp;
191 precisionSize -= scaleExp;
192 }
193
194 DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
195 const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
196 const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
197 if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
198 rounded.f++;
199 if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
200 rounded.f >>= 1;
201 rounded.e++;
202 }
203 }
204
205 *result = rounded.ToDouble();
206
207 return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
208}
209
210template<typename Ch>
211inline double StrtodBigInteger(double approx, const Ch* decimals, int dLen, int dExp) {
212 RAPIDJSON_ASSERT(dLen >= 0);
213 const BigInteger dInt(decimals, static_cast<unsigned>(dLen));
214 Double a(approx);
215 int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
216 if (cmp < 0)
217 return a.Value(); // within half ULP
218 else if (cmp == 0) {
219 // Round towards even
220 if (a.Significand() & 1)
221 return a.NextPositiveDouble();
222 else
223 return a.Value();
224 }
225 else // adjustment
226 return a.NextPositiveDouble();
227}
228
229template<typename Ch>
230inline double StrtodFullPrecision(double d, int p, const Ch* decimals, size_t length, size_t decimalPosition, int exp) {
231 RAPIDJSON_ASSERT(d >= 0.0);
232 RAPIDJSON_ASSERT(length >= 1);
233
234 double result = 0.0;
235 if (StrtodFast(d, p, &result))
236 return result;
237
238 RAPIDJSON_ASSERT(length <= INT_MAX);
239 int dLen = static_cast<int>(length);
240
241 RAPIDJSON_ASSERT(length >= decimalPosition);
242 RAPIDJSON_ASSERT(length - decimalPosition <= INT_MAX);
243 int dExpAdjust = static_cast<int>(length - decimalPosition);
244
245 RAPIDJSON_ASSERT(exp >= INT_MIN + dExpAdjust);
246 int dExp = exp - dExpAdjust;
247
248 // Make sure length+dExp does not overflow
249 RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen);
250
251 // Trim leading zeros
252 while (dLen > 0 && *decimals == '0') {
253 dLen--;
254 decimals++;
255 }
256
257 // Trim trailing zeros
258 while (dLen > 0 && decimals[dLen - 1] == '0') {
259 dLen--;
260 dExp++;
261 }
262
263 if (dLen == 0) { // Buffer only contains zeros.
264 return 0.0;
265 }
266
267 // Trim right-most digits
268 const int kMaxDecimalDigit = 767 + 1;
269 if (dLen > kMaxDecimalDigit) {
270 dExp += dLen - kMaxDecimalDigit;
271 dLen = kMaxDecimalDigit;
272 }
273
274 // If too small, underflow to zero.
275 // Any x <= 10^-324 is interpreted as zero.
276 if (dLen + dExp <= -324)
277 return 0.0;
278
279 // If too large, overflow to infinity.
280 // Any x >= 10^309 is interpreted as +infinity.
281 if (dLen + dExp > 309)
282 return std::numeric_limits<double>::infinity();
283
284 if (StrtodDiyFp(decimals, dLen, dExp, &result))
285 return result;
286
287 // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
288 return StrtodBigInteger(result, decimals, dLen, dExp);
289}
290
291} // namespace internal
293
294#endif // RAPIDJSON_STRTOD_
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:438
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:122
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:125
__host__ __device__ double exp(double v)
Exponentielle de v.
Definition Math.h:116
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition rapidjson.h:321