| Index: runtime/lib/double.cc
|
| diff --git a/runtime/lib/double.cc b/runtime/lib/double.cc
|
| index c28e1b1e0272b4eb687e859260abb63e9ae2e312..47a0ad3188e4323777694f918faa738776a060c0 100644
|
| --- a/runtime/lib/double.cc
|
| +++ b/runtime/lib/double.cc
|
| @@ -29,6 +29,14 @@ DEFINE_NATIVE_ENTRY(Double_doubleFromInteger, 2) {
|
| }
|
|
|
|
|
| +DEFINE_NATIVE_ENTRY(Double_doubleFromFraction, 2) {
|
| + const Integer& numerator = Integer::CheckedHandle(arguments->NativeArgAt(0));
|
| + const Integer& denominator =
|
| + Integer::CheckedHandle(arguments->NativeArgAt(1));
|
| + return Double::New(numerator.AsDoubleValue() / denominator.AsDoubleValue());
|
| +}
|
| +
|
| +
|
| 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 +119,57 @@ static RawInteger* DoubleToInteger(double val, const char* error_msg) {
|
| }
|
|
|
|
|
| +static void DoubleToFraction(
|
| + double val, const Array& fract, const char* error_msg) {
|
| + if (isinf(val) || isnan(val)) {
|
| + const Array& args = Array::Handle(Array::New(1));
|
| + args.SetAt(0, String::Handle(String::New(error_msg)));
|
| + Exceptions::ThrowByType(Exceptions::kUnsupported, args);
|
| + }
|
| + // TODO(regis): Convert inf and nan to fraction.
|
| + // Now that we have a C++ Fraction class, this call should return the
|
| + // fraction instance, rather than set the array elements.
|
| + DoubleInternals internals = DoubleInternals(val);
|
| + ASSERT(!internals.IsSpecial()); // Only Infinity and NaN are special.
|
| + uint64_t significand = internals.Significand();
|
| + intptr_t exponent = internals.Exponent();
|
| + Integer& denominator = Integer::Handle();
|
| + if (exponent < 0) {
|
| + if (exponent > -63) {
|
| + // The denominator fits in a Smi or Mint.
|
| + denominator = Integer::New(1LL << -exponent);
|
| + } else {
|
| + denominator = Bigint::NewFromShiftedInt64(1, -exponent);
|
| + }
|
| + exponent = 0;
|
| + } else {
|
| + denominator = Integer::New(1);
|
| + 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;
|
| + }
|
| + Integer& numerator = Integer::Handle();
|
| + if (exponent == 0) {
|
| + // The numerator fits in a Smi or Mint.
|
| + numerator = Integer::New(ival);
|
| + } else {
|
| + numerator = Bigint::NewFromShiftedInt64(ival, exponent);
|
| + numerator = numerator.AsValidInteger(); // May fit in a Smi or Mint.
|
| + }
|
| + fract.SetAt(0, numerator);
|
| + fract.SetAt(1, denominator);
|
| +}
|
| +
|
| +
|
| 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 +269,15 @@ DEFINE_NATIVE_ENTRY(Double_toInt, 1) {
|
| }
|
|
|
|
|
| +DEFINE_NATIVE_ENTRY(Double_toFraction, 2) {
|
| + const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0));
|
| + const Array& fract = Array::CheckedHandle(arguments->NativeArgAt(1));
|
| + ASSERT(fract.Length() == 2);
|
| + DoubleToFraction(arg.value(), fract, "Infinity or NaN toFraction");
|
| + return Object::null(); // Result in fract[0] and fract[1].
|
| +}
|
| +
|
| +
|
| 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));
|
|
|