strtod.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "../rapidjson.h"
  17. #include "ieee754.h"
  18. #include "biginteger.h"
  19. #include "diyfp.h"
  20. #include "pow10.h"
  21. RAPIDJSON_NAMESPACE_BEGIN
  22. namespace internal {
  23. inline double FastPath(double significand, int exp) {
  24. if (exp < -308)
  25. return 0.0;
  26. else if (exp >= 0)
  27. return significand * internal::Pow10(exp);
  28. else
  29. return significand / internal::Pow10(-exp);
  30. }
  31. inline double StrtodNormalPrecision(double d, int p) {
  32. if (p < -308) {
  33. // Prevent expSum < -308, making Pow10(p) = 0
  34. d = FastPath(d, -308);
  35. d = FastPath(d, p + 308);
  36. }
  37. else
  38. d = FastPath(d, p);
  39. return d;
  40. }
  41. template <typename T>
  42. inline T Min3(T a, T b, T c) {
  43. T m = a;
  44. if (m > b) m = b;
  45. if (m > c) m = c;
  46. return m;
  47. }
  48. inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
  49. const Double db(b);
  50. const uint64_t bInt = db.IntegerSignificand();
  51. const int bExp = db.IntegerExponent();
  52. const int hExp = bExp - 1;
  53. int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
  54. // Adjust for decimal exponent
  55. if (dExp >= 0) {
  56. dS_Exp2 += dExp;
  57. dS_Exp5 += dExp;
  58. }
  59. else {
  60. bS_Exp2 -= dExp;
  61. bS_Exp5 -= dExp;
  62. hS_Exp2 -= dExp;
  63. hS_Exp5 -= dExp;
  64. }
  65. // Adjust for binary exponent
  66. if (bExp >= 0)
  67. bS_Exp2 += bExp;
  68. else {
  69. dS_Exp2 -= bExp;
  70. hS_Exp2 -= bExp;
  71. }
  72. // Adjust for half ulp exponent
  73. if (hExp >= 0)
  74. hS_Exp2 += hExp;
  75. else {
  76. dS_Exp2 -= hExp;
  77. bS_Exp2 -= hExp;
  78. }
  79. // Remove common power of two factor from all three scaled values
  80. int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
  81. dS_Exp2 -= common_Exp2;
  82. bS_Exp2 -= common_Exp2;
  83. hS_Exp2 -= common_Exp2;
  84. BigInteger dS = d;
  85. dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2;
  86. BigInteger bS(bInt);
  87. bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2;
  88. BigInteger hS(1);
  89. hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2;
  90. BigInteger delta(0);
  91. dS.Difference(bS, &delta);
  92. return delta.Compare(hS);
  93. }
  94. inline bool StrtodFast(double d, int p, double* result) {
  95. // Use fast path for string-to-double conversion if possible
  96. // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  97. if (p > 22 && p < 22 + 16) {
  98. // Fast Path Cases In Disguise
  99. d *= internal::Pow10(p - 22);
  100. p = 22;
  101. }
  102. if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1
  103. *result = FastPath(d, p);
  104. return true;
  105. }
  106. else
  107. return false;
  108. }
  109. // Compute an approximation and see if it is within 1/2 ULP
  110. inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) {
  111. uint64_t significand = 0;
  112. size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
  113. for (; i < length; i++) {
  114. if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
  115. (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
  116. break;
  117. significand = significand * 10 + (decimals[i] - '0');
  118. }
  119. if (i < length && decimals[i] >= '5') // Rounding
  120. significand++;
  121. size_t remaining = length - i;
  122. const unsigned kUlpShift = 3;
  123. const unsigned kUlp = 1 << kUlpShift;
  124. int error = (remaining == 0) ? 0 : kUlp / 2;
  125. DiyFp v(significand, 0);
  126. v = v.Normalize();
  127. error <<= -v.e;
  128. const int dExp = (int)decimalPosition - (int)i + exp;
  129. int actualExp;
  130. DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
  131. if (actualExp != dExp) {
  132. static const DiyFp kPow10[] = {
  133. DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1
  134. DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2
  135. DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3
  136. DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4
  137. DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5
  138. DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6
  139. DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7
  140. };
  141. int adjustment = dExp - actualExp - 1;
  142. RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7);
  143. v = v * kPow10[adjustment];
  144. if (length + adjustment > 19) // has more digits than decimal digits in 64-bit
  145. error += kUlp / 2;
  146. }
  147. v = v * cachedPower;
  148. error += kUlp + (error == 0 ? 0 : 1);
  149. const int oldExp = v.e;
  150. v = v.Normalize();
  151. error <<= oldExp - v.e;
  152. const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
  153. unsigned precisionSize = 64 - effectiveSignificandSize;
  154. if (precisionSize + kUlpShift >= 64) {
  155. unsigned scaleExp = (precisionSize + kUlpShift) - 63;
  156. v.f >>= scaleExp;
  157. v.e += scaleExp;
  158. error = (error >> scaleExp) + 1 + kUlp;
  159. precisionSize -= scaleExp;
  160. }
  161. DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
  162. const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
  163. const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
  164. if (precisionBits >= halfWay + error) {
  165. rounded.f++;
  166. if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
  167. rounded.f >>= 1;
  168. rounded.e++;
  169. }
  170. }
  171. *result = rounded.ToDouble();
  172. return halfWay - error >= precisionBits || precisionBits >= halfWay + error;
  173. }
  174. inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) {
  175. const BigInteger dInt(decimals, length);
  176. const int dExp = (int)decimalPosition - (int)length + exp;
  177. Double a(approx);
  178. int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
  179. if (cmp < 0)
  180. return a.Value(); // within half ULP
  181. else if (cmp == 0) {
  182. // Round towards even
  183. if (a.Significand() & 1)
  184. return a.NextPositiveDouble();
  185. else
  186. return a.Value();
  187. }
  188. else // adjustment
  189. return a.NextPositiveDouble();
  190. }
  191. inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) {
  192. RAPIDJSON_ASSERT(d >= 0.0);
  193. RAPIDJSON_ASSERT(length >= 1);
  194. double result;
  195. if (StrtodFast(d, p, &result))
  196. return result;
  197. // Trim leading zeros
  198. while (*decimals == '0' && length > 1) {
  199. length--;
  200. decimals++;
  201. decimalPosition--;
  202. }
  203. // Trim trailing zeros
  204. while (decimals[length - 1] == '0' && length > 1) {
  205. length--;
  206. decimalPosition--;
  207. exp++;
  208. }
  209. // Trim right-most digits
  210. const int kMaxDecimalDigit = 780;
  211. if ((int)length > kMaxDecimalDigit) {
  212. int delta = (int(length) - kMaxDecimalDigit);
  213. exp += delta;
  214. decimalPosition -= delta;
  215. length = kMaxDecimalDigit;
  216. }
  217. // If too small, underflow to zero
  218. if (int(length) + exp < -324)
  219. return 0.0;
  220. if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result))
  221. return result;
  222. // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
  223. return StrtodBigInteger(result, decimals, length, decimalPosition, exp);
  224. }
  225. } // namespace internal
  226. RAPIDJSON_NAMESPACE_END
  227. #endif // RAPIDJSON_STRTOD_