Index: runtime/lib/integers.cc |
diff --git a/runtime/lib/integers.cc b/runtime/lib/integers.cc |
index 5be23af2fea9dbcd0f8a48ea9d524ea1cfc778ac..84dc9d6205fa1c82c40e82ebc78b3aff4ceb2af8 100644 |
--- a/runtime/lib/integers.cc |
+++ b/runtime/lib/integers.cc |
@@ -35,6 +35,12 @@ static bool CheckInteger(const Integer& i) { |
} |
+static int BitLengthInt64(int64_t value) { |
+ value ^= value >> (8 * sizeof(value) - 1); // flip bits if negative. |
+ return value == 0 ? 0 : Utils::HighestBit(value) + 1; |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Integer_bitAndFromInteger, 2) { |
const Integer& right = Integer::CheckedHandle(arguments->NativeArgAt(0)); |
GET_NON_NULL_NATIVE_ARGUMENT(Integer, left, arguments->NativeArgAt(1)); |
@@ -327,6 +333,18 @@ DEFINE_NATIVE_ENTRY(Smi_bitNegate, 1) { |
} |
+DEFINE_NATIVE_ENTRY(Smi_bitLength, 1) { |
+ const Smi& operand = Smi::CheckedHandle(arguments->NativeArgAt(0)); |
+ if (FLAG_trace_intrinsified_natives) { |
+ OS::Print("Smi_bitLength: %s\n", operand.ToCString()); |
+ } |
+ int64_t value = operand.AsInt64Value(); |
+ intptr_t result = BitLengthInt64(value); |
+ ASSERT(Smi::IsValid(result)); |
+ return Smi::New(result); |
+} |
+ |
+ |
// Mint natives. |
DEFINE_NATIVE_ENTRY(Mint_bitNegate, 1) { |
@@ -340,6 +358,19 @@ DEFINE_NATIVE_ENTRY(Mint_bitNegate, 1) { |
} |
+DEFINE_NATIVE_ENTRY(Mint_bitLength, 1) { |
+ const Mint& operand = Mint::CheckedHandle(arguments->NativeArgAt(0)); |
+ ASSERT(CheckInteger(operand)); |
+ if (FLAG_trace_intrinsified_natives) { |
+ OS::Print("Mint_bitLength: %s\n", operand.ToCString()); |
+ } |
+ int64_t value = operand.AsInt64Value(); |
+ intptr_t result = BitLengthInt64(value); |
+ ASSERT(Smi::IsValid(result)); |
+ return Smi::New(result); |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Mint_shlFromInt, 2) { |
// Use the preallocated out of memory exception to avoid calling |
// into dart code or allocating any code. |
@@ -362,6 +393,12 @@ DEFINE_NATIVE_ENTRY(Bigint_bitNegate, 1) { |
} |
+DEFINE_NATIVE_ENTRY(Bigint_bitLength, 1) { |
+ const Bigint& value = Bigint::CheckedHandle(arguments->NativeArgAt(0)); |
+ return Integer::New(BigintOperations::BitLength(value)); |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Bigint_shlFromInt, 2) { |
// Use the preallocated out of memory exception to avoid calling |
// into dart code or allocating any code. |