From 249c00f6a2609182a82c7c42f46665298ad6103c Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Fri, 29 Apr 2022 12:56:45 +0200 Subject: [PATCH] Implements mult and sqrt --- py2wasm/python.py | 22 ++++++++++++++++++++++ tests/integration/test_simple.py | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/py2wasm/python.py b/py2wasm/python.py index 6d52490..7639ee1 100644 --- a/py2wasm/python.py +++ b/py2wasm/python.py @@ -91,6 +91,24 @@ class Visitor: ] )) + # We create functions for these special instructions + # We'll let a future inline optimizer clean this up for us + # TODO: Either a) make a sqrt32 and a sqrt64; + # or b) decide to make the whole language auto-cast + module.functions.append(wasm.Function( + 'sqrt', + False, + [ + ('z', self._type_map['f32']), + ], + [], + self._type_map['f32'], + [ + wasm.Statement('local.get', '$z'), + wasm.Statement('f32.sqrt'), + ] + )) + # Do a check first for all function definitions # to get their types. Otherwise you cannot call # a method that you haven't defined just yet, @@ -404,6 +422,10 @@ class Visitor: yield wasm.Statement('{}.add'.format(exp_type.to_wasm())) return + if isinstance(node.op, ast.Mult): + yield wasm.Statement('{}.mul'.format(exp_type.to_wasm())) + return + if isinstance(node.op, ast.Sub): yield wasm.Statement('{}.sub'.format(exp_type.to_wasm())) return diff --git a/tests/integration/test_simple.py b/tests/integration/test_simple.py index 0772cca..fd89412 100644 --- a/tests/integration/test_simple.py +++ b/tests/integration/test_simple.py @@ -334,3 +334,22 @@ def helper(vector: Tuple[i32, i32, i32]) -> i32: assert 161 == result.returned_value assert [] == result.log_int32_list + +@pytest.mark.integration_test +def test_tuple_float(): + code_py = """ + +@exported +def testEntry() -> f32: + return helper((1, 2, 3, )) + +def helper(v: Tuple[f32, f32, f32]) -> f32: + # sqrt is guaranteed by wasm, so we can use it + # ATM it's a 32 bit variant, but that might change. + return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) +""" + + result = Suite(code_py, 'test_call').run_code() + + assert 3.74 < result.returned_value < 3.75 + assert [] == result.log_int32_list