15 #ifndef RAPIDJSON_STRTOD_ 16 #define RAPIDJSON_STRTOD_ 19 #include "biginteger.h" 23 RAPIDJSON_NAMESPACE_BEGIN
26 inline double FastPath(
double significand,
int exp) {
30 return significand * internal::Pow10(exp);
32 return significand / internal::Pow10(-exp);
35 inline double StrtodNormalPrecision(
double d,
int p) {
38 d = FastPath(d, -308);
39 d = FastPath(d, p + 308);
47 inline T Min3(T a, T b, T c) {
54 inline int CheckWithinHalfULP(
double b,
const BigInteger& d,
int dExp) {
56 const uint64_t bInt = db.IntegerSignificand();
57 const int bExp = db.IntegerExponent();
58 const int hExp = bExp - 1;
60 int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
91 int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
92 dS_Exp2 -= common_Exp2;
93 bS_Exp2 -= common_Exp2;
94 hS_Exp2 -= common_Exp2;
97 dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<=
static_cast<unsigned>(dS_Exp2);
100 bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<=
static_cast<unsigned>(bS_Exp2);
103 hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<=
static_cast<unsigned>(hS_Exp2);
106 dS.Difference(bS, &delta);
108 return delta.Compare(hS);
111 inline bool StrtodFast(
double d,
int p,
double* result) {
114 if (p > 22 && p < 22 + 16) {
116 d *= internal::Pow10(p - 22);
120 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) {
121 *result = FastPath(d, p);
129 inline bool StrtodDiyFp(
const char* decimals,
size_t length,
size_t decimalPosition,
int exp,
double* result) {
130 uint64_t significand = 0;
132 for (; i < length; i++) {
136 significand = significand * 10u +
static_cast<unsigned>(decimals[i] -
'0');
139 if (i < length && decimals[i] >=
'5')
142 size_t remaining = length - i;
143 const unsigned kUlpShift = 3;
144 const unsigned kUlp = 1 << kUlpShift;
145 int64_t error = (remaining == 0) ? 0 : kUlp / 2;
147 DiyFp v(significand, 0);
151 const int dExp =
static_cast<int>(decimalPosition) - static_cast<int>(i) + exp;
154 DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
155 if (actualExp != dExp) {
156 static const DiyFp kPow10[] = {
165 int adjustment = dExp - actualExp - 1;
167 v = v * kPow10[adjustment];
168 if (length + static_cast<unsigned>(adjustment)> 19u)
174 error += kUlp + (error == 0 ? 0 : 1);
176 const int oldExp = v.e;
178 error <<= oldExp - v.e;
180 const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
181 unsigned precisionSize = 64 - effectiveSignificandSize;
182 if (precisionSize + kUlpShift >= 64) {
183 unsigned scaleExp = (precisionSize + kUlpShift) - 63;
186 error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
187 precisionSize -= scaleExp;
190 DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
191 const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
192 const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
193 if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
195 if (rounded.f & (DiyFp::kDpHiddenBit << 1)) {
201 *result = rounded.ToDouble();
203 return halfWay -
static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
206 inline double StrtodBigInteger(
double approx,
const char* decimals,
size_t length,
size_t decimalPosition,
int exp) {
207 const BigInteger dInt(decimals, length);
208 const int dExp =
static_cast<int>(decimalPosition) - static_cast<int>(length) + exp;
210 int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
215 if (a.Significand() & 1)
216 return a.NextPositiveDouble();
221 return a.NextPositiveDouble();
224 inline double StrtodFullPrecision(
double d,
int p,
const char* decimals,
size_t length,
size_t decimalPosition,
int exp) {
229 if (StrtodFast(d, p, &result))
233 while (*decimals ==
'0' && length > 1) {
240 while (decimals[length - 1] ==
'0' && length > 1) {
247 const int kMaxDecimalDigit = 780;
248 if (static_cast<int>(length) > kMaxDecimalDigit) {
249 int delta = (
static_cast<int>(length) - kMaxDecimalDigit);
251 decimalPosition -=
static_cast<unsigned>(delta);
252 length = kMaxDecimalDigit;
256 if (
int(length) + exp < -324)
259 if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
263 return StrtodBigInteger(result, decimals, length, decimalPosition, exp);
267 RAPIDJSON_NAMESPACE_END
269 #endif // RAPIDJSON_STRTOD_ #define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:289
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402