Implements mult and sqrt
This commit is contained in:
parent
c5d039aa1f
commit
249c00f6a2
@ -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
|
# Do a check first for all function definitions
|
||||||
# to get their types. Otherwise you cannot call
|
# to get their types. Otherwise you cannot call
|
||||||
# a method that you haven't defined just yet,
|
# a method that you haven't defined just yet,
|
||||||
@ -404,6 +422,10 @@ class Visitor:
|
|||||||
yield wasm.Statement('{}.add'.format(exp_type.to_wasm()))
|
yield wasm.Statement('{}.add'.format(exp_type.to_wasm()))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if isinstance(node.op, ast.Mult):
|
||||||
|
yield wasm.Statement('{}.mul'.format(exp_type.to_wasm()))
|
||||||
|
return
|
||||||
|
|
||||||
if isinstance(node.op, ast.Sub):
|
if isinstance(node.op, ast.Sub):
|
||||||
yield wasm.Statement('{}.sub'.format(exp_type.to_wasm()))
|
yield wasm.Statement('{}.sub'.format(exp_type.to_wasm()))
|
||||||
return
|
return
|
||||||
|
|||||||
@ -334,3 +334,22 @@ def helper(vector: Tuple[i32, i32, i32]) -> i32:
|
|||||||
|
|
||||||
assert 161 == result.returned_value
|
assert 161 == result.returned_value
|
||||||
assert [] == result.log_int32_list
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user