OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include <limits.h> | 31 #include <limits.h> |
32 | 32 |
33 #include "strtod.h" | 33 #include "strtod.h" |
34 #include "bignum.h" | 34 #include "bignum.h" |
35 #include "cached-powers.h" | 35 #include "cached-powers.h" |
36 #include "double.h" | 36 #include "double.h" |
37 | 37 |
38 namespace WTF { | 38 namespace WTF { |
39 | 39 |
40 namespace double_conversion { | 40 namespace double_conversion { |
41 | 41 |
42 // 2^53 = 9007199254740992. | 42 // 2^53 = 9007199254740992. |
43 // Any integer with at most 15 decimal digits will hence fit into a double | 43 // Any integer with at most 15 decimal digits will hence fit into a double |
44 // (which has a 53bit significand) without loss of precision. | 44 // (which has a 53bit significand) without loss of precision. |
45 static const int kMaxExactDoubleIntegerDecimalDigits = 15; | 45 static const int kMaxExactDoubleIntegerDecimalDigits = 15; |
46 // 2^64 = 18446744073709551616 > 10^19 | 46 // 2^64 = 18446744073709551616 > 10^19 |
47 static const int kMaxUint64DecimalDigits = 19; | 47 static const int kMaxUint64DecimalDigits = 19; |
48 | 48 |
49 // Max double: 1.7976931348623157 x 10^308 | 49 // Max double: 1.7976931348623157 x 10^308 |
50 // Min non-zero double: 4.9406564584124654 x 10^-324 | 50 // Min non-zero double: 4.9406564584124654 x 10^-324 |
51 // Any x >= 10^309 is interpreted as +infinity. | 51 // Any x >= 10^309 is interpreted as +infinity. |
52 // Any x <= 10^-324 is interpreted as 0. | 52 // Any x <= 10^-324 is interpreted as 0. |
53 // Note that 2.5e-324 (despite being smaller than the min double) will be re
ad | 53 // Note that 2.5e-324 (despite being smaller than the min double) will be re
ad |
54 // as non-zero (equal to the min non-zero double). | 54 // as non-zero (equal to the min non-zero double). |
55 static const int kMaxDecimalPower = 309; | 55 static const int kMaxDecimalPower = 309; |
56 static const int kMinDecimalPower = -324; | 56 static const int kMinDecimalPower = -324; |
57 | 57 |
58 // 2^64 = 18446744073709551616 | 58 // 2^64 = 18446744073709551616 |
59 static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); | 59 static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); |
60 | 60 |
61 | 61 |
62 static const double exact_powers_of_ten[] = { | 62 static const double exact_powers_of_ten[] = { |
63 1.0, // 10^0 | 63 1.0, // 10^0 |
64 10.0, | 64 10.0, |
65 100.0, | 65 100.0, |
66 1000.0, | 66 1000.0, |
67 10000.0, | 67 10000.0, |
68 100000.0, | 68 100000.0, |
69 1000000.0, | 69 1000000.0, |
70 10000000.0, | 70 10000000.0, |
71 100000000.0, | 71 100000000.0, |
72 1000000000.0, | 72 1000000000.0, |
73 10000000000.0, // 10^10 | 73 10000000000.0, // 10^10 |
74 100000000000.0, | 74 100000000000.0, |
75 1000000000000.0, | 75 1000000000000.0, |
76 10000000000000.0, | 76 10000000000000.0, |
77 100000000000000.0, | 77 100000000000000.0, |
78 1000000000000000.0, | 78 1000000000000000.0, |
79 10000000000000000.0, | 79 10000000000000000.0, |
80 100000000000000000.0, | 80 100000000000000000.0, |
81 1000000000000000000.0, | 81 1000000000000000000.0, |
82 10000000000000000000.0, | 82 10000000000000000000.0, |
83 100000000000000000000.0, // 10^20 | 83 100000000000000000000.0, // 10^20 |
84 1000000000000000000000.0, | 84 1000000000000000000000.0, |
85 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 | 85 // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 |
86 10000000000000000000000.0 | 86 10000000000000000000000.0 |
87 }; | 87 }; |
88 static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); | 88 static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); |
89 | 89 |
90 // Maximum number of significant digits in the decimal representation. | 90 // Maximum number of significant digits in the decimal representation. |
91 // In fact the value is 772 (see conversions.cc), but to give us some margin | 91 // In fact the value is 772 (see conversions.cc), but to give us some margin |
92 // we round up to 780. | 92 // we round up to 780. |
93 static const int kMaxSignificantDecimalDigits = 780; | 93 static const int kMaxSignificantDecimalDigits = 780; |
94 | 94 |
95 static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { | 95 static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { |
96 for (int i = 0; i < buffer.length(); i++) { | 96 for (int i = 0; i < buffer.length(); i++) { |
97 if (buffer[i] != '0') { | 97 if (buffer[i] != '0') { |
98 return buffer.SubVector(i, buffer.length()); | 98 return buffer.SubVector(i, buffer.length()); |
99 } | 99 } |
100 } | 100 } |
101 return Vector<const char>(buffer.start(), 0); | 101 return Vector<const char>(buffer.start(), 0); |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { | 105 static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { |
106 for (int i = buffer.length() - 1; i >= 0; --i) { | 106 for (int i = buffer.length() - 1; i >= 0; --i) { |
107 if (buffer[i] != '0') { | 107 if (buffer[i] != '0') { |
108 return buffer.SubVector(0, i + 1); | 108 return buffer.SubVector(0, i + 1); |
109 } | 109 } |
110 } | 110 } |
111 return Vector<const char>(buffer.start(), 0); | 111 return Vector<const char>(buffer.start(), 0); |
112 } | 112 } |
113 | 113 |
114 | 114 |
115 static void TrimToMaxSignificantDigits(Vector<const char> buffer, | 115 static void TrimToMaxSignificantDigits(Vector<const char> buffer, |
116 int exponent, | 116 int exponent, |
117 char* significant_buffer, | 117 char* significant_buffer, |
118 int* significant_exponent) { | 118 int* significant_exponent) { |
119 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { | 119 for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { |
120 significant_buffer[i] = buffer[i]; | 120 significant_buffer[i] = buffer[i]; |
121 } | 121 } |
122 // The input buffer has been trimmed. Therefore the last digit must be | 122 // The input buffer has been trimmed. Therefore the last digit must be |
123 // different from '0'. | 123 // different from '0'. |
124 ASSERT(buffer[buffer.length() - 1] != '0'); | 124 ASSERT(buffer[buffer.length() - 1] != '0'); |
125 // Set the last digit to be non-zero. This is sufficient to guarantee | 125 // Set the last digit to be non-zero. This is sufficient to guarantee |
126 // correct rounding. | 126 // correct rounding. |
127 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; | 127 significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; |
128 *significant_exponent = | 128 *significant_exponent = |
129 exponent + (buffer.length() - kMaxSignificantDecimalDigits); | 129 exponent + (buffer.length() - kMaxSignificantDecimalDigits); |
130 } | 130 } |
131 | 131 |
132 // Reads digits from the buffer and converts them to a uint64. | 132 // Reads digits from the buffer and converts them to a uint64. |
133 // Reads in as many digits as fit into a uint64. | 133 // Reads in as many digits as fit into a uint64. |
134 // When the string starts with "1844674407370955161" no further digit is rea
d. | 134 // When the string starts with "1844674407370955161" no further digit is rea
d. |
135 // Since 2^64 = 18446744073709551616 it would still be possible read another | 135 // Since 2^64 = 18446744073709551616 it would still be possible read another |
136 // digit if it was less or equal than 6, but this would complicate the code. | 136 // digit if it was less or equal than 6, but this would complicate the code. |
137 static uint64_t ReadUint64(Vector<const char> buffer, | 137 static uint64_t ReadUint64(Vector<const char> buffer, |
138 int* number_of_read_digits) { | 138 int* number_of_read_digits) { |
139 uint64_t result = 0; | 139 uint64_t result = 0; |
140 int i = 0; | 140 int i = 0; |
141 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { | 141 while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { |
142 int digit = buffer[i++] - '0'; | 142 int digit = buffer[i++] - '0'; |
143 ASSERT(0 <= digit && digit <= 9); | 143 ASSERT(0 <= digit && digit <= 9); |
144 result = 10 * result + digit; | 144 result = 10 * result + digit; |
145 } | 145 } |
146 *number_of_read_digits = i; | 146 *number_of_read_digits = i; |
147 return result; | 147 return result; |
148 } | 148 } |
149 | 149 |
150 | 150 |
151 // Reads a DiyFp from the buffer. | 151 // Reads a DiyFp from the buffer. |
152 // The returned DiyFp is not necessarily normalized. | 152 // The returned DiyFp is not necessarily normalized. |
153 // If remaining_decimals is zero then the returned DiyFp is accurate. | 153 // If remaining_decimals is zero then the returned DiyFp is accurate. |
154 // Otherwise it has been rounded and has error of at most 1/2 ulp. | 154 // Otherwise it has been rounded and has error of at most 1/2 ulp. |
155 static void ReadDiyFp(Vector<const char> buffer, | 155 static void ReadDiyFp(Vector<const char> buffer, |
156 DiyFp* result, | 156 DiyFp* result, |
157 int* remaining_decimals) { | 157 int* remaining_decimals) { |
158 int read_digits; | 158 int read_digits; |
159 uint64_t significand = ReadUint64(buffer, &read_digits); | 159 uint64_t significand = ReadUint64(buffer, &read_digits); |
160 if (buffer.length() == read_digits) { | 160 if (buffer.length() == read_digits) { |
161 *result = DiyFp(significand, 0); | 161 *result = DiyFp(significand, 0); |
162 *remaining_decimals = 0; | 162 *remaining_decimals = 0; |
163 } else { | 163 } else { |
164 // Round the significand. | 164 // Round the significand. |
165 if (buffer[read_digits] >= '5') { | 165 if (buffer[read_digits] >= '5') { |
166 significand++; | 166 significand++; |
167 } | 167 } |
168 // Compute the binary exponent. | 168 // Compute the binary exponent. |
169 int exponent = 0; | 169 int exponent = 0; |
170 *result = DiyFp(significand, exponent); | 170 *result = DiyFp(significand, exponent); |
171 *remaining_decimals = buffer.length() - read_digits; | 171 *remaining_decimals = buffer.length() - read_digits; |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 | 175 |
176 static bool DoubleStrtod(Vector<const char> trimmed, | 176 static bool DoubleStrtod(Vector<const char> trimmed, |
177 int exponent, | 177 int exponent, |
178 double* result) { | 178 double* result) { |
179 #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) | 179 #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) |
180 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is | 180 // On x86 the floating-point stack can be 64 or 80 bits wide. If it is |
181 // 80 bits wide (as is the case on Linux) then double-rounding occurs an
d the | 181 // 80 bits wide (as is the case on Linux) then double-rounding occurs an
d the |
182 // result is not accurate. | 182 // result is not accurate. |
183 // We know that Windows32 uses 64 bits and is therefore accurate. | 183 // We know that Windows32 uses 64 bits and is therefore accurate. |
184 // Note that the ARM simulator is compiled for 32bits. It therefore exhi
bits | 184 // Note that the ARM simulator is compiled for 32bits. It therefore exhi
bits |
185 // the same problem. | 185 // the same problem. |
(...skipping 30 matching lines...) Expand all Loading... |
216 // into a double too. | 216 // into a double too. |
217 *result = static_cast<double>(ReadUint64(trimmed, &read_digits))
; | 217 *result = static_cast<double>(ReadUint64(trimmed, &read_digits))
; |
218 ASSERT(read_digits == trimmed.length()); | 218 ASSERT(read_digits == trimmed.length()); |
219 *result *= exact_powers_of_ten[remaining_digits]; | 219 *result *= exact_powers_of_ten[remaining_digits]; |
220 *result *= exact_powers_of_ten[exponent - remaining_digits]; | 220 *result *= exact_powers_of_ten[exponent - remaining_digits]; |
221 return true; | 221 return true; |
222 } | 222 } |
223 } | 223 } |
224 return false; | 224 return false; |
225 } | 225 } |
226 | 226 |
227 | 227 |
228 // Returns 10^exponent as an exact DiyFp. | 228 // Returns 10^exponent as an exact DiyFp. |
229 // The given exponent must be in the range [1; kDecimalExponentDistance[. | 229 // The given exponent must be in the range [1; kDecimalExponentDistance[. |
230 static DiyFp AdjustmentPowerOfTen(int exponent) { | 230 static DiyFp AdjustmentPowerOfTen(int exponent) { |
231 ASSERT(0 < exponent); | 231 ASSERT(0 < exponent); |
232 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); | 232 ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); |
233 // Simply hardcode the remaining powers for the given decimal exponent | 233 // Simply hardcode the remaining powers for the given decimal exponent |
234 // distance. | 234 // distance. |
235 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); | 235 ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); |
236 switch (exponent) { | 236 switch (exponent) { |
237 case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); | 237 case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); |
238 case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); | 238 case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); |
239 case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); | 239 case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); |
240 case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); | 240 case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); |
241 case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); | 241 case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); |
242 case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); | 242 case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); |
243 case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); | 243 case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); |
244 default: | 244 default: |
245 UNREACHABLE(); | 245 UNREACHABLE(); |
246 return DiyFp(0, 0); | 246 return DiyFp(0, 0); |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 | 250 |
251 // If the function returns true then the result is the correct double. | 251 // If the function returns true then the result is the correct double. |
252 // Otherwise it is either the correct double or the double that is just belo
w | 252 // Otherwise it is either the correct double or the double that is just belo
w |
253 // the correct double. | 253 // the correct double. |
254 static bool DiyFpStrtod(Vector<const char> buffer, | 254 static bool DiyFpStrtod(Vector<const char> buffer, |
255 int exponent, | 255 int exponent, |
256 double* result) { | 256 double* result) { |
257 DiyFp input; | 257 DiyFp input; |
258 int remaining_decimals; | 258 int remaining_decimals; |
259 ReadDiyFp(buffer, &input, &remaining_decimals); | 259 ReadDiyFp(buffer, &input, &remaining_decimals); |
260 // Since we may have dropped some digits the input is not accurate. | 260 // Since we may have dropped some digits the input is not accurate. |
261 // If remaining_decimals is different than 0 than the error is at most | 261 // If remaining_decimals is different than 0 than the error is at most |
262 // .5 ulp (unit in the last place). | 262 // .5 ulp (unit in the last place). |
263 // We don't want to deal with fractions and therefore keep a common | 263 // We don't want to deal with fractions and therefore keep a common |
264 // denominator. | 264 // denominator. |
265 const int kDenominatorLog = 3; | 265 const int kDenominatorLog = 3; |
266 const int kDenominator = 1 << kDenominatorLog; | 266 const int kDenominator = 1 << kDenominatorLog; |
267 // Move the remaining decimals into the exponent. | 267 // Move the remaining decimals into the exponent. |
268 exponent += remaining_decimals; | 268 exponent += remaining_decimals; |
269 int error = (remaining_decimals == 0 ? 0 : kDenominator / 2); | 269 int error = (remaining_decimals == 0 ? 0 : kDenominator / 2); |
270 | 270 |
271 int old_e = input.e(); | 271 int old_e = input.e(); |
272 input.Normalize(); | 272 input.Normalize(); |
273 error <<= old_e - input.e(); | 273 error <<= old_e - input.e(); |
274 | 274 |
275 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); | 275 ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); |
276 if (exponent < PowersOfTenCache::kMinDecimalExponent) { | 276 if (exponent < PowersOfTenCache::kMinDecimalExponent) { |
277 *result = 0.0; | 277 *result = 0.0; |
278 return true; | 278 return true; |
279 } | 279 } |
280 DiyFp cached_power; | 280 DiyFp cached_power; |
281 int cached_decimal_exponent; | 281 int cached_decimal_exponent; |
282 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, | 282 PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, |
283 &cached_power, | 283 &cached_power, |
284 &cached_decimal_expon
ent); | 284 &cached_decimal_expon
ent); |
285 | 285 |
286 if (cached_decimal_exponent != exponent) { | 286 if (cached_decimal_exponent != exponent) { |
287 int adjustment_exponent = exponent - cached_decimal_exponent; | 287 int adjustment_exponent = exponent - cached_decimal_exponent; |
288 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); | 288 DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); |
289 input.Multiply(adjustment_power); | 289 input.Multiply(adjustment_power); |
290 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent
) { | 290 if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent
) { |
291 // The product of input with the adjustment power fits into a 64
bit | 291 // The product of input with the adjustment power fits into a 64
bit |
292 // integer. | 292 // integer. |
293 ASSERT(DiyFp::kSignificandSize == 64); | 293 ASSERT(DiyFp::kSignificandSize == 64); |
294 } else { | 294 } else { |
295 // The adjustment power is exact. There is hence only an error o
f 0.5. | 295 // The adjustment power is exact. There is hence only an error o
f 0.5. |
296 error += kDenominator / 2; | 296 error += kDenominator / 2; |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 input.Multiply(cached_power); | 300 input.Multiply(cached_power); |
301 // The error introduced by a multiplication of a*b equals | 301 // The error introduced by a multiplication of a*b equals |
302 // error_a + error_b + error_a*error_b/2^64 + 0.5 | 302 // error_a + error_b + error_a*error_b/2^64 + 0.5 |
303 // Substituting a with 'input' and b with 'cached_power' we have | 303 // Substituting a with 'input' and b with 'cached_power' we have |
304 // error_b = 0.5 (all cached powers have an error of less than 0.5 ul
p), | 304 // error_b = 0.5 (all cached powers have an error of less than 0.5 ul
p), |
305 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 | 305 // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 |
306 int error_b = kDenominator / 2; | 306 int error_b = kDenominator / 2; |
307 int error_ab = (error == 0 ? 0 : 1); // We round up to 1. | 307 int error_ab = (error == 0 ? 0 : 1); // We round up to 1. |
308 int fixed_error = kDenominator / 2; | 308 int fixed_error = kDenominator / 2; |
309 error += error_b + error_ab + fixed_error; | 309 error += error_b + error_ab + fixed_error; |
310 | 310 |
311 old_e = input.e(); | 311 old_e = input.e(); |
312 input.Normalize(); | 312 input.Normalize(); |
313 error <<= old_e - input.e(); | 313 error <<= old_e - input.e(); |
314 | 314 |
315 // See if the double's significand changes if we add/subtract the error. | 315 // See if the double's significand changes if we add/subtract the error. |
316 int order_of_magnitude = DiyFp::kSignificandSize + input.e(); | 316 int order_of_magnitude = DiyFp::kSignificandSize + input.e(); |
317 int effective_significand_size = | 317 int effective_significand_size = |
318 Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); | 318 Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); |
319 int precision_digits_count = | 319 int precision_digits_count = |
320 DiyFp::kSignificandSize - effective_significand_size; | 320 DiyFp::kSignificandSize - effective_significand_size; |
321 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize)
{ | 321 if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize)
{ |
322 // This can only happen for very small denormals. In this case the | 322 // This can only happen for very small denormals. In this case the |
323 // half-way multiplied by the denominator exceeds the range of an ui
nt64. | 323 // half-way multiplied by the denominator exceeds the range of an ui
nt64. |
324 // Simply shift everything to the right. | 324 // Simply shift everything to the right. |
(...skipping 16 matching lines...) Expand all Loading... |
341 precision_bits *= kDenominator; | 341 precision_bits *= kDenominator; |
342 half_way *= kDenominator; | 342 half_way *= kDenominator; |
343 DiyFp rounded_input(input.f() >> precision_digits_count, | 343 DiyFp rounded_input(input.f() >> precision_digits_count, |
344 input.e() + precision_digits_count); | 344 input.e() + precision_digits_count); |
345 if (precision_bits >= half_way + error) { | 345 if (precision_bits >= half_way + error) { |
346 rounded_input.set_f(rounded_input.f() + 1); | 346 rounded_input.set_f(rounded_input.f() + 1); |
347 } | 347 } |
348 // If the last_bits are too close to the half-way case than we are too | 348 // If the last_bits are too close to the half-way case than we are too |
349 // inaccurate and round down. In this case we return false so that we ca
n | 349 // inaccurate and round down. In this case we return false so that we ca
n |
350 // fall back to a more precise algorithm. | 350 // fall back to a more precise algorithm. |
351 | 351 |
352 *result = Double(rounded_input).value(); | 352 *result = Double(rounded_input).value(); |
353 if (half_way - error < precision_bits && precision_bits < half_way + err
or) { | 353 if (half_way - error < precision_bits && precision_bits < half_way + err
or) { |
354 // Too imprecise. The caller will have to fall back to a slower vers
ion. | 354 // Too imprecise. The caller will have to fall back to a slower vers
ion. |
355 // However the returned number is guaranteed to be either the correc
t | 355 // However the returned number is guaranteed to be either the correc
t |
356 // double, or the next-lower double. | 356 // double, or the next-lower double. |
357 return false; | 357 return false; |
358 } else { | 358 } else { |
359 return true; | 359 return true; |
360 } | 360 } |
361 } | 361 } |
362 | 362 |
363 | 363 |
364 // Returns the correct double for the buffer*10^exponent. | 364 // Returns the correct double for the buffer*10^exponent. |
365 // The variable guess should be a close guess that is either the correct dou
ble | 365 // The variable guess should be a close guess that is either the correct dou
ble |
366 // or its lower neighbor (the nearest double less than the correct one). | 366 // or its lower neighbor (the nearest double less than the correct one). |
367 // Preconditions: | 367 // Preconditions: |
368 // buffer.length() + exponent <= kMaxDecimalPower + 1 | 368 // buffer.length() + exponent <= kMaxDecimalPower + 1 |
369 // buffer.length() + exponent > kMinDecimalPower | 369 // buffer.length() + exponent > kMinDecimalPower |
370 // buffer.length() <= kMaxDecimalSignificantDigits | 370 // buffer.length() <= kMaxDecimalSignificantDigits |
371 static double BignumStrtod(Vector<const char> buffer, | 371 static double BignumStrtod(Vector<const char> buffer, |
372 int exponent, | 372 int exponent, |
373 double guess) { | 373 double guess) { |
374 if (guess == Double::Infinity()) { | 374 if (guess == Double::Infinity()) { |
375 return guess; | 375 return guess; |
376 } | 376 } |
377 | 377 |
378 DiyFp upper_boundary = Double(guess).UpperBoundary(); | 378 DiyFp upper_boundary = Double(guess).UpperBoundary(); |
379 | 379 |
380 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); | 380 ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); |
381 ASSERT(buffer.length() + exponent > kMinDecimalPower); | 381 ASSERT(buffer.length() + exponent > kMinDecimalPower); |
382 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); | 382 ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); |
383 // Make sure that the Bignum will be able to hold all our numbers. | 383 // Make sure that the Bignum will be able to hold all our numbers. |
384 // Our Bignum implementation has a separate field for exponents. Shifts
will | 384 // Our Bignum implementation has a separate field for exponents. Shifts
will |
385 // consume at most one bigit (< 64 bits). | 385 // consume at most one bigit (< 64 bits). |
386 // ln(10) == 3.3219... | 386 // ln(10) == 3.3219... |
387 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBit
s); | 387 ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBit
s); |
388 Bignum input; | 388 Bignum input; |
389 Bignum boundary; | 389 Bignum boundary; |
(...skipping 14 matching lines...) Expand all Loading... |
404 return guess; | 404 return guess; |
405 } else if (comparison > 0) { | 405 } else if (comparison > 0) { |
406 return Double(guess).NextDouble(); | 406 return Double(guess).NextDouble(); |
407 } else if ((Double(guess).Significand() & 1) == 0) { | 407 } else if ((Double(guess).Significand() & 1) == 0) { |
408 // Round towards even. | 408 // Round towards even. |
409 return guess; | 409 return guess; |
410 } else { | 410 } else { |
411 return Double(guess).NextDouble(); | 411 return Double(guess).NextDouble(); |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 | 415 |
416 double Strtod(Vector<const char> buffer, int exponent) { | 416 double Strtod(Vector<const char> buffer, int exponent) { |
417 Vector<const char> left_trimmed = TrimLeadingZeros(buffer); | 417 Vector<const char> left_trimmed = TrimLeadingZeros(buffer); |
418 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed); | 418 Vector<const char> trimmed = TrimTrailingZeros(left_trimmed); |
419 exponent += left_trimmed.length() - trimmed.length(); | 419 exponent += left_trimmed.length() - trimmed.length(); |
420 if (trimmed.length() == 0) return 0.0; | 420 if (trimmed.length() == 0) return 0.0; |
421 if (trimmed.length() > kMaxSignificantDecimalDigits) { | 421 if (trimmed.length() > kMaxSignificantDecimalDigits) { |
422 char significant_buffer[kMaxSignificantDecimalDigits]; | 422 char significant_buffer[kMaxSignificantDecimalDigits]; |
423 int significant_exponent; | 423 int significant_exponent; |
424 TrimToMaxSignificantDigits(trimmed, exponent, | 424 TrimToMaxSignificantDigits(trimmed, exponent, |
425 significant_buffer, &significant_exponent
); | 425 significant_buffer, &significant_exponent
); |
426 return Strtod(Vector<const char>(significant_buffer, | 426 return Strtod(Vector<const char>(significant_buffer, |
427 kMaxSignificantDecimalDigits), | 427 kMaxSignificantDecimalDigits), |
428 significant_exponent); | 428 significant_exponent); |
429 } | 429 } |
430 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { | 430 if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { |
431 return Double::Infinity(); | 431 return Double::Infinity(); |
432 } | 432 } |
433 if (exponent + trimmed.length() <= kMinDecimalPower) { | 433 if (exponent + trimmed.length() <= kMinDecimalPower) { |
434 return 0.0; | 434 return 0.0; |
435 } | 435 } |
436 | 436 |
437 double guess; | 437 double guess; |
438 if (DoubleStrtod(trimmed, exponent, &guess) || | 438 if (DoubleStrtod(trimmed, exponent, &guess) || |
439 DiyFpStrtod(trimmed, exponent, &guess)) { | 439 DiyFpStrtod(trimmed, exponent, &guess)) { |
440 return guess; | 440 return guess; |
441 } | 441 } |
442 return BignumStrtod(trimmed, exponent, guess); | 442 return BignumStrtod(trimmed, exponent, guess); |
443 } | 443 } |
444 | 444 |
445 } // namespace double_conversion | 445 } // namespace double_conversion |
446 | 446 |
447 } // namespace WTF | 447 } // namespace WTF |
OLD | NEW |