v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bignum-dtoa.cc
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <cmath>
8
9#include "src/base/logging.h"
12
13namespace v8 {
14namespace base {
15
16static int NormalizedExponent(uint64_t significand, int exponent) {
17 DCHECK_NE(significand, 0);
18 while ((significand & Double::kHiddenBit) == 0) {
19 significand = significand << 1;
20 exponent = exponent - 1;
21 }
22 return exponent;
23}
24
25// Forward declarations:
26// Returns an estimation of k such that 10^(k-1) <= v < 10^k.
27static int EstimatePower(int exponent);
28// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
29// and denominator.
30static void InitialScaledStartValues(double v, int estimated_power,
31 bool need_boundary_deltas,
32 Bignum* numerator, Bignum* denominator,
33 Bignum* delta_minus, Bignum* delta_plus);
34// Multiplies numerator/denominator so that its values lies in the range 1-10.
35// Returns decimal_point s.t.
36// v = numerator'/denominator' * 10^(decimal_point-1)
37// where numerator' and denominator' are the values of numerator and
38// denominator after the call to this function.
39static void FixupMultiply10(int estimated_power, bool is_even,
40 int* decimal_point, Bignum* numerator,
41 Bignum* denominator, Bignum* delta_minus,
42 Bignum* delta_plus);
43// Generates digits from the left to the right and stops when the generated
44// digits yield the shortest decimal representation of v.
45static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
46 Bignum* delta_minus, Bignum* delta_plus,
47 bool is_even, Vector<char> buffer,
48 int* length);
49// Generates 'requested_digits' after the decimal point.
50static void BignumToFixed(int requested_digits, int* decimal_point,
51 Bignum* numerator, Bignum* denominator,
52 Vector<char>(buffer), int* length);
53// Generates 'count' digits of numerator/denominator.
54// Once 'count' digits have been produced rounds the result depending on the
55// remainder (remainders of exactly .5 round upwards). Might update the
56// decimal_point when rounding up (for example for 0.9999).
57static void GenerateCountedDigits(int count, int* decimal_point,
58 Bignum* numerator, Bignum* denominator,
59 Vector<char>(buffer), int* length);
60
61void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
62 Vector<char> buffer, int* length, int* decimal_point) {
63 DCHECK_GT(v, 0);
64 DCHECK(!Double(v).IsSpecial());
65 uint64_t significand = Double(v).Significand();
66 bool is_even = (significand & 1) == 0;
67 int exponent = Double(v).Exponent();
68 int normalized_exponent = NormalizedExponent(significand, exponent);
69 // estimated_power might be too low by 1.
70 int estimated_power = EstimatePower(normalized_exponent);
71
72 // Shortcut for Fixed.
73 // The requested digits correspond to the digits after the point. If the
74 // number is much too small, then there is no need in trying to get any
75 // digits.
76 if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) {
77 buffer[0] = '\0';
78 *length = 0;
79 // Set decimal-point to -requested_digits. This is what Gay does.
80 // Note that it should not have any effect anyways since the string is
81 // empty.
82 *decimal_point = -requested_digits;
83 return;
84 }
85
86 Bignum numerator;
87 Bignum denominator;
88 Bignum delta_minus;
89 Bignum delta_plus;
90 // Make sure the bignum can grow large enough. The smallest double equals
91 // 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
92 // The maximum double is 1.7976931348623157e308 which needs fewer than
93 // 308*4 binary digits.
95 bool need_boundary_deltas = (mode == BIGNUM_DTOA_SHORTEST);
96 InitialScaledStartValues(v, estimated_power, need_boundary_deltas, &numerator,
97 &denominator, &delta_minus, &delta_plus);
98 // We now have v = (numerator / denominator) * 10^estimated_power.
99 FixupMultiply10(estimated_power, is_even, decimal_point, &numerator,
100 &denominator, &delta_minus, &delta_plus);
101 // We now have v = (numerator / denominator) * 10^(decimal_point-1), and
102 // 1 <= (numerator + delta_plus) / denominator < 10
103 switch (mode) {
105 GenerateShortestDigits(&numerator, &denominator, &delta_minus,
106 &delta_plus, is_even, buffer, length);
107 break;
109 BignumToFixed(requested_digits, decimal_point, &numerator, &denominator,
110 buffer, length);
111 break;
113 GenerateCountedDigits(requested_digits, decimal_point, &numerator,
114 &denominator, buffer, length);
115 break;
116 default:
117 UNREACHABLE();
118 }
119 buffer[*length] = '\0';
120}
121
122// The procedure starts generating digits from the left to the right and stops
123// when the generated digits yield the shortest decimal representation of v. A
124// decimal representation of v is a number lying closer to v than to any other
125// double, so it converts to v when read.
126//
127// This is true if d, the decimal representation, is between m- and m+, the
128// upper and lower boundaries. d must be strictly between them if !is_even.
129// m- := (numerator - delta_minus) / denominator
130// m+ := (numerator + delta_plus) / denominator
131//
132// Precondition: 0 <= (numerator+delta_plus) / denominator < 10.
133// If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit
134// will be produced. This should be the standard precondition.
135static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
136 Bignum* delta_minus, Bignum* delta_plus,
137 bool is_even, Vector<char> buffer,
138 int* length) {
139 // Small optimization: if delta_minus and delta_plus are the same just reuse
140 // one of the two bignums.
141 if (Bignum::Equal(*delta_minus, *delta_plus)) {
142 delta_plus = delta_minus;
143 }
144 *length = 0;
145 while (true) {
146 uint16_t digit;
147 digit = numerator->DivideModuloIntBignum(*denominator);
148 DCHECK_LE(digit, 9); // digit is a uint16_t and therefore always positive.
149 // digit = numerator / denominator (integer division).
150 // numerator = numerator % denominator.
151 buffer[(*length)++] = digit + '0';
152
153 // Can we stop already?
154 // If the remainder of the division is less than the distance to the lower
155 // boundary we can stop. In this case we simply round down (discarding the
156 // remainder).
157 // Similarly we test if we can round up (using the upper boundary).
158 bool in_delta_room_minus;
159 bool in_delta_room_plus;
160 if (is_even) {
161 in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus);
162 } else {
163 in_delta_room_minus = Bignum::Less(*numerator, *delta_minus);
164 }
165 if (is_even) {
166 in_delta_room_plus =
167 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
168 } else {
169 in_delta_room_plus =
170 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
171 }
172 if (!in_delta_room_minus && !in_delta_room_plus) {
173 // Prepare for next iteration.
174 numerator->Times10();
175 delta_minus->Times10();
176 // We optimized delta_plus to be equal to delta_minus (if they share the
177 // same value). So don't multiply delta_plus if they point to the same
178 // object.
179 if (delta_minus != delta_plus) {
180 delta_plus->Times10();
181 }
182 } else if (in_delta_room_minus && in_delta_room_plus) {
183 // Let's see if 2*numerator < denominator.
184 // If yes, then the next digit would be < 5 and we can round down.
185 int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator);
186 if (compare < 0) {
187 // Remaining digits are less than .5. -> Round down (== do nothing).
188 } else if (compare > 0) {
189 // Remaining digits are more than .5 of denominator. -> Round up.
190 // Note that the last digit could not be a '9' as otherwise the whole
191 // loop would have stopped earlier.
192 // We still have an assert here in case the preconditions were not
193 // satisfied.
194 DCHECK_NE(buffer[(*length) - 1], '9');
195 buffer[(*length) - 1]++;
196 } else {
197 // Halfway case.
198 // TODO(floitsch): need a way to solve half-way cases.
199 // For now let's round towards even (since this is what Gay seems to
200 // do).
201
202 if ((buffer[(*length) - 1] - '0') % 2 == 0) {
203 // Round down => Do nothing.
204 } else {
205 DCHECK_NE(buffer[(*length) - 1], '9');
206 buffer[(*length) - 1]++;
207 }
208 }
209 return;
210 } else if (in_delta_room_minus) {
211 // Round down (== do nothing).
212 return;
213 } else { // in_delta_room_plus
214 // Round up.
215 // Note again that the last digit could not be '9' since this would have
216 // stopped the loop earlier.
217 // We still have an DCHECK here, in case the preconditions were not
218 // satisfied.
219 DCHECK_NE(buffer[(*length) - 1], '9');
220 buffer[(*length) - 1]++;
221 return;
222 }
223 }
224}
225
226// Let v = numerator / denominator < 10.
227// Then we generate 'count' digits of d = x.xxxxx... (without the decimal point)
228// from left to right. Once 'count' digits have been produced we decide wether
229// to round up or down. Remainders of exactly .5 round upwards. Numbers such
230// as 9.999999 propagate a carry all the way, and change the
231// exponent (decimal_point), when rounding upwards.
232static void GenerateCountedDigits(int count, int* decimal_point,
233 Bignum* numerator, Bignum* denominator,
234 Vector<char>(buffer), int* length) {
235 DCHECK_GE(count, 0);
236 for (int i = 0; i < count - 1; ++i) {
237 uint16_t digit;
238 digit = numerator->DivideModuloIntBignum(*denominator);
239 DCHECK_LE(digit, 9); // digit is a uint16_t and therefore always positive.
240 // digit = numerator / denominator (integer division).
241 // numerator = numerator % denominator.
242 buffer[i] = digit + '0';
243 // Prepare for next iteration.
244 numerator->Times10();
245 }
246 // Generate the last digit.
247 uint16_t digit;
248 digit = numerator->DivideModuloIntBignum(*denominator);
249 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
250 digit++;
251 }
252 buffer[count - 1] = digit + '0';
253 // Correct bad digits (in case we had a sequence of '9's). Propagate the
254 // carry until we hat a non-'9' or til we reach the first digit.
255 for (int i = count - 1; i > 0; --i) {
256 if (buffer[i] != '0' + 10) break;
257 buffer[i] = '0';
258 buffer[i - 1]++;
259 }
260 if (buffer[0] == '0' + 10) {
261 // Propagate a carry past the top place.
262 buffer[0] = '1';
263 (*decimal_point)++;
264 }
265 *length = count;
266}
267
268// Generates 'requested_digits' after the decimal point. It might omit
269// trailing '0's. If the input number is too small then no digits at all are
270// generated (ex.: 2 fixed digits for 0.00001).
271//
272// Input verifies: 1 <= (numerator + delta) / denominator < 10.
273static void BignumToFixed(int requested_digits, int* decimal_point,
274 Bignum* numerator, Bignum* denominator,
275 Vector<char>(buffer), int* length) {
276 // Note that we have to look at more than just the requested_digits, since
277 // a number could be rounded up. Example: v=0.5 with requested_digits=0.
278 // Even though the power of v equals 0 we can't just stop here.
279 if (-(*decimal_point) > requested_digits) {
280 // The number is definitively too small.
281 // Ex: 0.001 with requested_digits == 1.
282 // Set decimal-point to -requested_digits. This is what Gay does.
283 // Note that it should not have any effect anyways since the string is
284 // empty.
285 *decimal_point = -requested_digits;
286 *length = 0;
287 return;
288 } else if (-(*decimal_point) == requested_digits) {
289 // We only need to verify if the number rounds down or up.
290 // Ex: 0.04 and 0.06 with requested_digits == 1.
291 DCHECK(*decimal_point == -requested_digits);
292 // Initially the fraction lies in range (1, 10]. Multiply the denominator
293 // by 10 so that we can compare more easily.
294 denominator->Times10();
295 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
296 // If the fraction is >= 0.5 then we have to include the rounded
297 // digit.
298 buffer[0] = '1';
299 *length = 1;
300 (*decimal_point)++;
301 } else {
302 // Note that we caught most of similar cases earlier.
303 *length = 0;
304 }
305 return;
306 } else {
307 // The requested digits correspond to the digits after the point.
308 // The variable 'needed_digits' includes the digits before the point.
309 int needed_digits = (*decimal_point) + requested_digits;
310 GenerateCountedDigits(needed_digits, decimal_point, numerator, denominator,
311 buffer, length);
312 }
313}
314
315// Returns an estimation of k such that 10^(k-1) <= v < 10^k where
316// v = f * 2^exponent and 2^52 <= f < 2^53.
317// v is hence a normalized double with the given exponent. The output is an
318// approximation for the exponent of the decimal approimation .digits * 10^k.
319//
320// The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
321// Note: this property holds for v's upper boundary m+ too.
322// 10^k <= m+ < 10^k+1.
323// (see explanation below).
324//
325// Examples:
326// EstimatePower(0) => 16
327// EstimatePower(-52) => 0
328//
329// Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0.
330static int EstimatePower(int exponent) {
331 // This function estimates log10 of v where v = f*2^e (with e == exponent).
332 // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)).
333 // Note that f is bounded by its container size. Let p = 53 (the double's
334 // significand size). Then 2^(p-1) <= f < 2^p.
335 //
336 // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close
337 // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)).
338 // The computed number undershoots by less than 0.631 (when we compute log3
339 // and not log10).
340 //
341 // Optimization: since we only need an approximated result this computation
342 // can be performed on 64 bit integers. On x86/x64 architecture the speedup is
343 // not really measurable, though.
344 //
345 // Since we want to avoid overshooting we decrement by 1e10 so that
346 // floating-point imprecisions don't affect us.
347 //
348 // Explanation for v's boundary m+: the computation takes advantage of
349 // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement
350 // (even for denormals where the delta can be much more important).
351
352 const double k1Log10 = 0.30102999566398114; // 1/lg(10)
353
354 // For doubles len(f) == 53 (don't forget the hidden bit).
355 const int kSignificandSize = 53;
356 double estimate =
357 std::ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10);
358 return static_cast<int>(estimate);
359}
360
361// See comments for InitialScaledStartValues.
363 double v, int estimated_power, bool need_boundary_deltas, Bignum* numerator,
364 Bignum* denominator, Bignum* delta_minus, Bignum* delta_plus) {
365 // A positive exponent implies a positive power.
366 DCHECK_GE(estimated_power, 0);
367 // Since the estimated_power is positive we simply multiply the denominator
368 // by 10^estimated_power.
369
370 // numerator = v.
371 numerator->AssignUInt64(Double(v).Significand());
372 numerator->ShiftLeft(Double(v).Exponent());
373 // denominator = 10^estimated_power.
374 denominator->AssignPowerUInt16(10, estimated_power);
375
376 if (need_boundary_deltas) {
377 // Introduce a common denominator so that the deltas to the boundaries are
378 // integers.
379 denominator->ShiftLeft(1);
380 numerator->ShiftLeft(1);
381 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
382 // denominator (of 2) delta_plus equals 2^e.
383 delta_plus->AssignUInt16(1);
384 delta_plus->ShiftLeft(Double(v).Exponent());
385 // Same for delta_minus (with adjustments below if f == 2^p-1).
386 delta_minus->AssignUInt16(1);
387 delta_minus->ShiftLeft(Double(v).Exponent());
388
389 // If the significand (without the hidden bit) is 0, then the lower
390 // boundary is closer than just half a ulp (unit in the last place).
391 // There is only one exception: if the next lower number is a denormal then
392 // the distance is 1 ulp. This cannot be the case for exponent >= 0 (but we
393 // have to test it in the other function where exponent < 0).
394 uint64_t v_bits = Double(v).AsUint64();
395 if ((v_bits & Double::kSignificandMask) == 0) {
396 // The lower boundary is closer at half the distance of "normal" numbers.
397 // Increase the common denominator and adapt all but the delta_minus.
398 denominator->ShiftLeft(1); // *2
399 numerator->ShiftLeft(1); // *2
400 delta_plus->ShiftLeft(1); // *2
401 }
402 }
403}
404
405// See comments for InitialScaledStartValues
407 double v, int estimated_power, bool need_boundary_deltas, Bignum* numerator,
408 Bignum* denominator, Bignum* delta_minus, Bignum* delta_plus) {
409 uint64_t significand = Double(v).Significand();
410 int exponent = Double(v).Exponent();
411 // v = f * 2^e with e < 0, and with estimated_power >= 0.
412 // This means that e is close to 0 (have a look at how estimated_power is
413 // computed).
414
415 // numerator = significand
416 // since v = significand * 2^exponent this is equivalent to
417 // numerator = v * / 2^-exponent
418 numerator->AssignUInt64(significand);
419 // denominator = 10^estimated_power * 2^-exponent (with exponent < 0)
420 denominator->AssignPowerUInt16(10, estimated_power);
421 denominator->ShiftLeft(-exponent);
422
423 if (need_boundary_deltas) {
424 // Introduce a common denominator so that the deltas to the boundaries are
425 // integers.
426 denominator->ShiftLeft(1);
427 numerator->ShiftLeft(1);
428 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
429 // denominator (of 2) delta_plus equals 2^e.
430 // Given that the denominator already includes v's exponent the distance
431 // to the boundaries is simply 1.
432 delta_plus->AssignUInt16(1);
433 // Same for delta_minus (with adjustments below if f == 2^p-1).
434 delta_minus->AssignUInt16(1);
435
436 // If the significand (without the hidden bit) is 0, then the lower
437 // boundary is closer than just one ulp (unit in the last place).
438 // There is only one exception: if the next lower number is a denormal
439 // then the distance is 1 ulp. Since the exponent is close to zero
440 // (otherwise estimated_power would have been negative) this cannot happen
441 // here either.
442 uint64_t v_bits = Double(v).AsUint64();
443 if ((v_bits & Double::kSignificandMask) == 0) {
444 // The lower boundary is closer at half the distance of "normal" numbers.
445 // Increase the denominator and adapt all but the delta_minus.
446 denominator->ShiftLeft(1); // *2
447 numerator->ShiftLeft(1); // *2
448 delta_plus->ShiftLeft(1); // *2
449 }
450 }
451}
452
453// See comments for InitialScaledStartValues
455 double v, int estimated_power, bool need_boundary_deltas, Bignum* numerator,
456 Bignum* denominator, Bignum* delta_minus, Bignum* delta_plus) {
457 const uint64_t kMinimalNormalizedExponent = 0x0010'0000'0000'0000;
458 uint64_t significand = Double(v).Significand();
459 int exponent = Double(v).Exponent();
460 // Instead of multiplying the denominator with 10^estimated_power we
461 // multiply all values (numerator and deltas) by 10^-estimated_power.
462
463 // Use numerator as temporary container for power_ten.
464 Bignum* power_ten = numerator;
465 power_ten->AssignPowerUInt16(10, -estimated_power);
466
467 if (need_boundary_deltas) {
468 // Since power_ten == numerator we must make a copy of 10^estimated_power
469 // before we complete the computation of the numerator.
470 // delta_plus = delta_minus = 10^estimated_power
471 delta_plus->AssignBignum(*power_ten);
472 delta_minus->AssignBignum(*power_ten);
473 }
474
475 // numerator = significand * 2 * 10^-estimated_power
476 // since v = significand * 2^exponent this is equivalent to
477 // numerator = v * 10^-estimated_power * 2 * 2^-exponent.
478 // Remember: numerator has been abused as power_ten. So no need to assign it
479 // to itself.
480 DCHECK(numerator == power_ten);
481 numerator->MultiplyByUInt64(significand);
482
483 // denominator = 2 * 2^-exponent with exponent < 0.
484 denominator->AssignUInt16(1);
485 denominator->ShiftLeft(-exponent);
486
487 if (need_boundary_deltas) {
488 // Introduce a common denominator so that the deltas to the boundaries are
489 // integers.
490 numerator->ShiftLeft(1);
491 denominator->ShiftLeft(1);
492 // With this shift the boundaries have their correct value, since
493 // delta_plus = 10^-estimated_power, and
494 // delta_minus = 10^-estimated_power.
495 // These assignments have been done earlier.
496
497 // The special case where the lower boundary is twice as close.
498 // This time we have to look out for the exception too.
499 uint64_t v_bits = Double(v).AsUint64();
500 if ((v_bits & Double::kSignificandMask) == 0 &&
501 // The only exception where a significand == 0 has its boundaries at
502 // "normal" distances:
503 (v_bits & Double::kExponentMask) != kMinimalNormalizedExponent) {
504 numerator->ShiftLeft(1); // *2
505 denominator->ShiftLeft(1); // *2
506 delta_plus->ShiftLeft(1); // *2
507 }
508 }
509}
510
511// Let v = significand * 2^exponent.
512// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
513// and denominator. The functions GenerateShortestDigits and
514// GenerateCountedDigits will then convert this ratio to its decimal
515// representation d, with the required accuracy.
516// Then d * 10^estimated_power is the representation of v.
517// (Note: the fraction and the estimated_power might get adjusted before
518// generating the decimal representation.)
519//
520// The initial start values consist of:
521// - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power.
522// - a scaled (common) denominator.
523// optionally (used by GenerateShortestDigits to decide if it has the shortest
524// decimal converting back to v):
525// - v - m-: the distance to the lower boundary.
526// - m+ - v: the distance to the upper boundary.
527//
528// v, m+, m-, and therefore v - m- and m+ - v all share the same denominator.
529//
530// Let ep == estimated_power, then the returned values will satisfy:
531// v / 10^ep = numerator / denominator.
532// v's boundarys m- and m+:
533// m- / 10^ep == v / 10^ep - delta_minus / denominator
534// m+ / 10^ep == v / 10^ep + delta_plus / denominator
535// Or in other words:
536// m- == v - delta_minus * 10^ep / denominator;
537// m+ == v + delta_plus * 10^ep / denominator;
538//
539// Since 10^(k-1) <= v < 10^k (with k == estimated_power)
540// or 10^k <= v < 10^(k+1)
541// we then have 0.1 <= numerator/denominator < 1
542// or 1 <= numerator/denominator < 10
543//
544// It is then easy to kickstart the digit-generation routine.
545//
546// The boundary-deltas are only filled if need_boundary_deltas is set.
547static void InitialScaledStartValues(double v, int estimated_power,
548 bool need_boundary_deltas,
549 Bignum* numerator, Bignum* denominator,
550 Bignum* delta_minus, Bignum* delta_plus) {
551 if (Double(v).Exponent() >= 0) {
553 v, estimated_power, need_boundary_deltas, numerator, denominator,
554 delta_minus, delta_plus);
555 } else if (estimated_power >= 0) {
557 v, estimated_power, need_boundary_deltas, numerator, denominator,
558 delta_minus, delta_plus);
559 } else {
561 v, estimated_power, need_boundary_deltas, numerator, denominator,
562 delta_minus, delta_plus);
563 }
564}
565
566// This routine multiplies numerator/denominator so that its values lies in the
567// range 1-10. That is after a call to this function we have:
568// 1 <= (numerator + delta_plus) /denominator < 10.
569// Let numerator the input before modification and numerator' the argument
570// after modification, then the output-parameter decimal_point is such that
571// numerator / denominator * 10^estimated_power ==
572// numerator' / denominator' * 10^(decimal_point - 1)
573// In some cases estimated_power was too low, and this is already the case. We
574// then simply adjust the power so that 10^(k-1) <= v < 10^k (with k ==
575// estimated_power) but do not touch the numerator or denominator.
576// Otherwise the routine multiplies the numerator and the deltas by 10.
577static void FixupMultiply10(int estimated_power, bool is_even,
578 int* decimal_point, Bignum* numerator,
579 Bignum* denominator, Bignum* delta_minus,
580 Bignum* delta_plus) {
581 bool in_range;
582 if (is_even) {
583 // For IEEE doubles half-way cases (in decimal system numbers ending with 5)
584 // are rounded to the closest floating-point number with even significand.
585 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
586 } else {
587 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
588 }
589 if (in_range) {
590 // Since numerator + delta_plus >= denominator we already have
591 // 1 <= numerator/denominator < 10. Simply update the estimated_power.
592 *decimal_point = estimated_power + 1;
593 } else {
594 *decimal_point = estimated_power;
595 numerator->Times10();
596 if (Bignum::Equal(*delta_minus, *delta_plus)) {
597 delta_minus->Times10();
598 delta_plus->AssignBignum(*delta_minus);
599 } else {
600 delta_minus->Times10();
601 delta_plus->Times10();
602 }
603 }
604}
605
606} // namespace base
607} // namespace v8
void AssignUInt16(uint16_t value)
Definition bignum.cc:25
static bool LessEqual(const Bignum &a, const Bignum &b)
Definition bignum.h:56
uint16_t DivideModuloIntBignum(const Bignum &other)
Definition bignum.cc:448
void Times10()
Definition bignum.h:43
void MultiplyByUInt64(uint64_t factor)
Definition bignum.cc:239
void AssignUInt64(uint64_t value)
Definition bignum.cc:35
static bool Equal(const Bignum &a, const Bignum &b)
Definition bignum.h:53
static const int kMaxSignificantBits
Definition bignum.h:18
void AssignBignum(const Bignum &other)
Definition bignum.cc:51
void ShiftLeft(int shift_amount)
Definition bignum.cc:206
static bool Less(const Bignum &a, const Bignum &b)
Definition bignum.h:59
void AssignPowerUInt16(uint16_t base, int exponent)
Definition bignum.cc:375
static int PlusCompare(const Bignum &a, const Bignum &b, const Bignum &c)
Definition bignum.cc:584
constexpr uint64_t AsUint64() const
Definition double.h:66
static constexpr uint64_t kSignificandMask
Definition double.h:29
static constexpr uint64_t kExponentMask
Definition double.h:28
static constexpr uint64_t kHiddenBit
Definition double.h:30
constexpr int Exponent() const
Definition double.h:82
constexpr uint64_t Significand() const
Definition double.h:91
uint32_t count
static void InitialScaledStartValuesPositiveExponent(double v, int estimated_power, bool need_boundary_deltas, Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus)
static void BignumToFixed(int requested_digits, int *decimal_point, Bignum *numerator, Bignum *denominator, Vector< char >(buffer), int *length)
static void GenerateShortestDigits(Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus, bool is_even, Vector< char > buffer, int *length)
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
static int NormalizedExponent(uint64_t significand, int exponent)
static int EstimatePower(int exponent)
static void GenerateCountedDigits(int count, int *decimal_point, Bignum *numerator, Bignum *denominator, Vector< char >(buffer), int *length)
static void InitialScaledStartValuesNegativeExponentNegativePower(double v, int estimated_power, bool need_boundary_deltas, Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus)
static void InitialScaledStartValues(double v, int estimated_power, bool need_boundary_deltas, Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus)
static void InitialScaledStartValuesNegativeExponentPositivePower(double v, int estimated_power, bool need_boundary_deltas, Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus)
static void FixupMultiply10(int estimated_power, bool is_even, int *decimal_point, Bignum *numerator, Bignum *denominator, Bignum *delta_minus, Bignum *delta_plus)
@ BIGNUM_DTOA_SHORTEST
Definition bignum-dtoa.h:17
@ BIGNUM_DTOA_FIXED
Definition bignum-dtoa.h:21
@ BIGNUM_DTOA_PRECISION
Definition bignum-dtoa.h:23
uint32_t compare
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_GT(v1, v2)
Definition logging.h:487