Revision: 19433
Author: [email protected]
Date: Tue Feb 18 10:43:06 2014 UTC
Log: Harmony: implement Math.fround.
[email protected]
BUG=v8:2938
LOG=N
Review URL: https://codereview.chromium.org/169513002
http://code.google.com/p/v8/source/detail?r=19433
Added:
/branches/bleeding_edge/test/mjsunit/harmony/math-fround.js
Modified:
/branches/bleeding_edge/src/harmony-math.js
/branches/bleeding_edge/src/runtime.cc
/branches/bleeding_edge/src/runtime.h
=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/mjsunit/harmony/math-fround.js Tue Feb 18
10:43:06 2014 UTC
@@ -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);
+ }
+}
=======================================
--- /branches/bleeding_edge/src/harmony-math.js Tue Feb 11 11:48:32 2014 UTC
+++ /branches/bleeding_edge/src/harmony-math.js Tue Feb 18 10:43:06 2014 UTC
@@ -109,19 +109,19 @@
}
-//ES6 draft 09-27-13, section 20.2.2.21.
+// ES6 draft 09-27-13, section 20.2.2.21.
function MathLog10(x) {
return MathLog(x) * 0.434294481903251828; // log10(x) = log(x)/log(10).
}
-//ES6 draft 09-27-13, section 20.2.2.22.
+// ES6 draft 09-27-13, section 20.2.2.22.
function MathLog2(x) {
return MathLog(x) * 1.442695040888963407; // log2(x) = log(x)/log(2).
}
-//ES6 draft 09-27-13, section 20.2.2.17.
+// ES6 draft 09-27-13, section 20.2.2.17.
function MathHypot(x, y) { // Function length is 2.
// We may want to introduce fast paths for two arguments and when
// normalization to avoid overflow is not necessary. For now, we
@@ -152,6 +152,12 @@
}
return MathSqrt(sum) * max;
}
+
+
+// ES6 draft 09-27-13, section 20.2.2.16.
+function MathFround(x) {
+ return %Math_fround(TO_NUMBER_INLINE(x));
+}
function ExtendMath() {
@@ -169,7 +175,8 @@
"atanh", MathAtanh,
"log10", MathLog10,
"log2", MathLog2,
- "hypot", MathHypot
+ "hypot", MathHypot,
+ "fround", MathFround
));
}
=======================================
--- /branches/bleeding_edge/src/runtime.cc Tue Feb 18 10:10:06 2014 UTC
+++ /branches/bleeding_edge/src/runtime.cc Tue Feb 18 10:43:06 2014 UTC
@@ -7827,6 +7827,16 @@
CONVERT_DOUBLE_ARG_CHECKED(x, 0);
return isolate->heap()->AllocateHeapNumber(fast_sqrt(x));
}
+
+
+RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_fround) {
+ SealHandleScope shs(isolate);
+ ASSERT(args.length() == 1);
+
+ CONVERT_DOUBLE_ARG_CHECKED(x, 0);
+ float xf = static_cast<float>(x);
+ return isolate->heap()->AllocateHeapNumber(xf);
+}
RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) {
=======================================
--- /branches/bleeding_edge/src/runtime.h Wed Feb 12 22:04:19 2014 UTC
+++ /branches/bleeding_edge/src/runtime.h Tue Feb 18 10:43:06 2014 UTC
@@ -185,6 +185,7 @@
F(Math_pow_cfunction, 2, 1) \
F(RoundNumber, 1, 1) \
F(Math_sqrt, 1, 1) \
+ F(Math_fround, 1, 1) \
\
/* Regular expressions */ \
F(RegExpCompile, 3, 1) \
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.