asm-wasm-f64.js 5.12 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --validate-asm --allow-natives-syntax
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

function WrapInAsmModule(func) {
  function MODULE_NAME(stdlib) {
    "use asm";
    var Math_ceil = stdlib.Math.ceil;
    var Math_floor = stdlib.Math.floor;
    var Math_sqrt = stdlib.Math.sqrt;
    var Math_abs = stdlib.Math.abs;
    var Math_min = stdlib.Math.min;
    var Math_max = stdlib.Math.max;
    var Math_acos = stdlib.Math.acos;
    var Math_asin = stdlib.Math.asin;
    var Math_atan = stdlib.Math.atan;
    var Math_cos = stdlib.Math.cos;
    var Math_sin = stdlib.Math.sin;
    var Math_tan = stdlib.Math.tan;
    var Math_exp = stdlib.Math.exp;
    var Math_log = stdlib.Math.log;
    var Math_atan2 = stdlib.Math.atan2;

    FUNC_BODY
    return {main: FUNC_NAME};
  }

  var source = MODULE_NAME.toString()
    .replace(/MODULE_NAME/g, func.name + "_module")
    .replace(/FUNC_BODY/g, func.toString())
    .replace(/FUNC_NAME/g, func.name);
  return eval("(" + source + ")");
}

37
function RunAsmJsTest(asmfunc, expect) {
38 39 40 41 42
  var asm_source = asmfunc.toString();
  var nonasm_source = asm_source.replace(new RegExp("use asm"), "");
  var stdlib = {Math: Math};

  print("Testing " + asmfunc.name + " (js)...");
43
  var js_module = eval("(" + nonasm_source + ")")(stdlib);
44 45 46 47
  expect(js_module);

  print("Testing " + asmfunc.name + " (asm.js)...");
  var asm_module = asmfunc(stdlib);
48
  assertTrue(%IsAsmWasmCode(asmfunc));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  expect(asm_module);
}

const Math_ceil = Math.ceil;
const Math_floor = Math.floor;
const Math_sqrt = Math.sqrt;
const Math_abs = Math.abs;
const Math_min = Math.min;
const Math_max = Math.max;
const Math_acos = Math.acos;
const Math_asin = Math.asin;
const Math_atan = Math.atan;
const Math_cos = Math.cos;
const Math_sin = Math.sin;
const Math_tan = Math.tan;
const Math_exp = Math.exp;
const Math_log = Math.log;
const Math_atan2 = Math.atan2;

function f64_add(a, b) {
  a = +a;
  b = +b;
  return +(+a + +b);
}

function f64_sub(a, b) {
  a = +a;
  b = +b;
  return +(+a - +b);
}

function f64_mul(a, b) {
  a = +a;
  b = +b;
  return +(+a * +b);
}

function f64_div(a, b) {
  a = +a;
  b = +b;
  return +(+a / +b);
}

function f64_eq(a, b) {
  a = +a;
  b = +b;
  if (+a == +b) {
    return 1;
  }
  return 0;
}

function f64_ne(a, b) {
  a = +a;
  b = +b;
  if (+a != +b) {
    return 1;
  }
  return 0;
}

function f64_lt(a, b) {
  a = +a;
  b = +b;
  if (+a < +b) {
    return 1;
  }
  return 0;
}

function f64_lteq(a, b) {
  a = +a;
  b = +b;
  if (+a <= +b) {
    return 1;
  }
  return 0;
}

function f64_gt(a, b) {
  a = +a;
  b = +b;
  if (+a > +b) {
    return 1;
  }
  return 0;
}

function f64_gteq(a, b) {
  a = +a;
  b = +b;
  if (+a >= +b) {
    return 1;
  }
  return 0;
}

function f64_ceil(a) {
  a = +a;
  return +(Math_ceil(+a));
}

function f64_floor(a) {
  a = +a;
  return +(Math_floor(+a));
}

function f64_sqrt(a) {
  a = +a;
  return +(Math_sqrt(+a));
}

function f64_abs(a) {
  a = +a;
  return +(Math_abs(+a));
}

function f64_min(a, b) {
  a = +a;
  b = +b;
  return +(Math_min(+a, +b));
}

function f64_max(a, b) {
  a = +a;
  b = +b;
  return +(Math_max(+a, +b));
}

function f64_acos(a) {
  a = +a;
  return +Math_acos(+a);
}

function f64_asin(a) {
  a = +a;
  return +Math_asin(+a);
}

function f64_atan(a) {
  a = +a;
  return +Math_atan(+a);
}

function f64_cos(a) {
  a = +a;
  return +Math_cos(+a);
}

function f64_sin(a) {
  a = +a;
  return +Math_sin(+a);
}

function f64_tan(a) {
  a = +a;
  return +Math_tan(+a);
}

208
function f64_exp(a) {
209
  a = +a;
210 211 212 213 214 215
  return +Math_exp(+a);
}

function f64_log(a) {
  a = +a;
  return +Math_log(+a);
216 217
}

218
function f64_atan2(a, b) {
219 220
  a = +a;
  b = +b;
221
  return +Math_atan2(+a, +b);
222 223
}

224
function f64_neg(a) {
225
  a = +a;
226
  return +(-a);
227 228 229 230
}


var inputs = [
Ng Zhi An's avatar
Ng Zhi An committed
231
  0, 1,
232 233 234
  NaN,
  Infinity,
  -Infinity,
235 236 237 238
  2147483646,
  2147483647,
  2147483648,
  2147483649,
Ng Zhi An's avatar
Ng Zhi An committed
239 240 241 242
  4026531840, // 0xf0000000
  4294967293, // 0xfffffffd
  4294967295, // 0xffffffff
  -0, -1,
243 244 245 246 247 248 249 250 251 252
  -2147483646,
  -2147483647,
  -2147483648,
  -2147483649,
  0.1,
  1.1e-2,
  1.6e-13
];

var funcs = [
253 254 255 256
  f64_add,  f64_sub, f64_mul,  f64_div,  f64_eq,    f64_ne,   f64_lt,
  f64_lteq, f64_gt,  f64_gteq, f64_ceil, f64_floor, f64_sqrt, f64_abs,
  f64_neg,  f64_min, f64_max,  f64_acos, f64_asin,  f64_atan, f64_cos,
  f64_sin,  f64_tan, f64_exp,  f64_log,  f64_atan2,
257 258 259 260
];

(function () {
  for (func of funcs) {
261
    RunAsmJsTest(WrapInAsmModule(func), function (module) {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
      if (func.length == 1) {
        for (a of inputs) {
          assertEquals(func(a), module.main(a));
          assertEquals(func(a / 10), module.main(a / 10));
          assertEquals(func(a / 440.9), module.main(a / 440.9));
          assertEquals(func(a / -33.1), module.main(a / -33.1));
        }
      } else {
        for (a of inputs) {
          for (b of inputs) {
            assertEquals(func(a, b), module.main(a, b));
            assertEquals(func(a / 10,  b), module.main(a / 10, b));
            assertEquals(func(a, b / 440.9), module.main(a, b / 440.9));
            assertEquals(func(a / -33.1, b), module.main(a / -33.1, b));
          }
        }
      }
    });
  }
})();