Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
("i64.rotr", "makeBinary(BinaryOp::RotRInt64)"),
("i64.add128", "makeWideIntAddSub(WideIntAddSubOp::AddInt128)"),
("i64.sub128", "makeWideIntAddSub(WideIntAddSubOp::SubInt128)"),
("i64.mul_wide_s", "makeWideIntMul(WideIntMulOp::MulWideSInt64)"),
("i64.mul_wide_u", "makeWideIntMul(WideIntMulOp::MulWideUInt64)"),
("f32.abs", "makeUnary(UnaryOp::AbsFloat32)"),
("f32.neg", "makeUnary(UnaryOp::NegFloat32)"),
("f32.ceil", "makeUnary(UnaryOp::CeilFloat32)"),
Expand Down
1 change: 0 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
'threads/atomic.wast',
]
SPEC_TESTSUITE_PROPOSALS_TO_SKIP = [
'wide-arithmetic',
]

# Paths are relative to the test/spec/testsuite directory
Expand Down
32 changes: 27 additions & 5 deletions src/gen-s-parser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3991,12 +3991,34 @@ switch (buf[0]) {
default: goto parse_error;
}
}
case 'm':
if (op == "i64.mul"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::MulInt64));
return Ok{};
case 'm': {
switch (buf[7]) {
case '\0':
if (op == "i64.mul"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::MulInt64));
return Ok{};
}
goto parse_error;
case '_': {
switch (buf[13]) {
case 's':
if (op == "i64.mul_wide_s"sv) {
CHECK_ERR(makeWideIntMul(ctx, pos, annotations, WideIntMulOp::MulWideSInt64));
return Ok{};
}
goto parse_error;
case 'u':
if (op == "i64.mul_wide_u"sv) {
CHECK_ERR(makeWideIntMul(ctx, pos, annotations, WideIntMulOp::MulWideUInt64));
return Ok{};
}
goto parse_error;
default: goto parse_error;
}
}
default: goto parse_error;
}
goto parse_error;
}
case 'n':
if (op == "i64.ne"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::NeInt64));
Expand Down
1 change: 1 addition & 0 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
}
}
Flow visitWideIntAddSub(WideIntAddSub* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitWideIntMul(WideIntMul* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitReturn(Return* curr) { WASM_UNREACHABLE("TODO"); }
Expand Down
1 change: 1 addition & 0 deletions src/ir/ReFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void ReFinalize::visitConst(Const* curr) { curr->finalize(); }
void ReFinalize::visitUnary(Unary* curr) { curr->finalize(); }
void ReFinalize::visitBinary(Binary* curr) { curr->finalize(); }
void ReFinalize::visitWideIntAddSub(WideIntAddSub* curr) { curr->finalize(); }
void ReFinalize::visitWideIntMul(WideIntMul* curr) { curr->finalize(); }
void ReFinalize::visitSelect(Select* curr) { curr->finalize(); }
void ReFinalize::visitDrop(Drop* curr) { curr->finalize(); }
void ReFinalize::visitReturn(Return* curr) { curr->finalize(); }
Expand Down
5 changes: 5 additions & 0 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
note(&curr->rightHigh, Type::i64);
}

void visitWideIntMul(WideIntMul* curr) {
note(&curr->left, Type::i64);
note(&curr->right, Type::i64);
}

void visitSelect(Select* curr, std::optional<Type> type = std::nullopt) {
if (type) {
note(&curr->ifTrue, *type);
Expand Down
3 changes: 3 additions & 0 deletions src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
return 1 + visit(curr->leftLow) + visit(curr->leftHigh) +
visit(curr->rightLow) + visit(curr->rightHigh);
}
CostType visitWideIntMul(WideIntMul* curr) {
return 4 + visit(curr->left) + visit(curr->right);
}
CostType visitSelect(Select* curr) {
return 1 + visit(curr->condition) + visit(curr->ifTrue) +
visit(curr->ifFalse);
Expand Down
1 change: 1 addition & 0 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ class EffectAnalyzer {
}
}
void visitWideIntAddSub(WideIntAddSub* curr) {}
void visitWideIntMul(WideIntMul* curr) {}
void visitSelect(Select* curr) {}
void visitDrop(Drop* curr) {}
void visitReturn(Return* curr) { parent.branchesOut = true; }
Expand Down
1 change: 1 addition & 0 deletions src/ir/possible-contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ struct InfoCollector
}
void visitBinary(Binary* curr) { addRoot(curr); }
void visitWideIntAddSub(WideIntAddSub* curr) { addRoot(curr); }
void visitWideIntMul(WideIntMul* curr) { addRoot(curr); }
void visitSelect(Select* curr) {
receiveChildValue(curr->ifTrue, curr);
receiveChildValue(curr->ifFalse, curr);
Expand Down
1 change: 1 addition & 0 deletions src/ir/subtype-exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
void visitUnary(Unary* curr) {}
void visitBinary(Binary* curr) {}
void visitWideIntAddSub(WideIntAddSub* curr) {}
void visitWideIntMul(WideIntMul* curr) {}
void visitSelect(Select* curr) {
self()->noteSubtype(curr->ifTrue, curr);
self()->noteSubtype(curr->ifFalse, curr);
Expand Down
10 changes: 10 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ struct NullInstrParserCtx {
return Ok{};
}

Result<> makeWideIntMul(Index, const std::vector<Annotation>&, WideIntMulOp) {
return Ok{};
}

Result<> makeUnary(Index, const std::vector<Annotation>&, UnaryOp) {
return Ok{};
}
Expand Down Expand Up @@ -2170,6 +2174,12 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
return withLoc(pos, irBuilder.makeWideIntAddSub(op));
}

Result<> makeWideIntMul(Index pos,
const std::vector<Annotation>& annotations,
WideIntMulOp op) {
return withLoc(pos, irBuilder.makeWideIntMul(op));
}

Result<>
makeUnary(Index pos, const std::vector<Annotation>& annotations, UnaryOp op) {
return withLoc(pos, irBuilder.makeUnary(op));
Expand Down
11 changes: 11 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Result<> makeWideIntAddSub(Ctx&,
const std::vector<Annotation>&,
WideIntAddSubOp op);
template<typename Ctx>
Result<>
makeWideIntMul(Ctx&, Index, const std::vector<Annotation>&, WideIntMulOp op);
template<typename Ctx>
Result<> makeUnary(Ctx&, Index, const std::vector<Annotation>&, UnaryOp op);
template<typename Ctx>
Result<> makeSelect(Ctx&, Index, const std::vector<Annotation>&);
Expand Down Expand Up @@ -1605,6 +1608,14 @@ Result<> makeWideIntAddSub(Ctx& ctx,
return ctx.makeWideIntAddSub(pos, annotations, op);
}

template<typename Ctx>
Result<> makeWideIntMul(Ctx& ctx,
Index pos,
const std::vector<Annotation>& annotations,
WideIntMulOp op) {
return ctx.makeWideIntMul(pos, annotations, op);
}

template<typename Ctx>
Result<> makeUnary(Ctx& ctx,
Index pos,
Expand Down
4 changes: 4 additions & 0 deletions src/passes/I64ToI32Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,10 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
WASM_UNREACHABLE("TODO: wide arithmetic lowering");
}

void visitWideIntMul(WideIntMul* curr) {
WASM_UNREACHABLE("TODO: wide arithmetic lowering");
}

void visitSelect(Select* curr) {
if (handleUnreachable(curr)) {
return;
Expand Down
14 changes: 14 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,20 @@ struct PrintExpressionContents
}
restoreNormalColor(o);
}
void visitWideIntMul(WideIntMul* curr) {
prepareColor(o);
switch (curr->op) {
case MulWideSInt64: {
o << "i64.mul_wide_s";
break;
}
case MulWideUInt64: {
o << "i64.mul_wide_u";
break;
}
}
restoreNormalColor(o);
}
void visitSelect(Select* curr) {
prepareColor(o) << "select";
restoreNormalColor(o);
Expand Down
1 change: 1 addition & 0 deletions src/passes/TypeGeneralizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ struct TransferFn : OverriddenVisitor<TransferFn> {
void visitUnary(Unary* curr) {}
void visitBinary(Binary* curr) {}
void visitWideIntAddSub(WideIntAddSub* curr) {}
void visitWideIntMul(WideIntMul* curr) {}

void visitSelect(Select* curr) {
if (curr->type.isRef()) {
Expand Down
1 change: 1 addition & 0 deletions src/support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(support_SOURCES
debug.cpp
dfa_minimization.cpp
file.cpp
int128.cpp
intervals.cpp
istring.cpp
json.cpp
Expand Down
120 changes: 120 additions & 0 deletions src/support/int128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2026 WebAssembly Community Group participants
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "support/int128.h"

namespace wasm {

#ifdef __SIZEOF_INT128__

Int128 mul_wide_s(uint64_t lhs, uint64_t rhs) {
__int128 result = static_cast<__int128>(static_cast<int64_t>(lhs)) *
static_cast<__int128>(static_cast<int64_t>(rhs));
return {static_cast<uint64_t>(result >> 64), static_cast<uint64_t>(result)};
}

Int128 mul_wide_u(uint64_t lhs, uint64_t rhs) {
unsigned __int128 result =
static_cast<unsigned __int128>(lhs) * static_cast<unsigned __int128>(rhs);
return {static_cast<uint64_t>(result >> 64), static_cast<uint64_t>(result)};
}

#else

Int128 mul_wide_s(uint64_t lhs, uint64_t rhs) {
return detail::mul_wide_s_fallback(lhs, rhs);
}

Int128 mul_wide_u(uint64_t lhs, uint64_t rhs) {
return detail::mul_wide_u_fallback(lhs, rhs);
}

#endif

namespace detail {

Int128 mul_wide_s_fallback(uint64_t lhs, uint64_t rhs) {
Comment thread
stevenfontanella marked this conversation as resolved.
auto [high, low] = mul_wide_u_fallback(lhs, rhs);

// If lhs is negative, then it looks like 2**64 is added to its unsigned value
// so we computed
// (lhs + 2**64) * rhs
// = lhs * rhs + 2**64 * rhs
// We need to subtract 2**64 * rhs, so just subtract rhs directly from the
// high bits.
//
// If lhs AND rhs are negative then we computed
// (lhs + 2**64) * (rhs + 2**64)
// = lhs * rhs + 2**64 * rhs + 2**64 * lhs + 2**128
// The last term overflowed and had no effect, so it's enough to do the two
// subtractions in the two different branches.
if (static_cast<int64_t>(lhs) < 0) {
high -= rhs;
}
if (static_cast<int64_t>(rhs) < 0) {
high -= lhs;
}

return {high, low};
}

Int128 mul_wide_u_fallback(uint64_t lhs, uint64_t rhs) {
// Decompose lhs and rhs into 4 32-bit numbers and distribute to compute:
// (lhsHigh * 2^32 + lhsLow) * (rhsHigh * 2^32 + rhsLow)
//
// (lhsHigh * rhsHigh) * 2^64 [Upper 64 bits]
// (lhsHigh * rhsLow + lhsLow * rhsHigh) * 2^32 [Middle 64 bits]
// (lhsLow * rhsLow) [Lower 64 bits]

uint64_t lhsLow = lhs & 0xffffffff;
uint64_t lhsHigh = lhs >> 32;
uint64_t rhsLow = rhs & 0xffffffff;
uint64_t rhsHigh = rhs >> 32;

uint64_t lowLow = lhsLow * rhsLow;
uint64_t lowHigh = lhsLow * rhsHigh;
uint64_t highLow = lhsHigh * rhsLow;
uint64_t highHigh = lhsHigh * rhsHigh;

// The lowest 32 bits consist only of lowLow (without its carry)
//
// The next 32 bits consist of `lowHigh + highLow + the carry of lowlow`
// (again this may carry to the next 32) Start by adding the carry which is
// guaranteed to not overflow 64 bits. Overflow can't happen because lowHigh
// is max (2**32 - 1)**2 and `lowLow >> 32` is no more than 32 bits, (2**32 -
// 1)**2 + (2**32 -1) < 2**64 - 1
uint64_t highOfLow = (lowLow >> 32) + lowHigh;

// We might have a carry into the next 32 (the low of the high), mask it out
// now so we can add highLow.
uint64_t carry = highOfLow >> 32;

// This is also guaranteed to not overflow by the same logic.
highOfLow = (highOfLow & 0xffffffff) + highLow;

// highOfLow might have exceeded 32 bits again, carry it again
uint64_t carry2 = highOfLow >> 32;
Comment on lines +97 to +107
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we calculate carry and carry2 separately? It seems that if we started with highOfLow = (lowLow >> 32) + lowHigh + highLow, then we could just calculate a single combined carry. Does that open us up to overflow problems?


uint64_t lower = (lowLow & 0xffffffff) | (highOfLow << 32);

// No need to worry about overflow here, since 128 bits is always enough to
// store the product of two 64-bit ints.
uint64_t higher = carry + carry2 + highHigh;

return {higher, lower};
}

} // namespace detail

} // namespace wasm
45 changes: 45 additions & 0 deletions src/support/int128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2026 WebAssembly Community Group participants
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef wasm_support_int128_h
#define wasm_support_int128_h

#include <cstdint>

namespace wasm {

struct Int128 {
uint64_t high;
uint64_t low;

bool operator==(const Int128& other) const {
return high == other.high && low == other.low;
}
};

// Computes the 128-bit product of two signed 64-bit integers.
Int128 mul_wide_s(uint64_t lhs, uint64_t rhs);

// Computes the 128-bit product of two unsigned 64-bit integers.
Int128 mul_wide_u(uint64_t lhs, uint64_t rhs);

namespace detail {
// Fallback implementations exposed for testing.
Int128 mul_wide_s_fallback(uint64_t lhs, uint64_t rhs);
Int128 mul_wide_u_fallback(uint64_t lhs, uint64_t rhs);
} // namespace detail

} // namespace wasm

#endif // wasm_support_i128_h
2 changes: 2 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,8 @@ enum ASTNodes {

I64Add128 = 0x13,
I64Sub128 = 0x14,
I64MulWideS = 0x15,
I64MulWideU = 0x16,

// reference types opcodes

Expand Down
Loading
Loading