15 #ifndef RAPIDJSON_STRTOD_
16 #define RAPIDJSON_STRTOD_
18 #include "../rapidjson.h"
20 #include "biginteger.h"
24 RAPIDJSON_NAMESPACE_BEGIN
27 inline double FastPath(
double significand,
int exp) {
31 return significand * internal::Pow10(exp);
33 return significand / internal::Pow10(-exp);
36 inline double StrtodNormalPrecision(
double d,
int p) {
39 d = FastPath(d, -308);
40 d = FastPath(d, p + 308);
48 inline T Min3(T a, T b, T c) {
55 inline int CheckWithinHalfULP(
double b,
const BigInteger& d,
int dExp) {
57 const uint64_t bInt = db.IntegerSignificand();
58 const int bExp = db.IntegerExponent();
59 const int hExp = bExp - 1;
61 int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
92 int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
93 dS_Exp2 -= common_Exp2;
94 bS_Exp2 -= common_Exp2;
95 hS_Exp2 -= common_Exp2;
98 dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<=
static_cast<unsigned>(dS_Exp2);
101 bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<=
static_cast<unsigned>(bS_Exp2);
104 hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<=
static_cast<unsigned>(hS_Exp2);
107 dS.Difference(bS, &delta);
109 return delta.Compare(hS);
112 inline bool StrtodFast(
double d,
int p,
double* result) {
115 if (p > 22 && p < 22 + 16) {
117 d *= internal::Pow10(p - 22);
121 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) {
122 *result = FastPath(d, p);
130 inline bool StrtodDiyFp(
const char* decimals,
size_t length,
size_t decimalPosition,
int exp,
double* result) {
131 uint64_t significand = 0;
133 for (; i < length; i++) {
137 significand = significand * 10u +
static_cast<unsigned>(decimals[i] -
'0');
140 if (i < length && decimals[i] >=
'5')
143 size_t remaining = length - i;
144 const unsigned kUlpShift = 3;
145 const unsigned kUlp = 1 << kUlpShift;
146 int error = (remaining == 0) ? 0 : kUlp / 2;
148 DiyFp v(significand, 0);
152 const int dExp = (int)decimalPosition - (
int)i + exp;
155 DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
156 if (actualExp != dExp) {
157 static const DiyFp kPow10[] = {
166 int adjustment = dExp - actualExp - 1;
168 v = v * kPow10[adjustment];
169 if (length + static_cast<unsigned>(adjustment)> 19u)
175 error += kUlp + (error == 0 ? 0 : 1);
177 const int oldExp = v.e;
179 error <<= oldExp - v.e;
181 const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
182 unsigned precisionSize = 64 - effectiveSignificandSize;
183 if (precisionSize + kUlpShift >= 64) {
184 unsigned scaleExp = (precisionSize + kUlpShift) - 63;
187 error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
188 precisionSize -= scaleExp;
191 DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
192 const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
193 const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
194 if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
196 if (rounded.f & (DiyFp::kDpHiddenBit << 1)) {
202 *result = rounded.ToDouble();
204 return halfWay -
static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
207 inline double StrtodBigInteger(
double approx,
const char* decimals,
size_t length,
size_t decimalPosition,
int exp) {
208 const BigInteger dInt(decimals, length);
209 const int dExp = (int)decimalPosition - (
int)length + exp;
211 int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
216 if (a.Significand() & 1)
217 return a.NextPositiveDouble();
222 return a.NextPositiveDouble();
225 inline double StrtodFullPrecision(
double d,
int p,
const char* decimals,
size_t length,
size_t decimalPosition,
int exp) {
230 if (StrtodFast(d, p, &result))
234 while (*decimals ==
'0' && length > 1) {
241 while (decimals[length - 1] ==
'0' && length > 1) {
248 const int kMaxDecimalDigit = 780;
249 if ((
int)length > kMaxDecimalDigit) {
250 int delta = (int(length) - kMaxDecimalDigit);
252 decimalPosition -=
static_cast<unsigned>(delta);
253 length = kMaxDecimalDigit;
257 if (
int(length) + exp < -324)
260 if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
264 return StrtodBigInteger(result, decimals, length, decimalPosition, exp);
268 RAPIDJSON_NAMESPACE_END
270 #endif // RAPIDJSON_STRTOD_
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344