#+TITLE: Quick sort binding for Fortran * C template #+NAME: c_template #+BEGIN_SRC c struct TYPE_comp_big { TYPE x; int32_t i; }; int compare_TYPE_big( const void * l, const void * r ) { const struct TYPE_comp_big * restrict _l= l; const struct TYPE_comp_big * restrict _r= r; if( _l->x > _r->x ) return 1; if( _l->x < _r->x ) return -1; return 0; } void qsort_TYPE_big(TYPE* A_in, int32_t* iorder, int32_t isize) { struct TYPE_comp_big* A = malloc(isize * sizeof(struct TYPE_comp_big)); if (A == NULL) return; for (int i=0 ; i> """ for typ in ["int16_t", "int32_t", "int64_t", "double", "float"]: print( data.replace("TYPE", typ).replace("_big", "") ) print( data.replace("int32_t", "int64_t").replace("TYPE", typ) ) #+end_src #+NAME: replaced_f #+begin_src python :results output :noweb yes data = """ <> """ c1 = { "int16_t": "i2", "int32_t": "i", "int64_t": "i8", "double": "d", "float": "" } c2 = { "int16_t": "integer", "int32_t": "integer", "int64_t": "integer", "double": "real", "float": "real" } for typ in ["int16_t", "int32_t", "int64_t", "double", "float"]: print( data.replace("real",c2[typ]).replace("L",c1[typ]).replace("TYPE", typ).replace("_big", "") ) print( data.replace("real",c2[typ]).replace("L",c1[typ]).replace("int32_t", "int64_t").replace("TYPE", typ) ) #+end_src #+NAME: replaced_f2 #+begin_src python :results output :noweb yes data = """ <> """ c1 = { "int16_t": "i2", "int32_t": "i", "int64_t": "i8", "double": "d", "float": "" } c2 = { "int16_t": "integer", "int32_t": "integer", "int64_t": "integer", "double": "real", "float": "real" } for typ in ["int16_t", "int32_t", "int64_t", "double", "float"]: print( data.replace("real",c2[typ]).replace("L",c1[typ]).replace("TYPE", typ).replace("_big", "") ) print( data.replace("real",c2[typ]).replace("L",c1[typ]).replace("int32_t", "int64_t").replace("TYPE", typ) ) #+end_src * Generated C file #+BEGIN_SRC c :comments link :tangle qsort.c :noweb yes #include #include <> #+END_SRC * Generated Fortran file #+BEGIN_SRC f90 :tangle qsort_module.f90 :noweb yes module qsort_module use iso_c_binding interface <> end interface end module qsort_module <> #+END_SRC