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

Unified Diff: runtime/lib/double.cc

Issue 2005723004: Fraction class prototype and test (not to be committed). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: work in progress Created 4 years, 5 months 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/lib/core_sources.gypi ('k') | runtime/lib/double.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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));
« no previous file with comments | « runtime/lib/core_sources.gypi ('k') | runtime/lib/double.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698