OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 | 2 |
3 #include "vm/bigint_operations.h" | 3 #include "vm/bigint_operations.h" |
4 | 4 |
5 #include "platform/utils.h" | 5 #include "platform/utils.h" |
6 | 6 |
7 #include "vm/double_internals.h" | 7 #include "vm/double_internals.h" |
8 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
10 #include "vm/zone.h" | 10 #include "vm/zone.h" |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 348 } |
349 ASSERT(required_size == static_cast<intptr_t>(required_size)); | 349 ASSERT(required_size == static_cast<intptr_t>(required_size)); |
350 // We will fill the result in the inverse order and then exchange at the end. | 350 // We will fill the result in the inverse order and then exchange at the end. |
351 char* result = | 351 char* result = |
352 reinterpret_cast<char*>(allocator(static_cast<intptr_t>(required_size))); | 352 reinterpret_cast<char*>(allocator(static_cast<intptr_t>(required_size))); |
353 ASSERT(result != NULL); | 353 ASSERT(result != NULL); |
354 int result_pos = 0; | 354 int result_pos = 0; |
355 | 355 |
356 // We divide the input into pieces of ~27 bits which can be efficiently | 356 // We divide the input into pieces of ~27 bits which can be efficiently |
357 // handled. | 357 // handled. |
358 const intptr_t kDivisorValue = 100000000; | 358 const intptr_t kChunkDivisor = 100000000; |
359 const int kPowerOfTen = 8; | 359 const int kChunkDigits = 8; |
360 ASSERT(pow(10.0, kPowerOfTen) == kDivisorValue); | 360 ASSERT(pow(10.0, kChunkDigits) == kChunkDivisor); |
361 ASSERT(static_cast<Chunk>(kDivisorValue) < kDigitMaxValue); | 361 ASSERT(static_cast<Chunk>(kChunkDivisor) < kDigitMaxValue); |
362 ASSERT(Smi::IsValid(kDivisorValue)); | 362 ASSERT(Smi::IsValid(kChunkDivisor)); |
363 const Bigint& divisor = Bigint::Handle(NewFromInt64(kDivisorValue)); | 363 const Bigint& divisor = Bigint::Handle(NewFromInt64(kChunkDivisor)); |
364 | 364 |
365 // Rest contains the remaining bigint that needs to be printed. | 365 // Rest contains the remaining bigint that needs to be printed. |
366 Bigint& rest = Bigint::Handle(bigint.raw()); | 366 Bigint& rest = Bigint::Handle(bigint.raw()); |
367 Bigint& quotient = Bigint::Handle(); | 367 Bigint& quotient = Bigint::Handle(); |
368 Bigint& remainder = Bigint::Handle(); | 368 Bigint& remainder = Bigint::Handle(); |
369 while (!rest.IsZero()) { | 369 while (!rest.IsZero()) { |
370 DivideRemainder(rest, divisor, "ient, &remainder); | 370 DivideRemainder(rest, divisor, "ient, &remainder); |
371 ASSERT(remainder.Length() <= 1); | 371 ASSERT(remainder.Length() <= 1); |
372 intptr_t part = (remainder.Length() == 1) | 372 intptr_t part = (remainder.Length() == 1) |
373 ? static_cast<intptr_t>(remainder.GetChunkAt(0)) | 373 ? static_cast<intptr_t>(remainder.GetChunkAt(0)) |
374 : 0; | 374 : 0; |
375 for (int i = 0; i < kPowerOfTen; i++) { | 375 for (int i = 0; i < kChunkDigits; i++) { |
376 result[result_pos++] = '0' + (part % 10); | 376 result[result_pos++] = '0' + (part % 10); |
377 part /= 10; | 377 part /= 10; |
378 } | 378 } |
379 ASSERT(part == 0); | 379 ASSERT(part == 0); |
380 rest = quotient.raw(); | 380 rest = quotient.raw(); |
381 } | 381 } |
382 // Move the resulting position back until we don't have any zeroes anymore. | 382 // Move the resulting position back until we don't have any zeroes anymore. |
383 // This is done so that we can remove all leading zeroes. | 383 // This is done so that we can remove all leading zeroes. |
384 while (result_pos > 1 && result[result_pos - 1] == '0') { | 384 while (result_pos > 1 && result[result_pos - 1] == '0') { |
385 result_pos--; | 385 result_pos--; |
(...skipping 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1645 int BigintOperations::CountBits(Chunk digit) { | 1645 int BigintOperations::CountBits(Chunk digit) { |
1646 int result = 0; | 1646 int result = 0; |
1647 while (digit != 0) { | 1647 while (digit != 0) { |
1648 digit >>= 1; | 1648 digit >>= 1; |
1649 result++; | 1649 result++; |
1650 } | 1650 } |
1651 return result; | 1651 return result; |
1652 } | 1652 } |
1653 | 1653 |
1654 } // namespace dart | 1654 } // namespace dart |
OLD | NEW |