floordiv added

This commit is contained in:
Nicolas Kruse 2025-10-09 22:53:51 +02:00
parent 3485584e5e
commit 3118826515
2 changed files with 42 additions and 5 deletions

34
tests/test_ops.py Normal file
View File

@ -0,0 +1,34 @@
from copapy import CPVariable, Target
from pytest import approx
def function1(c1):
return [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4]
def function2(c1):
return [c1 / 4, c1 / -4, c1 / 4, c1 / -4, (c1 * -1) / 4]
def test_compile():
c1 = CPVariable(9)
ret = function1(c1)
tg = Target()
print('* compile and copy ...')
tg.compile(ret)
#time.sleep(5)
print('* run and copy ...')
tg.run()
#print('* finished')
ret_ref = function1(9)
for test, ref, name in zip(ret, ret_ref, ['r1', 'r2', 'r3', 'r4', 'r5']):
val = tg.read_value(test)
print('+', name, val, ref)
assert val == approx(ref, 1e-5), name
if __name__ == "__main__":
test_compile()

View File

@ -2,7 +2,7 @@ from typing import Generator
import argparse import argparse
op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', 'floordiv': '/',
'gt': '>', 'eq': '==', 'mod': '%'} 'gt': '>', 'eq': '==', 'mod': '%'}
entry_func_prefix = '' entry_func_prefix = ''
@ -123,9 +123,6 @@ if __name__ == "__main__":
if args.abi: if args.abi:
entry_func_prefix = f"__attribute__(({args.abi}_abi)) " entry_func_prefix = f"__attribute__(({args.abi}_abi)) "
types = ['int', 'float']
ops = ['add', 'sub', 'mul', 'div', 'gt', 'eq']
code = """ code = """
// Auto-generated stencils for copapy // Auto-generated stencils for copapy
// Do not edit manually // Do not edit manually
@ -134,6 +131,10 @@ if __name__ == "__main__":
volatile float dummy_float = 1337; volatile float dummy_float = 1337;
""" """
# Scalar arithmetic:
types = ['int', 'float']
ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'eq']
for t1 in types: for t1 in types:
code += get_result_stubs1(t1) code += get_result_stubs1(t1)
@ -143,9 +144,11 @@ if __name__ == "__main__":
for op, t1, t2 in permutate(ops, types, types): for op, t1, t2 in permutate(ops, types, types):
t_out = t1 if t1 == t2 else 'float' t_out = t1 if t1 == t2 else 'float'
if op == 'floordiv': if op == 'floordiv':
code += get_op_code_int('div', t1, t2) code += get_op_code_int('floordiv', t1, t2)
elif op == 'div': elif op == 'div':
code += get_op_code_float(op, t1, t2) code += get_op_code_float(op, t1, t2)
elif op == 'gt' or op == 'eq':
code += get_op_code(op, t1, t2, 'int')
else: else:
code += get_op_code(op, t1, t2, t_out) code += get_op_code(op, t1, t2, t_out)