15 #ifndef RAPIDJSON_STRTOD_
16 #define RAPIDJSON_STRTOD_
18 #include "../rapidjson.h"
48 inline T
Min3(T a, T b, T c) {
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;
115 if (p > 22 && p < 22 + 16) {
121 if (p >= -22 && p <= 22 && d <= 9007199254740991.0) {
130 inline bool StrtodDiyFp(
const char* decimals,
size_t length,
size_t decimalPosition,
int exp,
double* result) {
137 significand = significand * 10 + (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;
156 if (actualExp != dExp) {
157 static const DiyFp kPow10[] = {
166 int adjustment = dExp - actualExp - 1;
168 v = v * kPow10[adjustment];
169 if (length + adjustment > 19)
175 error += kUlp + (error == 0 ? 0 : 1);
177 const int oldExp = v.
e;
179 error <<= oldExp - v.
e;
182 unsigned precisionSize = 64 - effectiveSignificandSize;
183 if (precisionSize + kUlpShift >= 64) {
184 unsigned scaleExp = (precisionSize + kUlpShift) - 63;
187 error = (error >> scaleExp) + 1 + kUlp;
188 precisionSize -= scaleExp;
191 DiyFp rounded(v.
f >> precisionSize, v.
e + precisionSize);
192 const uint64_t precisionBits = (v.
f & ((
uint64_t(1) << precisionSize) - 1)) * kUlp;
194 if (precisionBits >= halfWay + error) {
204 return halfWay - error >= precisionBits || precisionBits >= halfWay + error;
209 const int dExp = (int)decimalPosition - (
int)length +
exp;
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 -= delta;
253 length = kMaxDecimalDigit;
257 if (
int(length) + exp < -324)
260 if (
StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
270 #endif // RAPIDJSON_STRTOD_
Quat exp(const Quat &q)
Definition: Quat.h:729
double StrtodFullPrecision(double d, int p, const char *decimals, size_t length, size_t decimalPosition, int exp)
Definition: strtod.h:225
static unsigned EffectiveSignificandSize(int order)
Definition: ieee754.h:50
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
uint64_t Significand() const
Definition: ieee754.h:38
DiyFp GetCachedPower10(int exp, int *outExp)
Definition: diyfp.h:234
int Compare(const BigInteger &rhs) const
Definition: biginteger.h:198
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
DiyFp Normalize() const
Definition: diyfp.h:95
double Value() const
Definition: ieee754.h:29
double NextPositiveDouble() const
Definition: ieee754.h:32
bool StrtodFast(double d, int p, double *result)
Definition: strtod.h:112
double StrtodNormalPrecision(double d, int p)
Definition: strtod.h:36
int CheckWithinHalfULP(double b, const BigInteger &d, int dExp)
Definition: strtod.h:55
double StrtodBigInteger(double approx, const char *decimals, size_t length, size_t decimalPosition, int exp)
Definition: strtod.h:207
double ToDouble() const
Definition: diyfp.h:133
unsigned __int64 uint64_t
Definition: stdint.h:90
uint64_t f
Definition: diyfp.h:154
uint64_t IntegerSignificand() const
Definition: ieee754.h:46
float length(float v)
Definition: vectorMath.h:208
Definition: document.h:390
double Pow10(int n)
Computes integer powers of 10 in double (10.0^n).
Definition: pow10.h:28
int IntegerExponent() const
Definition: ieee754.h:47
T Min3(T a, T b, T c)
Definition: strtod.h:48
bool StrtodDiyFp(const char *decimals, size_t length, size_t decimalPosition, int exp, double *result)
Definition: strtod.h:130
int e
Definition: diyfp.h:155
Definition: biginteger.h:27
BigInteger & MultiplyPow5(unsigned exp)
Definition: biginteger.h:152
static const uint64_t kDpHiddenBit
Definition: diyfp.h:152
double FastPath(double significand, int exp)
Definition: strtod.h:27
bool Difference(const BigInteger &rhs, BigInteger *out) const
Definition: biginteger.h:176