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

Unified Diff: runtime/lib/math_patch.dart

Issue 2858353003: Move Dart versions of math.min() and math.max() into VM patch file. (Closed)
Patch Set: Merge branch 'master' into min-max Created 3 years, 7 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 | « pkg/dev_compiler/tool/sdk_expected_errors.txt ('k') | sdk/lib/_internal/js_runtime/lib/math_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/math_patch.dart
diff --git a/runtime/lib/math_patch.dart b/runtime/lib/math_patch.dart
index 63459fa1fcc54bc6bc7b11359c77b51afb8dfdff..fe8f8deef085d01e2fdf0b966aa855eb9bd6fcdf 100644
--- a/runtime/lib/math_patch.dart
+++ b/runtime/lib/math_patch.dart
@@ -6,6 +6,64 @@ import "dart:typed_data";
// A VM patch of the dart:math library.
+@patch
+T min<T extends num>(T a, T b) {
+ // These partially redundant type checks improve code quality for dart2js.
+ // Most of the improvement is at call sites from the inferred non-null num
+ // return type.
+ if (a is! num) throw new ArgumentError(a);
+ if (b is! num) throw new ArgumentError(b);
+
+ if (a > b) return b;
+ if (a < b) return a;
+ if (b is double) {
+ // Special case for NaN and -0.0. If one argument is NaN return NaN.
+ // [min] must also distinguish between -0.0 and 0.0.
+ if (a is double) {
+ if (a == 0.0) {
+ // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN.
+ // The following returns -0.0 if either a or b is -0.0, and it
+ // returns NaN if b is NaN.
+ return (a + b) * a * b;
+ }
+ }
+ // Check for NaN and b == -0.0.
+ if (a == 0 && b.isNegative || b.isNaN) return b;
+ return a;
+ }
+ return a;
+}
+
+@patch
+T max<T extends num>(T a, T b) {
+ // These partially redundant type checks improve code quality for dart2js.
+ // Most of the improvement is at call sites from the inferred non-null num
+ // return type.
+ if (a is! num) throw new ArgumentError(a);
+ if (b is! num) throw new ArgumentError(b);
+
+ if (a > b) return a;
+ if (a < b) return b;
+ if (b is double) {
+ // Special case for NaN and -0.0. If one argument is NaN return NaN.
+ // [max] must also distinguish between -0.0 and 0.0.
+ if (a is double) {
+ if (a == 0.0) {
+ // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN.
+ // The following returns 0.0 if either a or b is 0.0, and it
+ // returns NaN if b is NaN.
+ return a + b;
+ }
+ }
+ // Check for NaN.
+ if (b.isNaN) return b;
+ return a;
+ }
+ // max(-0.0, 0) must return 0.
+ if (b == 0 && a.isNegative) return b;
+ return a;
+}
+
// If [x] is an [int] and [exponent] is a non-negative [int], the result is
// an [int], otherwise the result is a [double].
@patch
« no previous file with comments | « pkg/dev_compiler/tool/sdk_expected_errors.txt ('k') | sdk/lib/_internal/js_runtime/lib/math_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698