strtod.h 8.5 KB

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