Index: runtime/lib/double.cc |
diff --git a/runtime/lib/double.cc b/runtime/lib/double.cc |
index c28e1b1e0272b4eb687e859260abb63e9ae2e312..e108b3c8c16edebb70e91f06bbd1695cb01dfe01 100644 |
--- a/runtime/lib/double.cc |
+++ b/runtime/lib/double.cc |
@@ -29,6 +29,13 @@ DEFINE_NATIVE_ENTRY(Double_doubleFromInteger, 2) { |
} |
+DEFINE_NATIVE_ENTRY(Double_doubleFromPixels, 1) { |
+ const Integer& pixels = Integer::CheckedHandle(arguments->NativeArgAt(0)); |
+ // TODO(regis): Recognize +/- infinity. |
+ return Double::New(pixels.AsDoubleValue() / Pixels::kScaling); |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Double_add, 2) { |
double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
@@ -111,6 +118,48 @@ static RawInteger* DoubleToInteger(double val, const char* error_msg) { |
} |
+static RawInteger* DoubleToPixels(double val, const char* error_msg) { |
+ if (isnan(val)) { |
+ const Array& args = Array::Handle(Array::New(1)); |
+ args.SetAt(0, String::Handle(String::New(error_msg))); |
+ Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
+ } |
+ if (isinf(val)) { |
+ if (val < 0) { |
+ return Integer::New(Pixels::kMinusInfinity); |
+ } else { |
+ return Integer::New(Pixels::kPlusInfinity); |
+ } |
+ } |
+ DoubleInternals internals = DoubleInternals(val); |
+ ASSERT(!internals.IsSpecial()); // Only Infinity and NaN are special. |
+ uint64_t significand = internals.Significand(); |
+ intptr_t exponent = internals.Exponent() + Pixels::kFractionalBits; |
+ if (exponent <= 0) { |
+ significand >>= -exponent; |
+ exponent = 0; |
+ } else if (exponent <= 10) { |
+ // A double significand has at most 53 bits. The following shift will |
+ // hence not overflow, and yield an integer of at most 63 bits. |
+ significand <<= exponent; |
+ exponent = 0; |
+ } |
+ // A significand has at most 63 bits (after the shift above). |
+ // The cast to int64_t is hence safe. |
+ int64_t ival = static_cast<int64_t>(significand); |
+ if (internals.Sign() < 0) { |
+ ival = -ival; |
+ } |
+ if (exponent == 0) { |
+ // The double fits in a Smi or Mint. |
+ return Integer::New(ival); |
+ } |
+ Integer& result = Integer::Handle(); |
+ result = Bigint::NewFromShiftedInt64(ival, exponent); |
+ return result.AsValidInteger(); |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Double_trunc_div, 2) { |
double left = Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
GET_NON_NULL_NATIVE_ARGUMENT(Double, right_object, arguments->NativeArgAt(1)); |
@@ -210,6 +259,12 @@ DEFINE_NATIVE_ENTRY(Double_toInt, 1) { |
} |
+DEFINE_NATIVE_ENTRY(Double_toPixels, 1) { |
+ const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
+ return DoubleToPixels(arg.value(), "NaN toPixels"); |
+} |
+ |
+ |
DEFINE_NATIVE_ENTRY(Double_parse, 3) { |
GET_NON_NULL_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); |
GET_NON_NULL_NATIVE_ARGUMENT(Integer, startValue, arguments->NativeArgAt(1)); |