Line data Source code
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 :
5 : #include <stdint.h>
6 :
7 : #include <cmath>
8 :
9 : #include "src/base/logging.h"
10 : #include "src/utils.h"
11 :
12 : #include "src/double.h"
13 : #include "src/fixed-dtoa.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : // Represents a 128bit type. This class should be replaced by a native type on
19 : // platforms that support 128bit integers.
20 : class UInt128 {
21 : public:
22 : UInt128() : high_bits_(0), low_bits_(0) { }
23 549337 : UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { }
24 :
25 5490241 : void Multiply(uint32_t multiplicand) {
26 : uint64_t accumulator;
27 :
28 5490241 : accumulator = (low_bits_ & kMask32) * multiplicand;
29 : uint32_t part = static_cast<uint32_t>(accumulator & kMask32);
30 5490241 : accumulator >>= 32;
31 5490241 : accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
32 5490241 : low_bits_ = (accumulator << 32) + part;
33 5490241 : accumulator >>= 32;
34 5490241 : accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
35 : part = static_cast<uint32_t>(accumulator & kMask32);
36 5490241 : accumulator >>= 32;
37 5490241 : accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
38 5490241 : high_bits_ = (accumulator << 32) + part;
39 : DCHECK((accumulator >> 32) == 0);
40 5490241 : }
41 :
42 549337 : void Shift(int shift_amount) {
43 : DCHECK(-64 <= shift_amount && shift_amount <= 64);
44 549337 : if (shift_amount == 0) {
45 549337 : return;
46 549337 : } else if (shift_amount == -64) {
47 0 : high_bits_ = low_bits_;
48 0 : low_bits_ = 0;
49 549337 : } else if (shift_amount == 64) {
50 8574 : low_bits_ = high_bits_;
51 8574 : high_bits_ = 0;
52 540763 : } else if (shift_amount <= 0) {
53 0 : high_bits_ <<= -shift_amount;
54 0 : high_bits_ += low_bits_ >> (64 + shift_amount);
55 0 : low_bits_ <<= -shift_amount;
56 : } else {
57 540763 : low_bits_ >>= shift_amount;
58 540763 : low_bits_ += high_bits_ << (64 - shift_amount);
59 540763 : high_bits_ >>= shift_amount;
60 : }
61 : }
62 :
63 : // Modifies *this to *this MOD (2^power).
64 : // Returns *this DIV (2^power).
65 : int DivModPowerOf2(int power) {
66 5490241 : if (power >= 64) {
67 5490241 : int result = static_cast<int>(high_bits_ >> (power - 64));
68 5490241 : high_bits_ -= static_cast<uint64_t>(result) << (power - 64);
69 : return result;
70 : } else {
71 0 : uint64_t part_low = low_bits_ >> power;
72 0 : uint64_t part_high = high_bits_ << (64 - power);
73 0 : int result = static_cast<int>(part_low + part_high);
74 0 : high_bits_ = 0;
75 0 : low_bits_ -= part_low << power;
76 : return result;
77 : }
78 : }
79 :
80 : bool IsZero() const {
81 5490241 : return high_bits_ == 0 && low_bits_ == 0;
82 : }
83 :
84 : int BitAt(int position) {
85 549337 : if (position >= 64) {
86 549337 : return static_cast<int>(high_bits_ >> (position - 64)) & 1;
87 : } else {
88 0 : return static_cast<int>(low_bits_ >> position) & 1;
89 : }
90 : }
91 :
92 : private:
93 : static const uint64_t kMask32 = 0xFFFFFFFF;
94 : // Value == (high_bits_ << 64) + low_bits_
95 : uint64_t high_bits_;
96 : uint64_t low_bits_;
97 : };
98 :
99 :
100 : static const int kDoubleSignificandSize = 53; // Includes the hidden bit.
101 :
102 :
103 : static void FillDigits32FixedLength(uint32_t number, int requested_length,
104 : Vector<char> buffer, int* length) {
105 3787066 : for (int i = requested_length - 1; i >= 0; --i) {
106 7574132 : buffer[(*length) + i] = '0' + number % 10;
107 3787066 : number /= 10;
108 : }
109 568646 : *length += requested_length;
110 : }
111 :
112 :
113 592655 : static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) {
114 : int number_length = 0;
115 : // We fill the digits in reverse order and exchange them afterwards.
116 3329827 : while (number != 0) {
117 2737172 : int digit = number % 10;
118 2737172 : number /= 10;
119 5474344 : buffer[(*length) + number_length] = '0' + digit;
120 2737172 : number_length++;
121 : }
122 : // Exchange the digits.
123 592655 : int i = *length;
124 592655 : int j = *length + number_length - 1;
125 1805653 : while (i < j) {
126 1212998 : char tmp = buffer[i];
127 1212998 : buffer[i] = buffer[j];
128 1212998 : buffer[j] = tmp;
129 1212998 : i++;
130 1212998 : j--;
131 : }
132 592655 : *length += number_length;
133 592655 : }
134 :
135 :
136 48364 : static void FillDigits64FixedLength(uint64_t number, int requested_length,
137 : Vector<char> buffer, int* length) {
138 : const uint32_t kTen7 = 10000000;
139 : // For efficiency cut the number into 3 uint32_t parts, and print those.
140 48364 : uint32_t part2 = static_cast<uint32_t>(number % kTen7);
141 48364 : number /= kTen7;
142 48364 : uint32_t part1 = static_cast<uint32_t>(number % kTen7);
143 48364 : uint32_t part0 = static_cast<uint32_t>(number / kTen7);
144 :
145 : FillDigits32FixedLength(part0, 3, buffer, length);
146 : FillDigits32FixedLength(part1, 7, buffer, length);
147 : FillDigits32FixedLength(part2, 7, buffer, length);
148 48364 : }
149 :
150 :
151 272159 : static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
152 : const uint32_t kTen7 = 10000000;
153 : // For efficiency cut the number into 3 uint32_t parts, and print those.
154 272159 : uint32_t part2 = static_cast<uint32_t>(number % kTen7);
155 272159 : number /= kTen7;
156 272159 : uint32_t part1 = static_cast<uint32_t>(number % kTen7);
157 272159 : uint32_t part0 = static_cast<uint32_t>(number / kTen7);
158 :
159 272159 : if (part0 != 0) {
160 151395 : FillDigits32(part0, buffer, length);
161 : FillDigits32FixedLength(part1, 7, buffer, length);
162 : FillDigits32FixedLength(part2, 7, buffer, length);
163 120764 : } else if (part1 != 0) {
164 120764 : FillDigits32(part1, buffer, length);
165 : FillDigits32FixedLength(part2, 7, buffer, length);
166 : } else {
167 0 : FillDigits32(part2, buffer, length);
168 : }
169 272159 : }
170 :
171 :
172 325904 : static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
173 : // An empty buffer represents 0.
174 325904 : if (*length == 0) {
175 576 : buffer[0] = '1';
176 576 : *decimal_point = 1;
177 576 : *length = 1;
178 : return;
179 : }
180 : // Round the last digit until we either have a digit that was not '9' or until
181 : // we reached the first digit.
182 650656 : buffer[(*length) - 1]++;
183 683054 : for (int i = (*length) - 1; i > 0; --i) {
184 355856 : if (buffer[i] != '0' + 10) {
185 : return;
186 : }
187 32398 : buffer[i] = '0';
188 64796 : buffer[i - 1]++;
189 : }
190 : // If the first digit is now '0' + 10, we would need to set it to '0' and add
191 : // a '1' in front. However we reach the first digit only if all following
192 : // digits had been '9' before rounding up. Now all trailing digits are '0' and
193 : // we simply switch the first digit to '1' and update the decimal-point
194 : // (indicating that the point is now one digit to the right).
195 1870 : if (buffer[0] == '0' + 10) {
196 157 : buffer[0] = '1';
197 157 : (*decimal_point)++;
198 : }
199 : }
200 :
201 :
202 : // The given fractionals number represents a fixed-point number with binary
203 : // point at bit (-exponent).
204 : // Preconditions:
205 : // -128 <= exponent <= 0.
206 : // 0 <= fractionals * 2^exponent < 1
207 : // The buffer holds the result.
208 : // The function will round its result. During the rounding-process digits not
209 : // generated by this function might be updated, and the decimal-point variable
210 : // might be updated. If this function generates the digits 99 and the buffer
211 : // already contained "199" (thus yielding a buffer of "19999") then a
212 : // rounding-up will change the contents of the buffer to "20000".
213 1093349 : static void FillFractionals(uint64_t fractionals, int exponent,
214 : int fractional_count, Vector<char> buffer,
215 : int* length, int* decimal_point) {
216 : DCHECK(-128 <= exponent && exponent <= 0);
217 : // 'fractionals' is a fixed-point number, with binary point at bit
218 : // (-exponent). Inside the function the non-converted remainder of fractionals
219 : // is a fixed-point number, with binary point at bit 'point'.
220 1093349 : if (-exponent <= 64) {
221 : // One 64 bit number is sufficient.
222 : DCHECK(fractionals >> 56 == 0);
223 : int point = -exponent;
224 4805677 : for (int i = 0; i < fractional_count; ++i) {
225 4891486 : if (fractionals == 0) break;
226 : // Instead of multiplying by 10 we multiply by 5 and adjust the point
227 : // location. This way the fractionals variable will not overflow.
228 : // Invariant at the beginning of the loop: fractionals < 2^point.
229 : // Initially we have: point <= 64 and fractionals < 2^56
230 : // After each iteration the point is decremented by one.
231 : // Note that 5^3 = 125 < 128 = 2^7.
232 : // Therefore three iterations of this loop will not overflow fractionals
233 : // (even without the subtraction at the end of the loop body). At this
234 : // time point will satisfy point <= 61 and therefore fractionals < 2^point
235 : // and any further multiplication of fractionals by 5 will not overflow.
236 4805677 : fractionals *= 5;
237 4805677 : point--;
238 4805677 : int digit = static_cast<int>(fractionals >> point);
239 9611354 : buffer[*length] = '0' + digit;
240 4805677 : (*length)++;
241 4805677 : fractionals -= static_cast<uint64_t>(digit) << point;
242 : }
243 : // If the first bit after the point is set we have to round up.
244 544012 : if (((fractionals >> (point - 1)) & 1) == 1) {
245 224057 : RoundUp(buffer, length, decimal_point);
246 : }
247 : } else { // We need 128 bits.
248 : DCHECK(64 < -exponent && -exponent <= 128);
249 : UInt128 fractionals128 = UInt128(fractionals, 0);
250 549337 : fractionals128.Shift(-exponent - 64);
251 : int point = 128;
252 5490241 : for (int i = 0; i < fractional_count; ++i) {
253 5490241 : if (fractionals128.IsZero()) break;
254 : // As before: instead of multiplying by 10 we multiply by 5 and adjust the
255 : // point location.
256 : // This multiplication will not overflow for the same reasons as before.
257 5490241 : fractionals128.Multiply(5);
258 5490241 : point--;
259 : int digit = fractionals128.DivModPowerOf2(point);
260 10980482 : buffer[*length] = '0' + digit;
261 5490241 : (*length)++;
262 : }
263 1098674 : if (fractionals128.BitAt(point - 1) == 1) {
264 101847 : RoundUp(buffer, length, decimal_point);
265 : }
266 : }
267 1093349 : }
268 :
269 :
270 : // Removes leading and trailing zeros.
271 : // If leading zeros are removed then the decimal point position is adjusted.
272 1304829 : static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) {
273 7534058 : while (*length > 0 && buffer[(*length) - 1] == '0') {
274 2671781 : (*length)--;
275 : }
276 : int first_non_zero = 0;
277 5925738 : while (first_non_zero < *length && buffer[first_non_zero] == '0') {
278 1867621 : first_non_zero++;
279 : }
280 1304829 : if (first_non_zero != 0) {
281 1776366 : for (int i = first_non_zero; i < *length; ++i) {
282 3552732 : buffer[i - first_non_zero] = buffer[i];
283 : }
284 263754 : *length -= first_non_zero;
285 263754 : *decimal_point -= first_non_zero;
286 : }
287 1304829 : }
288 :
289 :
290 1304829 : bool FastFixedDtoa(double v,
291 : int fractional_count,
292 : Vector<char> buffer,
293 : int* length,
294 : int* decimal_point) {
295 : const uint32_t kMaxUInt32 = 0xFFFFFFFF;
296 : uint64_t significand = Double(v).Significand();
297 : int exponent = Double(v).Exponent();
298 : // v = significand * 2^exponent (with significand a 53bit integer).
299 : // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we
300 : // don't know how to compute the representation. 2^73 ~= 9.5*10^21.
301 : // If necessary this limit could probably be increased, but we don't need
302 : // more.
303 1304829 : if (exponent > 20) return false;
304 1304829 : if (fractional_count > 20) return false;
305 1304829 : *length = 0;
306 : // At most kDoubleSignificandSize bits of the significand are non-zero.
307 : // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero
308 : // bits: 0..11*..0xxx..53*..xx
309 1304829 : if (exponent + kDoubleSignificandSize > 64) {
310 : // The exponent must be > 11.
311 : //
312 : // We know that v = significand * 2^exponent.
313 : // And the exponent > 11.
314 : // We simplify the task by dividing v by 10^17.
315 : // The quotient delivers the first digits, and the remainder fits into a 64
316 : // bit number.
317 : // Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
318 : const uint64_t kFive17 = V8_2PART_UINT64_C(0xB1, A2BC2EC5); // 5^17
319 : uint64_t divisor = kFive17;
320 : int divisor_power = 17;
321 : uint64_t dividend = significand;
322 : uint32_t quotient;
323 : uint64_t remainder;
324 : // Let v = f * 2^e with f == significand and e == exponent.
325 : // Then need q (quotient) and r (remainder) as follows:
326 : // v = q * 10^17 + r
327 : // f * 2^e = q * 10^17 + r
328 : // f * 2^e = q * 5^17 * 2^17 + r
329 : // If e > 17 then
330 : // f * 2^(e-17) = q * 5^17 + r/2^17
331 : // else
332 : // f = q * 5^17 * 2^(17-e) + r/2^e
333 48364 : if (exponent > divisor_power) {
334 : // We only allow exponents of up to 20 and therefore (17 - e) <= 3
335 7 : dividend <<= exponent - divisor_power;
336 7 : quotient = static_cast<uint32_t>(dividend / divisor);
337 7 : remainder = (dividend % divisor) << divisor_power;
338 : } else {
339 48357 : divisor <<= divisor_power - exponent;
340 48357 : quotient = static_cast<uint32_t>(dividend / divisor);
341 48357 : remainder = (dividend % divisor) << exponent;
342 : }
343 48364 : FillDigits32(quotient, buffer, length);
344 48364 : FillDigits64FixedLength(remainder, divisor_power, buffer, length);
345 48364 : *decimal_point = *length;
346 1256465 : } else if (exponent >= 0) {
347 : // 0 <= exponent <= 11
348 103243 : significand <<= exponent;
349 103243 : FillDigits64(significand, buffer, length);
350 103243 : *decimal_point = *length;
351 1153222 : } else if (exponent > -kDoubleSignificandSize) {
352 : // We have to cut the number.
353 441048 : uint64_t integrals = significand >> -exponent;
354 441048 : uint64_t fractionals = significand - (integrals << -exponent);
355 441048 : if (integrals > kMaxUInt32) {
356 168916 : FillDigits64(integrals, buffer, length);
357 : } else {
358 272132 : FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
359 : }
360 441048 : *decimal_point = *length;
361 : FillFractionals(fractionals, exponent, fractional_count,
362 441048 : buffer, length, decimal_point);
363 712174 : } else if (exponent < -128) {
364 : // This configuration (with at most 20 digits) means that all digits must be
365 : // 0.
366 : DCHECK(fractional_count <= 20);
367 59873 : buffer[0] = '\0';
368 59873 : *length = 0;
369 59873 : *decimal_point = -fractional_count;
370 : } else {
371 652301 : *decimal_point = 0;
372 : FillFractionals(significand, exponent, fractional_count,
373 652301 : buffer, length, decimal_point);
374 : }
375 1304829 : TrimZeros(buffer, length, decimal_point);
376 2609658 : buffer[*length] = '\0';
377 1304829 : if ((*length) == 0) {
378 : // The string is empty and the decimal_point thus has no importance. Mimick
379 : // Gay's dtoa and and set it to -fractional_count.
380 419162 : *decimal_point = -fractional_count;
381 : }
382 : return true;
383 : }
384 :
385 : } // namespace internal
386 : } // namespace v8
|