Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: runtime/vm/bigint_operations.cc

Issue 207303002: Handle 0 in BigintOperations::ToDecimalCString. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/vm/bigint_operations_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/assert.h" 5 #include "platform/assert.h"
6 #include "platform/utils.h" 6 #include "platform/utils.h"
7 7
8 #include "vm/double_internals.h" 8 #include "vm/double_internals.h"
9 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
10 #include "vm/object_store.h" 10 #include "vm/object_store.h"
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 // into dart code or allocating any code. 358 // into dart code or allocating any code.
359 const Instance& exception = 359 const Instance& exception =
360 Instance::Handle(isolate->object_store()->out_of_memory()); 360 Instance::Handle(isolate->object_store()->out_of_memory());
361 Exceptions::Throw(exception); 361 Exceptions::Throw(exception);
362 UNREACHABLE(); 362 UNREACHABLE();
363 } 363 }
364 364
365 // Approximate the size of the resulting string. We prefer overestimating 365 // Approximate the size of the resulting string. We prefer overestimating
366 // to not allocating enough. 366 // to not allocating enough.
367 int64_t bit_length = length * kDigitBitSize; 367 int64_t bit_length = length * kDigitBitSize;
368 ASSERT(bit_length > length); 368 ASSERT(bit_length > length || length == 0);
369 int64_t decimal_length = (bit_length * kLog2Dividend / kLog2Divisor) + 1; 369 int64_t decimal_length = (bit_length * kLog2Dividend / kLog2Divisor) + 1;
370 // Add one byte for the trailing \0 character. 370 // Add one byte for the trailing \0 character.
371 int64_t required_size = decimal_length + 1; 371 int64_t required_size = decimal_length + 1;
372 if (bigint.IsNegative()) { 372 if (bigint.IsNegative()) {
373 required_size++; 373 required_size++;
374 } 374 }
375 ASSERT(required_size == static_cast<intptr_t>(required_size)); 375 ASSERT(required_size == static_cast<intptr_t>(required_size));
376 // We will fill the result in the inverse order and then exchange at the end. 376 // We will fill the result in the inverse order and then exchange at the end.
377 char* result = 377 char* result =
378 reinterpret_cast<char*>(allocator(static_cast<intptr_t>(required_size))); 378 reinterpret_cast<char*>(allocator(static_cast<intptr_t>(required_size)));
(...skipping 13 matching lines...) Expand all
392 const Bigint& rest = Bigint::Handle(Copy(bigint)); 392 const Bigint& rest = Bigint::Handle(Copy(bigint));
393 while (!rest.IsZero()) { 393 while (!rest.IsZero()) {
394 Chunk remainder = InplaceUnsignedDivideRemainderDigit(rest, divisor); 394 Chunk remainder = InplaceUnsignedDivideRemainderDigit(rest, divisor);
395 intptr_t part = static_cast<intptr_t>(remainder); 395 intptr_t part = static_cast<intptr_t>(remainder);
396 for (int i = 0; i < kChunkDigits; i++) { 396 for (int i = 0; i < kChunkDigits; i++) {
397 result[result_pos++] = '0' + (part % 10); 397 result[result_pos++] = '0' + (part % 10);
398 part /= 10; 398 part /= 10;
399 } 399 }
400 ASSERT(part == 0); 400 ASSERT(part == 0);
401 } 401 }
402 // Move the resulting position back until we don't have any zeroes anymore. 402 // Add a leading zero, so that we have at least one digit.
403 // This is done so that we can remove all leading zeroes. 403 result[result_pos++] = '0';
404 // Move the resulting position back until we don't have any zeroes anymore
405 // or we reach the first digit. This is done so that we can remove all
406 // redundant leading zeroes.
404 while (result_pos > 1 && result[result_pos - 1] == '0') { 407 while (result_pos > 1 && result[result_pos - 1] == '0') {
405 result_pos--; 408 result_pos--;
406 } 409 }
407 if (bigint.IsNegative()) { 410 if (bigint.IsNegative()) {
408 result[result_pos++] = '-'; 411 result[result_pos++] = '-';
409 } 412 }
410 // Reverse the string. 413 // Reverse the string.
411 int i = 0; 414 int i = 0;
412 int j = result_pos - 1; 415 int j = result_pos - 1;
413 while (i < j) { 416 while (i < j) {
(...skipping 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after
1811 intptr_t BigintOperations::CountBits(Chunk digit) { 1814 intptr_t BigintOperations::CountBits(Chunk digit) {
1812 intptr_t result = 0; 1815 intptr_t result = 0;
1813 while (digit != 0) { 1816 while (digit != 0) {
1814 digit >>= 1; 1817 digit >>= 1;
1815 result++; 1818 result++;
1816 } 1819 }
1817 return result; 1820 return result;
1818 } 1821 }
1819 1822
1820 } // namespace dart 1823 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/bigint_operations_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698