Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/mfbt/double-conversion/double-conversion/fixed-dtoa.cc
Line
Count
Source (jump to first uncovered line)
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.
15
//
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.
27
28
#include <cmath>
29
30
#include <double-conversion/fixed-dtoa.h>
31
#include <double-conversion/ieee.h>
32
33
namespace double_conversion {
34
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
0
  UInt128() : high_bits_(0), low_bits_(0) { }
40
0
  UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { }
41
42
0
  void Multiply(uint32_t multiplicand) {
43
0
    uint64_t accumulator;
44
0
45
0
    accumulator = (low_bits_ & kMask32) * multiplicand;
46
0
    uint32_t part = static_cast<uint32_t>(accumulator & kMask32);
47
0
    accumulator >>= 32;
48
0
    accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
49
0
    low_bits_ = (accumulator << 32) + part;
50
0
    accumulator >>= 32;
51
0
    accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
52
0
    part = static_cast<uint32_t>(accumulator & kMask32);
53
0
    accumulator >>= 32;
54
0
    accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
55
0
    high_bits_ = (accumulator << 32) + part;
56
0
    ASSERT((accumulator >> 32) == 0);
57
0
  }
58
59
0
  void Shift(int shift_amount) {
60
0
    ASSERT(-64 <= shift_amount && shift_amount <= 64);
61
0
    if (shift_amount == 0) {
62
0
      return;
63
0
    } else if (shift_amount == -64) {
64
0
      high_bits_ = low_bits_;
65
0
      low_bits_ = 0;
66
0
    } else if (shift_amount == 64) {
67
0
      low_bits_ = high_bits_;
68
0
      high_bits_ = 0;
69
0
    } else if (shift_amount <= 0) {
70
0
      high_bits_ <<= -shift_amount;
71
0
      high_bits_ += low_bits_ >> (64 + shift_amount);
72
0
      low_bits_ <<= -shift_amount;
73
0
    } else {
74
0
      low_bits_ >>= shift_amount;
75
0
      low_bits_ += high_bits_ << (64 - shift_amount);
76
0
      high_bits_ >>= shift_amount;
77
0
    }
78
0
  }
79
80
  // Modifies *this to *this MOD (2^power).
81
  // Returns *this DIV (2^power).
82
0
  int DivModPowerOf2(int power) {
83
0
    if (power >= 64) {
84
0
      int result = static_cast<int>(high_bits_ >> (power - 64));
85
0
      high_bits_ -= static_cast<uint64_t>(result) << (power - 64);
86
0
      return result;
87
0
    } else {
88
0
      uint64_t part_low = low_bits_ >> power;
89
0
      uint64_t part_high = high_bits_ << (64 - power);
90
0
      int result = static_cast<int>(part_low + part_high);
91
0
      high_bits_ = 0;
92
0
      low_bits_ -= part_low << power;
93
0
      return result;
94
0
    }
95
0
  }
96
97
0
  bool IsZero() const {
98
0
    return high_bits_ == 0 && low_bits_ == 0;
99
0
  }
100
101
0
  int BitAt(int position) const {
102
0
    if (position >= 64) {
103
0
      return static_cast<int>(high_bits_ >> (position - 64)) & 1;
104
0
    } else {
105
0
      return static_cast<int>(low_bits_ >> position) & 1;
106
0
    }
107
0
  }
108
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_;
114
};
115
116
117
static const int kDoubleSignificandSize = 53;  // Includes the hidden bit.
118
119
120
static void FillDigits32FixedLength(uint32_t number, int requested_length,
121
0
                                    Vector<char> buffer, int* length) {
122
0
  for (int i = requested_length - 1; i >= 0; --i) {
123
0
    buffer[(*length) + i] = '0' + number % 10;
124
0
    number /= 10;
125
0
  }
126
0
  *length += requested_length;
127
0
}
128
129
130
0
static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) {
131
0
  int number_length = 0;
132
0
  // We fill the digits in reverse order and exchange them afterwards.
133
0
  while (number != 0) {
134
0
    int digit = number % 10;
135
0
    number /= 10;
136
0
    buffer[(*length) + number_length] = static_cast<char>('0' + digit);
137
0
    number_length++;
138
0
  }
139
0
  // Exchange the digits.
140
0
  int i = *length;
141
0
  int j = *length + number_length - 1;
142
0
  while (i < j) {
143
0
    char tmp = buffer[i];
144
0
    buffer[i] = buffer[j];
145
0
    buffer[j] = tmp;
146
0
    i++;
147
0
    j--;
148
0
  }
149
0
  *length += number_length;
150
0
}
151
152
153
static void FillDigits64FixedLength(uint64_t number,
154
0
                                    Vector<char> buffer, int* length) {
155
0
  const uint32_t kTen7 = 10000000;
156
0
  // For efficiency cut the number into 3 uint32_t parts, and print those.
157
0
  uint32_t part2 = static_cast<uint32_t>(number % kTen7);
158
0
  number /= kTen7;
159
0
  uint32_t part1 = static_cast<uint32_t>(number % kTen7);
160
0
  uint32_t part0 = static_cast<uint32_t>(number / kTen7);
161
0
162
0
  FillDigits32FixedLength(part0, 3, buffer, length);
163
0
  FillDigits32FixedLength(part1, 7, buffer, length);
164
0
  FillDigits32FixedLength(part2, 7, buffer, length);
165
0
}
166
167
168
0
static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
169
0
  const uint32_t kTen7 = 10000000;
