This commit is contained in:
Nicolas 2025-10-08 22:21:33 +02:00
parent 6b5bc0e28a
commit 37dfabfdf7
3 changed files with 16 additions and 14 deletions

View File

@ -37,11 +37,21 @@ def get_op_code(op: str, type1: str, type2: str, type_out: str) -> str:
"""
def get_conv_code(type1: str, type2: str, type_out: str) -> str:
return f"""
void conv_{type1}_{type2}({type1} arg1, {type2} arg2) {{
asm volatile (".long 0xE1401F0F");
result_{type_out}_{type2}(({type_out})arg1, arg2);
asm volatile (".long 0xE2401F0F");
}}
"""
def get_op_code_float(op: str, type1: str, type2: str) -> str:
return f"""
void {op}_{type1}_{type2}({type1} arg1, {type2} arg2) {{
asm volatile (".long 0xE1401F0F");
result_float_{type2}((float)arg1 {op_signs[op]} arg2, arg2);
result_float_{type2}((float)arg1 {op_signs[op]} (float)arg2, arg2);
asm volatile (".long 0xE2401F0F");
}}
"""
@ -140,7 +150,7 @@ if __name__ == "__main__":
t_out = t1 if t1 == t2 else 'float'
if op == 'floordiv':
code += get_op_code_int('div', t1, t2)
elif op == 'div' and t1 == 'int':
elif op == 'div':
code += get_op_code_float(op, t1, t2)
else:
code += get_op_code(op, t1, t2, t_out)

View File

@ -2,15 +2,6 @@
#include <Python.h>
#include "runmem.h"
/*
* coparun(PyObject *self, PyObject *args)
* Accepts a Python `bytes` (or objects supporting the buffer protocol).
* We use the "y#" format in PyArg_ParseTuple which returns a pointer to
* the internal bytes buffer and its length (Py_ssize_t). For safety and
* performance we pass that pointer directly to parse_commands which expects
* a uint8_t* buffer. If parse_commands needs the length, consider
* extending its API to accept a length parameter.
*/
static PyObject* coparun(PyObject* self, PyObject* args) {
const char *buf;
Py_ssize_t buf_len;

View File

@ -6,7 +6,7 @@ def function(c1, c2):
i1 = c1 * 3.3 + 5
i2 = c2 * 5 + c1
#r1 = i1 + i2 * 55 / 4
r1 = i1 + i2 * 55 * 4
r1 = i1 + i2 * 55 / 4
r2 = 4 * i2 + 5
return i1, i2, r1, r2
@ -30,8 +30,9 @@ def test_compile():
ret_ref = function(4, 2)
for test, ref, name in zip(ret, ret_ref, ['i1', 'i2', 'r1', 'r2']):
print('+', name)
assert tg.read_value(test) == approx(ref, 1e-5), name
val = tg.read_value(test)
print('+', name, val, ref)
assert val == approx(ref, 1e-5), name
if __name__ == "__main__":