OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 // Flags: --allow-natives-syntax |
| 6 |
5 // Monkey-patch Float32Array. | 7 // Monkey-patch Float32Array. |
6 Float32Array = function(x) { this[0] = 0; }; | 8 Float32Array = function(x) { this[0] = 0; }; |
7 | 9 |
8 assertTrue(isNaN(Math.fround(NaN))); | 10 assertTrue(isNaN(Math.fround(NaN))); |
9 assertTrue(isNaN(Math.fround(function() {}))); | 11 assertTrue(isNaN(Math.fround(function() {}))); |
10 assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } }))); | 12 assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } }))); |
11 assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } }))); | 13 assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } }))); |
12 assertEquals("Infinity", String(1/Math.fround(0))); | 14 assertTrue(isNaN(Math.fround(NaN))); |
13 assertEquals("-Infinity", String(1/Math.fround(-0))); | 15 assertTrue(isNaN(Math.fround(function() {}))); |
14 assertEquals("Infinity", String(Math.fround(Infinity))); | 16 assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } }))); |
15 assertEquals("-Infinity", String(Math.fround(-Infinity))); | 17 assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } }))); |
16 | 18 |
17 assertEquals("Infinity", String(Math.fround(1E200))); | 19 function unopt(x) { return Math.fround(x); } |
18 assertEquals("-Infinity", String(Math.fround(-1E200))); | 20 function opt(y) { return Math.fround(y); } |
19 assertEquals("Infinity", String(1/Math.fround(1E-300))); | 21 |
20 assertEquals("-Infinity", String(1/Math.fround(-1E-300))); | 22 opt(0.1); |
| 23 opt(0.1); |
| 24 unopt(0.1); |
| 25 %NeverOptimizeFunction(unopt); |
| 26 %OptimizeFunctionOnNextCall(opt); |
| 27 |
| 28 function test(f) { |
| 29 assertEquals("Infinity", String(1/f(0))); |
| 30 assertEquals("-Infinity", String(1/f(-0))); |
| 31 assertEquals("Infinity", String(f(Infinity))); |
| 32 assertEquals("-Infinity", String(f(-Infinity))); |
| 33 assertEquals("Infinity", String(f(1E200))); |
| 34 assertEquals("-Infinity", String(f(-1E200))); |
| 35 assertEquals("Infinity", String(1/f(1E-300))); |
| 36 assertEquals("-Infinity", String(1/f(-1E-300))); |
| 37 } |
| 38 |
| 39 test(opt); |
| 40 test(unopt); |
21 | 41 |
22 mantissa_23_shift = Math.pow(2, -23); | 42 mantissa_23_shift = Math.pow(2, -23); |
23 mantissa_29_shift = Math.pow(2, -23-29); | 43 mantissa_29_shift = Math.pow(2, -23-29); |
24 | 44 |
25 // Javascript implementation of IEEE 754 to test double to single conversion. | 45 // Javascript implementation of IEEE 754 to test double to single conversion. |
26 function ieee754float(sign_bit, | 46 function ieee754float(sign_bit, |
27 exponent_bits, | 47 exponent_bits, |
28 mantissa_23_bits, | 48 mantissa_23_bits, |
29 mantissa_29_bits) { | 49 mantissa_29_bits) { |
30 this.sign_bit = sign_bit & 1; | 50 this.sign_bit = sign_bit & 1; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 var result_already_even = (mantissa_23_bits & result_last_bit_mask) == 0; | 92 var result_already_even = (mantissa_23_bits & result_last_bit_mask) == 0; |
73 if (tied && result_already_even) round = 0; | 93 if (tied && result_already_even) round = 0; |
74 } | 94 } |
75 mantissa_23_bits >>= shift; | 95 mantissa_23_bits >>= shift; |
76 var mantissa = (mantissa_23_bits + round) * mantissa_23_shift; | 96 var mantissa = (mantissa_23_bits + round) * mantissa_23_shift; |
77 return sign * Math.pow(2, -126) * mantissa; | 97 return sign * Math.pow(2, -126) * mantissa; |
78 } | 98 } |
79 | 99 |
80 | 100 |
81 var pi = new ieee754float(0, 0x400, 0x490fda, 0x14442d18); | 101 var pi = new ieee754float(0, 0x400, 0x490fda, 0x14442d18); |
82 assertEquals(pi.toSingle(), Math.fround(pi.toDouble())); | 102 assertEquals(pi.toSingle(), opt(pi.toDouble())); |
| 103 assertEquals(pi.toSingle(), unopt(pi.toDouble())); |
| 104 |
83 | 105 |
84 function fuzz_mantissa(sign, exp, m1inc, m2inc) { | 106 function fuzz_mantissa(sign, exp, m1inc, m2inc) { |
85 for (var m1 = 0; m1 < (1 << 23); m1 += m1inc) { | 107 for (var m1 = 0; m1 < (1 << 23); m1 += m1inc) { |
86 for (var m2 = 0; m2 < (1 << 29); m2 += m2inc) { | 108 for (var m2 = 0; m2 < (1 << 29); m2 += m2inc) { |
87 var float = new ieee754float(sign, exp, m1, m2); | 109 var float = new ieee754float(sign, exp, m1, m2); |
88 assertEquals(float.toSingle(), Math.fround(float.toDouble())); | 110 assertEquals(float.toSingle(), unopt(float.toDouble())); |
| 111 assertEquals(float.toSingle(), opt(float.toDouble())); |
89 } | 112 } |
90 } | 113 } |
91 } | 114 } |
92 | 115 |
93 for (var sign = 0; sign < 2; sign++) { | 116 for (var sign = 0; sign < 2; sign++) { |
94 for (var exp = 1024 - 170; exp < 1024 + 170; exp++) { | 117 for (var exp = 1024 - 170; exp < 1024 + 170; exp++) { |
95 fuzz_mantissa(sign, exp, 1337 * exp - sign, 127913 * exp - sign); | 118 fuzz_mantissa(sign, exp, 1337 * exp - sign, 127913 * exp - sign); |
96 } | 119 } |
97 } | 120 } |
OLD | NEW |