170
0
  // For efficiency cut the number into 3 uint32_t parts, and print those.
171
0
  uint32_t part2 = static_cast<uint32_t>(number % kTen7);
172
0
  number /= kTen7;
173
0
  uint32_t part1 = static_cast<uint32_t>(number % kTen7);
174
0
  uint32_t part0 = static_cast<uint32_t>(number / kTen7);
175
0
176
0
  if (part0 != 0) {
177
0
    FillDigits32(part0, buffer, length);
178
0
    FillDigits32FixedLength(part1, 7, buffer, length);
179
0
    FillDigits32FixedLength(part2, 7, buffer, length);
180
0
  } else if (part1 != 0) {
181
0
    FillDigits32(part1, buffer, length);
182
0
    FillDigits32FixedLength(part2, 7, buffer, length);
183
0
  } else {
184
0
    FillDigits32(part2, buffer, length);
185
0
  }
186
0
}
187
188
189
0
static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
190
0
  // An empty buffer represents 0.
191
0
  if (*length == 0) {
192
0
    buffer[0] = '1';
193
0
    *decimal_point = 1;
194
0
    *length = 1;
195
0
    return;
196
0
  }
197
0
  // Round the last digit until we either have a digit that was not '9' or until
198
0
  // we reached the first digit.
199
0
  buffer[(*length) - 1]++;
200
0
  for (int i = (*length) - 1; i > 0; --i) {
201
0
    if (buffer[i] != '0' + 10) {
202
0
      return;
203
0
    }
204
0
    buffer[i] = '0';
205
0
    buffer[i - 1]++;
206
0
  }
207
0
  // If the first digit is now '0' + 10, we would need to set it to '0' and add
208
0
  // a '1' in front. However we reach the first digit only if all following
209
0
  // digits had been '9' before rounding up. Now all trailing digits are '0' and
210
0
  // we simply switch the first digit to '1' and update the decimal-point
211
0
  // (indicating that the point is now one digit to the right).
212
0
  if (buffer[0] == '0' + 10) {
213
0
    buffer[0] = '1';
214
0
    (*decimal_point)++;
215
0
  }
216
0
}
217
218
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
0
                            int* length, int* decimal_point) {
233
0
  ASSERT(-128 <= exponent && exponent <= 0);
234
0
  // 'fractionals' is a fixed-point number, with binary point at bit
235
0
  // (-exponent). Inside the function the non-converted remainder of fractionals
236
0
  // is a fixed-point number, with binary point at bit 'point'.
237
0
  if (-exponent <= 64) {
238
0
    // One 64 bit number is sufficient.
239
0
    ASSERT(fractionals >> 56 == 0);
240
0
    int point = -exponent;
241
0
    for (int i = 0; i < fractional_count; ++i) {
242
0
      if (fractionals == 0) break;
243
0
      // Instead of multiplying by 10 we multiply by 5 and adjust the point
244
0
      // location. This way the fractionals variable will not overflow.
245
0
      // Invariant at the beginning of the loop: fractionals < 2^point.
246
0
      // Initially we have: point <= 64 and fractionals < 2^56
247
0
      // After each iteration the point is decremented by one.
248
0
      // Note that 5^3 = 125 < 128 = 2^7.
249
0
      // Therefore three iterations of this loop will not overflow fractionals
250
0
      // (even without the subtraction at the end of the loop body). At this
251
0
      // time point will satisfy point <= 61 and therefore fractionals < 2^point
252
0
      // and any further multiplication of fractionals by 5 will not overflow.
253
0
      fractionals *= 5;
254
0
      point--;
255
0
      int digit = static_cast<int>(fractionals >> point);
256
0
      ASSERT(digit <= 9);
257
0
      buffer[*length] = static_cast<char>('0' + digit);
258
0
      (*length)++;
259
0
      fractionals -= static_cast<uint64_t>(digit) << point;
260
0
    }
261
0
    // If the first bit after the point is set we have to round up.
262
0
    ASSERT(fractionals == 0 || point - 1 >= 0);
263
0
    if ((fractionals != 0) && ((fractionals >> (point - 1)) & 1) == 1) {
264
0
      RoundUp(buffer, length, decimal_point);
265
0
    }
266
0
  } else {  // We need 128 bits.
267
0
    ASSERT(64 < -exponent && -exponent <= 128);
268
0
    UInt128 fractionals128 = UInt128(fractionals, 0);
269
0
    fractionals128.Shift(-exponent - 64);
270
0
    int point = 128;
271
0
    for (int i = 0; i < fractional_count; ++i) {
272
0
      if (fractionals128.IsZero()) break;
273
0
      // As before: instead of multiplying by 10 we multiply by 5 and adjust the
274
0
      // point location.
275
0
      // This multiplication will not overflow for the same reasons as before.
276
0
      fractionals128.Multiply(5);
277
0
      point--;
278
0
      int digit = fractionals128.DivModPowerOf2(point);
279
0
      ASSERT(digit <= 9);
280
0
      buffer[*length] = static_cast<char>('0' + digit);
281
0
      (*length)++;
282
0
    }
283
0
    if (fractionals128.BitAt(point - 1) == 1) {
284
0
      RoundUp(buffer, length, decimal_point);
285
0
    }
286
0
  }
287
0
}
288
289
290
// Removes leading and trailing zeros.
291
// If leading zeros are removed then the decimal point position is adjusted.
292
0
static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) {
293
0
  while (*length > 0 && buffer[(*length) - 1] == '0') {
294
0
    (*length)--;
295
0
  }
296
0
  int first_non_zero = 0;
297
0
  while (first_non_zero < *length && buffer[first_non_zero] == '0') {
298
0
    first_non_zero++;
299
0
  }
300
0
  if (first_non_zero != 0) {
301
0
    for (int i = first_non_zero; i < *length; ++i) {
302
0
      buffer[i - first_non_zero] = buffer[i];
303
0
    }
304
0
    *length -= first_non_zero;
305
0
    *decimal_point -= first_non_zero;
306
0
  }
307
0
}
308
309
310
bool FastFixedDtoa(double v,
311
                   int fractional_count,
312
                   Vector<char> buffer,
313
                   int* length,
314
0
                   int* decimal_point) {
315
0
  const uint32_t kMaxUInt32 = 0xFFFFFFFF;
316
0
  uint64_t significand = Double(v).Significand();
317
0
  int exponent = Double(v).Exponent();
318
0
  // v = significand * 2^exponent (with significand a 53bit integer).
319
0
  // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we
320
0
  // don't know how to compute the representation. 2^73 ~= 9.5*10^21.
321
0
  // If necessary this limit could probably be increased, but we don't need
322
0
  // more.
323
0
  if (exponent > 20) return false;
324
0
  if (fractional_count > 20) return false;
325
0
  *length = 0;
326
0
  // At most kDoubleSignificandSize bits of the significand are non-zero.
327
0
  // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero
328
0
  // bits:  0..11*..0xxx..53*..xx
329
0
  if (exponent + kDoubleSignificandSize > 64) {
330
0
    // The exponent must be > 11.
331
0
    //
332
0
    // We know that v = significand * 2^exponent.
333
0
    // And the exponent > 11.
334
0
    // We simplify the task by dividing v by 10^17.
335
0
    // The quotient delivers the first digits, and the remainder fits into a 64
336
0
    // bit number.
337
0
    // Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
338
0
    const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5);  // 5^17
339
0
    uint64_t divisor = kFive17;
340
0
    int divisor_power = 17;
341
0
    uint64_t dividend = significand;
342
0
    uint32_t quotient;
343
0
    uint64_t remainder;
344
0
    // Let v = f * 2^e with f == significand and e == exponent.
345
0
    // Then need q (quotient) and r (remainder) as follows:
346
0
    //   v            = q * 10^17       + r
347
0
    //   f * 2^e      = q * 10^17       + r
348
0
    //   f * 2^e      = q * 5^17 * 2^17 + r
349
0
    // If e > 17 then
350
0
    //   f * 2^(e-17) = q * 5^17        + r/2^17
351
0
    // else
352
0
    //   f  = q * 5^17 * 2^(17-e) + r/2^e
353
0
    if (exponent > divisor_power) {
354
0
      // We only allow exponents of up to 20 and therefore (17 - e) <= 3
355
0
      dividend <<= exponent - divisor_power;
356
0
      quotient = static_cast<uint32_t>(dividend / divisor);
357
0
      remainder = (dividend % divisor) << divisor_power;
358
0
    } else {
359
0
      divisor <<= divisor_power - exponent;
360
0
      quotient = static_cast<uint32_t>(dividend / divisor);
361
0
      remainder = (dividend % divisor) << exponent;
362
0
    }
363
0
    FillDigits32(quotient, buffer, length);
364
0
    FillDigits64FixedLength(remainder, buffer, length);
365
0
    *decimal_point = *length;
366
0
  } else if (exponent >= 0) {
367
0
    // 0 <= exponent <= 11
368
0
    significand <<= exponent;
369
0
    FillDigits64(significand, buffer, length);
370
0
    *decimal_point = *length;
371
0
  } else if (exponent > -kDoubleSignificandSize) {
372
0
    // We have to cut the number.
373
0
    uint64_t integrals = significand >> -exponent;
374
0
    uint64_t fractionals = significand - (integrals << -exponent);
375
0
    if (integrals > kMaxUInt32) {
376
0
      FillDigits64(integrals, buffer, length);
377
0
    } else {
378
0
      FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
379
0
    }
380
0
    *decimal_point = *length;
381
0
    FillFractionals(fractionals, exponent, fractional_count,
382
0
                    buffer, length, decimal_point);
383
0
  } else if (exponent < -128) {
384
0
    // This configuration (with at most 20 digits) means that all digits must be
385
0
    // 0.
386
0
    ASSERT(fractional_count <= 20);
387
0
    buffer[0] = '\0';
388
0
    *length = 0;
389
0
    *decimal_point = -fractional_count;
390
0
  } else {
391
0
    *decimal_point = 0;
392
0
    FillFractionals(significand, exponent, fractional_count,
393
0
                    buffer, length, decimal_point);
394
0
  }
395
0
  TrimZeros(buffer, length, decimal_point);
396
0
  buffer[*length] = '\0';
397
0
  if ((*length) == 0) {
398
0
    // The string is empty and the decimal_point thus has no importance. Mimick
399
0
    // Gay's dtoa and and set it to -fractional_count.
400
0
    *decimal_point = -fractional_count;
401
0
  }
402
0
  return true;
403
0
}
404
405
}  // namespace double_conversion