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