15#ifndef RAPIDJSON_STRTOD_
16#define RAPIDJSON_STRTOD_
19#include "biginteger.h"
25RAPIDJSON_NAMESPACE_BEGIN
28inline double FastPath(
double significand,
int exp) {
32 return significand * internal::Pow10(exp);
34 return significand / internal::Pow10(-exp);
37inline double StrtodNormalPrecision(
double d,
int p) {
40 d = FastPath(d, -308);
41 d = FastPath(d, p + 308);
49inline T Min3(T a, T b, T c) {
56inline int CheckWithinHalfULP(
double b,
const BigInteger& d,
int dExp) {
58 const uint64_t bInt = db.IntegerSignificand();
59 const int bExp = db.IntegerExponent();
60 const int hExp = bExp - 1;
62 int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
93 int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
94 dS_Exp2 -= common_Exp2;
95 bS_Exp2 -= common_Exp2;
96 hS_Exp2 -= common_Exp2;
99 dS.MultiplyPow5(
static_cast<unsigned>(dS_Exp5)) <<=
static_cast<unsigned>(dS_Exp2);
102 bS.MultiplyPow5(
static_cast<unsigned>(bS_Exp5)) <<=
static_cast<unsigned>(bS_Exp2);
105 hS.MultiplyPow5(
static_cast<unsigned>(hS_Exp5)) <<=
static_cast<unsigned>(hS_Exp2);
108 dS.Difference(bS, &delta);
110 return delta.Compare(hS);
113inline bool StrtodFast(
double d,
int p,
double* result) {
116 if (p > 22 && p < 22 + 16) {
118 d *= internal::Pow10(p - 22);
122 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) {
123 *result = FastPath(d, p);
131inline bool StrtodDiyFp(
const char* decimals,
size_t length, ptrdiff_t decimalPosition,
int exp,
double* result) {
132 uint64_t significand = 0;
134 for (; i < length; i++) {
138 significand = significand * 10u +
static_cast<unsigned>(decimals[i] -
'0');
141 if (i < length && decimals[i] >=
'5')
144 size_t remaining = length - i;
145 const unsigned kUlpShift = 3;
146 const unsigned kUlp = 1 << kUlpShift;
147 int64_t error = (remaining == 0) ? 0 : kUlp / 2;
149 DiyFp v(significand, 0);
153 const int dExp =
static_cast<int>(decimalPosition) -
static_cast<int>(i) + exp;
165 *result = std::numeric_limits<double>::infinity();
170 DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
171 if (actualExp != dExp) {
172 static const DiyFp kPow10[] = {
181 int adjustment = dExp - actualExp - 1;
183 v = v * kPow10[adjustment];
184 if (length +
static_cast<unsigned>(adjustment)> 19u)
190 error += kUlp + (error == 0 ? 0 : 1);
192 const int oldExp = v.e;
194 error <<= oldExp - v.e;
196 const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
197 unsigned precisionSize = 64 - effectiveSignificandSize;
198 if (precisionSize + kUlpShift >= 64) {
199 unsigned scaleExp = (precisionSize + kUlpShift) - 63;
202 error = (error >> scaleExp) + 1 +
static_cast<int>(kUlp);
203 precisionSize -= scaleExp;
206 DiyFp rounded(v.f >> precisionSize, v.e +
static_cast<int>(precisionSize));
207 const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
208 const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
209 if (precisionBits >= halfWay +
static_cast<unsigned>(error)) {
211 if (rounded.f & (DiyFp::kDpHiddenBit << 1)) {
217 *result = rounded.ToDouble();
219 return halfWay -
static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay +
static_cast<unsigned>(error);
222inline double StrtodBigInteger(
double approx,
const char* decimals,
size_t length, ptrdiff_t decimalPosition,
int exp) {
223 const BigInteger dInt(decimals, length);
224 const int dExp =
static_cast<int>(decimalPosition) -
static_cast<int>(length) + exp;
226 int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
231 if (a.Significand() & 1)
232 return a.NextPositiveDouble();
237 return a.NextPositiveDouble();
240inline double StrtodFullPrecision(
double d,
int p,
const char* decimals,
size_t length,
size_t decimalPosition,
int exp) {
245 if (StrtodFast(d, p, &result))
249 if (decimalPosition > length)
250 decimalPosition = length;
255 ptrdiff_t dPos =
static_cast<ptrdiff_t
>(decimalPosition);
258 while (*decimals ==
'0' && length > 1) {
265 while (decimals[length - 1] ==
'0' && length > 1) {
272 const int kMaxDecimalDigit = 780;
273 if (
static_cast<int>(length) > kMaxDecimalDigit) {
274 int delta = (
static_cast<int>(length) - kMaxDecimalDigit);
277 length =
static_cast<size_t>(kMaxDecimalDigit);
281 if (
int(length) + exp < -324)
284 if (StrtodDiyFp(decimals, length, dPos, exp, &result))
288 return StrtodBigInteger(result, decimals, length, dPos, exp);
292RAPIDJSON_NAMESPACE_END
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:402
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition rapidjson.h:289