1 | // © 2018 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html |
3 | // |
4 | // From the double-conversion library. Original license: |
5 | // |
6 | // Copyright 2010 the V8 project authors. All rights reserved. |
7 | // Redistribution and use in source and binary forms, with or without |
8 | // modification, are permitted provided that the following conditions are |
9 | // met: |
10 | // |
11 | // * Redistributions of source code must retain the above copyright |
12 | // notice, this list of conditions and the following disclaimer. |
13 | // * Redistributions in binary form must reproduce the above |
14 | // copyright notice, this list of conditions and the following |
15 | // disclaimer in the documentation and/or other materials provided |
16 | // with the distribution. |
17 | // * Neither the name of Google Inc. nor the names of its |
18 | // contributors may be used to endorse or promote products derived |
19 | // from this software without specific prior written permission. |
20 | // |
21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | |
33 | // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING |
34 | #include "unicode/utypes.h" |
35 | #if !UCONFIG_NO_FORMATTING |
36 | |
37 | #include <climits> |
38 | #include <cstdarg> |
39 | |
40 | // ICU PATCH: Customize header file paths for ICU. |
41 | |
42 | #include "double-conversion-bignum.h" |
43 | #include "double-conversion-cached-powers.h" |
44 | #include "double-conversion-ieee.h" |
45 | #include "double-conversion-strtod.h" |
46 | |
47 | // ICU PATCH: Wrap in ICU namespace |
48 | U_NAMESPACE_BEGIN |
49 | |
50 | namespace double_conversion { |
51 | |
52 | // 2^53 = 9007199254740992. |
53 | // Any integer with at most 15 decimal digits will hence fit into a double |
54 | // (which has a 53bit significand) without loss of precision. |
55 | static const int kMaxExactDoubleIntegerDecimalDigits = 15; |
56 | // 2^64 = 18446744073709551616 > 10^19 |
57 | static const int kMaxUint64DecimalDigits = 19; |
58 | |
59 | // Max double: 1.7976931348623157 x 10^308 |
60 | // Min non-zero double: 4.9406564584124654 x 10^-324 |
61 | // Any x >= 10^309 is interpreted as +infinity. |
62 | // Any x <= 10^-324 is interpreted as 0. |
63 | // Note that 2.5e-324 (despite being smaller than the min double) will be read |
64 | // as non-zero (equal to the min non-zero double). |
65 | static const int kMaxDecimalPower = 309; |
66 | static const int kMinDecimalPower = -324; |
67 | |
68 | // 2^64 = 18446744073709551616 |
69 | static const uint64_t kMaxUint64 = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); |
70 | |
71 | |
72 | static const double exact_powers_of_ten[] = { |
73 | 1.0, // 10^0 |
74 | 10.0, |
75 | 100.0, |
76 | 1000.0, |
77 | 10000.0, |
78 | 100000.0, |
79 | 1000000.0, |
80 | 10000000.0, |
81 | 100000000.0, |
82 | 1000000000.0, |
83 | 10000000000.0, // 10^10 |
84 | 100000000000.0, |
85 | 1000000000000.0, |
86 | 10000000000000.0, |
87 | 100000000000000.0, |
88 | 1000000000000000.0, |
89 | 10000000000000000.0, |
90 | 100000000000000000.0, |
91 | 1000000000000000000.0, |
92 | 10000000000000000000.0, |
93 | 100000000000000000000.0, // 10^20 |
94 | 1000000000000000000000.0, |
95 | // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 |
96 | 10000000000000000000000.0 |
97 | }; |
98 | static const int kExactPowersOfTenSize = DOUBLE_CONVERSION_ARRAY_SIZE(exact_powers_of_ten); |
99 | |
100 | // Maximum number of significant digits in the decimal representation. |
101 | // In fact the value is 772 (see conversions.cc), but to give us some margin |
102 | // we round up to 780. |
103 | static const int kMaxSignificantDecimalDigits = 780; |
104 | |
105 | static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) { |
106 | for (int i = 0; i < buffer.length(); i++) { |
107 | if (buffer[i] != '0') { |
108 | return buffer.SubVector(i, buffer.length()); |
109 | } |
110 | } |
111 | return Vector<const char>(buffer.start(), 0); |
112 | } |
113 | |
114 | |
115 | static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) { |
116 | for (int i = buffer.length() - 1; i >= 0; --i) { |
117 | if (buffer[i] != '0') { |
118 | return buffer.SubVector(0, i + 1); |
119 | } |
120 | } |
121 | return Vector<const char>(buffer.start(), 0); |
122 | } |
123 | |
124 | |
125 | static void CutToMaxSignificantDigits(Vector<const char> buffer, |
126 | int exponent, |
127 | char* significant_buffer, |
128 | int* significant_exponent) { |
129 | for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { |
130 | significant_buffer[i] = buffer[i]; |
131 | } |
132 | // The input buffer has been trimmed. Therefore the last digit must be |
133 | // different from '0'. |
134 | DOUBLE_CONVERSION_ASSERT(buffer[buffer.length() - 1] != '0'); |
135 | // Set the last digit to be non-zero. This is sufficient to guarantee |
136 | // correct rounding. |
137 | significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; |
138 | *significant_exponent = |
139 | exponent + (buffer.length() - kMaxSignificantDecimalDigits); |
140 | } |
141 | |
142 | |
143 | // Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits. |
144 | // If possible the input-buffer is reused, but if the buffer needs to be |
145 | // modified (due to cutting), then the input needs to be copied into the |
146 | // buffer_copy_space. |
147 | static void TrimAndCut(Vector<const char> buffer, int exponent, |
148 | char* buffer_copy_space, int space_size, |
149 | Vector<const char>* trimmed, int* updated_exponent) { |
150 | Vector<const char> left_trimmed = TrimLeadingZeros(buffer); |
151 | Vector<const char> right_trimmed = TrimTrailingZeros(left_trimmed); |
152 | exponent += left_trimmed.length() - right_trimmed.length(); |
153 | if (right_trimmed.length() > kMaxSignificantDecimalDigits) { |
154 | (void) space_size; // Mark variable as used. |
155 | DOUBLE_CONVERSION_ASSERT(space_size >= kMaxSignificantDecimalDigits); |
156 | CutToMaxSignificantDigits(right_trimmed, exponent, |
157 | buffer_copy_space, updated_exponent); |
158 | *trimmed = Vector<const char>(buffer_copy_space, |
159 | kMaxSignificantDecimalDigits); |
160 | } else { |
161 | *trimmed = right_trimmed; |
162 | *updated_exponent = exponent; |
163 | } |
164 | } |
165 | |
166 | |
167 | // Reads digits from the buffer and converts them to a uint64. |
168 | // Reads in as many digits as fit into a uint64. |
169 | // When the string starts with "1844674407370955161" no further digit is read. |
170 | // Since 2^64 = 18446744073709551616 it would still be possible read another |
171 | // digit if it was less or equal than 6, but this would complicate the code. |
172 | static uint64_t ReadUint64(Vector<const char> buffer, |
173 | int* number_of_read_digits) { |
174 | uint64_t result = 0; |
175 | int i = 0; |
176 | while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { |
177 | int digit = buffer[i++] - '0'; |
178 | DOUBLE_CONVERSION_ASSERT(0 <= digit && digit <= 9); |
179 | result = 10 * result + digit; |
180 | } |
181 | *number_of_read_digits = i; |
182 | return result; |
183 | } |
184 | |
185 | |
186 | // Reads a DiyFp from the buffer. |
187 | // The returned DiyFp is not necessarily normalized. |
188 | // If remaining_decimals is zero then the returned DiyFp is accurate. |
189 | // Otherwise it has been rounded and has error of at most 1/2 ulp. |
190 | static void ReadDiyFp(Vector<const char> buffer, |
191 | DiyFp* result, |
192 | int* remaining_decimals) { |
193 | int read_digits; |
194 | uint64_t significand = ReadUint64(buffer, &read_digits); |
195 | if (buffer.length() == read_digits) { |
196 | *result = DiyFp(significand, 0); |
197 | *remaining_decimals = 0; |
198 | } else { |
199 | // Round the significand. |
200 | if (buffer[read_digits] >= '5') { |
201 | significand++; |
202 | } |
203 | // Compute the binary exponent. |
204 | int exponent = 0; |
205 | *result = DiyFp(significand, exponent); |
206 | *remaining_decimals = buffer.length() - read_digits; |
207 | } |
208 | } |
209 | |
210 | |
211 | static bool DoubleStrtod(Vector<const char> trimmed, |
212 | int exponent, |
213 | double* result) { |
214 | #if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) |
215 | // On x86 the floating-point stack can be 64 or 80 bits wide. If it is |
216 | // 80 bits wide (as is the case on Linux) then double-rounding occurs and the |
217 | // result is not accurate. |
218 | // We know that Windows32 uses 64 bits and is therefore accurate. |
219 | // Note that the ARM simulator is compiled for 32bits. It therefore exhibits |
220 | // the same problem. |
221 | return false; |
222 | #else |
223 | if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { |
224 | int read_digits; |
225 | // The trimmed input fits into a double. |
226 | // If the 10^exponent (resp. 10^-exponent) fits into a double too then we |
227 | // can compute the result-double simply by multiplying (resp. dividing) the |
228 | // two numbers. |
229 | // This is possible because IEEE guarantees that floating-point operations |
230 | // return the best possible approximation. |
231 | if (exponent < 0 && -exponent < kExactPowersOfTenSize) { |
232 | // 10^-exponent fits into a double. |
233 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
234 | DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); |
235 | *result /= exact_powers_of_ten[-exponent]; |
236 | return true; |
237 | } |
238 | if (0 <= exponent && exponent < kExactPowersOfTenSize) { |
239 | // 10^exponent fits into a double. |
240 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
241 | DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); |
242 | *result *= exact_powers_of_ten[exponent]; |
243 | return true; |
244 | } |
245 | int remaining_digits = |
246 | kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); |
247 | if ((0 <= exponent) && |
248 | (exponent - remaining_digits < kExactPowersOfTenSize)) { |
249 | // The trimmed string was short and we can multiply it with |
250 | // 10^remaining_digits. As a result the remaining exponent now fits |
251 | // into a double too. |
252 | *result = static_cast<double>(ReadUint64(trimmed, &read_digits)); |
253 | DOUBLE_CONVERSION_ASSERT(read_digits == trimmed.length()); |
254 | *result *= exact_powers_of_ten[remaining_digits]; |
255 | *result *= exact_powers_of_ten[exponent - remaining_digits]; |
256 | return true; |
257 | } |
258 | } |
259 | return false; |
260 | #endif |
261 | } |
262 | |
263 | |
264 | // Returns 10^exponent as an exact DiyFp. |
265 | // The given exponent must be in the range [1; kDecimalExponentDistance[. |
266 | static DiyFp AdjustmentPowerOfTen(int exponent) { |
267 | DOUBLE_CONVERSION_ASSERT(0 < exponent); |
268 | DOUBLE_CONVERSION_ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); |
269 | // Simply hardcode the remaining powers for the given decimal exponent |
270 | // distance. |
271 | DOUBLE_CONVERSION_ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); |
272 | switch (exponent) { |
273 | case 1: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xa0000000, 00000000), -60); |
274 | case 2: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc8000000, 00000000), -57); |
275 | case 3: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xfa000000, 00000000), -54); |
276 | case 4: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50); |
277 | case 5: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xc3500000, 00000000), -47); |
278 | case 6: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0xf4240000, 00000000), -44); |
279 | case 7: return DiyFp(DOUBLE_CONVERSION_UINT64_2PART_C(0x98968000, 00000000), -40); |
280 | default: |
281 | DOUBLE_CONVERSION_UNREACHABLE(); |
282 | } |
283 | } |
284 | |
285 | |
286 | // If the function returns true then the result is the correct double. |
287 | // Otherwise it is either the correct double or the double that is just below |
288 | // the correct double. |
289 | static bool DiyFpStrtod(Vector<const char> buffer, |
290 | int exponent, |
291 | double* result) { |
292 | DiyFp input; |
293 | int remaining_decimals; |
294 | ReadDiyFp(buffer, &input, &remaining_decimals); |
295 | // Since we may have dropped some digits the input is not accurate. |
296 | // If remaining_decimals is different than 0 than the error is at most |
297 | // .5 ulp (unit in the last place). |
298 | // We don't want to deal with fractions and therefore keep a common |
299 | // denominator. |
300 | const int kDenominatorLog = 3; |
301 | const int kDenominator = 1 << kDenominatorLog; |
302 | // Move the remaining decimals into the exponent. |
303 | exponent += remaining_decimals; |
304 | uint64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2); |
305 | |
306 | int old_e = input.e(); |
307 | input.Normalize(); |
308 | error <<= old_e - input.e(); |
309 | |
310 | DOUBLE_CONVERSION_ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); |
311 | if (exponent < PowersOfTenCache::kMinDecimalExponent) { |
312 | *result = 0.0; |
313 | return true; |
314 | } |
315 | DiyFp cached_power; |
316 | int cached_decimal_exponent; |
317 | PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, |
318 | &cached_power, |
319 | &cached_decimal_exponent); |
320 | |
321 | if (cached_decimal_exponent != exponent) { |
322 | int adjustment_exponent = exponent - cached_decimal_exponent; |
323 | DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); |
324 | input.Multiply(adjustment_power); |
325 | if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { |
326 | // The product of input with the adjustment power fits into a 64 bit |
327 | // integer. |
328 | DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64); |
329 | } else { |
330 | // The adjustment power is exact. There is hence only an error of 0.5. |
331 | error += kDenominator / 2; |
332 | } |
333 | } |
334 | |
335 | input.Multiply(cached_power); |
336 | // The error introduced by a multiplication of a*b equals |
337 | // error_a + error_b + error_a*error_b/2^64 + 0.5 |
338 | // Substituting a with 'input' and b with 'cached_power' we have |
339 | // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp), |
340 | // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 |
341 | int error_b = kDenominator / 2; |
342 | int error_ab = (error == 0 ? 0 : 1); // We round up to 1. |
343 | int fixed_error = kDenominator / 2; |
344 | error += error_b + error_ab + fixed_error; |
345 | |
346 | old_e = input.e(); |
347 | input.Normalize(); |
348 | error <<= old_e - input.e(); |
349 | |
350 | // See if the double's significand changes if we add/subtract the error. |
351 | int order_of_magnitude = DiyFp::kSignificandSize + input.e(); |
352 | int effective_significand_size = |
353 | Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); |
354 | int precision_digits_count = |
355 | DiyFp::kSignificandSize - effective_significand_size; |
356 | if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { |
357 | // This can only happen for very small denormals. In this case the |
358 | // half-way multiplied by the denominator exceeds the range of an uint64. |
359 | // Simply shift everything to the right. |
360 | int shift_amount = (precision_digits_count + kDenominatorLog) - |
361 | DiyFp::kSignificandSize + 1; |
362 | input.set_f(input.f() >> shift_amount); |
363 | input.set_e(input.e() + shift_amount); |
364 | // We add 1 for the lost precision of error, and kDenominator for |
365 | // the lost precision of input.f(). |
366 | error = (error >> shift_amount) + 1 + kDenominator; |
367 | precision_digits_count -= shift_amount; |
368 | } |
369 | // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. |
370 | DOUBLE_CONVERSION_ASSERT(DiyFp::kSignificandSize == 64); |
371 | DOUBLE_CONVERSION_ASSERT(precision_digits_count < 64); |
372 | uint64_t one64 = 1; |
373 | uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; |
374 | uint64_t precision_bits = input.f() & precision_bits_mask; |
375 | uint64_t half_way = one64 << (precision_digits_count - 1); |
376 | precision_bits *= kDenominator; |
377 | half_way *= kDenominator; |
378 | DiyFp rounded_input(input.f() >> precision_digits_count, |
379 | input.e() + precision_digits_count); |
380 | if (precision_bits >= half_way + error) { |
381 | rounded_input.set_f(rounded_input.f() + 1); |
382 | } |
383 | // If the last_bits are too close to the half-way case than we are too |
384 | // inaccurate and round down. In this case we return false so that we can |
385 | // fall back to a more precise algorithm. |
386 | |
387 | *result = Double(rounded_input).value(); |
388 | if (half_way - error < precision_bits && precision_bits < half_way + error) { |
389 | // Too imprecise. The caller will have to fall back to a slower version. |
390 | // However the returned number is guaranteed to be either the correct |
391 | // double, or the next-lower double. |
392 | return false; |
393 | } else { |
394 | return true; |
395 | } |
396 | } |
397 | |
398 | |
399 | // Returns |
400 | // - -1 if buffer*10^exponent < diy_fp. |
401 | // - 0 if buffer*10^exponent == diy_fp. |
402 | // - +1 if buffer*10^exponent > diy_fp. |
403 | // Preconditions: |
404 | // buffer.length() + exponent <= kMaxDecimalPower + 1 |
405 | // buffer.length() + exponent > kMinDecimalPower |
406 | // buffer.length() <= kMaxDecimalSignificantDigits |
407 | static int CompareBufferWithDiyFp(Vector<const char> buffer, |
408 | int exponent, |
409 | DiyFp diy_fp) { |
410 | DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); |
411 | DOUBLE_CONVERSION_ASSERT(buffer.length() + exponent > kMinDecimalPower); |
412 | DOUBLE_CONVERSION_ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); |
413 | // Make sure that the Bignum will be able to hold all our numbers. |
414 | // Our Bignum implementation has a separate field for exponents. Shifts will |
415 | // consume at most one bigit (< 64 bits). |
416 | // ln(10) == 3.3219... |
417 | DOUBLE_CONVERSION_ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); |
418 | Bignum buffer_bignum; |
419 | Bignum diy_fp_bignum; |
420 | buffer_bignum.AssignDecimalString(buffer); |
421 | diy_fp_bignum.AssignUInt64(diy_fp.f()); |
422 | if (exponent >= 0) { |
423 | buffer_bignum.MultiplyByPowerOfTen(exponent); |
424 | } else { |
425 | diy_fp_bignum.MultiplyByPowerOfTen(-exponent); |
426 | } |
427 | if (diy_fp.e() > 0) { |
428 | diy_fp_bignum.ShiftLeft(diy_fp.e()); |
429 | } else { |
430 | buffer_bignum.ShiftLeft(-diy_fp.e()); |
431 | } |
432 | return Bignum::Compare(buffer_bignum, diy_fp_bignum); |
433 | } |
434 | |
435 | |
436 | // Returns true if the guess is the correct double. |
437 | // Returns false, when guess is either correct or the next-lower double. |
438 | static bool ComputeGuess(Vector<const char> trimmed, int exponent, |
439 | double* guess) { |
440 | if (trimmed.length() == 0) { |
441 | *guess = 0.0; |
442 | return true; |
443 | } |
444 | if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { |
445 | *guess = Double::Infinity(); |
446 | return true; |
447 | } |
448 | if (exponent + trimmed.length() <= kMinDecimalPower) { |
449 | *guess = 0.0; |
450 | return true; |
451 | } |
452 | |
453 | if (DoubleStrtod(trimmed, exponent, guess) || |
454 | DiyFpStrtod(trimmed, exponent, guess)) { |
455 | return true; |
456 | } |
457 | if (*guess == Double::Infinity()) { |
458 | return true; |
459 | } |
460 | return false; |
461 | } |
462 | |
463 | #if U_DEBUG // needed for ICU only in debug mode |
464 | static bool IsDigit(const char d) { |
465 | return ('0' <= d) && (d <= '9'); |
466 | } |
467 | |
468 | static bool IsNonZeroDigit(const char d) { |
469 | return ('1' <= d) && (d <= '9'); |
470 | } |
471 | |
472 | static bool AssertTrimmedDigits(const Vector<const char>& buffer) { |
473 | for(int i = 0; i < buffer.length(); ++i) { |
474 | if(!IsDigit(buffer[i])) { |
475 | return false; |
476 | } |
477 | } |
478 | return (buffer.length() == 0) || (IsNonZeroDigit(buffer[0]) && IsNonZeroDigit(buffer[buffer.length()-1])); |
479 | } |
480 | #endif // needed for ICU only in debug mode |
481 | |
482 | double StrtodTrimmed(Vector<const char> trimmed, int exponent) { |
483 | DOUBLE_CONVERSION_ASSERT(trimmed.length() <= kMaxSignificantDecimalDigits); |
484 | DOUBLE_CONVERSION_ASSERT(AssertTrimmedDigits(trimmed)); |
485 | double guess; |
486 | const bool is_correct = ComputeGuess(trimmed, exponent, &guess); |
487 | if (is_correct) { |
488 | return guess; |
489 | } |
490 | DiyFp upper_boundary = Double(guess).UpperBoundary(); |
491 | int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); |
492 | if (comparison < 0) { |
493 | return guess; |
494 | } else if (comparison > 0) { |
495 | return Double(guess).NextDouble(); |
496 | } else if ((Double(guess).Significand() & 1) == 0) { |
497 | // Round towards even. |
498 | return guess; |
499 | } else { |
500 | return Double(guess).NextDouble(); |
501 | } |
502 | } |
503 | |
504 | double Strtod(Vector<const char> buffer, int exponent) { |
505 | char copy_buffer[kMaxSignificantDecimalDigits]; |
506 | Vector<const char> trimmed; |
507 | int updated_exponent; |
508 | TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, |
509 | &trimmed, &updated_exponent); |
510 | return StrtodTrimmed(trimmed, updated_exponent); |
511 | } |
512 | |
513 | static float SanitizedDoubletof(double d) { |
514 | DOUBLE_CONVERSION_ASSERT(d >= 0.0); |
515 | // ASAN has a sanitize check that disallows casting doubles to floats if |
516 | // they are too big. |
517 | // https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks |
518 | // The behavior should be covered by IEEE 754, but some projects use this |
519 | // flag, so work around it. |
520 | float max_finite = 3.4028234663852885981170418348451692544e+38; |
521 | // The half-way point between the max-finite and infinity value. |
522 | // Since infinity has an even significand everything equal or greater than |
523 | // this value should become infinity. |
524 | double half_max_finite_infinity = |
525 | 3.40282356779733661637539395458142568448e+38; |
526 | if (d >= max_finite) { |
527 | if (d >= half_max_finite_infinity) { |
528 | return Single::Infinity(); |
529 | } else { |
530 | return max_finite; |
531 | } |
532 | } else { |
533 | return static_cast<float>(d); |
534 | } |
535 | } |
536 | |
537 | float Strtof(Vector<const char> buffer, int exponent) { |
538 | char copy_buffer[kMaxSignificantDecimalDigits]; |
539 | Vector<const char> trimmed; |
540 | int updated_exponent; |
541 | TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, |
542 | &trimmed, &updated_exponent); |
543 | exponent = updated_exponent; |
544 | |
545 | double double_guess; |
546 | bool is_correct = ComputeGuess(trimmed, exponent, &double_guess); |
547 | |
548 | float float_guess = SanitizedDoubletof(double_guess); |
549 | if (float_guess == double_guess) { |
550 | // This shortcut triggers for integer values. |
551 | return float_guess; |
552 | } |
553 | |
554 | // We must catch double-rounding. Say the double has been rounded up, and is |
555 | // now a boundary of a float, and rounds up again. This is why we have to |
556 | // look at previous too. |
557 | // Example (in decimal numbers): |
558 | // input: 12349 |
559 | // high-precision (4 digits): 1235 |
560 | // low-precision (3 digits): |
561 | // when read from input: 123 |
562 | // when rounded from high precision: 124. |
563 | // To do this we simply look at the neigbors of the correct result and see |
564 | // if they would round to the same float. If the guess is not correct we have |
565 | // to look at four values (since two different doubles could be the correct |
566 | // double). |
567 | |
568 | double double_next = Double(double_guess).NextDouble(); |
569 | double double_previous = Double(double_guess).PreviousDouble(); |
570 | |
571 | float f1 = SanitizedDoubletof(double_previous); |
572 | float f2 = float_guess; |
573 | float f3 = SanitizedDoubletof(double_next); |
574 | float f4; |
575 | if (is_correct) { |
576 | f4 = f3; |
577 | } else { |
578 | double double_next2 = Double(double_next).NextDouble(); |
579 | f4 = SanitizedDoubletof(double_next2); |
580 | } |
581 | (void) f2; // Mark variable as used. |
582 | DOUBLE_CONVERSION_ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4); |
583 | |
584 | // If the guess doesn't lie near a single-precision boundary we can simply |
585 | // return its float-value. |
586 | if (f1 == f4) { |
587 | return float_guess; |
588 | } |
589 | |
590 | DOUBLE_CONVERSION_ASSERT((f1 != f2 && f2 == f3 && f3 == f4) || |
591 | (f1 == f2 && f2 != f3 && f3 == f4) || |
592 | (f1 == f2 && f2 == f3 && f3 != f4)); |
593 | |
594 | // guess and next are the two possible candidates (in the same way that |
595 | // double_guess was the lower candidate for a double-precision guess). |
596 | float guess = f1; |
597 | float next = f4; |
598 | DiyFp upper_boundary; |
599 | if (guess == 0.0f) { |
600 | float min_float = 1e-45f; |
601 | upper_boundary = Double(static_cast<double>(min_float) / 2).AsDiyFp(); |
602 | } else { |
603 | upper_boundary = Single(guess).UpperBoundary(); |
604 | } |
605 | int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); |
606 | if (comparison < 0) { |
607 | return guess; |
608 | } else if (comparison > 0) { |
609 | return next; |
610 | } else if ((Single(guess).Significand() & 1) == 0) { |
611 | // Round towards even. |
612 | return guess; |
613 | } else { |
614 | return next; |
615 | } |
616 | } |
617 | |
618 | } // namespace double_conversion |
619 | |
620 | // ICU PATCH: Close ICU namespace |
621 | U_NAMESPACE_END |
622 | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |
623 | |