"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "include/rapidjson/internal/strtod.h" between
rapidjson-1.0.2.tar.gz and rapidjson-1.1.0.tar.gz

About: RapidJSON is a fast JSON parser/generator for C++ with both SAX/DOM style API.

strtod.h  (rapidjson-1.0.2):strtod.h  (rapidjson-1.1.0)
skipping to change at line 18 skipping to change at line 18
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distribut ed // Unless required by applicable law or agreed to in writing, software distribut ed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_STRTOD_ #ifndef RAPIDJSON_STRTOD_
#define RAPIDJSON_STRTOD_ #define RAPIDJSON_STRTOD_
#include "../rapidjson.h"
#include "ieee754.h" #include "ieee754.h"
#include "biginteger.h" #include "biginteger.h"
#include "diyfp.h" #include "diyfp.h"
#include "pow10.h" #include "pow10.h"
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
inline double FastPath(double significand, int exp) { inline double FastPath(double significand, int exp) {
if (exp < -308) if (exp < -308)
skipping to change at line 98 skipping to change at line 97
bS_Exp2 -= hExp; bS_Exp2 -= hExp;
} }
// Remove common power of two factor from all three scaled values // Remove common power of two factor from all three scaled values
int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
dS_Exp2 -= common_Exp2; dS_Exp2 -= common_Exp2;
bS_Exp2 -= common_Exp2; bS_Exp2 -= common_Exp2;
hS_Exp2 -= common_Exp2; hS_Exp2 -= common_Exp2;
BigInteger dS = d; BigInteger dS = d;
dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2; dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<= static_cast<unsigned>(dS _Exp2);
BigInteger bS(bInt); BigInteger bS(bInt);
bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2; bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<= static_cast<unsigned>(bS _Exp2);
BigInteger hS(1); BigInteger hS(1);
hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2; hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<= static_cast<unsigned>(hS _Exp2);
BigInteger delta(0); BigInteger delta(0);
dS.Difference(bS, &delta); dS.Difference(bS, &delta);
return delta.Compare(hS); return delta.Compare(hS);
} }
inline bool StrtodFast(double d, int p, double* result) { inline bool StrtodFast(double d, int p, double* result) {
// Use fast path for string-to-double conversion if possible // Use fast path for string-to-double conversion if possible
// see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-co nversion/ // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-co nversion/
skipping to change at line 137 skipping to change at line 136
} }
// Compute an approximation and see if it is within 1/2 ULP // Compute an approximation and see if it is within 1/2 ULP
inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit ion, int exp, double* result) { inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit ion, int exp, double* result) {
uint64_t significand = 0; uint64_t significand = 0;
size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x 1999999999999999 size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x 1999999999999999
for (; i < length; i++) { for (; i < length; i++) {
if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
(significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decim als[i] > '5')) (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decim als[i] > '5'))
break; break;
significand = significand * 10 + (decimals[i] - '0'); significand = significand * 10u + static_cast<unsigned>(decimals[i] - '0 ');
} }
if (i < length && decimals[i] >= '5') // Rounding if (i < length && decimals[i] >= '5') // Rounding
significand++; significand++;
size_t remaining = length - i; size_t remaining = length - i;
const unsigned kUlpShift = 3; const unsigned kUlpShift = 3;
const unsigned kUlp = 1 << kUlpShift; const unsigned kUlp = 1 << kUlpShift;
int error = (remaining == 0) ? 0 : kUlp / 2; int64_t error = (remaining == 0) ? 0 : kUlp / 2;
DiyFp v(significand, 0); DiyFp v(significand, 0);
v = v.Normalize(); v = v.Normalize();
error <<= -v.e; error <<= -v.e;
const int dExp = (int)decimalPosition - (int)i + exp; const int dExp = static_cast<int>(decimalPosition) - static_cast<int>(i) + e xp;
int actualExp; int actualExp;
DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
if (actualExp != dExp) { if (actualExp != dExp) {
static const DiyFp kPow10[] = { static const DiyFp kPow10[] = {
DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1
DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2
DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3
DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4
DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5
DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6
DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7
}; };
int adjustment = dExp - actualExp - 1; int adjustment = dExp - actualExp - 1;
RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7);
v = v * kPow10[adjustment]; v = v * kPow10[adjustment];
if (length + adjustment > 19) // has more digits than decimal digits in 64-bit if (length + static_cast<unsigned>(adjustment)> 19u) // has more digits than decimal digits in 64-bit
error += kUlp / 2; error += kUlp / 2;
} }
v = v * cachedPower; v = v * cachedPower;
error += kUlp + (error == 0 ? 0 : 1); error += kUlp + (error == 0 ? 0 : 1);
const int oldExp = v.e; const int oldExp = v.e;
v = v.Normalize(); v = v.Normalize();
error <<= oldExp - v.e; error <<= oldExp - v.e;
const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(6 4 + v.e); const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(6 4 + v.e);
unsigned precisionSize = 64 - effectiveSignificandSize; unsigned precisionSize = 64 - effectiveSignificandSize;
if (precisionSize + kUlpShift >= 64) { if (precisionSize + kUlpShift >= 64) {
unsigned scaleExp = (precisionSize + kUlpShift) - 63; unsigned scaleExp = (precisionSize + kUlpShift) - 63;
v.f >>= scaleExp; v.f >>= scaleExp;
v.e += scaleExp; v.e += scaleExp;
error = (error >> scaleExp) + 1 + kUlp; error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
precisionSize -= scaleExp; precisionSize -= scaleExp;
} }
DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
if (precisionBits >= halfWay + error) { if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
rounded.f++; rounded.f++;
if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mant issa (issue #340) if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mant issa (issue #340)
rounded.f >>= 1; rounded.f >>= 1;
rounded.e++; rounded.e++;
} }
} }
*result = rounded.ToDouble(); *result = rounded.ToDouble();
return halfWay - error >= precisionBits || precisionBits >= halfWay + error; return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionB its >= halfWay + static_cast<unsigned>(error);
} }
inline double StrtodBigInteger(double approx, const char* decimals, size_t lengt h, size_t decimalPosition, int exp) { inline double StrtodBigInteger(double approx, const char* decimals, size_t lengt h, size_t decimalPosition, int exp) {
const BigInteger dInt(decimals, length); const BigInteger dInt(decimals, length);
const int dExp = (int)decimalPosition - (int)length + exp; const int dExp = static_cast<int>(decimalPosition) - static_cast<int>(length ) + exp;
Double a(approx); Double a(approx);
int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
if (cmp < 0) if (cmp < 0)
return a.Value(); // within half ULP return a.Value(); // within half ULP
else if (cmp == 0) { else if (cmp == 0) {
// Round towards even // Round towards even
if (a.Significand() & 1) if (a.Significand() & 1)
return a.NextPositiveDouble(); return a.NextPositiveDouble();
else else
return a.Value(); return a.Value();
skipping to change at line 249 skipping to change at line 248
// Trim trailing zeros // Trim trailing zeros
while (decimals[length - 1] == '0' && length > 1) { while (decimals[length - 1] == '0' && length > 1) {
length--; length--;
decimalPosition--; decimalPosition--;
exp++; exp++;
} }
// Trim right-most digits // Trim right-most digits
const int kMaxDecimalDigit = 780; const int kMaxDecimalDigit = 780;
if ((int)length > kMaxDecimalDigit) { if (static_cast<int>(length) > kMaxDecimalDigit) {
int delta = (int(length) - kMaxDecimalDigit); int delta = (static_cast<int>(length) - kMaxDecimalDigit);
exp += delta; exp += delta;
decimalPosition -= delta; decimalPosition -= static_cast<unsigned>(delta);
length = kMaxDecimalDigit; length = kMaxDecimalDigit;
} }
// If too small, underflow to zero // If too small, underflow to zero
if (int(length) + exp < -324) if (int(length) + exp < -324)
return 0.0; return 0.0;
if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
return result; return result;
 End of changes. 16 change blocks. 
17 lines changed or deleted 16 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)