asm-wasm-i32.js 4.59 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

function WrapInAsmModule(func) {
  function MODULE_NAME(stdlib) {
    "use asm";
    var imul = stdlib.Math.imul;
11 12 13
    var Math_max = stdlib.Math.max;
    var Math_min = stdlib.Math.min;
    var Math_abs = stdlib.Math.abs;
14 15 16 17 18 19 20 21 22 23 24 25

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

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

  print("Testing " + asmfunc.name + " (js)...");
32
  var js_module = eval("(" + nonasm_source + ")")(stdlib);
33 34 35 36
  expect(js_module);

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

const imul = Math.imul;
42 43 44
const Math_max = Math.max;
const Math_min = Math.min;
const Math_abs = Math.abs;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

function i32_add(a, b) {
  a = a | 0;
  b = b | 0;
  return (a + b) | 0;
}

function i32_sub(a, b) {
  a = a | 0;
  b = b | 0;
  return (a - b) | 0;
}

function i32_mul(a, b) {
  a = a | 0;
  b = b | 0;
  return imul(a, b) | 0;
}

function i32_div(a, b) {
  a = a | 0;
  b = b | 0;
67
  return ((a | 0) / (b | 0)) | 0;
68 69 70 71 72
}

function i32_mod(a, b) {
  a = a | 0;
  b = b | 0;
73
  return ((a | 0) % (b | 0)) | 0;
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
}

function i32_and(a, b) {
  a = a | 0;
  b = b | 0;
  return (a & b) | 0;
}

function i32_or(a, b) {
  a = a | 0;
  b = b | 0;
  return (a | b) | 0;
}

function i32_xor(a, b) {
  a = a | 0;
  b = b | 0;
  return (a ^ b) | 0;
}

function i32_shl(a, b) {
  a = a | 0;
  b = b | 0;
  return (a << b) | 0;
}

function i32_shr(a, b) {
  a = a | 0;
  b = b | 0;
  return (a >> b) | 0;
}

function i32_sar(a, b) {
  a = a | 0;
  b = b | 0;
  return (a >>> b) | 0;
}

function i32_eq(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) == (b | 0)) {
    return 1;
  }
  return 0;
}

function i32_ne(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) < (b | 0)) {
    return 1;
  }
  return 0;
}

function i32_lt(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) < (b | 0)) {
    return 1;
  }
  return 0;
}

function i32_lteq(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) <= (b | 0)) {
    return 1;
  }
  return 0;
}

function i32_gt(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) > (b | 0)) {
    return 1;
  }
  return 0;
}

function i32_gteq(a, b) {
  a = a | 0;
  b = b | 0;
  if ((a | 0) >= (b | 0)) {
    return 1;
  }
  return 0;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
function i32_min(a, b) {
  a = a | 0;
  b = b | 0;
  return Math_min(a | 0, b | 0) | 0;
}

function i32_max(a, b) {
  a = a | 0;
  b = b | 0;
  return Math_max(a | 0, b | 0) | 0;
}

function i32_abs(a) {
  a = a | 0;
  return Math_abs(a | 0) | 0;
}

183 184 185 186 187 188 189 190 191 192
function i32_neg(a) {
  a = a | 0;
  return (-a) | 0;
}

function i32_invert(a) {
  a = a | 0;
  return (~a) | 0;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
var inputs = [
  0, 1, 2, 3, 4,
  10, 20, 30, 31, 32, 33, 100, 2000,
  30000, 400000, 5000000,
  100000000, 2000000000,
  2147483646,
  2147483647,
  2147483648,
  2147483649,
  0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344,
  0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
  0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00,
  0x761c4761, 0x80000000, 0x88888888, 0xa0000000, 0xdddddddd, 0xe0000000,
  0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff,
  0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff,
  0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
  -1, -2, -3, -4,
  -10, -20, -30, -31, -32, -33, -100, -2000,
  -30000, -400000, -5000000,
  -100000000, -2000000000,
  -2147483646,
  -2147483647,
  -2147483648,
  -2147483649,
];

var funcs = [
  i32_add,
  i32_sub,
  i32_mul,
223 224
  i32_div,
  i32_mod,
225 226 227
  i32_and,
  i32_or,
  i32_xor,
228 229 230
  i32_shl,
  i32_shr,
  i32_sar,
231 232 233 234 235 236
  i32_eq,
  i32_ne,
  i32_lt,
  i32_lteq,
  i32_gt,
  i32_gteq,
237 238
  i32_min,
  i32_max,
239 240 241
  i32_abs,
  i32_neg,
  i32_invert,
242 243 244 245
];

(function () {
  for (func of funcs) {
246
    RunAsmJsTest(WrapInAsmModule(func), function (module) {
247 248 249 250 251 252 253 254 255
      if (func.length == 1) {
        for (a of inputs) {
          assertEquals(func(a), module.main(a));
        }
      } else {
        for (a of inputs) {
          for (b of inputs) {
            assertEquals(func(a, b), module.main(a, b));
          }
256 257 258 259 260 261
        }
      }
    });
  }

})();