Home | History | Annotate | Download | only in wasm
      1 // Copyright 2016 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: --expose-wasm
      6 
      7 (function TestStdlibConstants() {
      8   function Module(stdlib) {
      9     "use asm";
     10 
     11     var StdlibInfinity = stdlib.Infinity;
     12     var StdlibNaN = stdlib.NaN;
     13     var StdlibMathE = stdlib.Math.E;
     14     var StdlibMathLN10 = stdlib.Math.LN10;
     15     var StdlibMathLN2 = stdlib.Math.LN2;
     16     var StdlibMathLOG2E = stdlib.Math.LOG2E;
     17     var StdlibMathLOG10E = stdlib.Math.LOG10E;
     18     var StdlibMathPI = stdlib.Math.PI;
     19     var StdlibMathSQRT1_2 = stdlib.Math.SQRT1_2;
     20     var StdlibMathSQRT2 = stdlib.Math.SQRT2;
     21 
     22     function caller() {
     23       if (StdlibInfinity != 1.0 / 0.0) return 0;
     24       if (StdlibMathE != 2.718281828459045) return 0;
     25       if (StdlibMathLN10 != 2.302585092994046) return 0;
     26       if (StdlibMathLN2 != 0.6931471805599453) return 0;
     27       if (StdlibMathLOG2E != 1.4426950408889634) return 0;
     28       if (StdlibMathLOG10E != 0.4342944819032518) return 0;
     29       if (StdlibMathPI != 3.141592653589793) return 0;
     30       if (StdlibMathSQRT1_2 != 0.7071067811865476) return 0;
     31       if (StdlibMathSQRT2 != 1.4142135623730951) return 0;
     32       return 1;
     33     }
     34 
     35     function nanCheck() {
     36       return +StdlibNaN;
     37     }
     38 
     39     return {caller:caller, nanCheck:nanCheck};
     40   }
     41 
     42   var m =Wasm.instantiateModuleFromAsm(Module.toString());
     43   assertEquals(1, m.caller());
     44   assertTrue(isNaN(m.nanCheck()));
     45 })();
     46 
     47 
     48 (function TestStdlibFunctionsInside() {
     49   function Module(stdlib) {
     50     "use asm";
     51 
     52     var StdlibMathCeil = stdlib.Math.ceil;
     53     var StdlibMathFloor = stdlib.Math.floor;
     54     var StdlibMathSqrt = stdlib.Math.sqrt;
     55     var StdlibMathAbs = stdlib.Math.abs;
     56     var StdlibMathMin = stdlib.Math.min;
     57     var StdlibMathMax = stdlib.Math.max;
     58 
     59     var StdlibMathAcos = stdlib.Math.acos;
     60     var StdlibMathAsin = stdlib.Math.asin;
     61     var StdlibMathAtan = stdlib.Math.atan;
     62     var StdlibMathCos = stdlib.Math.cos;
     63     var StdlibMathSin = stdlib.Math.sin;
     64     var StdlibMathTan = stdlib.Math.tan;
     65     var StdlibMathExp = stdlib.Math.exp;
     66     var StdlibMathLog = stdlib.Math.log;
     67 
     68     var StdlibMathAtan2 = stdlib.Math.atan2;
     69     var StdlibMathPow = stdlib.Math.pow;
     70     var StdlibMathImul = stdlib.Math.imul;
     71 
     72     var fround = stdlib.Math.fround;
     73 
     74     function deltaEqual(x, y) {
     75       x = +x;
     76       y = +y;
     77       var t = 0.0;
     78       t = x - y;
     79       if (t < 0.0) {
     80         t = t * -1.0;
     81       }
     82       return (t < 1.0e-13) | 0;
     83     }
     84 
     85     function caller() {
     86       if (!deltaEqual(StdlibMathSqrt(123.0), 11.090536506409418)) return 0;
     87       if (StdlibMathSqrt(fround(256.0)) != fround(16.0)) return 0;
     88       if (StdlibMathCeil(123.7) != 124.0) return 0;
     89       if (StdlibMathCeil(fround(123.7)) != fround(124.0)) return 0;
     90       if (StdlibMathFloor(123.7) != 123.0) return 0;
     91       if (StdlibMathFloor(fround(123.7)) != fround(123.0)) return 0;
     92       if (StdlibMathAbs(-123.0) != 123.0) return 0;
     93       if (StdlibMathAbs(fround(-123.0)) != fround(123.0)) return 0;
     94       if (StdlibMathMin(123.4, 1236.4) != 123.4) return 0;
     95       if (StdlibMathMin(fround(123.4),
     96             fround(1236.4)) != fround(123.4)) return 0;
     97       if (StdlibMathMax(123.4, 1236.4) != 1236.4) return 0;
     98       if (StdlibMathMax(fround(123.4), fround(1236.4))
     99           != fround(1236.4)) return 0;
    100 
    101       if (!deltaEqual(StdlibMathAcos(0.1), 1.4706289056333368)) return 0;
    102       if (!deltaEqual(StdlibMathAsin(0.2), 0.2013579207903308)) return 0;
    103       if (!deltaEqual(StdlibMathAtan(0.2), 0.19739555984988078)) return 0;
    104       if (!deltaEqual(StdlibMathCos(0.2), 0.9800665778412416)) return 0;
    105       if (!deltaEqual(StdlibMathSin(0.2), 0.19866933079506122)) return 0;
    106       if (!deltaEqual(StdlibMathTan(0.2), 0.20271003550867250)) return 0;
    107       if (!deltaEqual(StdlibMathExp(0.2), 1.2214027581601699)) return 0;
    108       if (!deltaEqual(StdlibMathLog(0.2), -1.6094379124341003)) return 0;
    109 
    110       if (StdlibMathImul(6, 7) != 42) return 0;
    111       if (!deltaEqual(StdlibMathAtan2(6.0, 7.0), 0.7086262721276703)) return 0;
    112       if (StdlibMathPow(6.0, 7.0) != 279936.0) return 0;
    113 
    114       return 1;
    115     }
    116 
    117     return {caller:caller};
    118   }
    119 
    120   var m = Wasm.instantiateModuleFromAsm(Module.toString());
    121   assertEquals(1, m.caller());
    122 })();
    123 
    124 
    125 (function TestStdlibFunctionOutside() {
    126   function looseEqual(x, y, delta) {
    127     if (delta === undefined) {
    128       delta = 1.0e-10;
    129     }
    130     if (isNaN(x) && isNaN(y)) {
    131       return true;
    132     }
    133     if (!isFinite(x) && !isFinite(y)) {
    134       return true;
    135     }
    136     x = +x;
    137     y = +y;
    138     var t = 0.0;
    139     t = x - y;
    140     if (t < 0.0) {
    141       t = t * -1.0;
    142     }
    143     return (t < delta) | 0;
    144   }
    145 
    146   function plainEqual(x, y) {
    147     if (isNaN(x) && isNaN(y)) {
    148       return true;
    149     }
    150     return x === y;
    151   }
    152 
    153   function Module(stdlib) {
    154     "use asm";
    155     var ceil = stdlib.Math.ceil;
    156     var floor = stdlib.Math.floor;
    157     var sqrt = stdlib.Math.sqrt;
    158     var abs = stdlib.Math.abs;
    159     var fround = stdlib.Math.fround;
    160     var fround2 = stdlib.Math.fround;
    161 
    162     var acos = stdlib.Math.acos;
    163     var asin = stdlib.Math.asin;
    164     var atan = stdlib.Math.atan;
    165     var cos = stdlib.Math.cos;
    166     var sin = stdlib.Math.sin;
    167     var tan = stdlib.Math.tan;
    168     var exp = stdlib.Math.exp;
    169     var log = stdlib.Math.log;
    170 
    171     var atan2 = stdlib.Math.atan2;
    172     var pow = stdlib.Math.pow;
    173     var imul = stdlib.Math.imul;
    174     var min = stdlib.Math.min;
    175     var max = stdlib.Math.max;
    176 
    177     function ceil_f64(x) { x = +x; return +ceil(x); }
    178     function ceil_f32(x) { x = fround(x); return fround(ceil(x)); }
    179 
    180     function floor_f64(x) { x = +x; return +floor(x); }
    181     function floor_f32(x) { x = fround(x); return fround(floor(x)); }
    182 
    183     function sqrt_f64(x) { x = +x; return +sqrt(x); }
    184     function sqrt_f32(x) { x = fround(x); return fround(sqrt(x)); }
    185 
    186     function abs_f64(x) { x = +x; return +abs(x); }
    187     function abs_f32(x) { x = fround(x); return fround(abs(x)); }
    188     function abs_i32(x) { x = x | 0; return abs(x|0) | 0; }
    189 
    190     function acos_f64(x) { x = +x; return +acos(x); }
    191     function asin_f64(x) { x = +x; return +asin(x); }
    192     function atan_f64(x) { x = +x; return +atan(x); }
    193     function cos_f64(x) { x = +x; return +cos(x); }
    194     function sin_f64(x) { x = +x; return +sin(x); }
    195     function tan_f64(x) { x = +x; return +tan(x); }
    196     function exp_f64(x) { x = +x; return +exp(x); }
    197     function log_f64(x) { x = +x; return +log(x); }
    198 
    199     function atan2_f64(x, y) { x = +x; y = +y; return +atan2(x, y); }
    200     function pow_f64(x, y) { x = +x; y = +y; return +atan2(x, y); }
    201 
    202     function imul_i32(x, y) { x = x | 0; y = y | 0; return imul(x, y) | 0; }
    203     function imul_u32(x, y) {
    204       x = x | 0; y = y | 0; return imul(x>>>0, y>>>0) | 0; }
    205 
    206     // type -> f32
    207     function fround_i32(x) { x = x | 0; return fround(x|0); }
    208     function fround_u32(x) { x = x | 0; return fround(x>>>0); }
    209     function fround_f32(x) { x = fround(x); return fround(x); }
    210     function fround_f64(x) { x = +x; return fround(x); }
    211 
    212     // type -> f32 -> type
    213     function fround2_i32(x) { x = x | 0; return ~~fround2(x|0) | 0; }
    214     function fround2_u32(x) { x = x | 0; return ~~fround2(x>>>0) | 0; }
    215     function fround2_f32(x) { x = fround2(x); return fround2(x); }
    216     function fround2_f64(x) { x = +x; return +fround2(x); }
    217 
    218     function min_i32(x, y) { x = x | 0; y = y | 0; return min(x|0, y|0) | 0; }
    219     function min_f32(x, y) {
    220       x = fround(x); y = fround(y); return fround(min(x, y)); }
    221     function min_f64(x, y) { x = +x; y = +y; return +min(x, y); }
    222 
    223     function max_i32(x, y) { x = x | 0; y = y | 0; return max(x|0, y|0) | 0; }
    224     function max_f32(x, y) {
    225       x = fround(x); y = fround(y); return fround(max(x, y)); }
    226     function max_f64(x, y) { x = +x; y = +y; return +max(x, y); }
    227 
    228     return {
    229       ceil_f64: ceil_f64,
    230       ceil_f32: ceil_f32,
    231       floor_f64: floor_f64,
    232       floor_f32: floor_f32,
    233       sqrt_f64: sqrt_f64,
    234       sqrt_f32: sqrt_f32,
    235       abs_f64: abs_f64,
    236       abs_f32: abs_f32,
    237       abs_i32: abs_i32,
    238       acos_f64: acos_f64,
    239       asin_f64: asin_f64,
    240       atan_f64: atan_f64,
    241       cos_f64: cos_f64,
    242       sin_f64: sin_f64,
    243       tan_f64: tan_f64,
    244       exp_f64: exp_f64,
    245       log_f64: log_f64,
    246       imul_i32: imul_i32,
    247       imul_u32: imul_u32,
    248       fround_i32: fround_i32,
    249       fround_u32: fround_u32,
    250       fround_f32: fround_f32,
    251       fround_f64: fround_f64,
    252       fround2_i32: fround2_i32,
    253       fround2_u32: fround2_u32,
    254       fround2_f32: fround2_f32,
    255       fround2_f64: fround2_f64,
    256       min_i32: min_i32,
    257       min_f32: min_f32,
    258       min_f64: min_f64,
    259       max_i32: max_i32,
    260       max_f32: max_f32,
    261       max_f64: max_f64,
    262     };
    263   }
    264   var m = Wasm.instantiateModuleFromAsm(Module.toString());
    265   var values = {
    266     i32: [
    267       0, 1, -1, 123, 456, -123, -456,
    268       0x40000000, 0x7FFFFFFF, -0x80000000,
    269     ],
    270     u32: [
    271       0, 1, 123, 456,
    272       0x40000000, 0x7FFFFFFF, 0xFFFFFFFF, 0x80000000,
    273     ],
    274     f32: [
    275       0, -0, 1, -1, 0.25, 0.125, 0.9, -0.9, 1.414,
    276       0x7F, -0x80, -0x8000, -0x80000000,
    277       0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN,
    278     ],
    279     f64: [
    280       0, -0, 1, -1, 0.25, 0.125, 0.9, -0.9, 1.414,
    281       0x7F, -0x80, -0x8000, -0x80000000,
    282       0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN,
    283     ],
    284   };
    285   var converts = {
    286     i32: function(x) { return x | 0; },
    287     u32: function(x) { return x >>> 0; },
    288     f32: function(x) { return Math.fround(x); },
    289     f64: function(x) { return x; },
    290   };
    291   var two_args = {atan2: true, pow: true, imul: true,
    292                   min: true, max: true};
    293   var funcs = {
    294     ceil: ['f32', 'f64'],
    295     floor: ['f32', 'f64'],
    296     sqrt: ['f32', 'f64'],
    297     abs: ['i32', 'f32', 'f64'],
    298     acos: ['f64'],
    299     asin: ['f64'],
    300     atan: ['f64'],
    301     cos: ['f64'],
    302     sin: ['f64'],
    303     tan: ['f64'],
    304     exp: ['f64'],
    305     log: ['f64'],
    306     imul: ['i32', 'u32'],
    307     fround: ['i32', 'u32', 'f32', 'f64'],
    308     min: ['i32', 'f32', 'f64'],
    309     max: ['i32', 'f32', 'f64'],
    310   };
    311   var per_func_equals = {
    312     // JS uses fdlib for these, so they may not match.
    313     // ECMAscript does not required them to have a particular precision.
    314     exp_f64: function(x, y) { return looseEqual(x, y, 1e55); },
    315     sqrt_f32: function(x, y) { return looseEqual(x, y, 1e-5); },
    316     cos_f64: looseEqual,
    317     sin_f64: looseEqual,
    318     tan_f64: looseEqual,
    319     // TODO(bradnelson):
    320     // Figure out why some builds (avx2, rel_ng) return a uint.
    321     imul_u32: function(x, y) { return (x | 0) === (y | 0); },
    322   };
    323   for (var func in funcs) {
    324     var types = funcs[func];
    325     for (var i = 0; i < types.length; i++) {
    326       var type = types[i];
    327       var interesting = values[type];
    328       for (var j = 0; j < interesting.length; j++) {
    329         for (var k = 0; k < interesting.length; k++) {
    330           var val0 = interesting[j];
    331           var val1 = interesting[k];
    332           var name = func + '_' + type;
    333           if (func === 'fround') {
    334             // fround returns f32 regardless of input.
    335             var expected = Math[func](val0);
    336             var actual = m[name](val0);
    337           } else if (two_args[func]) {
    338             var expected = converts[type](Math[func](val0, val1));
    339             var actual = m[name](val0, val1);
    340           } else {
    341             var expected = converts[type](Math[func](val0, val1));
    342             var actual = m[name](val0, val1);
    343           }
    344           var compare = per_func_equals[name];
    345           if (compare === undefined) {
    346             compare = plainEqual;
    347           }
    348           assertTrue(typeof(compare) === 'function');
    349           if (!compare(expected, actual)) {
    350             print(expected + ' !== ' + actual + ' for ' + name +
    351                   ' with input ' + val0 + ' ' + val1);
    352             assertTrue(false);
    353           }
    354         }
    355       }
    356     }
    357   }
    358 })();
    359