js/src/jit-test/tests/ion/testFloat32-correctness.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 setJitCompilerOption("ion.usecount.trigger", 50);
     3 var f32 = new Float32Array(10);
     5 function test(setup, f) {
     6     if (f === undefined) {
     7         f = setup;
     8         setup = function(){};
     9     }
    10     setup();
    11     for(var n = 200; n; --n) {
    12         f();
    13     }
    14 }
    16 // Basic arithmetic
    17 function setupBasicArith() {
    18     f32[0] = -Infinity;
    19     f32[1] = -1;
    20     f32[2] = -0;
    21     f32[3] = 0;
    22     f32[4] = 1.337;
    23     f32[5] = 42;
    24     f32[6] = Infinity;
    25     f32[7] = NaN;
    26 }
    27 function basicArith() {
    28     for (var i = 0; i < 7; ++i) {
    29         var opf = Math.fround(f32[i] + f32[i+1]);
    30         var opd = (1 / (1 / f32[i])) + f32[i+1];
    31         assertFloat32(opf, true);
    32         assertFloat32(opd, false);
    33         assertEq(opf, Math.fround(opd));
    35         opf = Math.fround(f32[i] - f32[i+1]);
    36         opd = (1 / (1 / f32[i])) - f32[i+1];
    37         assertFloat32(opf, true);
    38         assertFloat32(opd, false);
    39         assertEq(opf, Math.fround(opd));
    41         opf = Math.fround(f32[i] * f32[i+1]);
    42         opd = (1 / (1 / f32[i])) * f32[i+1];
    43         assertFloat32(opf, true);
    44         assertFloat32(opd, false);
    45         assertEq(opf, Math.fround(opd));
    47         opf = Math.fround(f32[i] / f32[i+1]);
    48         opd = (1 / (1 / f32[i])) / f32[i+1];
    49         assertFloat32(opf, true);
    50         assertFloat32(opd, false);
    51         assertEq(opf, Math.fround(opd));
    52     }
    53 }
    54 test(setupBasicArith, basicArith);
    56 // MAbs
    57 function setupAbs() {
    58     f32[0] = -0;
    59     f32[1] = 0;
    60     f32[2] = -3.14159;
    61     f32[3] = 3.14159;
    62     f32[4] = -Infinity;
    63     f32[5] = Infinity;
    64     f32[6] = NaN;
    65 }
    66 function abs() {
    67     for(var i = 0; i < 7; ++i) {
    68         assertEq( Math.fround(Math.abs(f32[i])), Math.abs(f32[i]) );
    69     }
    70 }
    71 test(setupAbs, abs);
    73 // MSqrt
    74 function setupSqrt() {
    75     f32[0] = 0;
    76     f32[1] = 1;
    77     f32[2] = 4;
    78     f32[3] = -1;
    79     f32[4] = Infinity;
    80     f32[5] = NaN;
    81     f32[6] = 13.37;
    82 }
    83 function sqrt() {
    84     for(var i = 0; i < 7; ++i) {
    85         var sqrtf = Math.fround(Math.sqrt(f32[i]));
    86         var sqrtd = 1 + Math.sqrt(f32[i]) - 1; // force no float32 by chaining arith ops
    87         assertEq( sqrtf, Math.fround(sqrtd) );
    88     }
    89 }
    90 test(setupSqrt, sqrt);
    92 // MTruncateToInt32
    93 // The only way to get a MTruncateToInt32 with a Float32 input is to use Math.imul
    94 function setupTruncateToInt32() {
    95     f32[0] = -1;
    96     f32[1] = 4;
    97     f32[2] = 5.13;
    98 }
    99 function truncateToInt32() {
   100     assertEq( Math.imul(f32[0], f32[1]), Math.imul(-1, 4) );
   101     assertEq( Math.imul(f32[1], f32[2]), Math.imul(4, 5) );
   102 }
   103 test(setupTruncateToInt32, truncateToInt32);
   105 // MCompare
   106 function comp() {
   107     for(var i = 0; i < 9; ++i) {
   108         assertEq( f32[i] < f32[i+1], true );
   109     }
   110 }
   111 function setupComp() {
   112     f32[0] = -Infinity;
   113     f32[1] = -1;
   114     f32[2] = -0.01;
   115     f32[3] = 0;
   116     f32[4] = 0.01;
   117     f32[5] = 1;
   118     f32[6] = 10;
   119     f32[7] = 13.37;
   120     f32[8] = 42;
   121     f32[9] = Infinity;
   122 }
   123 test(setupComp, comp);
   125 // MNot
   126 function setupNot() {
   127     f32[0] = -0;
   128     f32[1] = 0;
   129     f32[2] = 1;
   130     f32[3] = NaN;
   131     f32[4] = Infinity;
   132     f32[5] = 42;
   133     f32[5] = -23;
   134 }
   135 function not() {
   136     assertEq( !f32[0], true );
   137     assertEq( !f32[1], true );
   138     assertEq( !f32[2], false );
   139     assertEq( !f32[3], true );
   140     assertEq( !f32[4], false );
   141     assertEq( !f32[5], false );
   142     assertEq( !f32[6], false );
   143 }
   144 test(setupNot, not);
   146 // MToInt32
   147 var str = "can haz cheezburger? okthxbye;";
   148 function setupToInt32() {
   149     f32[0] = 0;
   150     f32[1] = 1;
   151     f32[2] = 2;
   152     f32[3] = 4;
   153     f32[4] = 5;
   154 }
   155 function testToInt32() {
   156     assertEq(str[f32[0]], 'c');
   157     assertEq(str[f32[1]], 'a');
   158     assertEq(str[f32[2]], 'n');
   159     assertEq(str[f32[3]], 'h');
   160     assertEq(str[f32[4]], 'a');
   161 }
   162 test(setupToInt32, testToInt32);
   164 function setupBailoutToInt32() {
   165     f32[0] = .5;
   166 }
   167 function testBailoutToInt32() {
   168     assertEq(typeof str[f32[0]], 'undefined');
   169 }
   170 test(setupBailoutToInt32, testBailoutToInt32);
   172 // MMath (no trigo - see also testFloat32-trigo.js
   173 function assertNear(a, b) {
   174     var r = (a != a && b != b) || Math.abs(a-b) < 1e-1 || a === b;
   175     if (!r) {
   176         print('Precision error: ');
   177         print(new Error().stack);
   178         print('Got', a, ', expected near', b);
   179         assertEq(false, true);
   180     }
   181 }
   183 function setupOtherMath() {
   184     setupComp();
   185     f32[8] = 4.2;
   186 }
   187 function otherMath() {
   188     for (var i = 0; i < 9; ++i) {
   189         assertNear(Math.fround(Math.exp(f32[i])), Math.exp(f32[i]));
   190         assertNear(Math.fround(Math.log(f32[i])), Math.log(f32[i]));
   191     }
   192 };
   193 test(setupOtherMath, otherMath);
   195 function setupFloor() {
   196     f32[0] = -5.5;
   197     f32[1] = -0.5;
   198     f32[2] = 0;
   199     f32[3] = 1.5;
   200 }
   201 function setupFloorDouble() {
   202     f32[4] = NaN;
   203     f32[5] = -0;
   204     f32[6] = Infinity;
   205     f32[7] = -Infinity;
   206     f32[8] = Math.pow(2,31); // too big to fit into a int
   207 }
   208 function testFloor() {
   209     for (var i = 0; i < 4; ++i) {
   210         var f = Math.floor(f32[i]);
   211         assertFloat32(f, false); // f is an int32
   213         var g = Math.floor(-0 + f32[i]);
   214         assertFloat32(g, false);
   216         assertEq(f, g);
   217     }
   218 }
   219 function testFloorDouble() {
   220     for (var i = 4; i < 9; ++i) {
   221         var f = Math.fround(Math.floor(f32[i]));
   222         assertFloat32(f, true);
   224         var g = Math.floor(-0 + f32[i]);
   225         assertFloat32(g, false);
   227         assertEq(f, g);
   228     }
   229 }
   230 test(setupFloor, testFloor);
   231 test(setupFloorDouble, testFloorDouble);
   233 function setupRound() {
   234     f32[0] = -5.5;
   235     f32[1] = -0.6;
   236     f32[2] = 1.5;
   237     f32[3] = 1;
   238 }
   239 function setupRoundDouble() {
   240     f32[4] = NaN;
   241     f32[5] = -0.49;          // rounded to -0
   242     f32[6] = Infinity;
   243     f32[7] = -Infinity;
   244     f32[8] = Math.pow(2,31); // too big to fit into a int
   245     f32[9] = -0;
   246 }
   247 function testRound() {
   248     for (var i = 0; i < 4; ++i) {
   249         var r32 = Math.round(f32[i]);
   250         assertFloat32(r32, false); // r32 is an int32
   252         var r64 = Math.round(-0 + f32[i]);
   253         assertFloat32(r64, false);
   255         assertEq(r32, r64);
   256     }
   257 }
   258 function testRoundDouble() {
   259     for (var i = 4; i < 10; ++i) {
   260         var r32 = Math.fround(Math.round(f32[i]));
   261         assertFloat32(r32, true);
   263         var r64 = Math.round(-0 + f32[i]);
   264         assertFloat32(r64, false);
   266         assertEq(r32, r64);
   267     }
   268 }
   269 test(setupRound, testRound);
   270 test(setupRoundDouble, testRoundDouble);
   272 function setupCeil() {
   273     f32[0] = -5.5;
   274     f32[1] = -1.5;
   275     f32[2] = 0;
   276     f32[3] = 1.5;
   277 }
   278 function setupCeilDouble() {
   279     f32[4] = NaN;
   280     f32[5] = -0;
   281     f32[6] = Infinity;
   282     f32[7] = -Infinity;
   283     f32[8] = Math.pow(2,31); // too big to fit into a int
   284 }
   285 function testCeil() {
   286     for(var i = 0; i < 2; ++i) {
   287         var f = Math.ceil(f32[i]);
   288         assertFloat32(f, false);
   290         var g = Math.ceil(-0 + f32[i]);
   291         assertFloat32(g, false);
   293         assertEq(f, g);
   294     }
   295 }
   296 function testCeilDouble() {
   297     for(var i = 4; i < 9; ++i) {
   298         var f = Math.fround(Math.ceil(f32[i]));
   299         assertFloat32(f, true);
   301         var g = Math.ceil(-0 + f32[i]);
   302         assertFloat32(g, false);
   304         assertEq(f, g);
   305     }
   306 }
   307 test(setupCeil, testCeil);
   308 test(setupCeilDouble, testCeilDouble);

mercurial