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

Unified Diff: test/mjsunit/harmony/math-fround.js

Issue 169513002: Harmony: implement Math.fround. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updated comments Created 6 years, 10 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 | « src/runtime.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/math-fround.js
diff --git a/test/mjsunit/harmony/math-fround.js b/test/mjsunit/harmony/math-fround.js
new file mode 100644
index 0000000000000000000000000000000000000000..ea432ea2de441b8dca7f894971736306aa65b073
--- /dev/null
+++ b/test/mjsunit/harmony/math-fround.js
@@ -0,0 +1,99 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-maths
+
+// Monkey-patch Float32Array.
+Float32Array = function(x) { this[0] = 0; };
+
+assertTrue(isNaN(Math.fround(NaN)));
+assertTrue(isNaN(Math.fround(function() {})));
+assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } })));
+assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } })));
+assertEquals("Infinity", String(1/Math.fround(0)));
+assertEquals("-Infinity", String(1/Math.fround(-0)));
+assertEquals("Infinity", String(Math.fround(Infinity)));
+assertEquals("-Infinity", String(Math.fround(-Infinity)));
+
+assertEquals("Infinity", String(Math.fround(1E200)));
+assertEquals("-Infinity", String(Math.fround(-1E200)));
+assertEquals("Infinity", String(1/Math.fround(1E-300)));
+assertEquals("-Infinity", String(1/Math.fround(-1E-300)));
+
+mantissa_23_shift = Math.pow(2, -23);
+mantissa_29_shift = Math.pow(2, -23-29);
+
+// Javascript implementation of IEEE 754 to test double to single conversion.
+function ieee754float(sign_bit,
+ exponent_bits,
+ mantissa_23_bits,
+ mantissa_29_bits) {
+ this.sign_bit = sign_bit & 1;
+ this.exponent_bits = exponent_bits & ((1 << 11) - 1);
+ this.mantissa_23_bits = mantissa_23_bits & ((1 << 23) - 1);
+ this.mantissa_29_bits = mantissa_29_bits & ((1 << 29) - 1);
+}
+
+ieee754float.prototype.returnSpecial = function() {
+ if (mantissa_23_bits == 0 && mantissa_29_bits == 0) return sign * Infinity;
+ return NaN;
+}
+
+ieee754float.prototype.toDouble = function() {
+ var sign = this.sign_bit ? -1 : 1;
+ var exponent = this.exponent_bits - 1023;
+ if (exponent == -1023) returnSpecial();
+ var mantissa = 1 + this.mantissa_23_bits * mantissa_23_shift +
+ this.mantissa_29_bits * mantissa_29_shift;
+ return sign * Math.pow(2, exponent) * mantissa;
+}
+
+ieee754float.prototype.toSingle = function() {
+ var sign = this.sign_bit ? -1 : 1;
+ var exponent = this.exponent_bits - 1023;
+ if (exponent == -1023) returnSpecial();
+ if (exponent > 127) return sign * Infinity;
+ if (exponent < -126) return this.toSingleSubnormal(sign, exponent);
+ var round = this.mantissa_29_bits >> 28;
+ var mantissa = 1 + (this.mantissa_23_bits + round) * mantissa_23_shift;
+ return sign * Math.pow(2, exponent) * mantissa;
+}
+
+ieee754float.prototype.toSingleSubnormal = function(sign, exponent) {
+ var shift = -126 - exponent;
+ if (shift > 24) return sign * 0;
+ var round_mask = 1 << (shift - 1);
+ var mantissa_23_bits = this.mantissa_23_bits + (1 << 23);
+ var round = ((mantissa_23_bits & round_mask) != 0) | 0;
+ if (round) { // Round to even if tied.
+ var tied_mask = round_mask - 1;
+ var result_last_bit_mask = 1 << shift;
+ var tied = this.mantissa_29_bits == 0 &&
+ (mantissa_23_bits & tied_mask ) == 0;
+ var result_already_even = (mantissa_23_bits & result_last_bit_mask) == 0;
+ if (tied && result_already_even) round = 0;
+ }
+ mantissa_23_bits >>= shift;
+ var mantissa = (mantissa_23_bits + round) * mantissa_23_shift;
+ return sign * Math.pow(2, -126) * mantissa;
+}
+
+
+var pi = new ieee754float(0, 0x400, 0x490fda, 0x14442d18);
+assertEquals(pi.toSingle(), Math.fround(pi.toDouble()));
+
+function fuzz_mantissa(sign, exp, m1inc, m2inc) {
+ for (var m1 = 0; m1 < (1 << 23); m1 += m1inc) {
+ for (var m2 = 0; m2 < (1 << 29); m2 += m2inc) {
+ var float = new ieee754float(sign, exp, m1, m2);
+ assertEquals(float.toSingle(), Math.fround(float.toDouble()));
+ }
+ }
+}
+
+for (var sign = 0; sign < 2; sign++) {
+ for (var exp = 1024 - 170; exp < 1024 + 170; exp++) {
+ fuzz_mantissa(sign, exp, 1337 * exp - sign, 127913 * exp - sign);
+ }
+}
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698