2016-03-23 23:18:44 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
// Flags: --validate-asm --allow-natives-syntax
|
2016-03-23 23:18:44 +00:00
|
|
|
|
|
|
|
function WrapInAsmModule(func) {
|
|
|
|
function MODULE_NAME(stdlib) {
|
|
|
|
"use asm";
|
|
|
|
var imul = stdlib.Math.imul;
|
|
|
|
|
|
|
|
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 + ")");
|
|
|
|
}
|
|
|
|
|
2016-08-23 04:06:52 +00:00
|
|
|
function RunAsmJsTest(asmfunc, expect) {
|
2016-03-23 23:18:44 +00:00
|
|
|
var asm_source = asmfunc.toString();
|
|
|
|
var nonasm_source = asm_source.replace(new RegExp("use asm"), "");
|
|
|
|
var stdlib = {Math: Math};
|
|
|
|
|
|
|
|
print("Testing " + asmfunc.name + " (js)...");
|
2016-08-23 04:06:52 +00:00
|
|
|
var js_module = eval("(" + nonasm_source + ")")(stdlib);
|
2016-03-23 23:18:44 +00:00
|
|
|
expect(js_module);
|
|
|
|
|
|
|
|
print("Testing " + asmfunc.name + " (asm.js)...");
|
|
|
|
var asm_module = asmfunc(stdlib);
|
2016-08-23 04:06:52 +00:00
|
|
|
assertTrue(%IsAsmWasmCode(asmfunc));
|
2016-03-23 23:18:44 +00:00
|
|
|
expect(asm_module);
|
|
|
|
}
|
|
|
|
|
|
|
|
const imul = Math.imul;
|
|
|
|
|
|
|
|
function u32_add(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
2016-03-30 11:37:24 +00:00
|
|
|
return +(((a >>> 0) + (b >>> 0)) >>> 0);
|
2016-03-23 23:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function u32_sub(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
2016-03-30 11:37:24 +00:00
|
|
|
return +(((a >>> 0) - (b >>> 0)) >>> 0);
|
2016-03-23 23:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function u32_mul(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +imul(a >>> 0, b >>> 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_div(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
2016-03-30 11:37:24 +00:00
|
|
|
return +(((a >>> 0) / (b >>> 0)) >>> 0);
|
2016-03-23 23:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function u32_mod(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
2016-03-30 11:37:24 +00:00
|
|
|
return +(((a >>> 0) % (b >>> 0)) >>> 0);
|
2016-03-23 23:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function u32_and(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +((a >>> 0) & (b >>> 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_or(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +((a >>> 0) | (b >>> 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_xor(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +((a >>> 0) ^ (b >>> 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_shl(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +((a >>> 0) << (b >>> 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_shr(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return +((a >>> 0) >> (b >>> 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_sar(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
return ((a >>> 0) >>> (b >>> 0)) | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_eq(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) == (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_ne(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) < (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_lt(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) < (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_lteq(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) <= (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_gt(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) > (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_gteq(a, b) {
|
|
|
|
a = a | 0;
|
|
|
|
b = b | 0;
|
|
|
|
if ((a >>> 0) >= (b >>> 0)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-28 16:36:37 +00:00
|
|
|
function u32_neg(a) {
|
|
|
|
a = a | 0;
|
|
|
|
return (-a) | 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function u32_invert(a) {
|
|
|
|
a = a | 0;
|
|
|
|
return (~a) | 0;
|
|
|
|
}
|
|
|
|
|
2016-03-23 23:18:44 +00:00
|
|
|
|
|
|
|
var inputs = [
|
|
|
|
0, 1, 2, 3, 4,
|
|
|
|
2147483646,
|
2019-09-10 16:09:52 +00:00
|
|
|
2147483647, // max positive int32
|
|
|
|
2147483648, // overflow max positive int32
|
2016-03-23 23:18:44 +00:00
|
|
|
0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c,
|
2019-09-10 16:09:52 +00:00
|
|
|
0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x0003ffff, 0x00001fff,
|
2016-03-23 23:18:44 +00:00
|
|
|
-1, -2, -3, -4,
|
|
|
|
-2147483647,
|
2019-09-10 16:09:52 +00:00
|
|
|
-2147483648, // min negative int32
|
|
|
|
-2147483649, // overflow min negative int32
|
2016-03-23 23:18:44 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
var funcs = [
|
2016-03-30 11:37:24 +00:00
|
|
|
u32_add,
|
|
|
|
u32_sub,
|
|
|
|
u32_div,
|
|
|
|
u32_mod,
|
2016-03-23 23:18:44 +00:00
|
|
|
// TODO(titzer): u32_mul crashes turbofan in asm.js mode
|
|
|
|
u32_and,
|
|
|
|
u32_or,
|
|
|
|
u32_xor,
|
2016-03-30 12:38:47 +00:00
|
|
|
u32_shl,
|
|
|
|
u32_shr,
|
|
|
|
u32_sar,
|
2016-03-23 23:18:44 +00:00
|
|
|
u32_eq,
|
|
|
|
u32_ne,
|
|
|
|
u32_lt,
|
|
|
|
u32_lteq,
|
|
|
|
u32_gt,
|
|
|
|
u32_gteq,
|
2016-09-28 16:36:37 +00:00
|
|
|
u32_neg,
|
|
|
|
u32_invert,
|
2016-03-23 23:18:44 +00:00
|
|
|
// TODO(titzer): u32_min
|
|
|
|
// TODO(titzer): u32_max
|
|
|
|
// TODO(titzer): u32_abs
|
|
|
|
];
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
for (func of funcs) {
|
2016-08-23 04:06:52 +00:00
|
|
|
RunAsmJsTest(WrapInAsmModule(func), function (module) {
|
2016-03-23 23:18:44 +00:00
|
|
|
for (a of inputs) {
|
|
|
|
for (b of inputs) {
|
|
|
|
var expected = func(a, b);
|
|
|
|
assertEquals(expected, module.main(a, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|