mirror of https://github.com/Nonannet/copapy.git
pow and cast partially implemented
This commit is contained in:
parent
140d1f0f9f
commit
47375a2f3c
|
|
@ -295,9 +295,11 @@ class cpint(CPNumber):
|
||||||
|
|
||||||
|
|
||||||
class cpfloat(CPNumber):
|
class cpfloat(CPNumber):
|
||||||
def __init__(self, source: float | Node):
|
def __init__(self, source: float | Node | CPNumber):
|
||||||
if isinstance(source, Node):
|
if isinstance(source, Node):
|
||||||
self.source = source
|
self.source = source
|
||||||
|
elif isinstance(source, CPNumber):
|
||||||
|
self.source = _add_op('cast_float', [source]).source
|
||||||
else:
|
else:
|
||||||
self.source = InitVar(float(source))
|
self.source = InitVar(float(source))
|
||||||
self.dtype = 'float'
|
self.dtype = 'float'
|
||||||
|
|
@ -373,7 +375,7 @@ def _add_op(op: str, args: list[CPNumber | int | float], commutative: bool = Fal
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def cpvalue(value: bool) -> cpbool:
|
def cpvalue(value: bool) -> cpbool: # pyright: ignore[reportOverlappingOverload]
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -653,7 +655,6 @@ def compile_to_instruction_list(node_list: Iterable[Node], sdb: stencil_database
|
||||||
data = sdb.get_function_code('entry_function_shell', 'end')
|
data = sdb.get_function_code('entry_function_shell', 'end')
|
||||||
data_list.append(data)
|
data_list.append(data)
|
||||||
offset += len(data)
|
offset += len(data)
|
||||||
# print('function_end', offset, data)
|
|
||||||
|
|
||||||
# allocate program data
|
# allocate program data
|
||||||
dw.write_com(binw.Command.ALLOCATE_CODE)
|
dw.write_com(binw.Command.ALLOCATE_CODE)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
#include "runmem.h"
|
#include "runmem.h"
|
||||||
#include "mem_man.h"
|
#include "mem_man.h"
|
||||||
|
|
||||||
|
|
@ -116,6 +117,9 @@ int parse_commands(uint8_t *bytes) {
|
||||||
patch(executable_memory + offs, reloc_type, value + data_offs);
|
patch(executable_memory + offs, reloc_type, value + data_offs);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case PATCH_MATH_POW:
|
||||||
|
break;
|
||||||
|
|
||||||
case ENTRY_POINT:
|
case ENTRY_POINT:
|
||||||
LOG("ENTRY_POINT rel_entr_point=%i\n", rel_entr_point);
|
LOG("ENTRY_POINT rel_entr_point=%i\n", rel_entr_point);
|
||||||
rel_entr_point = *(uint32_t*)bytes; bytes += 4;
|
rel_entr_point = *(uint32_t*)bytes; bytes += 4;
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
#define READ_DATA 65
|
#define READ_DATA 65
|
||||||
#define END_COM 256
|
#define END_COM 256
|
||||||
#define FREE_MEMORY 257
|
#define FREE_MEMORY 257
|
||||||
|
#define PATCH_MATH_POW 512
|
||||||
|
|
||||||
/* Relocation types */
|
/* Relocation types */
|
||||||
#define PATCH_RELATIVE_32 0
|
#define PATCH_RELATIVE_32 0
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/',
|
op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', 'pow': '**',
|
||||||
'gt': '>', 'eq': '==', 'ne': '!=', 'mod': '%'}
|
'gt': '>', 'eq': '==', 'ne': '!=', 'mod': '%'}
|
||||||
|
|
||||||
entry_func_prefix = ''
|
entry_func_prefix = ''
|
||||||
|
|
@ -37,6 +37,14 @@ def get_op_code(op: str, type1: str, type2: str, type_out: str) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_cast(type1: str, type2: str, type_out: str) -> str:
|
||||||
|
return f"""
|
||||||
|
{stencil_func_prefix}void cast_{type_out}_{type1}_{type2}({type1} arg1, {type2} arg2) {{
|
||||||
|
result_{type_out}_{type2}(({type1})arg1, arg2);
|
||||||
|
}}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def get_conv_code(type1: str, type2: str, type_out: str) -> str:
|
def get_conv_code(type1: str, type2: str, type_out: str) -> str:
|
||||||
return f"""
|
return f"""
|
||||||
{stencil_func_prefix}void conv_{type1}_{type2}({type1} arg1, {type2} arg2) {{
|
{stencil_func_prefix}void conv_{type1}_{type2}({type1} arg1, {type2} arg2) {{
|
||||||
|
|
@ -53,6 +61,15 @@ def get_op_code_float(op: str, type1: str, type2: str) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_pow(type1: str, type2: str) -> str:
|
||||||
|
return f"""
|
||||||
|
{stencil_func_prefix}void pow_{type1}_{type2}({type1} arg1, {type2} arg2) {{
|
||||||
|
ret = math_pow((double)arg1, (double)arg2);
|
||||||
|
result_float_{type2}((float)ret);
|
||||||
|
}}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def get_floordiv(op: str, type1: str, type2: str) -> str:
|
def get_floordiv(op: str, type1: str, type2: str) -> str:
|
||||||
if type1 == 'int' and type2 == 'int':
|
if type1 == 'int' and type2 == 'int':
|
||||||
return f"""
|
return f"""
|
||||||
|
|
@ -128,17 +145,23 @@ if __name__ == "__main__":
|
||||||
// Auto-generated stencils for copapy
|
// Auto-generated stencils for copapy
|
||||||
// Do not edit manually
|
// Do not edit manually
|
||||||
|
|
||||||
|
void math_pow(double arg1, double arg2);
|
||||||
|
|
||||||
volatile int dummy_int = 1337;
|
volatile int dummy_int = 1337;
|
||||||
volatile float dummy_float = 1337;
|
volatile float dummy_float = 1337;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Scalar arithmetic:
|
# Scalar arithmetic:
|
||||||
types = ['int', 'float']
|
types = ['int', 'float']
|
||||||
ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'eq', 'ne']
|
ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'eq', 'ne', 'pow']
|
||||||
|
|
||||||
for t1 in types:
|
for t1 in types:
|
||||||
code += get_result_stubs1(t1)
|
code += get_result_stubs1(t1)
|
||||||
|
|
||||||
|
for t1, t2 in permutate(types, types):
|
||||||
|
t_out = 'int' if t1 == 'float' else 'float'
|
||||||
|
code += get_cast(t1, t2, t_out)
|
||||||
|
|
||||||
for t1, t2 in permutate(types, types):
|
for t1, t2 in permutate(types, types):
|
||||||
code += get_result_stubs2(t1, t2)
|
code += get_result_stubs2(t1, t2)
|
||||||
|
|
||||||
|
|
@ -150,6 +173,8 @@ if __name__ == "__main__":
|
||||||
code += get_floordiv('floordiv', t1, t2)
|
code += get_floordiv('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 == 'pow':
|
||||||
|
code += get_pow(t1, t2)
|
||||||
elif op == 'gt' or op == 'eq' or op == 'ne':
|
elif op == 'gt' or op == 'eq' or op == 'ne':
|
||||||
code += get_op_code(op, t1, t2, 'int')
|
code += get_op_code(op, t1, t2, 'int')
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue