30 #include "../include/v8stdint.h"
44 UInt128() : high_bits_(0), low_bits_(0) { }
45 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { }
50 accumulator = (low_bits_ & kMask32) * multiplicand;
51 uint32_t part =
static_cast<uint32_t
>(accumulator & kMask32);
53 accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
54 low_bits_ = (accumulator << 32) + part;
56 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
57 part =
static_cast<uint32_t
>(accumulator & kMask32);
59 accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
60 high_bits_ = (accumulator << 32) + part;
61 ASSERT((accumulator >> 32) == 0);
65 ASSERT(-64 <= shift_amount && shift_amount <= 64);
66 if (shift_amount == 0) {
68 }
else if (shift_amount == -64) {
69 high_bits_ = low_bits_;
71 }
else if (shift_amount == 64) {
72 low_bits_ = high_bits_;
74 }
else if (shift_amount <= 0) {
75 high_bits_ <<= -shift_amount;
76 high_bits_ += low_bits_ >> (64 + shift_amount);
77 low_bits_ <<= -shift_amount;
79 low_bits_ >>= shift_amount;
80 low_bits_ += high_bits_ << (64 - shift_amount);
81 high_bits_ >>= shift_amount;
89 int result =
static_cast<int>(high_bits_ >> (power - 64));
90 high_bits_ -=
static_cast<uint64_t
>(result) << (power - 64);
93 uint64_t part_low = low_bits_ >> power;
94 uint64_t part_high = high_bits_ << (64 - power);
95 int result =
static_cast<int>(part_low + part_high);
97 low_bits_ -= part_low << power;
103 return high_bits_ == 0 && low_bits_ == 0;
107 if (position >= 64) {
108 return static_cast<int>(high_bits_ >> (position - 64)) & 1;
110 return static_cast<int>(low_bits_ >> position) & 1;
115 static const uint64_t kMask32 = 0xFFFFFFFF;
122 static const int kDoubleSignificandSize = 53;
125 static void FillDigits32FixedLength(uint32_t number,
int requested_length,
126 Vector<char> buffer,
int* length) {
127 for (
int i = requested_length - 1; i >= 0; --i) {
128 buffer[(*length) + i] =
'0' + number % 10;
131 *length += requested_length;
135 static void FillDigits32(uint32_t number, Vector<char> buffer,
int* length) {
136 int number_length = 0;
138 while (number != 0) {
139 int digit = number % 10;
141 buffer[(*length) + number_length] =
'0' + digit;
146 int j = *length + number_length - 1;
148 char tmp = buffer[i];
149 buffer[i] = buffer[j];
154 *length += number_length;
158 static void FillDigits64FixedLength(uint64_t number,
int requested_length,
159 Vector<char> buffer,
int* length) {
160 const uint32_t kTen7 = 10000000;
162 uint32_t part2 =
static_cast<uint32_t
>(number % kTen7);
164 uint32_t part1 =
static_cast<uint32_t
>(number % kTen7);
165 uint32_t part0 =
static_cast<uint32_t
>(number / kTen7);
167 FillDigits32FixedLength(part0, 3, buffer, length);
168 FillDigits32FixedLength(part1, 7, buffer, length);
169 FillDigits32FixedLength(part2, 7, buffer, length);
173 static void FillDigits64(uint64_t number, Vector<char> buffer,
int* length) {
174 const uint32_t kTen7 = 10000000;
176 uint32_t part2 =
static_cast<uint32_t
>(number % kTen7);
178 uint32_t part1 =
static_cast<uint32_t
>(number % kTen7);
179 uint32_t part0 =
static_cast<uint32_t
>(number / kTen7);
182 FillDigits32(part0, buffer, length);
183 FillDigits32FixedLength(part1, 7, buffer, length);
184 FillDigits32FixedLength(part2, 7, buffer, length);
185 }
else if (part1 != 0) {
186 FillDigits32(part1, buffer, length);
187 FillDigits32FixedLength(part2, 7, buffer, length);
189 FillDigits32(part2, buffer, length);
194 static void RoundUp(Vector<char> buffer,
int* length,
int* decimal_point) {
204 buffer[(*length) - 1]++;
205 for (
int i = (*length) - 1; i > 0; --i) {
206 if (buffer[i] !=
'0' + 10) {
217 if (buffer[0] ==
'0' + 10) {
235 static void FillFractionals(uint64_t fractionals,
int exponent,
236 int fractional_count, Vector<char> buffer,
237 int* length,
int* decimal_point) {
238 ASSERT(-128 <= exponent && exponent <= 0);
242 if (-exponent <= 64) {
244 ASSERT(fractionals >> 56 == 0);
245 int point = -exponent;
246 for (
int i = 0; i < fractional_count; ++i) {
247 if (fractionals == 0)
break;
260 int digit =
static_cast<int>(fractionals >> point);
261 buffer[*length] =
'0' + digit;
263 fractionals -=
static_cast<uint64_t
>(digit) << point;
266 if (((fractionals >> (point - 1)) & 1) == 1) {
267 RoundUp(buffer, length, decimal_point);
270 ASSERT(64 < -exponent && -exponent <= 128);
271 UInt128 fractionals128 = UInt128(fractionals, 0);
272 fractionals128.Shift(-exponent - 64);
274 for (
int i = 0; i < fractional_count; ++i) {
275 if (fractionals128.IsZero())
break;
279 fractionals128.Multiply(5);
281 int digit = fractionals128.DivModPowerOf2(point);
282 buffer[*length] =
'0' + digit;
285 if (fractionals128.BitAt(point - 1) == 1) {
286 RoundUp(buffer, length, decimal_point);
294 static void TrimZeros(Vector<char> buffer,
int* length,
int* decimal_point) {
295 while (*length > 0 && buffer[(*length) - 1] ==
'0') {
298 int first_non_zero = 0;
299 while (first_non_zero < *length && buffer[first_non_zero] ==
'0') {
302 if (first_non_zero != 0) {
303 for (
int i = first_non_zero; i < *length; ++i) {
304 buffer[i - first_non_zero] = buffer[i];
306 *length -= first_non_zero;
307 *decimal_point -= first_non_zero;
313 int fractional_count,
316 int* decimal_point) {
325 if (exponent > 20)
return false;
326 if (fractional_count > 20)
return false;
331 if (exponent + kDoubleSignificandSize > 64) {
341 uint64_t divisor = kFive17;
342 int divisor_power = 17;
343 uint64_t dividend = significand;
355 if (exponent > divisor_power) {
357 dividend <<= exponent - divisor_power;
358 quotient =
static_cast<uint32_t
>(dividend / divisor);
359 remainder = (dividend % divisor) << divisor_power;
361 divisor <<= divisor_power - exponent;
362 quotient =
static_cast<uint32_t
>(dividend / divisor);
363 remainder = (dividend % divisor) << exponent;
365 FillDigits32(quotient, buffer, length);
366 FillDigits64FixedLength(remainder, divisor_power, buffer, length);
367 *decimal_point = *length;
368 }
else if (exponent >= 0) {
370 significand <<= exponent;
371 FillDigits64(significand, buffer, length);
372 *decimal_point = *length;
373 }
else if (exponent > -kDoubleSignificandSize) {
375 uint64_t integrals = significand >> -exponent;
376 uint64_t fractionals = significand - (integrals << -exponent);
377 if (integrals > kMaxUInt32) {
378 FillDigits64(integrals, buffer, length);
380 FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
382 *decimal_point = *length;
383 FillFractionals(fractionals, exponent, fractional_count,
384 buffer, length, decimal_point);
385 }
else if (exponent < -128) {
388 ASSERT(fractional_count <= 20);
391 *decimal_point = -fractional_count;
394 FillFractionals(significand, exponent, fractional_count,
395 buffer, length, decimal_point);
397 TrimZeros(buffer, length, decimal_point);
398 buffer[*length] =
'\0';
399 if ((*length) == 0) {
402 *decimal_point = -fractional_count;
uint64_t Significand() const
int DivModPowerOf2(int power)
void Multiply(uint32_t multiplicand)
#define ASSERT(condition)
#define V8_2PART_UINT64_C(a, b)
void Shift(int shift_amount)
const uint32_t kMaxUInt32
bool FastFixedDtoa(double v, int fractional_count, Vector< char > buffer, int *length, int *decimal_point)
UInt128(uint64_t high, uint64_t low)