asm-wasm-f32.js 4.96 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

function WrapInAsmModule(func) {
  function MODULE_NAME(stdlib) {
    "use asm";
    var fround = stdlib.Math.fround;
    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;

    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 + ")");
}

29
function RunAsmJsTest(asmfunc, expect) {
30 31 32 33 34
  var asm_source = asmfunc.toString();
  var nonasm_source = asm_source.replace(new RegExp("use asm"), "");
  var stdlib = {Math: Math};

  print("Testing " + asmfunc.name + " (js)...");
35
  var js_module = eval("(" + nonasm_source + ")")(stdlib);
36 37 38 39
  expect(js_module);

  print("Testing " + asmfunc.name + " (asm.js)...");
  var asm_module = asmfunc(stdlib);
40
  assertTrue(%IsAsmWasmCode(asmfunc));
41 42 43
  expect(asm_module);
}

44 45 46 47 48 49 50
var fround = Math.fround;
var Math_ceil = Math.ceil;
var Math_floor = Math.floor;
var Math_sqrt = Math.sqrt;
var Math_abs = Math.abs;
var Math_min = Math.min;
var Math_max = Math.max;
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

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

function f32_sub(a, b) {
  a = fround(a);
  b = fround(b);
  return fround(fround(a) - fround(b));
}

function f32_mul(a, b) {
  a = fround(a);
  b = fround(b);
  return fround(fround(a) * fround(b));
}

function f32_div(a, b) {
  a = fround(a);
  b = fround(b);
  return fround(fround(a) / fround(b));
}

function f32_ceil(a) {
  a = fround(a);
  return fround(Math_ceil(fround(a)));
}

function f32_floor(a) {
  a = fround(a);
  return fround(Math_floor(fround(a)));
}

function f32_sqrt(a) {
  a = fround(a);
  return fround(Math_sqrt(fround(a)));
}

function f32_abs(a) {
  a = fround(a);
  return fround(Math_abs(fround(a)));
}

function f32_min(a, b) {
  a = fround(a);
  b = fround(b);
  return fround(Math_min(fround(a), fround(b)));
}

function f32_max(a, b) {
  a = fround(a);
  b = fround(b);
  return fround(Math_max(fround(a), fround(b)));
}

function f32_eq(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) == fround(b)) {
    return 1;
  }
  return 0;
}

function f32_ne(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) != fround(b)) {
    return 1;
  }
  return 0;
}

function f32_lt(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) < fround(b)) {
    return 1;
  }
  return 0;
}

function f32_lteq(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) <= fround(b)) {
    return 1;
  }
  return 0;
}

function f32_gt(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) > fround(b)) {
    return 1;
  }
  return 0;
}

function f32_gteq(a, b) {
  a = fround(a);
  b = fround(b);
  if (fround(a) >= fround(b)) {
    return 1;
  }
  return 0;
}

162 163 164 165 166
function f32_neg(a) {
  a = fround(a);
  return fround(-a);
}

167 168 169

var inputs = [
  0, 1, 2, 3, 4,
170 171 172
  NaN,
  Infinity,
  -Infinity,
173 174 175 176 177 178 179 180
  10, 20, 30, 31, 32, 33, 100, 2000,
  30000, 400000, 5000000,
  100000000, 2000000000,
  2147483646,
  2147483647,
  2147483648,
  2147483649,
  0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344,
181
  0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
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
  0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00,
  0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff,
  -0,
  -1, -2, -3, -4,
  -10, -20, -30, -31, -32, -33, -100, -2000,
  -30000, -400000, -5000000,
  -100000000, -2000000000,
  -2147483646,
  -2147483647,
  -2147483648,
  -2147483649,
  0.1,
  1.1e-2,
  1.2e-4,
  1.3e-8,
  1.4e-11,
  1.5e-12,
  1.6e-13
];

var funcs = [
  f32_add,
  f32_sub,
  f32_mul,
  f32_div,
207 208
  f32_ceil,
  f32_floor,
209 210 211 212 213 214 215 216 217 218
// TODO(bradnelson) f32_sqrt,
// TODO(bradnelson) f32_abs,
// TODO(bradnelson) f32_min is wrong for -0
// TODO(bradnelson) f32_max is wrong for -0
  f32_eq,
  f32_ne,
  f32_lt,
  f32_lteq,
  f32_gt,
  f32_gteq,
219
  f32_neg,
220 221 222 223
];

(function () {
  for (func of funcs) {
224
    RunAsmJsTest(WrapInAsmModule(func), function (module) {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      if (func.length == 1) {
        for (a of inputs) {
          assertEquals(func(a), module.main(a));
          assertEquals(func(a / 11), module.main(a / 11));
          assertEquals(func(a / 430.9), module.main(a / 430.9));
          assertEquals(func(a / -31.1), module.main(a / -31.1));
        }
      } else {
        for (a of inputs) {
          for (b of inputs) {
            assertEquals(func(a, b), module.main(a, b));
            assertEquals(func(a / 11,  b), module.main(a / 11, b));
            assertEquals(func(a, b / 420.9), module.main(a, b / 420.9));
            assertEquals(func(a / -31.1, b), module.main(a / -31.1, b));
          }
240 241 242 243 244 245
        }
      }
    });
  }

})();