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

Unified Diff: runtime/vm/bigint_operations.cc

Issue 8539034: Add methods for creating Bigint objects from unsigned 64-bit integers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/bigint_operations.h ('k') | runtime/vm/bigint_operations_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/bigint_operations.cc
diff --git a/runtime/vm/bigint_operations.cc b/runtime/vm/bigint_operations.cc
index 820562f56a32420a5064b4f5344ec27c054681f5..8a5d257fc83d73a994f57a960647a9f3f9cf8f0c 100644
--- a/runtime/vm/bigint_operations.cc
+++ b/runtime/vm/bigint_operations.cc
@@ -73,6 +73,14 @@ RawBigint* BigintOperations::NewFromInt64(int64_t value, Heap::Space space) {
value = -value;
}
+ const Bigint& result = Bigint::Handle(NewFromUInt64(value, space));
+ result.SetSign(is_negative);
+
+ return result.raw();
+}
+
+
+RawBigint* BigintOperations::NewFromUInt64(uint64_t value, Heap::Space space) {
const int kNumBytes = sizeof(value);
unsigned char pch[kNumBytes];
for (int i = kNumBytes - 1; i >= 0; i--) {
@@ -80,13 +88,8 @@ RawBigint* BigintOperations::NewFromInt64(int64_t value, Heap::Space space) {
value >>=8;
pch[i] = c;
}
-
BN_bin2bn(pch, kNumBytes, TmpBN());
-
- const Bigint& result = Bigint::Handle(Bigint::New(TmpBN(), space));
- result.SetSign(is_negative);
-
- return result.raw();
+ return Bigint::New(TmpBN(), space);
}
@@ -330,6 +333,31 @@ int64_t BigintOperations::ToInt64(const Bigint& bigint) {
}
+bool BigintOperations::FitsIntoUInt64(const Bigint& bigint) {
+ const BIGNUM *bn = bigint.BNAddr();
+ if (bigint.IsNegative()) return false;
+ int bits = BN_num_bits(bn);
+ if (bits > 64) return false;
+ return true;
+}
+
+
+uint64_t BigintOperations::ToUInt64(const Bigint& bigint) {
+ ASSERT(FitsIntoUInt64(bigint));
+ unsigned char bytes[8];
+ ASSERT(BN_num_bytes(bigint.BNAddr()) <= static_cast<int>(sizeof bytes));
+ int n = BN_bn2bin(bigint.BNAddr(), bytes);
+ ASSERT(n >= 0);
+ int64_t value = 0;
+ ASSERT(n <= static_cast<int>(sizeof value));
+ for (int i = 0; i < n; ++i) {
+ value <<= 8;
+ value |= bytes[i];
+ }
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
+ return value;
+}
+
+
RawBigint* BigintOperations::Add(const Bigint& a, const Bigint& b) {
int status = BN_add(TmpBN(), a.BNAddr(), b.BNAddr());
ASSERT(status == 1);
« 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