diff --git a/examples/extern_C/README b/examples/extern_C/README index fcdcb1e..4bc0873 100644 --- a/examples/extern_C/README +++ b/examples/extern_C/README @@ -4,3 +4,8 @@ This example show how to import an external function written in C in an Heptagon - Only functions with no side effects can be safely imported. - The imported function must have the same calling convention as generated code (inputs given by value, outputs in a f_out structure). - The generated code will include "mathext.h", that should define the step function + +To make it work: + heptc mathext.epi + heptc imports.ept + gcc -std=c99 -I . mathext.c imports_c/*.c -o imports diff --git a/examples/extern_C/imports.ept b/examples/extern_C/imports.ept index 19aab75..9037d61 100644 --- a/examples/extern_C/imports.ept +++ b/examples/extern_C/imports.ept @@ -3,4 +3,9 @@ open Mathext fun f(a:float) returns (o:float) let o = mycos(a); +tel + +node g(a:float) returns (o:float) +let + o = st_cos(a); tel \ No newline at end of file diff --git a/examples/extern_C/mathext.c b/examples/extern_C/mathext.c index f3f7367..4e05bdf 100644 --- a/examples/extern_C/mathext.c +++ b/examples/extern_C/mathext.c @@ -1,7 +1,21 @@ #include #include "mathext.h" -void mycos(float a, mycos_out *out) +void mycos_step(float a, mycos_out *out) { out->o = cos(a); } + +void st_cos_reset(st_cos_mem *self) +{ + self->i = 0; + for(int j = 0; j < 100; ++j) + self->mem[j] = 0.0; +} + +void st_cos_step(float a, st_cos_out *out, st_cos_mem *self) +{ + out->o = self->mem[self->i]; + self->i = (self->i++) % 100; + self->mem[self->i] = cos(a); +} diff --git a/examples/extern_C/mathext.epi b/examples/extern_C/mathext.epi index 3eb4f32..dd574d3 100644 --- a/examples/extern_C/mathext.epi +++ b/examples/extern_C/mathext.epi @@ -1 +1,3 @@ -val fun mycos(a:float) returns (o:float) \ No newline at end of file +val fun mycos(a:float) returns (o:float) + +val node st_cos(a:float) returns (o:float) diff --git a/examples/extern_C/mathext.h b/examples/extern_C/mathext.h index 9c01477..f20f922 100644 --- a/examples/extern_C/mathext.h +++ b/examples/extern_C/mathext.h @@ -1,8 +1,25 @@ -#ifdef MATHEXT_H +#ifndef MATHEXT_H #define MATHEXT_H -struct mycos_out { +/* Example of a combinatorial function */ +typedef struct mycos_out { float o; -}; +} mycos_out; + +void mycos_step(float a, mycos_out *o); + +/* Example of a statefull function. */ +typedef struct st_cos_out { + float o; +} st_cos_out; + +typedef struct st_cos_mem { + int i; + float mem[100]; +} st_cos_mem; + +void st_cos_reset(st_cos_mem *self); +void st_cos_step(float a, st_cos_out *out, st_cos_mem *self); + +#endif -void mycos(float a, mycos_out *o);