15 #ifndef RAPIDJSON_STRTOD_
16 #define RAPIDJSON_STRTOD_
67 const int hExp = bExp - 1;
69 int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
104 int common_Exp2 =
Min3(dS_Exp2, bS_Exp2, hS_Exp2);
105 dS_Exp2 -= common_Exp2;
106 bS_Exp2 -= common_Exp2;
107 hS_Exp2 -= common_Exp2;
110 dS.
MultiplyPow5(
static_cast<unsigned>(dS_Exp5)) <<=
static_cast<unsigned>(dS_Exp2);
113 bS.
MultiplyPow5(
static_cast<unsigned>(bS_Exp5)) <<=
static_cast<unsigned>(bS_Exp2);
116 hS.
MultiplyPow5(
static_cast<unsigned>(hS_Exp5)) <<=
static_cast<unsigned>(hS_Exp2);
128 if(p > 22 && p < 22 + 16)
135 if(p >= -22 && p <= 22 && d <= 9007199254740991.0)
145 template <
typename Ch>
146 inline bool StrtodDiyFp(
const Ch* decimals,
int dLen,
int dExp,
double* result)
155 significand = significand * 10u +
static_cast<unsigned>(decimals[i] - Ch(
'0'));
158 if(i < dLen && decimals[i] >= Ch(
'5'))
161 int remaining = dLen - i;
162 const int kUlpShift = 3;
163 const int kUlp = 1 << kUlpShift;
164 int64_t error = (remaining == 0) ? 0 : kUlp / 2;
166 DiyFp v(significand, 0);
174 if(actualExp != dExp)
176 static const DiyFp kPow10[] = {
185 int adjustment = dExp - actualExp;
187 v = v * kPow10[adjustment - 1];
188 if(dLen + adjustment > 19)
194 error += kUlp + (error == 0 ? 0 : 1);
196 const int oldExp = v.
e;
198 error <<= oldExp - v.
e;
201 int precisionSize = 64 - effectiveSignificandSize;
202 if(precisionSize + kUlpShift >= 64)
204 int scaleExp = (precisionSize + kUlpShift) - 63;
207 error = (error >> scaleExp) + 1 + kUlp;
208 precisionSize -= scaleExp;
211 DiyFp rounded(v.
f >> precisionSize, v.
e + precisionSize);
212 const uint64_t precisionBits = (v.
f & ((
uint64_t(1) << precisionSize) - 1)) * kUlp;
214 if(precisionBits >= halfWay +
static_cast<unsigned>(error))
226 return halfWay -
static_cast<unsigned>(error) >= precisionBits ||
227 precisionBits >= halfWay +
static_cast<unsigned>(error);
230 template <
typename Ch>
234 const BigInteger dInt(decimals,
static_cast<unsigned>(dLen));
242 if(
a.Significand() & 1)
243 return a.NextPositiveDouble();
248 return a.NextPositiveDouble();
251 template <
typename Ch>
253 double d,
int p,
const Ch* decimals,
size_t length,
size_t decimalPosition,
int exp)
263 int dLen =
static_cast<int>(length);
267 int dExpAdjust =
static_cast<int>(length - decimalPosition);
270 int dExp =
exp - dExpAdjust;
276 while(dLen > 0 && *decimals ==
'0')
283 while(dLen > 0 && decimals[dLen - 1] ==
'0')
295 const int kMaxDecimalDigit = 767 + 1;
296 if(dLen > kMaxDecimalDigit)
298 dExp += dLen - kMaxDecimalDigit;
299 dLen = kMaxDecimalDigit;
304 if(dLen + dExp <= -324)
309 if(dLen + dExp > 309)
310 return std::numeric_limits<double>::infinity();
Definition: biginteger.h:33
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:188
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:215
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:249
uint64_t IntegerSignificand() const
Definition: ieee754.h:52
int IntegerExponent() const
Definition: ieee754.h:56
static int EffectiveSignificandSize(int order)
Definition: ieee754.h:62
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
__host__ T exp(T x)
Definition: math_v2.hpp:391
Definition: allocators.h:459
int CheckWithinHalfULP(double b, const BigInteger &d, int dExp)
Definition: strtod.h:62
bool StrtodFast(double d, int p, double *result)
Definition: strtod.h:124
double StrtodFullPrecision(double d, int p, const Ch *decimals, size_t length, size_t decimalPosition, int exp)
Definition: strtod.h:252
DiyFp GetCachedPower10(int exp, int *outExp)
Definition: diyfp.h:255
double StrtodNormalPrecision(double d, int p)
Definition: strtod.h:38
double FastPath(double significand, int exp)
Definition: strtod.h:28
double StrtodBigInteger(double approx, const Ch *decimals, int dLen, int dExp)
Definition: strtod.h:231
bool StrtodDiyFp(const Ch *decimals, int dLen, int dExp, double *result)
Definition: strtod.h:146
T Min3(T a, T b, T c)
Definition: strtod.h:52
double Pow10(int n)
Computes integer powers of 10 in double (10.0^n).
Definition: pow10.h:28
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1517
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:326
signed __int64 int64_t
Definition: stdint.h:135
unsigned __int64 uint64_t
Definition: stdint.h:136
uint64_t f
Definition: diyfp.h:175
static const uint64_t kDpHiddenBit
Definition: diyfp.h:173
DiyFp Normalize() const
Definition: diyfp.h:111
double ToDouble() const
Definition: diyfp.h:140
int e
Definition: diyfp.h:176