Bumping manifests a=b2g-bump
[gecko.git] / mfbt / double-conversion / fixed-dtoa.cc
blobd56b1449b264cc78e70c75c257d35ffeeb3b95e7
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <math.h>
30 #include "fixed-dtoa.h"
31 #include "ieee.h"
33 namespace double_conversion {
35 // Represents a 128bit type. This class should be replaced by a native type on
36 // platforms that support 128bit integers.
37 class UInt128 {
38 public:
39 UInt128() : high_bits_(0), low_bits_(0) { }
40 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { }
42 void Multiply(uint32_t multiplicand) {
43 uint64_t accumulator;
45 accumulator = (low_bits_ & kMask32) * multiplicand;
46 uint32_t part = static_cast<uint32_t>(accumulator & kMask32);
47 accumulator >>= 32;
48 accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
49 low_bits_ = (accumulator << 32) + part;
50 accumulator >>= 32;
51 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
52 part = static_cast<uint32_t>(accumulator & kMask32);
53 accumulator >>= 32;
54 accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
55 high_bits_ = (accumulator << 32) + part;
56 ASSERT((accumulator >> 32) == 0);
59 void Shift(int shift_amount) {
60 ASSERT(-64 <= shift_amount && shift_amount <= 64);
61 if (shift_amount == 0) {
62 return;
63 } else if (shift_amount == -64) {
64 high_bits_ = low_bits_;
65 low_bits_ = 0;
66 } else if (shift_amount == 64) {
67 low_bits_ = high_bits_;
68 high_bits_ = 0;
69 } else if (shift_amount <= 0) {
70 high_bits_ <<= -shift_amount;
71 high_bits_ += low_bits_ >> (64 + shift_amount);
72 low_bits_ <<= -shift_amount;
73 } else {
74 low_bits_ >>= shift_amount;
75 low_bits_ += high_bits_ << (64 - shift_amount);
76 high_bits_ >>= shift_amount;
80 // Modifies *this to *this MOD (2^power).
81 // Returns *this DIV (2^power).
82 int DivModPowerOf2(int power) {
83 if (power >= 64) {
84 int result = static_cast<int>(high_bits_ >> (power - 64));
85 high_bits_ -= static_cast<uint64_t>(result) << (power - 64);
86 return result;
87 } else {
88 uint64_t part_low = low_bits_ >> power;
89 uint64_t part_high = high_bits_ << (64 - power);
90 int result = static_cast<int>(part_low + part_high);
91 high_bits_ = 0;
92 low_bits_ -= part_low << power;
93 return result;
97 bool IsZero() const {
98 return high_bits_ == 0 && low_bits_ == 0;
101 int BitAt(int position) {
102 if (position >= 64) {
103 return static_cast<int>(high_bits_ >> (position - 64)) & 1;
104 } else {
105 return static_cast<int>(low_bits_ >> position) & 1;
109 private:
110 static const uint64_t kMask32 = 0xFFFFFFFF;
111 // Value == (high_bits_ << 64) + low_bits_
112 uint64_t high_bits_;
113 uint64_t low_bits_;
117 static const int kDoubleSignificandSize = 53; // Includes the hidden bit.
120 static void FillDigits32FixedLength(uint32_t number, int requested_length,
121 Vector<char> buffer, int* length) {
122 for (int i = requested_length - 1; i >= 0; --i) {
123 buffer[(*length) + i] = '0' + number % 10;
124 number /= 10;
126 *length += requested_length;
130 static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) {
131 int number_length = 0;
132 // We fill the digits in reverse order and exchange them afterwards.
133 while (number != 0) {
134 int digit = number % 10;
135 number /= 10;
136 buffer[(*length) + number_length] = '0' + digit;
137 number_length++;
139 // Exchange the digits.
140 int i = *length;
141 int j = *length + number_length - 1;
142 while (i < j) {
143 char tmp = buffer[i];
144 buffer[i] = buffer[j];
145 buffer[j] = tmp;
146 i++;
147 j--;
149 *length += number_length;
153 static void FillDigits64FixedLength(uint64_t number, int requested_length,
154 Vector<char> buffer, int* length) {
155 const uint32_t kTen7 = 10000000;
156 // For efficiency cut the number into 3 uint32_t parts, and print those.
157 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
158 number /= kTen7;
159 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
160 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
162 FillDigits32FixedLength(part0, 3, buffer, length);
163 FillDigits32FixedLength(part1, 7, buffer, length);
164 FillDigits32FixedLength(part2, 7, buffer, length);
168 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
169 const uint32_t kTen7 = 10000000;
170 // For efficiency cut the number into 3 uint32_t parts, and print those.
171 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
172 number /= kTen7;
173 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
174 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
176 if (part0 != 0) {
177 FillDigits32(part0, buffer, length);
178 FillDigits32FixedLength(part1, 7, buffer, length);
179 FillDigits32FixedLength(part2, 7, buffer, length);
180 } else if (part1 != 0) {
181 FillDigits32(part1, buffer, length);
182 FillDigits32FixedLength(part2, 7, buffer, length);
183 } else {
184 FillDigits32(part2, buffer, length);
189 static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
190 // An empty buffer represents 0.
191 if (*length == 0) {
192 buffer[0] = '1';
193 *decimal_point = 1;
194 *length = 1;
195 return;
197 // Round the last digit until we either have a digit that was not '9' or until
198 // we reached the first digit.
199 buffer[(*length) - 1]++;
200 for (int i = (*length) - 1; i > 0; --i) {
201 if (buffer[i] != '0' + 10) {
202 return;
204 buffer[i] = '0';
205 buffer[i - 1]++;
207 // If the first digit is now '0' + 10, we would need to set it to '0' and add
208 // a '1' in front. However we reach the first digit only if all following
209 // digits had been '9' before rounding up. Now all trailing digits are '0' and
210 // we simply switch the first digit to '1' and update the decimal-point
211 // (indicating that the point is now one digit to the right).
212 if (buffer[0] == '0' + 10) {
213 buffer[0] = '1';
214 (*decimal_point)++;
219 // The given fractionals number represents a fixed-point number with binary
220 // point at bit (-exponent).
221 // Preconditions:
222 // -128 <= exponent <= 0.
223 // 0 <= fractionals * 2^exponent < 1
224 // The buffer holds the result.
225 // The function will round its result. During the rounding-process digits not
226 // generated by this function might be updated, and the decimal-point variable
227 // might be updated. If this function generates the digits 99 and the buffer
228 // already contained "199" (thus yielding a buffer of "19999") then a
229 // rounding-up will change the contents of the buffer to "20000".
230 static void FillFractionals(uint64_t fractionals, int exponent,
231 int fractional_count, Vector<char> buffer,
232 int* length, int* decimal_point) {
233 ASSERT(-128 <= exponent && exponent <= 0);
234 // 'fractionals' is a fixed-point number, with binary point at bit
235 // (-exponent). Inside the function the non-converted remainder of fractionals
236 // is a fixed-point number, with binary point at bit 'point'.
237 if (-exponent <= 64) {
238 // One 64 bit number is sufficient.
239 ASSERT(fractionals >> 56 == 0);
240 int point = -exponent;
241 for (int i = 0; i < fractional_count; ++i) {
242 if (fractionals == 0) break;
243 // Instead of multiplying by 10 we multiply by 5 and adjust the point
244 // location. This way the fractionals variable will not overflow.
245 // Invariant at the beginning of the loop: fractionals < 2^point.
246 // Initially we have: point <= 64 and fractionals < 2^56
247 // After each iteration the point is decremented by one.
248 // Note that 5^3 = 125 < 128 = 2^7.
249 // Therefore three iterations of this loop will not overflow fractionals
250 // (even without the subtraction at the end of the loop body). At this
251 // time point will satisfy point <= 61 and therefore fractionals < 2^point
252 // and any further multiplication of fractionals by 5 will not overflow.
253 fractionals *= 5;
254 point--;
255 int digit = static_cast<int>(fractionals >> point);
256 buffer[*length] = '0' + digit;
257 (*length)++;
258 fractionals -= static_cast<uint64_t>(digit) << point;
260 // If the first bit after the point is set we have to round up.
261 if (((fractionals >> (point - 1)) & 1) == 1) {
262 RoundUp(buffer, length, decimal_point);
264 } else { // We need 128 bits.
265 ASSERT(64 < -exponent && -exponent <= 128);
266 UInt128 fractionals128 = UInt128(fractionals, 0);
267 fractionals128.Shift(-exponent - 64);
268 int point = 128;
269 for (int i = 0; i < fractional_count; ++i) {
270 if (fractionals128.IsZero()) break;
271 // As before: instead of multiplying by 10 we multiply by 5 and adjust the
272 // point location.
273 // This multiplication will not overflow for the same reasons as before.
274 fractionals128.Multiply(5);
275 point--;
276 int digit = fractionals128.DivModPowerOf2(point);
277 buffer[*length] = '0' + digit;
278 (*length)++;
280 if (fractionals128.BitAt(point - 1) == 1) {
281 RoundUp(buffer, length, decimal_point);
287 // Removes leading and trailing zeros.
288 // If leading zeros are removed then the decimal point position is adjusted.
289 static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) {
290 while (*length > 0 && buffer[(*length) - 1] == '0') {
291 (*length)--;
293 int first_non_zero = 0;
294 while (first_non_zero < *length && buffer[first_non_zero] == '0') {
295 first_non_zero++;
297 if (first_non_zero != 0) {
298 for (int i = first_non_zero; i < *length; ++i) {
299 buffer[i - first_non_zero] = buffer[i];
301 *length -= first_non_zero;
302 *decimal_point -= first_non_zero;
307 bool FastFixedDtoa(double v,
308 int fractional_count,
309 Vector<char> buffer,
310 int* length,
311 int* decimal_point) {
312 const uint32_t kMaxUInt32 = 0xFFFFFFFF;
313 uint64_t significand = Double(v).Significand();
314 int exponent = Double(v).Exponent();
315 // v = significand * 2^exponent (with significand a 53bit integer).
316 // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we
317 // don't know how to compute the representation. 2^73 ~= 9.5*10^21.
318 // If necessary this limit could probably be increased, but we don't need
319 // more.
320 if (exponent > 20) return false;
321 if (fractional_count > 20) return false;
322 *length = 0;
323 // At most kDoubleSignificandSize bits of the significand are non-zero.
324 // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero
325 // bits: 0..11*..0xxx..53*..xx
326 if (exponent + kDoubleSignificandSize > 64) {
327 // The exponent must be > 11.
329 // We know that v = significand * 2^exponent.
330 // And the exponent > 11.
331 // We simplify the task by dividing v by 10^17.
332 // The quotient delivers the first digits, and the remainder fits into a 64
333 // bit number.
334 // Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
335 const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17
336 uint64_t divisor = kFive17;
337 int divisor_power = 17;
338 uint64_t dividend = significand;
339 uint32_t quotient;
340 uint64_t remainder;
341 // Let v = f * 2^e with f == significand and e == exponent.
342 // Then need q (quotient) and r (remainder) as follows:
343 // v = q * 10^17 + r
344 // f * 2^e = q * 10^17 + r
345 // f * 2^e = q * 5^17 * 2^17 + r
346 // If e > 17 then
347 // f * 2^(e-17) = q * 5^17 + r/2^17
348 // else
349 // f = q * 5^17 * 2^(17-e) + r/2^e
350 if (exponent > divisor_power) {
351 // We only allow exponents of up to 20 and therefore (17 - e) <= 3
352 dividend <<= exponent - divisor_power;
353 quotient = static_cast<uint32_t>(dividend / divisor);
354 remainder = (dividend % divisor) << divisor_power;
355 } else {
356 divisor <<= divisor_power - exponent;
357 quotient = static_cast<uint32_t>(dividend / divisor);
358 remainder = (dividend % divisor) << exponent;
360 FillDigits32(quotient, buffer, length);
361 FillDigits64FixedLength(remainder, divisor_power, buffer, length);
362 *decimal_point = *length;
363 } else if (exponent >= 0) {
364 // 0 <= exponent <= 11
365 significand <<= exponent;
366 FillDigits64(significand, buffer, length);
367 *decimal_point = *length;
368 } else if (exponent > -kDoubleSignificandSize) {
369 // We have to cut the number.
370 uint64_t integrals = significand >> -exponent;
371 uint64_t fractionals = significand - (integrals << -exponent);
372 if (integrals > kMaxUInt32) {
373 FillDigits64(integrals, buffer, length);
374 } else {
375 FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
377 *decimal_point = *length;
378 FillFractionals(fractionals, exponent, fractional_count,
379 buffer, length, decimal_point);
380 } else if (exponent < -128) {
381 // This configuration (with at most 20 digits) means that all digits must be
382 // 0.
383 ASSERT(fractional_count <= 20);
384 buffer[0] = '\0';
385 *length = 0;
386 *decimal_point = -fractional_count;
387 } else {
388 *decimal_point = 0;
389 FillFractionals(significand, exponent, fractional_count,
390 buffer, length, decimal_point);
392 TrimZeros(buffer, length, decimal_point);
393 buffer[*length] = '\0';
394 if ((*length) == 0) {
395 // The string is empty and the decimal_point thus has no importance. Mimick
396 // Gay's dtoa and and set it to -fractional_count.
397 *decimal_point = -fractional_count;
399 return true;
402 } // namespace double_conversion