strtod.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.
  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. template<typename Ch>
  112. inline bool StrtodDiyFp(const Ch* decimals, int dLen, int dExp, double* result) {
  113. uint64_t significand = 0;
  114. int i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
  115. for (; i < dLen; i++) {
  116. if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
  117. (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > Ch('5')))
  118. break;
  119. significand = significand * 10u + static_cast<unsigned>(decimals[i] - Ch('0'));
  120. }
  121. if (i < dLen && decimals[i] >= Ch('5')) // Rounding
  122. significand++;
  123. int remaining = dLen - i;
  124. const int kUlpShift = 3;
  125. const int kUlp = 1 << kUlpShift;
  126. int64_t error = (remaining == 0) ? 0 : kUlp / 2;
  127. DiyFp v(significand, 0);
  128. v = v.Normalize();
  129. error <<= -v.e;
  130. dExp += remaining;
  131. int actualExp;
  132. DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
  133. if (actualExp != dExp) {
  134. static const DiyFp kPow10[] = {
  135. DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 0x00000000), -60), // 10^1
  136. DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 0x00000000), -57), // 10^2
  137. DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 0x00000000), -54), // 10^3
  138. DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), -50), // 10^4
  139. DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 0x00000000), -47), // 10^5
  140. DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 0x00000000), -44), // 10^6
  141. DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 0x00000000), -40) // 10^7
  142. };
  143. int adjustment = dExp - actualExp;
  144. RAPIDJSON_ASSERT(adjustment >= 1 && adjustment < 8);
  145. v = v * kPow10[adjustment - 1];
  146. if (dLen + adjustment > 19) // has more digits than decimal digits in 64-bit
  147. error += kUlp / 2;
  148. }
  149. v = v * cachedPower;
  150. error += kUlp + (error == 0 ? 0 : 1);
  151. const int oldExp = v.e;
  152. v = v.Normalize();
  153. error <<= oldExp - v.e;
  154. const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
  155. int precisionSize = 64 - effectiveSignificandSize;
  156. if (precisionSize + kUlpShift >= 64) {
  157. int scaleExp = (precisionSize + kUlpShift) - 63;
  158. v.f >>= scaleExp;
  159. v.e += scaleExp;
  160. error = (error >> scaleExp) + 1 + kUlp;
  161. precisionSize -= scaleExp;
  162. }
  163. DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
  164. const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
  165. const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
  166. if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
  167. rounded.f++;
  168. if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
  169. rounded.f >>= 1;
  170. rounded.e++;
  171. }
  172. }
  173. *result = rounded.ToDouble();
  174. return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
  175. }
  176. template<typename Ch>
  177. inline double StrtodBigInteger(double approx, const Ch* decimals, int dLen, int dExp) {
  178. RAPIDJSON_ASSERT(dLen >= 0);
  179. const BigInteger dInt(decimals, static_cast<unsigned>(dLen));
  180. Double a(approx);
  181. int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
  182. if (cmp < 0)
  183. return a.Value(); // within half ULP
  184. else if (cmp == 0) {
  185. // Round towards even
  186. if (a.Significand() & 1)
  187. return a.NextPositiveDouble();
  188. else
  189. return a.Value();
  190. }
  191. else // adjustment
  192. return a.NextPositiveDouble();
  193. }
  194. template<typename Ch>
  195. inline double StrtodFullPrecision(double d, int p, const Ch* decimals, size_t length, size_t decimalPosition, int exp) {
  196. RAPIDJSON_ASSERT(d >= 0.0);
  197. RAPIDJSON_ASSERT(length >= 1);
  198. double result = 0.0;
  199. if (StrtodFast(d, p, &result))
  200. return result;
  201. RAPIDJSON_ASSERT(length <= INT_MAX);
  202. int dLen = static_cast<int>(length);
  203. RAPIDJSON_ASSERT(length >= decimalPosition);
  204. RAPIDJSON_ASSERT(length - decimalPosition <= INT_MAX);
  205. int dExpAdjust = static_cast<int>(length - decimalPosition);
  206. RAPIDJSON_ASSERT(exp >= INT_MIN + dExpAdjust);
  207. int dExp = exp - dExpAdjust;
  208. // Make sure length+dExp does not overflow
  209. RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen);
  210. // Trim leading zeros
  211. while (dLen > 0 && *decimals == '0') {
  212. dLen--;
  213. decimals++;
  214. }
  215. // Trim trailing zeros
  216. while (dLen > 0 && decimals[dLen - 1] == '0') {
  217. dLen--;
  218. dExp++;
  219. }
  220. if (dLen == 0) { // Buffer only contains zeros.
  221. return 0.0;
  222. }
  223. // Trim right-most digits
  224. const int kMaxDecimalDigit = 767 + 1;
  225. if (dLen > kMaxDecimalDigit) {
  226. dExp += dLen - kMaxDecimalDigit;
  227. dLen = kMaxDecimalDigit;
  228. }
  229. // If too small, underflow to zero.
  230. // Any x <= 10^-324 is interpreted as zero.
  231. if (dLen + dExp <= -324)
  232. return 0.0;
  233. // If too large, overflow to infinity.
  234. // Any x >= 10^309 is interpreted as +infinity.
  235. if (dLen + dExp > 309)
  236. return std::numeric_limits<double>::infinity();
  237. if (StrtodDiyFp(decimals, dLen, dExp, &result))
  238. return result;
  239. // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
  240. return StrtodBigInteger(result, decimals, dLen, dExp);
  241. }
  242. } // namespace internal
  243. RAPIDJSON_NAMESPACE_END
  244. #endif // RAPIDJSON_STRTOD_