15 #ifndef RAPIDJSON_STRTOD_
16 #define RAPIDJSON_STRTOD_
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);
116 if (p > 22 && p < 22 + 16) {
122 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) {
131 template<
typename Ch>
132 inline bool StrtodDiyFp(
const Ch* decimals,
int dLen,
int dExp,
double* result) {
135 for (; i < dLen; i++) {
139 significand = significand * 10u +
static_cast<unsigned>(decimals[i] - Ch(
'0'));
142 if (i < dLen && decimals[i] >= Ch(
'5'))
145 int remaining = dLen - i;
146 const int kUlpShift = 3;
147 const int kUlp = 1 << kUlpShift;
148 int64_t error = (remaining == 0) ? 0 : kUlp / 2;
150 DiyFp v(significand, 0);
158 if (actualExp != dExp) {
159 static const DiyFp kPow10[] = {
168 int adjustment = dExp - actualExp;
170 v = v * kPow10[adjustment - 1];
171 if (dLen + adjustment > 19)
177 error += kUlp + (error == 0 ? 0 : 1);
179 const int oldExp = v.
e;
181 error <<= oldExp - v.
e;
184 int precisionSize = 64 - effectiveSignificandSize;
185 if (precisionSize + kUlpShift >= 64) {
186 int scaleExp = (precisionSize + kUlpShift) - 63;
189 error = (error >> scaleExp) + 1 + kUlp;
190 precisionSize -= scaleExp;
193 DiyFp rounded(v.
f >> precisionSize, v.
e + precisionSize);
194 const uint64_t precisionBits = (v.
f & ((
uint64_t(1) << precisionSize) - 1)) * kUlp;
196 if (precisionBits >= halfWay +
static_cast<unsigned>(error)) {
206 return halfWay -
static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay +
static_cast<unsigned>(error);
209 template<
typename Ch>
212 const BigInteger dInt(decimals,
static_cast<unsigned>(dLen));
219 if (
a.Significand() & 1)
220 return a.NextPositiveDouble();
225 return a.NextPositiveDouble();
228 template<
typename Ch>
238 int dLen =
static_cast<int>(length);
242 int dExpAdjust =
static_cast<int>(length - decimalPosition);
245 int dExp =
exp - dExpAdjust;
251 while (dLen > 0 && *decimals ==
'0') {
257 while (dLen > 0 && decimals[dLen - 1] ==
'0') {
267 const int kMaxDecimalDigit = 767 + 1;
268 if (dLen > kMaxDecimalDigit) {
269 dExp += dLen - kMaxDecimalDigit;
270 dLen = kMaxDecimalDigit;
275 if (dLen + dExp <= -324)
280 if (dLen + dExp > 309)
281 return std::numeric_limits<double>::infinity();
Definition: biginteger.h:32
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:167
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:191
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:213
uint64_t IntegerSignificand() const
Definition: ieee754.h:47
int IntegerExponent() const
Definition: ieee754.h:48
static int EffectiveSignificandSize(int order)
Definition: ieee754.h:51
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
#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:423
int CheckWithinHalfULP(double b, const BigInteger &d, int dExp)
Definition: strtod.h:56
bool StrtodFast(double d, int p, double *result)
Definition: strtod.h:113
double StrtodFullPrecision(double d, int p, const Ch *decimals, size_t length, size_t decimalPosition, int exp)
Definition: strtod.h:229
DiyFp GetCachedPower10(int exp, int *outExp)
Definition: diyfp.h:242
double StrtodNormalPrecision(double d, int p)
Definition: strtod.h:37
double FastPath(double significand, int exp)
Definition: strtod.h:28
double StrtodBigInteger(double approx, const Ch *decimals, int dLen, int dExp)
Definition: strtod.h:210
bool StrtodDiyFp(const Ch *decimals, int dLen, int dExp, double *result)
Definition: strtod.h:132
T Min3(T a, T b, T c)
Definition: strtod.h:49
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:1249
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:320
signed __int64 int64_t
Definition: stdint.h:135
unsigned __int64 uint64_t
Definition: stdint.h:136
uint64_t f
Definition: diyfp.h:161
static const uint64_t kDpHiddenBit
Definition: diyfp.h:159
DiyFp Normalize() const
Definition: diyfp.h:106
double ToDouble() const
Definition: diyfp.h:131
int e
Definition: diyfp.h:162