Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/bigint_operations.h" | 5 #include "vm/bigint_operations.h" |
| 6 | 6 |
| 7 #include <openssl/crypto.h> | 7 #include <openssl/crypto.h> |
| 8 | 8 |
| 9 #include "vm/bigint_store.h" | 9 #include "vm/bigint_store.h" |
| 10 #include "vm/double_internals.h" | 10 #include "vm/double_internals.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 RawBigint* BigintOperations::NewFromInt64(int64_t value, Heap::Space space) { | 69 RawBigint* BigintOperations::NewFromInt64(int64_t value, Heap::Space space) { |
| 70 bool is_negative = value < 0; | 70 bool is_negative = value < 0; |
| 71 | 71 |
| 72 if (is_negative) { | 72 if (is_negative) { |
| 73 value = -value; | 73 value = -value; |
| 74 } | 74 } |
| 75 | 75 |
| 76 const Bigint& result = Bigint::Handle(NewFromUInt64(value, space)); | |
| 77 result.SetSign(is_negative); | |
| 78 | |
| 79 return result.raw(); | |
| 80 } | |
| 81 | |
| 82 | |
| 83 RawBigint* BigintOperations::NewFromUInt64(uint64_t value, Heap::Space space) { | |
| 76 const int kNumBytes = sizeof(value); | 84 const int kNumBytes = sizeof(value); |
| 77 unsigned char pch[kNumBytes]; | 85 unsigned char pch[kNumBytes]; |
| 78 for (int i = kNumBytes - 1; i >= 0; i--) { | 86 for (int i = kNumBytes - 1; i >= 0; i--) { |
| 79 unsigned char c = value & 0xFF; | 87 unsigned char c = value & 0xFF; |
| 80 value >>=8; | 88 value >>=8; |
| 81 pch[i] = c; | 89 pch[i] = c; |
| 82 } | 90 } |
| 83 | |
| 84 BN_bin2bn(pch, kNumBytes, TmpBN()); | 91 BN_bin2bn(pch, kNumBytes, TmpBN()); |
| 85 | 92 return Bigint::New(TmpBN(), space); |
| 86 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN(), space)); | |
| 87 result.SetSign(is_negative); | |
| 88 | |
| 89 return result.raw(); | |
| 90 } | 93 } |
| 91 | 94 |
| 92 | 95 |
| 93 RawBigint* BigintOperations::NewFromCString(const char* str, | 96 RawBigint* BigintOperations::NewFromCString(const char* str, |
| 94 Heap::Space space) { | 97 Heap::Space space) { |
| 95 ASSERT(str != NULL); | 98 ASSERT(str != NULL); |
| 96 if (str[0] == '\0') { | 99 if (str[0] == '\0') { |
| 97 return NewFromInt64(0, space); | 100 return NewFromInt64(0, space); |
| 98 } | 101 } |
| 99 | 102 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 value <<= 8; | 326 value <<= 8; |
| 324 value |= bytes[i]; | 327 value |= bytes[i]; |
| 325 } | 328 } |
| 326 if (bigint.IsNegative()) { | 329 if (bigint.IsNegative()) { |
| 327 value = -value; | 330 value = -value; |
| 328 } | 331 } |
| 329 return value; | 332 return value; |
| 330 } | 333 } |
| 331 | 334 |
| 332 | 335 |
| 336 bool BigintOperations::FitsIntoUInt64(const Bigint& bigint) { | |
| 337 const BIGNUM *bn = bigint.BNAddr(); | |
| 338 if (bigint.IsNegative()) return false; | |
| 339 int bits = BN_num_bits(bn); | |
| 340 if (bits > 64) return false; | |
| 341 return true; | |
| 342 } | |
| 343 | |
| 344 | |
| 345 uint64_t BigintOperations::ToUInt64(const Bigint& bigint) { | |
| 346 ASSERT(FitsIntoUInt64(bigint)); | |
| 347 unsigned char bytes[8]; | |
| 348 ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes)); | |
| 349 int n = BN_bn2bin(bigint.BNAddr(), bytes); | |
| 350 ASSERT(n >= 0); | |
| 351 int64_t value = 0; | |
| 352 ASSERT(n <= static_cast<int>(sizeof value)); | |
| 353 for (int i = 0; i < n; ++i) { | |
| 354 value <<= 8; | |
| 355 value |= bytes[i]; | |
| 356 } | |
|
siva
2011/11/12 01:20:07
This code here seems identical to ToInt64 except t
cshapiro
2011/11/12 01:50:09
Good idea. I am not sure how I missed that. Done
| |
| 357 return value; | |
| 358 } | |
| 359 | |
| 360 | |
| 333 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) { | 361 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) { |
| 334 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr()); | 362 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr()); |
| 335 ASSERT(status == 1); | 363 ASSERT(status == 1); |
| 336 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN())); | 364 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN())); |
| 337 return result.raw(); | 365 return result.raw(); |
| 338 } | 366 } |
| 339 | 367 |
| 340 | 368 |
| 341 RawBigint* BigintOperations::Subtract(const Bigint& a, const Bigint& b) { | 369 RawBigint* BigintOperations::Subtract(const Bigint& a, const Bigint& b) { |
| 342 int status = BN_sub(TmpBN(), a.BNAddr(), b.BNAddr()); | 370 int status = BN_sub(TmpBN(), a.BNAddr(), b.BNAddr()); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 result.ToggleSign(); | 594 result.ToggleSign(); |
| 567 return result.raw(); | 595 return result.raw(); |
| 568 } | 596 } |
| 569 | 597 |
| 570 | 598 |
| 571 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { | 599 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { |
| 572 return BN_cmp(a.BNAddr(), b.BNAddr()); | 600 return BN_cmp(a.BNAddr(), b.BNAddr()); |
| 573 } | 601 } |
| 574 | 602 |
| 575 } // namespace dart | 603 } // namespace dart |
| OLD | NEW |