v8/test/cctest/wasm/test-run-wasm-64.cc
ahaas 2e6f3e8607 [wasm] Int64Lowering of Word64Popcnt.
Word64Popcnt is lowered to Word32Popcnt(low-word) + Word32Popcnt(high_word).
Since the optional Word64Popcnt operator does not exist on 32 bit platforms,
I introduced a new operator "Word64PopcntPlaceholder" which is generated
in the WasmCompiler and then lowered in the Int64Lowering.

R=titzer@chromium.org

Review URL: https://codereview.chromium.org/1803453003

Cr-Commit-Position: refs/heads/master@{#34777}
2016-03-15 10:42:08 +00:00

1308 lines
45 KiB
C++

// Copyright 2015 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.
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "src/wasm/wasm-macro-gen.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/value-helper.h"
#include "test/cctest/wasm/test-signatures.h"
#include "test/cctest/wasm/wasm-run-utils.h"
#define CHECK_TRAP32(x) \
CHECK_EQ(0xdeadbeef, (bit_cast<uint32_t>(x)) & 0xFFFFFFFF)
#define CHECK_TRAP64(x) \
CHECK_EQ(0xdeadbeefdeadbeef, (bit_cast<uint64_t>(x)) & 0xFFFFFFFFFFFFFFFF)
#define CHECK_TRAP(x) CHECK_TRAP32(x)
#define asi64(x) static_cast<int64_t>(x)
#define asu64(x) static_cast<uint64_t>(x)
#define B2(a, b) kExprBlock, 2, a, b
#define B1(a) kExprBlock, 1, a
// Can't bridge macro land with nested macros.
#if V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_X87
#define MIPS_OR_X87 true
#else
#define MIPS_OR_X87 false
#endif
#if V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_X87 || V8_TARGET_ARCH_ARM
#define MIPS_OR_ARM_OR_X87 true
#else
#define MIPS_OR_ARM_OR_X87 false
#endif
#define FOREACH_I64_OPERATOR(V) \
V(DepthFirst, true) \
V(I64Const, true) \
V(I64Return, true) \
V(I64Param, true) \
V(I64LoadStore, true) \
V(I64Add, true) \
V(I64Sub, false) \
V(I64Mul, false) \
V(I64DivS, true) \
V(I64DivU, true) \
V(I64RemS, true) \
V(I64RemU, true) \
V(I64And, true) \
V(I64Ior, true) \
V(I64Xor, true) \
V(I64Shl, !MIPS_OR_X87) \
V(I64ShrU, !MIPS_OR_X87) \
V(I64ShrS, !MIPS_OR_X87) \
V(I64Eq, true) \
V(I64Ne, true) \
V(I64LtS, true) \
V(I64LeS, true) \
V(I64LtU, true) \
V(I64LeU, true) \
V(I64GtS, true) \
V(I64GeS, true) \
V(I64GtU, true) \
V(I64GeU, true) \
V(I64Clz, false) \
V(I64Ctz, false) \
V(I64Popcnt, !MIPS_OR_X87) \
V(I32ConvertI64, true) \
V(I64SConvertF32, false) \
V(I64SConvertF64, false) \
V(I64UConvertF32, false) \
V(I64UConvertF64, false) \
V(I64SConvertI32, true) \
V(I64UConvertI32, true) \
V(F32SConvertI64, true) \
V(F32UConvertI64, true) \
V(F64SConvertI64, true) \
V(F64UConvertI64, true) \
V(F64ReinterpretI64, false) \
V(I64ReinterpretF64, false) \
V(I64Ror, false) \
V(I64Rol, false)
#define DECLARE_CONST(name, cond) static const bool kSupported_##name = cond;
FOREACH_I64_OPERATOR(DECLARE_CONST)
#undef DECLARE_CONST
#define REQUIRE(name) \
if (!WASM_64 && !kSupported_##name) return
TEST(Run_Wasm_I64Const) {
REQUIRE(I64Const);
WasmRunner<int64_t> r;
const int64_t kExpectedValue = 0x1122334455667788LL;
// return(kExpectedValue)
BUILD(r, WASM_I64V_9(kExpectedValue));
CHECK_EQ(kExpectedValue, r.Call());
}
TEST(Run_Wasm_I64Const_many) {
REQUIRE(I64Const);
int cntr = 0;
FOR_INT32_INPUTS(i) {
WasmRunner<int64_t> r;
const int64_t kExpectedValue = (static_cast<int64_t>(*i) << 32) | cntr;
// return(kExpectedValue)
BUILD(r, WASM_I64V(kExpectedValue));
CHECK_EQ(kExpectedValue, r.Call());
cntr++;
}
}
TEST(Run_Wasm_Return_I64) {
REQUIRE(I64Return);
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_RETURN(WASM_GET_LOCAL(0)));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
}
// todo(ahaas): I added a list of missing instructions here to make merging
// easier when I do them one by one.
// kExprI64Add:
TEST(Run_WasmI64Add) {
REQUIRE(I64Add);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i + *j, r.Call(*i, *j)); }
}
}
// kExprI64Sub:
// kExprI64Mul:
// kExprI64DivS:
TEST(Run_WasmI64DivS) {
REQUIRE(I64DivS);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) {
if (*j == 0) {
CHECK_TRAP64(r.Call(*i, *j));
} else if (*j == -1 && *i == std::numeric_limits<int64_t>::min()) {
CHECK_TRAP64(r.Call(*i, *j));
} else {
CHECK_EQ(*i / *j, r.Call(*i, *j));
}
}
}
}
TEST(Run_WasmI64DivS_Trap) {
REQUIRE(I64DivS);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(0, r.Call(asi64(0), asi64(100)));
CHECK_TRAP64(r.Call(asi64(100), asi64(0)));
CHECK_TRAP64(r.Call(asi64(-1001), asi64(0)));
CHECK_TRAP64(r.Call(std::numeric_limits<int64_t>::min(), asi64(-1)));
CHECK_TRAP64(r.Call(std::numeric_limits<int64_t>::min(), asi64(0)));
}
TEST(Run_WasmI64DivS_Byzero_Const) {
REQUIRE(I64DivS);
for (int8_t denom = -2; denom < 8; denom++) {
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64V_1(denom)));
for (int64_t val = -7; val < 8; val++) {
if (denom == 0) {
CHECK_TRAP64(r.Call(val));
} else {
CHECK_EQ(val / denom, r.Call(val));
}
}
}
}
// kExprI64DivU:
TEST(Run_WasmI64DivU) {
REQUIRE(I64DivU);
WasmRunner<uint64_t> r(MachineType::Uint64(), MachineType::Uint64());
BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) {
if (*j == 0) {
CHECK_TRAP64(r.Call(*i, *j));
} else {
CHECK_EQ(*i / *j, r.Call(*i, *j));
}
}
}
}
TEST(Run_WasmI64DivU_Trap) {
REQUIRE(I64DivU);
WasmRunner<uint64_t> r(MachineType::Uint64(), MachineType::Uint64());
BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(0, r.Call(asu64(0), asu64(100)));
CHECK_TRAP64(r.Call(asu64(100), asu64(0)));
CHECK_TRAP64(r.Call(asu64(1001), asu64(0)));
CHECK_TRAP64(r.Call(std::numeric_limits<uint64_t>::max(), asu64(0)));
}
TEST(Run_WasmI64DivU_Byzero_Const) {
REQUIRE(I64DivU);
for (uint64_t denom = 0xfffffffffffffffe; denom < 8; denom++) {
WasmRunner<uint64_t> r(MachineType::Uint64());
BUILD(r, WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64V_1(denom)));
for (uint64_t val = 0xfffffffffffffff0; val < 8; val++) {
if (denom == 0) {
CHECK_TRAP64(r.Call(val));
} else {
CHECK_EQ(val / denom, r.Call(val));
}
}
}
}
// kExprI64RemS:
TEST(Run_WasmI64RemS) {
REQUIRE(I64RemS);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) {
if (*j == 0) {
CHECK_TRAP64(r.Call(*i, *j));
} else {
CHECK_EQ(*i % *j, r.Call(*i, *j));
}
}
}
}
TEST(Run_WasmI64RemS_Trap) {
REQUIRE(I64RemS);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(33, r.Call(asi64(133), asi64(100)));
CHECK_EQ(0, r.Call(std::numeric_limits<int64_t>::min(), asi64(-1)));
CHECK_TRAP64(r.Call(asi64(100), asi64(0)));
CHECK_TRAP64(r.Call(asi64(-1001), asi64(0)));
CHECK_TRAP64(r.Call(std::numeric_limits<int64_t>::min(), asi64(0)));
}
// kExprI64RemU:
TEST(Run_WasmI64RemU) {
REQUIRE(I64RemU);
WasmRunner<uint64_t> r(MachineType::Uint64(), MachineType::Uint64());
BUILD(r, WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) {
if (*j == 0) {
CHECK_TRAP64(r.Call(*i, *j));
} else {
CHECK_EQ(*i % *j, r.Call(*i, *j));
}
}
}
}
TEST(Run_Wasm_I64RemU_Trap) {
REQUIRE(I64RemU);
WasmRunner<uint64_t> r(MachineType::Uint64(), MachineType::Uint64());
BUILD(r, WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(17, r.Call(asu64(217), asu64(100)));
CHECK_TRAP64(r.Call(asu64(100), asu64(0)));
CHECK_TRAP64(r.Call(asu64(1001), asu64(0)));
CHECK_TRAP64(r.Call(std::numeric_limits<uint64_t>::max(), asu64(0)));
}
// kExprI64And:
TEST(Run_Wasm_I64And) {
REQUIRE(I64And);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_AND(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ((*i) & (*j), r.Call(*i, *j)); }
}
}
// kExprI64Ior:
TEST(Run_Wasm_I64Ior) {
REQUIRE(I64Ior);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ((*i) | (*j), r.Call(*i, *j)); }
}
}
// kExprI64Xor:
TEST(Run_Wasm_I64Xor) {
REQUIRE(I64Xor);
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ((*i) ^ (*j), r.Call(*i, *j)); }
}
}
// kExprI64Shl:
TEST(Run_Wasm_I64Shl) {
REQUIRE(I64Shl);
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
for (int64_t j = 1; j < 64; j++) {
CHECK_EQ(*i << j, r.Call(*i, j));
}
}
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(0)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 0, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(32)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 32, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(20)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 20, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(40)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i << 40, r.Call(*i)); }
}
}
// kExprI64ShrU:
TEST(Run_Wasm_I64ShrU) {
REQUIRE(I64ShrU);
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
for (int64_t j = 1; j < 64; j++) {
CHECK_EQ(*i >> j, r.Call(*i, j));
}
}
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(0)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 0, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(32)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 32, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(20)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 20, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(40)));
FOR_UINT64_INPUTS(i) { CHECK_EQ(*i >> 40, r.Call(*i)); }
}
}
// kExprI64ShrS:
TEST(Run_Wasm_I64ShrS) {
REQUIRE(I64ShrS);
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
for (int64_t j = 1; j < 64; j++) {
CHECK_EQ(*i >> j, r.Call(*i, j));
}
}
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(0)));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 0, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(32)));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 32, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(20)));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 20, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(40)));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i >> 40, r.Call(*i)); }
}
}
// kExprI64Eq:
TEST(Run_Wasm_I64Eq) {
REQUIRE(I64Eq);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i == *j ? 1 : 0, r.Call(*i, *j)); }
}
}
// kExprI64Ne:
TEST(Run_Wasm_I64Ne) {
REQUIRE(I64Ne);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_NE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i != *j ? 1 : 0, r.Call(*i, *j)); }
}
}
// kExprI64LtS:
TEST(Run_Wasm_I64LtS) {
REQUIRE(I64LtS);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_LTS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i < *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64LeS) {
REQUIRE(I64LeS);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_LES(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i <= *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64LtU) {
REQUIRE(I64LtU);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_LTU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) { CHECK_EQ(*i < *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64LeU) {
REQUIRE(I64LeU);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_LEU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) { CHECK_EQ(*i <= *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64GtS) {
REQUIRE(I64GtS);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_GTS(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i > *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64GeS) {
REQUIRE(I64GeS);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_GES(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i >= *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64GtU) {
REQUIRE(I64GtU);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_GTU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) { CHECK_EQ(*i > *j ? 1 : 0, r.Call(*i, *j)); }
}
}
TEST(Run_Wasm_I64GeU) {
REQUIRE(I64GeU);
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_GEU(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_UINT64_INPUTS(i) {
FOR_UINT64_INPUTS(j) { CHECK_EQ(*i >= *j ? 1 : 0, r.Call(*i, *j)); }
}
}
// kExprI32ConvertI64:
TEST(Run_Wasm_I32ConvertI64) {
REQUIRE(I32ConvertI64);
FOR_INT64_INPUTS(i) {
WasmRunner<int32_t> r;
BUILD(r, WASM_I32_CONVERT_I64(WASM_I64V(*i)));
CHECK_EQ(static_cast<int32_t>(*i), r.Call());
}
}
// kExprI64SConvertI32:
TEST(Run_Wasm_I64SConvertI32) {
REQUIRE(I64SConvertI32);
WasmRunner<int64_t> r(MachineType::Int32());
BUILD(r, WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0)));
FOR_INT32_INPUTS(i) { CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i)); }
}
// kExprI64UConvertI32:
TEST(Run_Wasm_I64UConvertI32) {
REQUIRE(I64UConvertI32);
WasmRunner<int64_t> r(MachineType::Uint32());
BUILD(r, WASM_I64_UCONVERT_I32(WASM_GET_LOCAL(0)));
FOR_UINT32_INPUTS(i) { CHECK_EQ(static_cast<uint64_t>(*i), r.Call(*i)); }
}
// kExprF64ReinterpretI64:
// kExprI64ReinterpretF64:
// kExprI64Clz:
// kExprI64Ctz:
// kExprI64Popcnt:
TEST(Run_WasmI64Popcnt) {
struct {
int64_t expected;
uint64_t input;
} values[] = {{64, 0xffffffffffffffff},
{0, 0x0000000000000000},
{2, 0x0000080000008000},
{26, 0x1123456782345678},
{38, 0xffedcba09edcba09}};
WasmRunner<int64_t> r(MachineType::Uint64());
BUILD(r, WASM_I64_POPCNT(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(values[i].expected, r.Call(values[i].input));
}
}
// kExprF32SConvertI64:
TEST(Run_WasmF32SConvertI64) {
REQUIRE(F32SConvertI64);
WasmRunner<float> r(MachineType::Int64());
BUILD(r, WASM_F32_SCONVERT_I64(WASM_GET_LOCAL(0)));
FOR_INT64_INPUTS(i) { CHECK_EQ(static_cast<float>(*i), r.Call(*i)); }
}
// kExprF32UConvertI64:
TEST(Run_WasmF32UConvertI64) {
REQUIRE(F32UConvertI64);
struct {
uint64_t input;
uint32_t expected;
} values[] = {{0x0, 0x0},
{0x1, 0x3f800000},
{0xffffffff, 0x4f800000},
{0x1b09788b, 0x4dd84bc4},
{0x4c5fce8, 0x4c98bf9d},
{0xcc0de5bf, 0x4f4c0de6},
{0x2, 0x40000000},
{0x3, 0x40400000},
{0x4, 0x40800000},
{0x5, 0x40a00000},
{0x8, 0x41000000},
{0x9, 0x41100000},
{0xffffffffffffffff, 0x5f800000},
{0xfffffffffffffffe, 0x5f800000},
{0xfffffffffffffffd, 0x5f800000},
{0x0, 0x0},
{0x100000000, 0x4f800000},
{0xffffffff00000000, 0x5f800000},
{0x1b09788b00000000, 0x5dd84bc4},
{0x4c5fce800000000, 0x5c98bf9d},
{0xcc0de5bf00000000, 0x5f4c0de6},
{0x200000000, 0x50000000},
{0x300000000, 0x50400000},
{0x400000000, 0x50800000},
{0x500000000, 0x50a00000},
{0x800000000, 0x51000000},
{0x900000000, 0x51100000},
{0x273a798e187937a3, 0x5e1ce9e6},
{0xece3af835495a16b, 0x5f6ce3b0},
{0xb668ecc11223344, 0x5d3668ed},
{0x9e, 0x431e0000},
{0x43, 0x42860000},
{0xaf73, 0x472f7300},
{0x116b, 0x458b5800},
{0x658ecc, 0x4acb1d98},
{0x2b3b4c, 0x4a2ced30},
{0x88776655, 0x4f087766},
{0x70000000, 0x4ee00000},
{0x7200000, 0x4ce40000},
{0x7fffffff, 0x4f000000},
{0x56123761, 0x4eac246f},
{0x7fffff00, 0x4efffffe},
{0x761c4761eeeeeeee, 0x5eec388f},
{0x80000000eeeeeeee, 0x5f000000},
{0x88888888dddddddd, 0x5f088889},
{0xa0000000dddddddd, 0x5f200000},
{0xddddddddaaaaaaaa, 0x5f5dddde},
{0xe0000000aaaaaaaa, 0x5f600000},
{0xeeeeeeeeeeeeeeee, 0x5f6eeeef},
{0xfffffffdeeeeeeee, 0x5f800000},
{0xf0000000dddddddd, 0x5f700000},
{0x7fffffdddddddd, 0x5b000000},
{0x3fffffaaaaaaaa, 0x5a7fffff},
{0x1fffffaaaaaaaa, 0x59fffffd},
{0xfffff, 0x497ffff0},
{0x7ffff, 0x48ffffe0},
{0x3ffff, 0x487fffc0},
{0x1ffff, 0x47ffff80},
{0xffff, 0x477fff00},
{0x7fff, 0x46fffe00},
{0x3fff, 0x467ffc00},
{0x1fff, 0x45fff800},
{0xfff, 0x457ff000},
{0x7ff, 0x44ffe000},
{0x3ff, 0x447fc000},
{0x1ff, 0x43ff8000},
{0x3fffffffffff, 0x56800000},
{0x1fffffffffff, 0x56000000},
{0xfffffffffff, 0x55800000},
{0x7ffffffffff, 0x55000000},
{0x3ffffffffff, 0x54800000},
{0x1ffffffffff, 0x54000000},
{0x8000008000000000, 0x5f000000},
{0x8000008000000001, 0x5f000001},
{0x8000000000000400, 0x5f000000},
{0x8000000000000401, 0x5f000000}};
WasmRunner<float> r(MachineType::Uint64());
BUILD(r, WASM_F32_UCONVERT_I64(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(bit_cast<float>(values[i].expected), r.Call(values[i].input));
}
}
// kExprF64SConvertI64:
TEST(Run_WasmF64SConvertI64) {
REQUIRE(F64SConvertI64);
WasmRunner<double> r(MachineType::Int64());
BUILD(r, WASM_F64_SCONVERT_I64(WASM_GET_LOCAL(0)));
FOR_INT64_INPUTS(i) { CHECK_EQ(static_cast<double>(*i), r.Call(*i)); }
}
// kExprF64UConvertI64:
TEST(Run_Wasm_F64UConvertI64) {
REQUIRE(F64UConvertI64);
struct {
uint64_t input;
uint64_t expected;
} values[] = {{0x0, 0x0},
{0x1, 0x3ff0000000000000},
{0xffffffff, 0x41efffffffe00000},
{0x1b09788b, 0x41bb09788b000000},
{0x4c5fce8, 0x419317f3a0000000},
{0xcc0de5bf, 0x41e981bcb7e00000},
{0x2, 0x4000000000000000},
{0x3, 0x4008000000000000},
{0x4, 0x4010000000000000},
{0x5, 0x4014000000000000},
{0x8, 0x4020000000000000},
{0x9, 0x4022000000000000},
{0xffffffffffffffff, 0x43f0000000000000},
{0xfffffffffffffffe, 0x43f0000000000000},
{0xfffffffffffffffd, 0x43f0000000000000},
{0x100000000, 0x41f0000000000000},
{0xffffffff00000000, 0x43efffffffe00000},
{0x1b09788b00000000, 0x43bb09788b000000},
{0x4c5fce800000000, 0x439317f3a0000000},
{0xcc0de5bf00000000, 0x43e981bcb7e00000},
{0x200000000, 0x4200000000000000},
{0x300000000, 0x4208000000000000},
{0x400000000, 0x4210000000000000},
{0x500000000, 0x4214000000000000},
{0x800000000, 0x4220000000000000},
{0x900000000, 0x4222000000000000},
{0x273a798e187937a3, 0x43c39d3cc70c3c9c},
{0xece3af835495a16b, 0x43ed9c75f06a92b4},
{0xb668ecc11223344, 0x43a6cd1d98224467},
{0x9e, 0x4063c00000000000},
{0x43, 0x4050c00000000000},
{0xaf73, 0x40e5ee6000000000},
{0x116b, 0x40b16b0000000000},
{0x658ecc, 0x415963b300000000},
{0x2b3b4c, 0x41459da600000000},
{0x88776655, 0x41e10eeccaa00000},
{0x70000000, 0x41dc000000000000},
{0x7200000, 0x419c800000000000},
{0x7fffffff, 0x41dfffffffc00000},
{0x56123761, 0x41d5848dd8400000},
{0x7fffff00, 0x41dfffffc0000000},
{0x761c4761eeeeeeee, 0x43dd8711d87bbbbc},
{0x80000000eeeeeeee, 0x43e00000001dddde},
{0x88888888dddddddd, 0x43e11111111bbbbc},
{0xa0000000dddddddd, 0x43e40000001bbbbc},
{0xddddddddaaaaaaaa, 0x43ebbbbbbbb55555},
{0xe0000000aaaaaaaa, 0x43ec000000155555},
{0xeeeeeeeeeeeeeeee, 0x43edddddddddddde},
{0xfffffffdeeeeeeee, 0x43efffffffbdddde},
{0xf0000000dddddddd, 0x43ee0000001bbbbc},
{0x7fffffdddddddd, 0x435ffffff7777777},
{0x3fffffaaaaaaaa, 0x434fffffd5555555},
{0x1fffffaaaaaaaa, 0x433fffffaaaaaaaa},
{0xfffff, 0x412ffffe00000000},
{0x7ffff, 0x411ffffc00000000},
{0x3ffff, 0x410ffff800000000},
{0x1ffff, 0x40fffff000000000},
{0xffff, 0x40efffe000000000},
{0x7fff, 0x40dfffc000000000},
{0x3fff, 0x40cfff8000000000},
{0x1fff, 0x40bfff0000000000},
{0xfff, 0x40affe0000000000},
{0x7ff, 0x409ffc0000000000},
{0x3ff, 0x408ff80000000000},
{0x1ff, 0x407ff00000000000},
{0x3fffffffffff, 0x42cfffffffffff80},
{0x1fffffffffff, 0x42bfffffffffff00},
{0xfffffffffff, 0x42affffffffffe00},
{0x7ffffffffff, 0x429ffffffffffc00},
{0x3ffffffffff, 0x428ffffffffff800},
{0x1ffffffffff, 0x427ffffffffff000},
{0x8000008000000000, 0x43e0000010000000},
{0x8000008000000001, 0x43e0000010000000},
{0x8000000000000400, 0x43e0000000000000},
{0x8000000000000401, 0x43e0000000000001}};
WasmRunner<double> r(MachineType::Uint64());
BUILD(r, WASM_F64_UCONVERT_I64(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(bit_cast<double>(values[i].expected), r.Call(values[i].input));
}
}
// kExprI64SConvertF32:
TEST(Run_Wasm_I64SConvertF32a) {
WasmRunner<int64_t> r(MachineType::Float32());
BUILD(r, WASM_I64_SCONVERT_F32(WASM_GET_LOCAL(0)));
FOR_FLOAT32_INPUTS(i) {
if (*i < static_cast<float>(std::numeric_limits<int64_t>::max()) &&
*i >= static_cast<float>(std::numeric_limits<int64_t>::min())) {
CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
// kExprI64SConvertF64:
TEST(Run_Wasm_I64SConvertF64a) {
WasmRunner<int64_t> r(MachineType::Float64());
BUILD(r, WASM_I64_SCONVERT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
if (*i < static_cast<double>(std::numeric_limits<int64_t>::max()) &&
*i >= static_cast<double>(std::numeric_limits<int64_t>::min())) {
CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
// kExprI64UConvertF32:
TEST(Run_Wasm_I64UConvertF32a) {
WasmRunner<uint64_t> r(MachineType::Float32());
BUILD(r, WASM_I64_UCONVERT_F32(WASM_GET_LOCAL(0)));
FOR_FLOAT32_INPUTS(i) {
if (*i < static_cast<float>(std::numeric_limits<uint64_t>::max()) &&
*i > -1) {
CHECK_EQ(static_cast<uint64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
// kExprI64UConvertF64:
TEST(Run_Wasm_I64UConvertF64a) {
WasmRunner<uint64_t> r(MachineType::Float64());
BUILD(r, WASM_I64_UCONVERT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
if (*i < static_cast<float>(std::numeric_limits<uint64_t>::max()) &&
*i > -1) {
CHECK_EQ(static_cast<uint64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
TEST(Run_WasmCallI64Parameter) {
// Build the target function.
LocalType param_types[20];
for (int i = 0; i < 20; i++) param_types[i] = kAstI64;
param_types[3] = kAstI32;
param_types[4] = kAstI32;
FunctionSig sig(1, 19, param_types);
for (int i = 0; i < 19; i++) {
TestingModule module;
WasmFunctionCompiler t(&sig, &module);
if (i == 2 || i == 3) {
continue;
} else {
BUILD(t, WASM_GET_LOCAL(i));
}
uint32_t index = t.CompileAndAdd();
// Build the calling function.
WasmRunner<int32_t> r(&module);
BUILD(
r,
WASM_I32_CONVERT_I64(WASM_CALL_FUNCTION(
index, WASM_I64V_9(0xbcd12340000000b),
WASM_I64V_9(0xbcd12340000000c), WASM_I32V_1(0xd),
WASM_I32_CONVERT_I64(WASM_I64V_9(0xbcd12340000000e)),
WASM_I64V_9(0xbcd12340000000f), WASM_I64V_10(0xbcd1234000000010),
WASM_I64V_10(0xbcd1234000000011), WASM_I64V_10(0xbcd1234000000012),
WASM_I64V_10(0xbcd1234000000013), WASM_I64V_10(0xbcd1234000000014),
WASM_I64V_10(0xbcd1234000000015), WASM_I64V_10(0xbcd1234000000016),
WASM_I64V_10(0xbcd1234000000017), WASM_I64V_10(0xbcd1234000000018),
WASM_I64V_10(0xbcd1234000000019), WASM_I64V_10(0xbcd123400000001a),
WASM_I64V_10(0xbcd123400000001b), WASM_I64V_10(0xbcd123400000001c),
WASM_I64V_10(0xbcd123400000001d))));
CHECK_EQ(i + 0xb, r.Call());
}
}
void TestI64Binop(WasmOpcode opcode, int64_t expected, int64_t a, int64_t b) {
{
WasmRunner<int64_t> r;
// return K op K
BUILD(r, WASM_BINOP(opcode, WASM_I64V(a), WASM_I64V(b)));
CHECK_EQ(expected, r.Call());
}
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
// return a op b
BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(expected, r.Call(a, b));
}
}
void TestI64Cmp(WasmOpcode opcode, int64_t expected, int64_t a, int64_t b) {
{
WasmRunner<int32_t> r;
// return K op K
BUILD(r, WASM_BINOP(opcode, WASM_I64V(a), WASM_I64V(b)));
CHECK_EQ(expected, r.Call());
}
{
WasmRunner<int32_t> r(MachineType::Int64(), MachineType::Int64());
// return a op b
BUILD(r, WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
CHECK_EQ(expected, r.Call(a, b));
}
}
#define TEST_I64_BINOP(name, expected, a, b) \
do { \
if (WASM_64 || kSupported_##name) \
TestI64Binop(kExpr##name, expected, a, b); \
} while (false)
TEST(Run_Wasm_I64Binops) {
// TODO(titzer): real 64-bit numbers
TEST_I64_BINOP(I64Add, 8888888888888LL, 3333333333333LL, 5555555555555LL);
TEST_I64_BINOP(I64Sub, -111111111111LL, 777777777777LL, 888888888888LL);
TEST_I64_BINOP(I64Mul, 65130756, 88734, 734);
TEST_I64_BINOP(I64DivS, -66, -4777344, 72384);
TEST_I64_BINOP(I64DivU, 805306368, 0xF0000000, 5);
TEST_I64_BINOP(I64RemS, -3, -3003, 1000);
TEST_I64_BINOP(I64RemU, 4, 4004, 1000);
TEST_I64_BINOP(I64And, 0xEE, 0xFFEE, 0xFF0000FF);
TEST_I64_BINOP(I64Ior, 0xF0FF00FF, 0xF0F000EE, 0x000F0011);
TEST_I64_BINOP(I64Xor, 0xABCDEF01, 0xABCDEFFF, 0xFE);
TEST_I64_BINOP(I64Shl, 0xA0000000, 0xA, 28);
TEST_I64_BINOP(I64ShrU, 0x0700001000123456LL, 0x7000010001234567LL, 4);
TEST_I64_BINOP(I64ShrS, 0xFF00000000000000LL, 0x8000000000000000LL, 7);
TEST_I64_BINOP(I64Ror, 0x0100000000000000LL, 0x8000000000000000LL, 7);
TEST_I64_BINOP(I64Ror, 0x0100000000000000LL, 0x8000000000000000LL, 71);
TEST_I64_BINOP(I64Rol, 0x0000000000000040LL, 0x8000000000000000LL, 7);
TEST_I64_BINOP(I64Rol, 0x0000000000000040LL, 0x8000000000000000LL, 71);
}
#define TEST_I64_CMP(name, expected, a, b) \
do { \
if (WASM_64 || kSupported_##name) TestI64Cmp(kExpr##name, expected, a, b); \
} while (false)
TEST(Run_Wasm_I64Compare) {
// TODO(titzer): real 64-bit numbers
TEST_I64_CMP(I64Eq, 1, -9999, -9999);
TEST_I64_CMP(I64Ne, 1, -9199, -9999);
TEST_I64_CMP(I64LtS, 1, -4, 4);
TEST_I64_CMP(I64LeS, 0, -2, -3);
TEST_I64_CMP(I64LtU, 1, 0, -6);
TEST_I64_CMP(I64LeU, 1, 98978, 0xF0000000);
TEST_I64_CMP(I64GtS, 1, 4, -4);
TEST_I64_CMP(I64GeS, 0, -3, -2);
TEST_I64_CMP(I64GtU, 1, -6, 0);
TEST_I64_CMP(I64GeU, 1, 0xF0000000, 98978);
}
TEST(Run_Wasm_I64Clz) {
REQUIRE(I64Clz);
struct {
int64_t expected;
uint64_t input;
} values[] = {{0, 0x8000100000000000}, {1, 0x4000050000000000},
{2, 0x2000030000000000}, {3, 0x1000000300000000},
{4, 0x0805000000000000}, {5, 0x0400600000000000},
{6, 0x0200000000000000}, {7, 0x010000a000000000},
{8, 0x00800c0000000000}, {9, 0x0040000000000000},
{10, 0x0020000d00000000}, {11, 0x00100f0000000000},
{12, 0x0008000000000000}, {13, 0x0004100000000000},
{14, 0x0002002000000000}, {15, 0x0001030000000000},
{16, 0x0000804000000000}, {17, 0x0000400500000000},
{18, 0x0000205000000000}, {19, 0x0000170000000000},
{20, 0x0000087000000000}, {21, 0x0000040500000000},
{22, 0x0000020300000000}, {23, 0x0000010100000000},
{24, 0x0000008900000000}, {25, 0x0000004100000000},
{26, 0x0000002200000000}, {27, 0x0000001300000000},
{28, 0x0000000800000000}, {29, 0x0000000400000000},
{30, 0x0000000200000000}, {31, 0x0000000100000000},
{32, 0x0000000080001000}, {33, 0x0000000040000500},
{34, 0x0000000020000300}, {35, 0x0000000010000003},
{36, 0x0000000008050000}, {37, 0x0000000004006000},
{38, 0x0000000002000000}, {39, 0x00000000010000a0},
{40, 0x0000000000800c00}, {41, 0x0000000000400000},
{42, 0x000000000020000d}, {43, 0x0000000000100f00},
{44, 0x0000000000080000}, {45, 0x0000000000041000},
{46, 0x0000000000020020}, {47, 0x0000000000010300},
{48, 0x0000000000008040}, {49, 0x0000000000004005},
{50, 0x0000000000002050}, {51, 0x0000000000001700},
{52, 0x0000000000000870}, {53, 0x0000000000000405},
{54, 0x0000000000000203}, {55, 0x0000000000000101},
{56, 0x0000000000000089}, {57, 0x0000000000000041},
{58, 0x0000000000000022}, {59, 0x0000000000000013},
{60, 0x0000000000000008}, {61, 0x0000000000000004},
{62, 0x0000000000000002}, {63, 0x0000000000000001},
{64, 0x0000000000000000}};
WasmRunner<int64_t> r(MachineType::Uint64());
BUILD(r, WASM_I64_CLZ(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(values[i].expected, r.Call(values[i].input));
}
}
TEST(Run_Wasm_I64Ctz) {
REQUIRE(I64Ctz);
struct {
int64_t expected;
uint64_t input;
} values[] = {{64, 0x0000000000000000}, {63, 0x8000000000000000},
{62, 0x4000000000000000}, {61, 0x2000000000000000},
{60, 0x1000000000000000}, {59, 0xa800000000000000},
{58, 0xf400000000000000}, {57, 0x6200000000000000},
{56, 0x9100000000000000}, {55, 0xcd80000000000000},
{54, 0x0940000000000000}, {53, 0xaf20000000000000},
{52, 0xac10000000000000}, {51, 0xe0b8000000000000},
{50, 0x9ce4000000000000}, {49, 0xc792000000000000},
{48, 0xb8f1000000000000}, {47, 0x3b9f800000000000},
{46, 0xdb4c400000000000}, {45, 0xe9a3200000000000},
{44, 0xfca6100000000000}, {43, 0x6c8a780000000000},
{42, 0x8ce5a40000000000}, {41, 0xcb7d020000000000},
{40, 0xcb4dc10000000000}, {39, 0xdfbec58000000000},
{38, 0x27a9db4000000000}, {37, 0xde3bcb2000000000},
{36, 0xd7e8a61000000000}, {35, 0x9afdbc8800000000},
{34, 0x9afdbc8400000000}, {33, 0x9afdbc8200000000},
{32, 0x9afdbc8100000000}, {31, 0x0000000080000000},
{30, 0x0000000040000000}, {29, 0x0000000020000000},
{28, 0x0000000010000000}, {27, 0x00000000a8000000},
{26, 0x00000000f4000000}, {25, 0x0000000062000000},
{24, 0x0000000091000000}, {23, 0x00000000cd800000},
{22, 0x0000000009400000}, {21, 0x00000000af200000},
{20, 0x00000000ac100000}, {19, 0x00000000e0b80000},
{18, 0x000000009ce40000}, {17, 0x00000000c7920000},
{16, 0x00000000b8f10000}, {15, 0x000000003b9f8000},
{14, 0x00000000db4c4000}, {13, 0x00000000e9a32000},
{12, 0x00000000fca61000}, {11, 0x000000006c8a7800},
{10, 0x000000008ce5a400}, {9, 0x00000000cb7d0200},
{8, 0x00000000cb4dc100}, {7, 0x00000000dfbec580},
{6, 0x0000000027a9db40}, {5, 0x00000000de3bcb20},
{4, 0x00000000d7e8a610}, {3, 0x000000009afdbc88},
{2, 0x000000009afdbc84}, {1, 0x000000009afdbc82},
{0, 0x000000009afdbc81}};
WasmRunner<int64_t> r(MachineType::Uint64());
BUILD(r, WASM_I64_CTZ(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(values[i].expected, r.Call(values[i].input));
}
}
TEST(Run_Wasm_I64Popcnt) {
REQUIRE(I64Popcnt);
struct {
int64_t expected;
uint64_t input;
} values[] = {{64, 0xffffffffffffffff},
{0, 0x0000000000000000},
{2, 0x0000080000008000},
{26, 0x1123456782345678},
{38, 0xffedcba09edcba09}};
WasmRunner<int64_t> r(MachineType::Uint64());
BUILD(r, WASM_I64_POPCNT(WASM_GET_LOCAL(0)));
for (size_t i = 0; i < arraysize(values); i++) {
CHECK_EQ(values[i].expected, r.Call(values[i].input));
}
}
// Test the WasmRunner with an Int64 return value and different numbers of
// Int64 parameters.
TEST(Run_TestI64WasmRunner) {
REQUIRE(I64Param);
REQUIRE(I64Xor);
{FOR_INT64_INPUTS(i){WasmRunner<int64_t> r;
BUILD(r, WASM_I64V(*i));
CHECK_EQ(*i, r.Call());
}
}
{
WasmRunner<int64_t> r(MachineType::Int64());
BUILD(r, WASM_GET_LOCAL(0));
FOR_INT64_INPUTS(i) { CHECK_EQ(*i, r.Call(*i)); }
}
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) { CHECK_EQ(*i ^ *j, r.Call(*i, *j)); }
}
}
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64(),
MachineType::Int64());
BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0),
WASM_I64_XOR(WASM_GET_LOCAL(1), WASM_GET_LOCAL(2))));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) {
CHECK_EQ(*i ^ *j ^ *j, r.Call(*i, *j, *j));
CHECK_EQ(*j ^ *i ^ *j, r.Call(*j, *i, *j));
CHECK_EQ(*j ^ *j ^ *i, r.Call(*j, *j, *i));
}
}
}
{
WasmRunner<int64_t> r(MachineType::Int64(), MachineType::Int64(),
MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_I64_XOR(WASM_GET_LOCAL(0),
WASM_I64_XOR(WASM_GET_LOCAL(1),
WASM_I64_XOR(WASM_GET_LOCAL(2),
WASM_GET_LOCAL(3)))));
FOR_INT64_INPUTS(i) {
FOR_INT64_INPUTS(j) {
CHECK_EQ(*i ^ *j ^ *j ^ *j, r.Call(*i, *j, *j, *j));
CHECK_EQ(*j ^ *i ^ *j ^ *j, r.Call(*j, *i, *j, *j));
CHECK_EQ(*j ^ *j ^ *i ^ *j, r.Call(*j, *j, *i, *j));
CHECK_EQ(*j ^ *j ^ *j ^ *i, r.Call(*j, *j, *j, *i));
}
}
}
}
TEST(Run_WasmCall_Int64Sub) {
REQUIRE(I64Sub);
// Build the target function.
TestSignatures sigs;
TestingModule module;
WasmFunctionCompiler t(sigs.l_ll(), &module);
BUILD(t, WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
uint32_t index = t.CompileAndAdd();
// Build the caller function.
WasmRunner<int64_t> r(&module, MachineType::Int64(), MachineType::Int64());
BUILD(r, WASM_CALL_FUNCTION(index, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)));
FOR_INT32_INPUTS(i) {
FOR_INT32_INPUTS(j) {
int64_t a = static_cast<int64_t>(*i) << 32 |
(static_cast<int64_t>(*j) | 0xFFFFFFFF);
int64_t b = static_cast<int64_t>(*j) << 32 |
(static_cast<int64_t>(*i) | 0xFFFFFFFF);
int64_t expected = static_cast<int64_t>(static_cast<uint64_t>(a) -
static_cast<uint64_t>(b));
CHECK_EQ(expected, r.Call(a, b));
}
}
}
TEST(Run_Wasm_LoadStoreI64_sx) {
REQUIRE(I64LoadStore);
REQUIRE(DepthFirst); // TODO(titzer): temporary hack
byte loads[] = {kExprI64LoadMem8S, kExprI64LoadMem16S, kExprI64LoadMem32S,
kExprI64LoadMem};
for (size_t m = 0; m < arraysize(loads); m++) {
TestingModule module;
byte* memory = module.AddMemoryElems<byte>(16);
WasmRunner<int64_t> r(&module);
byte code[] = {kExprI64StoreMem, ZERO_ALIGNMENT,
ZERO_OFFSET, // --
kExprI8Const, 8, // --
loads[m], ZERO_ALIGNMENT,
ZERO_OFFSET, // --
kExprI8Const, 0}; // --
r.Build(code, code + arraysize(code));
// Try a bunch of different negative values.
for (int i = -1; i >= -128; i -= 11) {
int size = 1 << m;
module.BlankMemory();
memory[size - 1] = static_cast<byte>(i); // set the high order byte.
int64_t expected = static_cast<int64_t>(i) << ((size - 1) * 8);
CHECK_EQ(expected, r.Call());
CHECK_EQ(static_cast<byte>(i), memory[8 + size - 1]);
for (int j = size; j < 8; j++) {
CHECK_EQ(255, memory[8 + j]);
}
}
}
}
TEST(Run_Wasm_I64SConvertF32b) {
REQUIRE(I64SConvertF32);
WasmRunner<int64_t> r(MachineType::Float32());
BUILD(r, WASM_I64_SCONVERT_F32(WASM_GET_LOCAL(0)));
FOR_FLOAT32_INPUTS(i) {
if (*i < static_cast<float>(INT64_MAX) &&
*i >= static_cast<float>(INT64_MIN)) {
CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
TEST(Run_Wasm_I64SConvertF64b) {
REQUIRE(I64SConvertF64);
WasmRunner<int64_t> r(MachineType::Float64());
BUILD(r, WASM_I64_SCONVERT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
if (*i < static_cast<double>(INT64_MAX) &&
*i >= static_cast<double>(INT64_MIN)) {
CHECK_EQ(static_cast<int64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
TEST(Run_Wasm_I64UConvertF32b) {
REQUIRE(I64UConvertF32);
WasmRunner<uint64_t> r(MachineType::Float32());
BUILD(r, WASM_I64_UCONVERT_F32(WASM_GET_LOCAL(0)));
FOR_FLOAT32_INPUTS(i) {
if (*i < static_cast<float>(UINT64_MAX) && *i > -1) {
CHECK_EQ(static_cast<uint64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
TEST(Run_Wasm_I64UConvertF64b) {
REQUIRE(I64UConvertF64);
WasmRunner<uint64_t> r(MachineType::Float64());
BUILD(r, WASM_I64_UCONVERT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
if (*i < static_cast<float>(UINT64_MAX) && *i > -1) {
CHECK_EQ(static_cast<uint64_t>(*i), r.Call(*i));
} else {
CHECK_TRAP64(r.Call(*i));
}
}
}
TEST(Run_Wasm_F64ReinterpretI64) {
REQUIRE(F64ReinterpretI64);
TestingModule module;
int64_t* memory = module.AddMemoryElems<int64_t>(8);
WasmRunner<int64_t> r(&module);
BUILD(r, WASM_I64_REINTERPRET_F64(
WASM_LOAD_MEM(MachineType::Float64(), WASM_ZERO)));
FOR_INT32_INPUTS(i) {
int64_t expected = static_cast<int64_t>(*i) * 0x300010001;
memory[0] = expected;
CHECK_EQ(expected, r.Call());
}
}
TEST(Run_Wasm_I64ReinterpretF64) {
REQUIRE(I64ReinterpretF64);
TestingModule module;
int64_t* memory = module.AddMemoryElems<int64_t>(8);
WasmRunner<int64_t> r(&module, MachineType::Int64());
BUILD(r, WASM_BLOCK(
2, WASM_STORE_MEM(MachineType::Float64(), WASM_ZERO,
WASM_F64_REINTERPRET_I64(WASM_GET_LOCAL(0))),
WASM_GET_LOCAL(0)));
FOR_INT32_INPUTS(i) {
int64_t expected = static_cast<int64_t>(*i) * 0x300010001;
CHECK_EQ(expected, r.Call(expected));
CHECK_EQ(expected, memory[0]);
}
}
TEST(Run_Wasm_LoadMemI64) {
REQUIRE(I64LoadStore);
TestingModule module;
int64_t* memory = module.AddMemoryElems<int64_t>(8);
module.RandomizeMemory(1111);
WasmRunner<int64_t> r(&module);
BUILD(r, WASM_LOAD_MEM(MachineType::Int64(), WASM_I8(0)));
memory[0] = 0xaabbccdd00112233LL;
CHECK_EQ(0xaabbccdd00112233LL, r.Call());
memory[0] = 0x33aabbccdd001122LL;
CHECK_EQ(0x33aabbccdd001122LL, r.Call());
memory[0] = 77777777;
CHECK_EQ(77777777, r.Call());
}
TEST(Run_Wasm_MemI64_Sum) {
REQUIRE(I64LoadStore);
REQUIRE(I64Add);
REQUIRE(I64Sub);
const int kNumElems = 20;
TestingModule module;
uint64_t* memory = module.AddMemoryElems<uint64_t>(kNumElems);
WasmRunner<uint64_t> r(&module, MachineType::Int32());
const byte kSum = r.AllocateLocal(kAstI64);
BUILD(r, WASM_BLOCK(
2, WASM_WHILE(
WASM_GET_LOCAL(0),
WASM_BLOCK(
2, WASM_SET_LOCAL(
kSum, WASM_I64_ADD(
WASM_GET_LOCAL(kSum),
WASM_LOAD_MEM(MachineType::Int64(),
WASM_GET_LOCAL(0)))),
WASM_SET_LOCAL(
0, WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(8))))),
WASM_GET_LOCAL(1)));
// Run 4 trials.
for (int i = 0; i < 3; i++) {
module.RandomizeMemory(i * 33);
uint64_t expected = 0;
for (size_t j = kNumElems - 1; j > 0; j--) {
expected += memory[j];
}
uint64_t result = r.Call(8 * (kNumElems - 1));
CHECK_EQ(expected, result);
}
}
TEST(Run_Wasm_I64Global) {
REQUIRE(I64LoadStore);
REQUIRE(I64SConvertI32);
REQUIRE(I64And);
REQUIRE(DepthFirst); // TODO(titzer): temporary hack
TestingModule module;
int64_t* global = module.AddGlobal<int64_t>(MachineType::Int64());
WasmRunner<int32_t> r(&module, MachineType::Int32());
// global = global + p0
BUILD(r, B2(WASM_STORE_GLOBAL(
0, WASM_I64_AND(WASM_LOAD_GLOBAL(0),
WASM_I64_SCONVERT_I32(WASM_GET_LOCAL(0)))),
WASM_ZERO));
*global = 0xFFFFFFFFFFFFFFFFLL;
for (int i = 9; i < 444444; i += 111111) {
int64_t expected = *global & i;
r.Call(i);
CHECK_EQ(expected, *global);
}
}
TEST(Run_WasmI64Eqz) {
REQUIRE(I64Eq);
WasmRunner<int32_t> r(MachineType::Int64());
BUILD(r, WASM_I64_EQZ(WASM_GET_LOCAL(0)));
FOR_INT64_INPUTS(i) {
int32_t result = *i == 0 ? 1 : 0;
CHECK_EQ(result, r.Call(*i));
}
}