OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-maths | |
6 | |
7 // Monkey-patch Float32Array. | |
8 Float32Array = function(x) { this[0] = 0; }; | |
9 | |
10 assertTrue(isNaN(Math.fround(NaN))); | |
11 assertTrue(isNaN(Math.fround(function() {}))); | |
12 assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } }))); | |
13 assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } }))); | |
14 assertEquals("Infinity", String(1/Math.fround(0))); | |
15 assertEquals("-Infinity", String(1/Math.fround(-0))); | |
16 assertEquals("Infinity", String(Math.fround(Infinity))); | |
17 assertEquals("-Infinity", String(Math.fround(-Infinity))); | |
18 | |
19 assertEquals("Infinity", String(Math.fround(1E200))); | |
20 assertEquals("-Infinity", String(Math.fround(-1E200))); | |
21 assertEquals("Infinity", String(1/Math.fround(1E-300))); | |
22 assertEquals("-Infinity", String(1/Math.fround(-1E-300))); | |
23 | |
24 mantissa_23_shift = Math.pow(2, -23); | |
25 mantissa_29_shift = Math.pow(2, -23-29); | |
26 | |
27 // Javascript implementation of IEEE 754 to test double to single conversion. | |
28 function ieee754float(sign_bit, | |
29 exponent_bits, | |
30 mantissa_23_bits, | |
31 mantissa_29_bits) { | |
32 this.sign_bit = sign_bit & 1; | |
33 this.exponent_bits = exponent_bits & ((1 << 11) - 1); | |
34 this.mantissa_23_bits = mantissa_23_bits & ((1 << 23) - 1); | |
35 this.mantissa_29_bits = mantissa_29_bits & ((1 << 29) - 1); | |
36 } | |
37 | |
38 ieee754float.prototype.returnSpecial = function() { | |
39 if (mantissa_23_bits == 0 && mantissa_29_bits == 0) return sign * Infinity; | |
40 return NaN; | |
41 } | |
42 | |
43 ieee754float.prototype.toDouble = function() { | |
44 var sign = this.sign_bit ? -1 : 1; | |
45 var exponent = this.exponent_bits - 1023; | |
46 if (exponent == -1023) returnSpecial(); | |
47 var mantissa = 1 + this.mantissa_23_bits * mantissa_23_shift + | |
48 this.mantissa_29_bits * mantissa_29_shift; | |
49 return sign * Math.pow(2, exponent) * mantissa; | |
50 } | |
51 | |
52 ieee754float.prototype.toSingle = function() { | |
53 var sign = this.sign_bit ? -1 : 1; | |
54 var exponent = this.exponent_bits - 1023; | |
55 if (exponent == -1023) returnSpecial(); | |
56 if (exponent > 127) return sign * Infinity; | |
57 if (exponent < -126) return this.toSingleSubnormal(sign, exponent); | |
58 var round = this.mantissa_29_bits >> 28; | |
59 var mantissa = 1 + (this.mantissa_23_bits + round) * mantissa_23_shift; | |
60 return sign * Math.pow(2, exponent) * mantissa; | |
61 } | |
62 | |
63 ieee754float.prototype.toSingleSubnormal = function(sign, exponent) { | |
64 var shift = -126 - exponent; | |
65 if (shift > 24) return sign * 0; | |
66 var round_mask = 1 << (shift - 1); | |
67 var mantissa_23_bits = this.mantissa_23_bits + (1 << 23); | |
68 var round = ((mantissa_23_bits & round_mask) != 0) | 0; | |
69 if (round) { // Round to even if tied. | |
70 var tied_mask = round_mask - 1; | |
71 var result_last_bit_mask = 1 << shift; | |
72 var tied = this.mantissa_29_bits == 0 && | |
73 (mantissa_23_bits & tied_mask ) == 0; | |
74 var result_already_even = (mantissa_23_bits & result_last_bit_mask) == 0; | |
75 if (tied && result_already_even) round = 0; | |
76 } | |
77 mantissa_23_bits >>= shift; | |
78 var mantissa = (mantissa_23_bits + round) * mantissa_23_shift; | |
79 return sign * Math.pow(2, -126) * mantissa; | |
80 } | |
81 | |
82 | |
83 var pi = new ieee754float(0, 0x400, 0x490fda, 0x14442d18); | |
84 assertEquals(pi.toSingle(), Math.fround(pi.toDouble())); | |
85 | |
86 function fuzz_mantissa(sign, exp, m1inc, m2inc) { | |
87 for (var m1 = 0; m1 < (1 << 23); m1 += m1inc) { | |
88 for (var m2 = 0; m2 < (1 << 29); m2 += m2inc) { | |
89 var float = new ieee754float(sign, exp, m1, m2); | |
90 assertEquals(float.toSingle(), Math.fround(float.toDouble())); | |
91 } | |
92 } | |
93 } | |
94 | |
95 for (var sign = 0; sign < 2; sign++) { | |
96 for (var exp = 1024 - 170; exp < 1024 + 170; exp++) { | |
97 fuzz_mantissa(sign, exp, 1337 * exp - sign, 127913 * exp - sign); | |
98 } | |
99 } | |
OLD | NEW |