| 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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // one bit. | 307 // one bit. |
| 305 ASSERT(bigint.Bit(63) != 0); | 308 ASSERT(bigint.Bit(63) != 0); |
| 306 for (int i = 0; i < 63; i++) { | 309 for (int i = 0; i < 63; i++) { |
| 307 // Verify that all 63 least significant bits are 0. | 310 // Verify that all 63 least significant bits are 0. |
| 308 if (bigint.Bit(i) != 0) return false; | 311 if (bigint.Bit(i) != 0) return false; |
| 309 } | 312 } |
| 310 return true; | 313 return true; |
| 311 } | 314 } |
| 312 | 315 |
| 313 | 316 |
| 317 uint64_t BigintOperations::AbsToUInt64(const Bigint& bigint) { |
| 318 unsigned char bytes[8]; |
| 319 ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes)); |
| 320 int n = BN_bn2bin(bigint.BNAddr(), bytes); |
| 321 ASSERT(n >= 0); |
| 322 uint64_t value = 0; |
| 323 ASSERT(n <= static_cast<int>(sizeof value)); |
| 324 for (int i = 0; i < n; ++i) { |
| 325 value <<= 8; |
| 326 value |= bytes[i]; |
| 327 } |
| 328 return value; |
| 329 } |
| 330 |
| 331 |
| 314 int64_t BigintOperations::ToInt64(const Bigint& bigint) { | 332 int64_t BigintOperations::ToInt64(const Bigint& bigint) { |
| 315 ASSERT(FitsIntoInt64(bigint)); | 333 ASSERT(FitsIntoInt64(bigint)); |
| 316 unsigned char bytes[8]; | 334 int64_t value = AbsToUInt64(bigint); |
| 317 ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes)); | |
| 318 int n = BN_bn2bin(bigint.BNAddr(), bytes); | |
| 319 ASSERT(n >= 0); | |
| 320 int64_t value = 0; | |
| 321 ASSERT(n <= static_cast<int>(sizeof value)); | |
| 322 for (int i = 0; i < n; ++i) { | |
| 323 value <<= 8; | |
| 324 value |= bytes[i]; | |
| 325 } | |
| 326 if (bigint.IsNegative()) { | 335 if (bigint.IsNegative()) { |
| 327 value = -value; | 336 value = -value; |
| 328 } | 337 } |
| 329 return value; | 338 return value; |
| 330 } | 339 } |
| 331 | 340 |
| 332 | 341 |
| 342 bool BigintOperations::FitsIntoUInt64(const Bigint& bigint) { |
| 343 const BIGNUM *bn = bigint.BNAddr(); |
| 344 if (bigint.IsNegative()) return false; |
| 345 int bits = BN_num_bits(bn); |
| 346 if (bits > 64) return false; |
| 347 return true; |
| 348 } |
| 349 |
| 350 |
| 351 uint64_t BigintOperations::ToUInt64(const Bigint& bigint) { |
| 352 ASSERT(FitsIntoUInt64(bigint)); |
| 353 return AbsToUInt64(bigint); |
| 354 } |
| 355 |
| 356 |
| 333 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) { | 357 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) { |
| 334 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr()); | 358 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr()); |
| 335 ASSERT(status == 1); | 359 ASSERT(status == 1); |
| 336 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN())); | 360 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN())); |
| 337 return result.raw(); | 361 return result.raw(); |
| 338 } | 362 } |
| 339 | 363 |
| 340 | 364 |
| 341 RawBigint* BigintOperations::Subtract(const Bigint& a, const Bigint& b) { | 365 RawBigint* BigintOperations::Subtract(const Bigint& a, const Bigint& b) { |
| 342 int status = BN_sub(TmpBN(), a.BNAddr(), b.BNAddr()); | 366 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(); | 590 result.ToggleSign(); |
| 567 return result.raw(); | 591 return result.raw(); |
| 568 } | 592 } |
| 569 | 593 |
| 570 | 594 |
| 571 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { | 595 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { |
| 572 return BN_cmp(a.BNAddr(), b.BNAddr()); | 596 return BN_cmp(a.BNAddr(), b.BNAddr()); |
| 573 } | 597 } |
| 574 | 598 |
| 575 } // namespace dart | 599 } // namespace dart |
| OLD | NEW |