strtod.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef RAPIDJSON_STRTOD_
  15. #define RAPIDJSON_STRTOD_
  16. #include "ieee754.h"
  17. #include "biginteger.h"
  18. #include "diyfp.h"
  19. #include "pow10.h"
  20. #include <climits>
  21. #include <limits>
  22. RAPIDJSON_NAMESPACE_BEGIN
  23. namespace internal {
  24. inline double FastPath(double significand, int exp) {
  25. if (exp < -308)
  26. return 0.0;
  27. else if (exp >= 0)
  28. return significand * internal::Pow10(exp);
  29. else
  30. return significand / internal::Pow10(-exp);
  31. }
  32. inline double StrtodNormalPrecision(double d, int p) {
  33. if (p < -308) {
  34. // Prevent expSum < -308, making Pow10(p) = 0
  35. d = FastPath(d, -308);
  36. d = FastPath(d, p + 308);
  37. }
  38. else
  39. d = FastPath(d, p);
  40. return d;
  41. }
  42. template <typename T>
  43. inline T Min3(T a, T b, T c) {
  44. T m = a;
  45. if (m > b) m = b;
  46. if (m > c) m = c;
  47. return m;
  48. }
  49. inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
  50. const Double db(b);
  51. const uint64_t bInt = db.IntegerSignificand();
  52. const int bExp = db.IntegerExponent();
  53. const int hExp = bExp - 1;
  54. int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
  55. // Adjust for decimal exponent
  56. if (dExp >= 0) {
  57. dS_Exp2 += dExp;
  58. dS_Exp5 += dExp;
  59. }
  60. else {
  61. bS_Exp2 -= dExp;
  62. bS_Exp5 -= dExp;
  63. hS_Exp2 -= dExp;
  64. hS_Exp5 -= dExp;
  65. }
  66. // Adjust for binary exponent
  67. if (bExp >= 0)
  68. bS_Exp2 += bExp;
  69. else {
  70. dS_Exp2 -= bExp;
  71. hS_Exp2 -= bExp;
  72. }
  73. // Adjust for half ulp exponent
  74. if (hExp >= 0)
  75. hS_Exp2 += hExp;
  76. else {
  77. dS_Exp2 -= hExp;
  78. bS_Exp2 -= hExp;
  79. }
  80. // Remove common power of two factor from all three scaled values
  81. int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
  82. dS_Exp2 -= common_Exp2;
  83. bS_Exp2 -= common_Exp2;
  84. hS_Exp2 -= common_Exp2;
  85. BigInteger dS = d;
  86. dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<= static_cast<unsigned>(dS_Exp2);
  87. BigInteger bS(bInt);
  88. bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<= static_cast<unsigned>(bS_Exp2);
  89. BigInteger hS(1);
  90. hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<= static_cast<unsigned>(hS_Exp2);
  91. BigInteger delta(0);
  92. dS.Difference(bS, &delta);
  93. return delta.Compare(hS);
  94. }
  95. inline bool StrtodFast(double d, int p, double* result) {
  96. // Use fast path for string-to-double conversion if possible
  97. // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  98. if (p > 22 && p < 22 + 16) {
  99. // Fast Path Cases In Disguise
  100. d *= internal::Pow10(p - 22);
  101. p = 22;
  102. }
  103. if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1
  104. *result = FastPath(d, p);
  105. return true;
  106. }
  107. else
  108. return false;
  109. }
  110. // Compute an approximation and see if it is within 1/2 ULP
  111. inline bool StrtodDiyFp(const char* decimals, int dLen, int dExp, double* result) {
  112. uint64_t significand = 0;
  113. int i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
  114. for (; i < dLen; i++) {
  115. if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
  116. (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
  117. break;
  118. significand = significand * 10u + static_cast<unsigned>(decimals[i] - '0');
  119. }
  120. if (i < dLen && decimals[i] >= '5') // Rounding
  121. significand++;
  122. int remaining = dLen - i;
  123. const int kUlpShift = 3;
  124. const int kUlp = 1 << kUlpShift;
  125. int64_t error = (remaining == 0) ? 0 : kUlp / 2;
  126. DiyFp v(significand, 0);
  127. v = v.Normalize();
  128. error <<= -v.e;
  129. dExp += remaining;
  130. int actualExp;
  131. DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
  132. if (actualExp != dExp) {
  133. static const DiyFp kPow10[] = {
  134. DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 0x00000000), -60), // 10^1
  135. DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 0x00000000), -57), // 10^2
  136. DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 0x00000000), -54), // 10^3
  137. DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), -50), // 10^4
  138. DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 0x00000000), -47), // 10^5
  139. DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 0x00000000), -44), // 10^6
  140. DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 0x00000000), -40) // 10^7
  141. };
  142. int adjustment = dExp - actualExp;
  143. RAPIDJSON_ASSERT(adjustment >= 1 && adjustment < 8);
  144. v = v * kPow10[adjustment - 1];
  145. if (dLen + adjustment > 19) // has more digits than decimal digits in 64-bit
  146. error += kUlp / 2;
  147. }
  148. v = v * cachedPower;
  149. error += kUlp + (error == 0 ? 0 : 1);
  150. const int oldExp = v.e;
  151. v = v.Normalize();
  152. error <<= oldExp - v.e;
  153. const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
  154. int precisionSize = 64 - effectiveSignificandSize;
  155. if (precisionSize + kUlpShift >= 64) {
  156. int scaleExp = (precisionSize + kUlpShift) - 63;
  157. v.f >>= scaleExp;
  158. v.e += scaleExp;
  159. error = (error >> scaleExp) + 1 + kUlp;
  160. precisionSize -= scaleExp;
  161. }
  162. DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
  163. const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
  164. const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
  165. if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
  166. rounded.f++;
  167. if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
  168. rounded.f >>= 1;
  169. rounded.e++;
  170. }
  171. }
  172. *result = rounded.ToDouble();
  173. return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
  174. }
  175. inline double StrtodBigInteger(double approx, const char* decimals, int dLen, int dExp) {
  176. RAPIDJSON_ASSERT(dLen >= 0);
  177. const BigInteger dInt(decimals, static_cast<unsigned>(dLen));
  178. Double a(approx);
  179. int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
  180. if (cmp < 0)
  181. return a.Value(); // within half ULP
  182. else if (cmp == 0) {
  183. // Round towards even
  184. if (a.Significand() & 1)
  185. return a.NextPositiveDouble();
  186. else
  187. return a.Value();
  188. }
  189. else // adjustment
  190. return a.NextPositiveDouble();
  191. }
  192. inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) {
  193. RAPIDJSON_ASSERT(d >= 0.0);
  194. RAPIDJSON_ASSERT(length >= 1);
  195. double result = 0.0;
  196. if (StrtodFast(d, p, &result))
  197. return result;
  198. RAPIDJSON_ASSERT(length <= INT_MAX);
  199. int dLen = static_cast<int>(length);
  200. RAPIDJSON_ASSERT(length >= decimalPosition);
  201. RAPIDJSON_ASSERT(length - decimalPosition <= INT_MAX);
  202. int dExpAdjust = static_cast<int>(length - decimalPosition);
  203. RAPIDJSON_ASSERT(exp >= INT_MIN + dExpAdjust);
  204. int dExp = exp - dExpAdjust;
  205. // Make sure length+dExp does not overflow
  206. RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen);
  207. // Trim leading zeros
  208. while (dLen > 0 && *decimals == '0') {
  209. dLen--;
  210. decimals++;
  211. }
  212. // Trim trailing zeros
  213. while (dLen > 0 && decimals[dLen - 1] == '0') {
  214. dLen--;
  215. dExp++;
  216. }
  217. if (dLen == 0) { // Buffer only contains zeros.
  218. return 0.0;
  219. }
  220. // Trim right-most digits
  221. const int kMaxDecimalDigit = 767 + 1;
  222. if (dLen > kMaxDecimalDigit) {
  223. dExp += dLen - kMaxDecimalDigit;
  224. dLen = kMaxDecimalDigit;
  225. }
  226. // If too small, underflow to zero.
  227. // Any x <= 10^-324 is interpreted as zero.
  228. if (dLen + dExp <= -324)
  229. return 0.0;
  230. // If too large, overflow to infinity.
  231. // Any x >= 10^309 is interpreted as +infinity.
  232. if (dLen + dExp > 309)
  233. return std::numeric_limits<double>::infinity();
  234. if (StrtodDiyFp(decimals, dLen, dExp, &result))
  235. return result;
  236. // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
  237. return StrtodBigInteger(result, decimals, dLen, dExp);
  238. }
  239. } // namespace internal
  240. RAPIDJSON_NAMESPACE_END
  241. #endif // RAPIDJSON_STRTOD_