Skip to content

Commit 4ea373b

Browse files
authored
[Interpreter] Float64 (#7327)
Building on top of #7227 , the following are implemented and tested: - f64.add - f64.sub - f64.mul - f64.div - f64.sqrt - f64.ceil - f64.floor - f64.trunc - f64.nearbyint
1 parent 84cac7a commit 4ea373b

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

src/interpreter/interpreter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,23 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
9090
auto value = pop();
9191
switch (curr->op) {
9292
case SqrtFloat32:
93+
case SqrtFloat64:
9394
push(value.sqrt());
9495
return {};
9596
case CeilFloat32:
97+
case CeilFloat64:
9698
push(value.ceil());
9799
return {};
98100
case FloorFloat32:
101+
case FloorFloat64:
99102
push(value.floor());
100103
return {};
101104
case TruncFloat32:
105+
case TruncFloat64:
102106
push(value.trunc());
103107
return {};
104108
case NearestFloat32:
109+
case NearestFloat64:
105110
push(value.nearbyint());
106111
return {};
107112
default:
@@ -115,23 +120,29 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
115120
switch (curr->op) {
116121
case AddInt32:
117122
case AddFloat32:
123+
case AddFloat64:
118124
push(lhs.add(rhs));
119125
return {};
120126
case SubInt32:
121127
case SubFloat32:
128+
case SubFloat64:
122129
push(lhs.sub(rhs));
123130
return {};
124131
case MulInt32:
125132
case MulFloat32:
133+
case MulFloat64:
126134
push(lhs.mul(rhs));
127135
return {};
128136
case DivFloat32:
137+
case DivFloat64:
129138
push(lhs.div(rhs));
130139
return {};
131140
case MinFloat32:
141+
case MinFloat64:
132142
push(lhs.min(rhs));
133143
return {};
134144
case MaxFloat32:
145+
case MaxFloat64:
135146
push(lhs.max(rhs));
136147
return {};
137148
default:

test/gtest/interpreter.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
using namespace wasm;
2727

28+
// uInt32
2829
TEST(InterpreterTest, AddI32) {
2930
Module wasm;
3031
IRBuilder builder(wasm);
@@ -76,6 +77,7 @@ TEST(InterpreterTest, MulI32) {
7677
EXPECT_EQ(results, expected);
7778
}
7879

80+
// Float32
7981
TEST(InterpreterTest, AddF32) {
8082
Module wasm;
8183
IRBuilder builder(wasm);
@@ -223,3 +225,152 @@ TEST(InterpreterTest, NearF32) {
223225

224226
EXPECT_EQ(results, expected);
225227
}
228+
229+
// Float 64
230+
TEST(InterpreterTest, AddF64) {
231+
Module wasm;
232+
IRBuilder builder(wasm);
233+
234+
ASSERT_FALSE(builder.makeConst(Literal(double(0.0))).getErr());
235+
ASSERT_FALSE(builder.makeConst(Literal(double(1.0))).getErr());
236+
ASSERT_FALSE(builder.makeBinary(AddFloat64).getErr());
237+
238+
auto expr = builder.build();
239+
ASSERT_FALSE(expr.getErr());
240+
241+
auto results = Interpreter{}.run(*expr);
242+
std::vector<Literal> expected{Literal(double(1.0))};
243+
244+
EXPECT_EQ(results, expected);
245+
}
246+
247+
TEST(InterpreterTest, SubF64) {
248+
Module wasm;
249+
IRBuilder builder(wasm);
250+
251+
ASSERT_FALSE(builder.makeConst(Literal(double(1.0))).getErr());
252+
ASSERT_FALSE(builder.makeConst(Literal(double(2.0))).getErr());
253+
ASSERT_FALSE(builder.makeBinary(SubFloat64).getErr());
254+
255+
auto expr = builder.build();
256+
ASSERT_FALSE(expr.getErr());
257+
258+
auto results = Interpreter{}.run(*expr);
259+
std::vector<Literal> expected{Literal(double(-1.0))};
260+
261+
EXPECT_EQ(results, expected);
262+
}
263+
264+
TEST(InterpreterTest, MulF64) {
265+
Module wasm;
266+
IRBuilder builder(wasm);
267+
268+
ASSERT_FALSE(builder.makeConst(Literal(double(1.5))).getErr());
269+
ASSERT_FALSE(builder.makeConst(Literal(double(2.0))).getErr());
270+
ASSERT_FALSE(builder.makeBinary(MulFloat64).getErr());
271+
272+
auto expr = builder.build();
273+
ASSERT_FALSE(expr.getErr());
274+
275+
auto results = Interpreter{}.run(*expr);
276+
std::vector<Literal> expected{Literal(double(3.0))};
277+
278+
EXPECT_EQ(results, expected);
279+
}
280+
281+
TEST(InterpreterTest, DivF64) {
282+
Module wasm;
283+
IRBuilder builder(wasm);
284+
285+
ASSERT_FALSE(builder.makeConst(Literal(double(5.0))).getErr());
286+
ASSERT_FALSE(builder.makeConst(Literal(double(2.0))).getErr());
287+
ASSERT_FALSE(builder.makeBinary(DivFloat64).getErr());
288+
289+
auto expr = builder.build();
290+
ASSERT_FALSE(expr.getErr());
291+
292+
auto results = Interpreter{}.run(*expr);
293+
std::vector<Literal> expected{Literal(double(2.5))};
294+
295+
EXPECT_EQ(results, expected);
296+
}
297+
298+
TEST(InterpreterTest, SqrtF64) {
299+
Module wasm;
300+
IRBuilder builder(wasm);
301+
302+
ASSERT_FALSE(builder.makeConst(Literal(double(5.0))).getErr());
303+
ASSERT_FALSE(builder.makeUnary(SqrtFloat64).getErr());
304+
305+
auto expr = builder.build();
306+
ASSERT_FALSE(expr.getErr());
307+
308+
auto results = Interpreter{}.run(*expr);
309+
std::vector<Literal> expected{Literal(double(2.23606797749979))};
310+
311+
EXPECT_EQ(results, expected);
312+
}
313+
314+
TEST(InterpreterTest, CeilF64) {
315+
Module wasm;
316+
IRBuilder builder(wasm);
317+
318+
ASSERT_FALSE(builder.makeConst(Literal(double(1.5))).getErr());
319+
ASSERT_FALSE(builder.makeUnary(CeilFloat64).getErr());
320+
321+
auto expr = builder.build();
322+
ASSERT_FALSE(expr.getErr());
323+
324+
auto results = Interpreter{}.run(*expr);
325+
std::vector<Literal> expected{Literal(double(2.0))};
326+
327+
EXPECT_EQ(results, expected);
328+
}
329+
330+
TEST(InterpreterTest, FloorF64) {
331+
Module wasm;
332+
IRBuilder builder(wasm);
333+
334+
ASSERT_FALSE(builder.makeConst(Literal(double(1.5))).getErr());
335+
ASSERT_FALSE(builder.makeUnary(FloorFloat64).getErr());
336+
337+
auto expr = builder.build();
338+
ASSERT_FALSE(expr.getErr());
339+
340+
auto results = Interpreter{}.run(*expr);
341+
std::vector<Literal> expected{Literal(double(1.0))};
342+
343+
EXPECT_EQ(results, expected);
344+
}
345+
346+
TEST(InterpreterTest, TruncF64) {
347+
Module wasm;
348+
IRBuilder builder(wasm);
349+
350+
ASSERT_FALSE(builder.makeConst(Literal(double(2.017281))).getErr());
351+
ASSERT_FALSE(builder.makeUnary(TruncFloat64).getErr());
352+
353+
auto expr = builder.build();
354+
ASSERT_FALSE(expr.getErr());
355+
356+
auto results = Interpreter{}.run(*expr);
357+
std::vector<Literal> expected{Literal(double(2.0))};
358+
359+
EXPECT_EQ(results, expected);
360+
}
361+
362+
TEST(InterpreterTest, NearF64) {
363+
Module wasm;
364+
IRBuilder builder(wasm);
365+
366+
ASSERT_FALSE(builder.makeConst(Literal(double(2.5))).getErr());
367+
ASSERT_FALSE(builder.makeUnary(NearestFloat64).getErr());
368+
369+
auto expr = builder.build();
370+
ASSERT_FALSE(expr.getErr());
371+
372+
auto results = Interpreter{}.run(*expr);
373+
std::vector<Literal> expected{Literal(double(2.0))};
374+
375+
EXPECT_EQ(results, expected);
376+
}

0 commit comments

Comments
 (0)