From 3118826515d30337de0320be3461324d759df353 Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Thu, 9 Oct 2025 22:53:51 +0200 Subject: [PATCH] floordiv added --- tests/test_ops.py | 34 ++++++++++++++++++++++++++++++++++ tools/generate_stencils.py | 13 ++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tests/test_ops.py diff --git a/tests/test_ops.py b/tests/test_ops.py new file mode 100644 index 0000000..68af956 --- /dev/null +++ b/tests/test_ops.py @@ -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() diff --git a/tools/generate_stencils.py b/tools/generate_stencils.py index 7fe354e..47d6164 100644 --- a/tools/generate_stencils.py +++ b/tools/generate_stencils.py @@ -2,7 +2,7 @@ from typing import Generator import argparse -op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', +op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', 'floordiv': '/', 'gt': '>', 'eq': '==', 'mod': '%'} entry_func_prefix = '' @@ -123,9 +123,6 @@ if __name__ == "__main__": if args.abi: entry_func_prefix = f"__attribute__(({args.abi}_abi)) " - types = ['int', 'float'] - ops = ['add', 'sub', 'mul', 'div', 'gt', 'eq'] - code = """ // Auto-generated stencils for copapy // Do not edit manually @@ -134,6 +131,10 @@ if __name__ == "__main__": volatile float dummy_float = 1337; """ + # Scalar arithmetic: + types = ['int', 'float'] + ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'eq'] + for t1 in types: code += get_result_stubs1(t1) @@ -143,9 +144,11 @@ if __name__ == "__main__": for op, t1, t2 in permutate(ops, types, types): t_out = t1 if t1 == t2 else 'float' if op == 'floordiv': - code += get_op_code_int('div', t1, t2) + code += get_op_code_int('floordiv', t1, t2) elif op == 'div': code += get_op_code_float(op, t1, t2) + elif op == 'gt' or op == 'eq': + code += get_op_code(op, t1, t2, 'int') else: code += get_op_code(op, t1, t2, t_out)