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

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

Issue 8585049: Add an interface for retrieving the value of unsigned 64-bit integers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor consistency changes. Created 9 years 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 | « runtime/vm/bigint_operations.h ('k') | 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 (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
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)); 76 const Bigint& result = Bigint::Handle(NewFromUint64(value, space));
77 result.SetSign(is_negative); 77 result.SetSign(is_negative);
78 78
79 return result.raw(); 79 return result.raw();
80 } 80 }
81 81
82 82
83 RawBigint* BigintOperations::NewFromUInt64(uint64_t value, Heap::Space space) { 83 RawBigint* BigintOperations::NewFromUint64(uint64_t value, Heap::Space space) {
84 const int kNumBytes = sizeof(value); 84 const int kNumBytes = sizeof(value);
85 unsigned char pch[kNumBytes]; 85 unsigned char pch[kNumBytes];
86 for (int i = kNumBytes - 1; i >= 0; i--) { 86 for (int i = kNumBytes - 1; i >= 0; i--) {
87 unsigned char c = value & 0xFF; 87 unsigned char c = value & 0xFF;
88 value >>=8; 88 value >>=8;
89 pch[i] = c; 89 pch[i] = c;
90 } 90 }
91 BN_bin2bn(pch, kNumBytes, TmpBN()); 91 BN_bin2bn(pch, kNumBytes, TmpBN());
92 return Bigint::New(TmpBN(), space); 92 return Bigint::New(TmpBN(), space);
93 } 93 }
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 // one bit. 307 // one bit.
308 ASSERT(bigint.Bit(63) != 0); 308 ASSERT(bigint.Bit(63) != 0);
309 for (int i = 0; i < 63; i++) { 309 for (int i = 0; i < 63; i++) {
310 // Verify that all 63 least significant bits are 0. 310 // Verify that all 63 least significant bits are 0.
311 if (bigint.Bit(i) != 0) return false; 311 if (bigint.Bit(i) != 0) return false;
312 } 312 }
313 return true; 313 return true;
314 } 314 }
315 315
316 316
317 uint64_t BigintOperations::AbsToUInt64(const Bigint& bigint) { 317 uint64_t BigintOperations::AbsToUint64(const Bigint& bigint) {
318 unsigned char bytes[8]; 318 unsigned char bytes[8];
319 ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes)); 319 ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes));
320 int n = BN_bn2bin(bigint.BNAddr(), bytes); 320 int n = BN_bn2bin(bigint.BNAddr(), bytes);
321 ASSERT(n >= 0); 321 ASSERT(n >= 0);
322 uint64_t value = 0; 322 uint64_t value = 0;
323 ASSERT(n <= static_cast<int>(sizeof value)); 323 ASSERT(n <= static_cast<int>(sizeof value));
324 for (int i = 0; i < n; ++i) { 324 for (int i = 0; i < n; ++i) {
325 value <<= 8; 325 value <<= 8;
326 value |= bytes[i]; 326 value |= bytes[i];
327 } 327 }
328 return value; 328 return value;
329 } 329 }
330 330
331 331
332 int64_t BigintOperations::ToInt64(const Bigint& bigint) { 332 int64_t BigintOperations::ToInt64(const Bigint& bigint) {
333 ASSERT(FitsIntoInt64(bigint)); 333 ASSERT(FitsIntoInt64(bigint));
334 int64_t value = AbsToUInt64(bigint); 334 int64_t value = AbsToUint64(bigint);
335 if (bigint.IsNegative()) { 335 if (bigint.IsNegative()) {
336 value = -value; 336 value = -value;
337 } 337 }
338 return value; 338 return value;
339 } 339 }
340 340
341 341
342 bool BigintOperations::FitsIntoUInt64(const Bigint& bigint) { 342 bool BigintOperations::FitsIntoUint64(const Bigint& bigint) {
343 const BIGNUM *bn = bigint.BNAddr(); 343 const BIGNUM *bn = bigint.BNAddr();
344 if (bigint.IsNegative()) return false; 344 if (bigint.IsNegative()) return false;
345 int bits = BN_num_bits(bn); 345 int bits = BN_num_bits(bn);
346 if (bits > 64) return false; 346 if (bits > 64) return false;
347 return true; 347 return true;
348 } 348 }
349 349
350 350
351 uint64_t BigintOperations::ToUInt64(const Bigint& bigint) { 351 uint64_t BigintOperations::ToUint64(const Bigint& bigint) {
352 ASSERT(FitsIntoUInt64(bigint)); 352 ASSERT(FitsIntoUint64(bigint));
353 return AbsToUInt64(bigint); 353 return AbsToUint64(bigint);
354 } 354 }
355 355
356 356
357 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) { 357 RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) {
358 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr()); 358 int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr());
359 ASSERT(status == 1); 359 ASSERT(status == 1);
360 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN())); 360 const Bigint& result = Bigint::Handle(Bigint::New(TmpBN()));
361 return result.raw(); 361 return result.raw();
362 } 362 }
363 363
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 result.ToggleSign(); 590 result.ToggleSign();
591 return result.raw(); 591 return result.raw();
592 } 592 }
593 593
594 594
595 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { 595 int BigintOperations::Compare(const Bigint& a, const Bigint& b) {
596 return BN_cmp(a.BNAddr(), b.BNAddr()); 596 return BN_cmp(a.BNAddr(), b.BNAddr());
597 } 597 }
598 598
599 } // namespace dart 599 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/bigint_operations.h ('k') | runtime/vm/bigint_operations_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698