Home | History | Annotate | Download | only in es6
      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: --allow-natives-syntax
      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 assertTrue(isNaN(Math.fround(NaN)));
     15 assertTrue(isNaN(Math.fround(function() {})));
     16 assertTrue(isNaN(Math.fround({ toString: function() { return NaN; } })));
     17 assertTrue(isNaN(Math.fround({ valueOf: function() { return "abc"; } })));
     18 
     19 function unopt(x) { return Math.fround(x); }
     20 function opt(y) { return Math.fround(y); }
     21 
     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);
     41 
     42 mantissa_23_shift = Math.pow(2, -23);
     43 mantissa_29_shift = Math.pow(2, -23-29);
     44 
     45 // Javascript implementation of IEEE 754 to test double to single conversion.
     46 function ieee754float(sign_bit,
     47                       exponent_bits,
     48                       mantissa_23_bits,
     49                       mantissa_29_bits) {
     50   this.sign_bit = sign_bit & 1;
     51   this.exponent_bits = exponent_bits & ((1 << 11) - 1);
     52   this.mantissa_23_bits = mantissa_23_bits & ((1 << 23) - 1);
     53   this.mantissa_29_bits = mantissa_29_bits & ((1 << 29) - 1);
     54 }
     55 
     56 ieee754float.prototype.returnSpecial = function() {
     57   if (mantissa_23_bits == 0 && mantissa_29_bits == 0) return sign * Infinity;
     58   return NaN;
     59 }
     60 
     61 ieee754float.prototype.toDouble = function() {
     62   var sign = this.sign_bit ? -1 : 1;
     63   var exponent = this.exponent_bits - 1023;
     64   if (exponent == -1023) returnSpecial();
     65   var mantissa = 1 + this.mantissa_23_bits * mantissa_23_shift +
     66                      this.mantissa_29_bits * mantissa_29_shift;
     67   return sign * Math.pow(2, exponent) * mantissa;
     68 }
     69 
     70 ieee754float.prototype.toSingle = function() {
     71   var sign = this.sign_bit ? -1 : 1;
     72   var exponent = this.exponent_bits - 1023;
     73   if (exponent == -1023) returnSpecial();
     74   if (exponent > 127) return sign * Infinity;
     75   if (exponent < -126) return this.toSingleSubnormal(sign, exponent);
     76   var round = this.mantissa_29_bits >> 28;
     77   var mantissa = 1 + (this.mantissa_23_bits + round) * mantissa_23_shift;
     78   return sign * Math.pow(2, exponent) * mantissa;
     79 }
     80 
     81 ieee754float.prototype.toSingleSubnormal = function(sign, exponent) {
     82   var shift = -126 - exponent;
     83   if (shift > 24) return sign * 0;
     84   var round_mask = 1 << (shift - 1);
     85   var mantissa_23_bits = this.mantissa_23_bits + (1 << 23);
     86   var round = ((mantissa_23_bits & round_mask) != 0) | 0;
     87   if (round) {  // Round to even if tied.
     88     var tied_mask = round_mask - 1;
     89     var result_last_bit_mask = 1 << shift;
     90     var tied = this.mantissa_29_bits == 0 &&
     91                (mantissa_23_bits & tied_mask ) == 0;
     92     var result_already_even = (mantissa_23_bits & result_last_bit_mask) == 0;
     93     if (tied && result_already_even) round = 0;
     94   }
     95   mantissa_23_bits >>= shift;
     96   var mantissa = (mantissa_23_bits + round) * mantissa_23_shift;
     97   return sign * Math.pow(2, -126) * mantissa;
     98 }
     99 
    100 
    101 var pi = new ieee754float(0, 0x400, 0x490fda, 0x14442d18);
    102 assertEquals(pi.toSingle(), opt(pi.toDouble()));
    103 assertEquals(pi.toSingle(), unopt(pi.toDouble()));
    104 
    105 
    106 function fuzz_mantissa(sign, exp, m1inc, m2inc) {
    107   for (var m1 = 0; m1 < (1 << 23); m1 += m1inc) {
    108     for (var m2 = 0; m2 < (1 << 29); m2 += m2inc) {
    109       var float = new ieee754float(sign, exp, m1, m2);
    110       assertEquals(float.toSingle(), unopt(float.toDouble()));
    111       assertEquals(float.toSingle(), opt(float.toDouble()));
    112     }
    113   }
    114 }
    115 
    116 for (var sign = 0; sign < 2; sign++) {
    117   for (var exp = 1024 - 170; exp < 1024 + 170; exp++) {
    118     fuzz_mantissa(sign, exp, 1337 * exp - sign, 127913 * exp - sign);
    119   }
    120 }
    121