Skip to content

Commit 5f437b9

Browse files
Add sub
1 parent f475fe6 commit 5f437b9

9 files changed

Lines changed: 57 additions & 6 deletions

File tree

scripts/gen-s-parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
("i64.rotl", "makeBinary(BinaryOp::RotLInt64)"),
148148
("i64.rotr", "makeBinary(BinaryOp::RotRInt64)"),
149149
("i64.add128", "makeWideIntAddSub(WideIntAddSubOp::AddInt128)"),
150+
("i64.sub128", "makeWideIntAddSub(WideIntAddSubOp::SubInt128)"),
150151
("f32.abs", "makeUnary(UnaryOp::AbsFloat32)"),
151152
("f32.neg", "makeUnary(UnaryOp::NegFloat32)"),
152153
("f32.ceil", "makeUnary(UnaryOp::CeilFloat32)"),

src/interpreter/interpreter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,20 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
218218
}
219219
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
220220
Flow visitWideIntAddSub(WideIntAddSub* curr) {
221-
if (curr->op == AddInt128) {
221+
if (curr->op == AddInt128 || curr->op == SubInt128) {
222222
uint64_t highRHS = pop().geti64();
223223
uint64_t lowRHS = pop().geti64();
224224
uint64_t highLHS = pop().geti64();
225225
uint64_t lowLHS = pop().geti64();
226226

227-
uint64_t lowRes = lowLHS + lowRHS;
228-
uint64_t highRes = highLHS + highRHS + (lowRes < lowLHS);
227+
uint64_t lowRes, highRes;
228+
if (curr->op == AddInt128) {
229+
lowRes = lowLHS + lowRHS;
230+
highRes = highLHS + highRHS + (lowRes < lowLHS);
231+
} else {
232+
lowRes = lowLHS - lowRHS;
233+
highRes = highLHS - highRHS - (lowLHS < lowRHS);
234+
}
229235

230236
push(Literal(lowRes));
231237
push(Literal(highRes));

src/passes/Print.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,9 @@ struct PrintExpressionContents
20312031
case AddInt128:
20322032
o << "i64.add128";
20332033
break;
2034+
case SubInt128:
2035+
o << "i64.sub128";
2036+
break;
20342037
default:
20352038
WASM_UNREACHABLE("invalid wide int binary op");
20362039
}

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ enum ASTNodes {
11391139
// wide arithmetic opcodes
11401140

11411141
I64Add128 = 0x13,
1142+
I64Sub128 = 0x14,
11421143

11431144
// reference types opcodes
11441145

src/wasm-interpreter.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,14 +1802,20 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
18021802
VISIT(leftHigh, curr->leftHigh);
18031803
VISIT(rightLow, curr->rightLow);
18041804
VISIT(rightHigh, curr->rightHigh);
1805-
if (curr->op == AddInt128) {
1805+
if (curr->op == AddInt128 || curr->op == SubInt128) {
18061806
uint64_t lowLHS = leftLow.getSingleValue().geti64();
18071807
uint64_t highLHS = leftHigh.getSingleValue().geti64();
18081808
uint64_t lowRHS = rightLow.getSingleValue().geti64();
18091809
uint64_t highRHS = rightHigh.getSingleValue().geti64();
18101810

1811-
uint64_t lowRes = lowLHS + lowRHS;
1812-
uint64_t highRes = highLHS + highRHS + (lowRes < lowLHS);
1811+
uint64_t lowRes, highRes;
1812+
if (curr->op == AddInt128) {
1813+
lowRes = lowLHS + lowRHS;
1814+
highRes = highLHS + highRHS + (lowRes < lowLHS);
1815+
} else {
1816+
lowRes = lowLHS - lowRHS;
1817+
highRes = highLHS - highRHS - (lowLHS < lowRHS);
1818+
}
18131819

18141820
Literals results;
18151821
results.push_back(Literal(lowRes));

src/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ enum StringEqOp {
640640

641641
enum WideIntAddSubOp {
642642
AddInt128,
643+
SubInt128,
643644
};
644645

645646
//

src/wasm/wasm-binary.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,6 +4000,8 @@ Result<> WasmBinaryReader::readInst() {
40004000
return builder.makeMemoryFill(getMemoryName(getU32LEB()));
40014001
case BinaryConsts::I64Add128:
40024002
return builder.makeWideIntAddSub(AddInt128);
4003+
case BinaryConsts::I64Sub128:
4004+
return builder.makeWideIntAddSub(SubInt128);
40034005

40044006
case BinaryConsts::TableSize:
40054007
return builder.makeTableSize(getTableName(getU32LEB()));

src/wasm/wasm-stack.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,9 @@ void BinaryInstWriter::visitWideIntAddSub(WideIntAddSub* curr) {
22932293
case AddInt128:
22942294
o << U32LEB(BinaryConsts::I64Add128);
22952295
break;
2296+
case SubInt128:
2297+
o << U32LEB(BinaryConsts::I64Sub128);
2298+
break;
22962299
default:
22972300
WASM_UNREACHABLE("invalid wide int binary op");
22982301
}

test/spec/wide-arithmetic.wast

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
local.get 2
66
local.get 3
77
i64.add128)
8+
(func (export "i64.sub128") (param i64 i64 i64 i64) (result i64 i64)
9+
local.get 0
10+
local.get 1
11+
local.get 2
12+
local.get 3
13+
i64.sub128)
814
)
915

1016
;; simple addition
@@ -164,3 +170,25 @@
164170
i64.add128)
165171
)
166172
"type mismatch")
173+
174+
;; simple subtraction
175+
(assert_return (invoke "i64.sub128"
176+
(i64.const 0) (i64.const 0)
177+
(i64.const 0) (i64.const 0))
178+
(i64.const 0) (i64.const 0))
179+
(assert_return (invoke "i64.sub128"
180+
(i64.const 1) (i64.const 0)
181+
(i64.const 1) (i64.const 0))
182+
(i64.const 0) (i64.const 0))
183+
(assert_return (invoke "i64.sub128"
184+
(i64.const 0) (i64.const 0)
185+
(i64.const 1) (i64.const 0))
186+
(i64.const 0xffffffffffffffff) (i64.const 0xffffffffffffffff))
187+
(assert_return (invoke "i64.sub128"
188+
(i64.const 0) (i64.const 1)
189+
(i64.const 1) (i64.const 0))
190+
(i64.const 0xffffffffffffffff) (i64.const 0))
191+
(assert_return (invoke "i64.sub128"
192+
(i64.const 1) (i64.const 2)
193+
(i64.const 3) (i64.const 4))
194+
(i64.const 0xfffffffffffffffe) (i64.const 0xfffffffffffffffd))

0 commit comments

Comments
 (0)