Commit 2769a7c4 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Test binary operations for more inputs

Instead of hard-coding one or two inputs per binary operation, use all
inputs provided by FOR_INT32_INPUTS.

R=ahaas@chromium.org

Change-Id: I534227f93068f52d69ea0ff0fcf686a9af034bad
Reviewed-on: https://chromium-review.googlesource.com/753484
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49136}
parent 12eb3e97
......@@ -158,54 +158,68 @@ WASM_EXEC_TEST(Float64Add) {
CHECK_EQ(57, r.Call());
}
void TestInt32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode,
int32_t expected, int32_t a, int32_t b) {
{
WasmRunner<int32_t> r(execution_mode);
// K op K
BUILD(r, WASM_BINOP(opcode, WASM_I32V(a), WASM_I32V(b)));
CHECK_EQ(expected, r.Call());
// clang-format messes up the FOR_INT32_INPUTS macros.
// clang-format off
template<typename ctype>
static void TestInt32Binop(WasmExecutionMode execution_mode, WasmOpcode opcode,
ctype(*expected)(ctype, ctype)) {
FOR_INT32_INPUTS(i) {
FOR_INT32_INPUTS(j) {
WasmRunner<ctype> r(execution_mode);
// Apply {opcode} on two constants.
BUILD(r, WASM_BINOP(opcode, WASM_I32V(*i), WASM_I32V(*j)));
CHECK_EQ(expected(*i, *j), r.Call());
}
}
{
WasmRunner<int32_t, int32_t, int32_t> r(execution_mode);
// a op b
WasmRunner<ctype, ctype, ctype> r(execution_mode);
// Apply {opcode} on two parameters.
BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(expected, r.Call(a, b));
FOR_INT32_INPUTS(i) {
FOR_INT32_INPUTS(j) {
CHECK_EQ(expected(*i, *j), r.Call(*i, *j));
}
}
}
}
WASM_EXEC_TEST(Int32Binops) {
TestInt32Binop(execution_mode, kExprI32Add, 88888888, 33333333, 55555555);
TestInt32Binop(execution_mode, kExprI32Sub, -1111111, 7777777, 8888888);
TestInt32Binop(execution_mode, kExprI32Mul, 65130756, 88734, 734);
TestInt32Binop(execution_mode, kExprI32DivS, -66, -4777344, 72384);
TestInt32Binop(execution_mode, kExprI32DivU, 805306368, 0xF0000000, 5);
TestInt32Binop(execution_mode, kExprI32RemS, -3, -3003, 1000);
TestInt32Binop(execution_mode, kExprI32RemU, 4, 4004, 1000);
TestInt32Binop(execution_mode, kExprI32And, 0xEE, 0xFFEE, 0xFF0000FF);
TestInt32Binop(execution_mode, kExprI32Ior, 0xF0FF00FF, 0xF0F000EE,
0x000F0011);
TestInt32Binop(execution_mode, kExprI32Xor, 0xABCDEF01, 0xABCDEFFF, 0xFE);
TestInt32Binop(execution_mode, kExprI32Shl, 0xA0000000, 0xA, 28);
TestInt32Binop(execution_mode, kExprI32ShrU, 0x07000010, 0x70000100, 4);
TestInt32Binop(execution_mode, kExprI32ShrS, 0xFF000000, 0x80000000, 7);
TestInt32Binop(execution_mode, kExprI32Ror, 0x01000000, 0x80000000, 7);
TestInt32Binop(execution_mode, kExprI32Ror, 0x01000000, 0x80000000, 39);
TestInt32Binop(execution_mode, kExprI32Rol, 0x00000040, 0x80000000, 7);
TestInt32Binop(execution_mode, kExprI32Rol, 0x00000040, 0x80000000, 39);
TestInt32Binop(execution_mode, kExprI32Eq, 1, -99, -99);
TestInt32Binop(execution_mode, kExprI32Ne, 0, -97, -97);
TestInt32Binop(execution_mode, kExprI32LtS, 1, -4, 4);
TestInt32Binop(execution_mode, kExprI32LeS, 0, -2, -3);
TestInt32Binop(execution_mode, kExprI32LtU, 1, 0, -6);
TestInt32Binop(execution_mode, kExprI32LeU, 1, 98978, 0xF0000000);
TestInt32Binop(execution_mode, kExprI32GtS, 1, 4, -4);
TestInt32Binop(execution_mode, kExprI32GeS, 0, -3, -2);
TestInt32Binop(execution_mode, kExprI32GtU, 1, -6, 0);
TestInt32Binop(execution_mode, kExprI32GeU, 1, 0xF0000000, 98978);
}
// clang-format on
#define WASM_I32_BINOP_TEST(expr, ctype, expected) \
WASM_EXEC_TEST(I32Binop_##expr) { \
TestInt32Binop<ctype>(execution_mode, kExprI32##expr, \
[](ctype a, ctype b) -> ctype { return expected; }); \
}
WASM_I32_BINOP_TEST(Add, int32_t, a + b)
WASM_I32_BINOP_TEST(Sub, int32_t, a - b)
WASM_I32_BINOP_TEST(Mul, int32_t, a* b)
WASM_I32_BINOP_TEST(DivS, int32_t,
(a == kMinInt && b == -1) || b == 0
? static_cast<int32_t>(0xdeadbeef)
: a / b)
WASM_I32_BINOP_TEST(DivU, uint32_t, b == 0 ? 0xdeadbeef : a / b)
WASM_I32_BINOP_TEST(RemS, int32_t, b == 0 ? 0xdeadbeef : b == -1 ? 0 : a % b)
WASM_I32_BINOP_TEST(RemU, uint32_t, b == 0 ? 0xdeadbeef : a % b)
WASM_I32_BINOP_TEST(And, int32_t, a& b)
WASM_I32_BINOP_TEST(Ior, int32_t, a | b)
WASM_I32_BINOP_TEST(Xor, int32_t, a ^ b)
WASM_I32_BINOP_TEST(Shl, int32_t, a << b)
WASM_I32_BINOP_TEST(ShrU, uint32_t, a >> (b % 32))
WASM_I32_BINOP_TEST(ShrS, int32_t, a >> (b % 32))
WASM_I32_BINOP_TEST(Ror, uint32_t, (a >> (b % 32)) | (a << (32 - (b % 32))))
WASM_I32_BINOP_TEST(Rol, uint32_t, (a << (b % 32)) | (a >> (32 - (b % 32))))
WASM_I32_BINOP_TEST(Eq, int32_t, a == b)
WASM_I32_BINOP_TEST(Ne, int32_t, a != b)
WASM_I32_BINOP_TEST(LtS, int32_t, a < b)
WASM_I32_BINOP_TEST(LeS, int32_t, a <= b)
WASM_I32_BINOP_TEST(LtU, uint32_t, a < b)
WASM_I32_BINOP_TEST(LeU, uint32_t, a <= b)
WASM_I32_BINOP_TEST(GtS, int32_t, a > b)
WASM_I32_BINOP_TEST(GeS, int32_t, a >= b)
WASM_I32_BINOP_TEST(GtU, uint32_t, a > b)
WASM_I32_BINOP_TEST(GeU, uint32_t, a >= b)
#undef WASM_I32_BINOP_TEST
void TestInt32Unop(WasmExecutionMode execution_mode, WasmOpcode opcode,
int32_t expected, int32_t a) {
......@@ -311,41 +325,6 @@ WASM_EXEC_TEST(I32Eqz) {
TestInt32Unop(execution_mode, kExprI32Eqz, 1, 0);
}
WASM_EXEC_TEST(I32Shl) {
WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode);
BUILD(r, WASM_I32_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT32_INPUTS(i) {
FOR_UINT32_INPUTS(j) {
uint32_t expected = (*i) << (*j & 0x1f);
CHECK_EQ(expected, r.Call(*i, *j));
}
}
}
WASM_EXEC_TEST(I32Shr) {
WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_mode);
BUILD(r, WASM_I32_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT32_INPUTS(i) {
FOR_UINT32_INPUTS(j) {
uint32_t expected = (*i) >> (*j & 0x1f);
CHECK_EQ(expected, r.Call(*i, *j));
}
}
}
WASM_EXEC_TEST(I32Sar) {
WasmRunner<int32_t, int32_t, int32_t> r(execution_mode);
BUILD(r, WASM_I32_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT32_INPUTS(i) {
FOR_INT32_INPUTS(j) {
int32_t expected = (*i) >> (*j & 0x1f);
CHECK_EQ(expected, r.Call(*i, *j));
}
}
}
WASM_EXEC_TEST(Int32DivS_trap) {
WasmRunner<int32_t, int32_t, int32_t> r(execution_mode);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment