9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-09 06:53:38 +01:00
qp2/src/utils/qsort.org

3.8 KiB

Quick sort binding for Fortran

C template

struct TYPE_comp_big {
  TYPE    x;
  int32_t i;
};

int compare_TYPE_big( const void * l, const void * r )
{
  const TYPE * restrict _l= l;
  const TYPE * restrict _r= r;
  if( *_l > *_r ) return 1;
  if( *_l < *_r ) return -1;
  return 0;
}

void qsort_TYPE_big(TYPE* restrict A_in, int32_t* restrict 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<isize ; ++i) {
    A[i].x = A_in[i];
    A[i].i = iorder[i];
  }

  qsort( (void*) A, (size_t) isize, sizeof(struct TYPE_comp_big), compare_TYPE_big);

  for (int i=0 ; i<isize ; ++i) {
    A_in[i] = A[i].x;
    iorder[i] = A[i].i;
  }
  free(A);
}

void qsort_TYPE_noidx_big(TYPE* A, int32_t isize) {
  qsort( (void*) A, (size_t) isize, sizeof(TYPE), compare_TYPE_big);
}

Fortran template

#+NAME:f_template

subroutine Lsort_big_c(A, iorder, isize) bind(C, name="qsort_TYPE_big")
  use iso_c_binding
  integer(c_int32_t), value :: isize
  integer(c_int32_t)        :: iorder(isize)
  real   (c_TYPE)         :: A(isize)
end subroutine Lsort_big_c

subroutine Lsort_noidx_big_c(A, isize) bind(C, name="qsort_TYPE_noidx_big")
  use iso_c_binding
  integer(c_int32_t), value :: isize
  real   (c_TYPE)         :: A(isize)
end subroutine Lsort_noidx_big_c

#+NAME:f_template2

subroutine Lsort_big(A, iorder, isize) 
  use qsort_module
  use iso_c_binding
  integer(c_int32_t)        :: isize
  integer(c_int32_t)        :: iorder(isize)
  real   (c_TYPE)         :: A(isize)
  call Lsort_big_c(A, iorder, isize)
end subroutine Lsort_big

subroutine Lsort_noidx_big(A, isize)
  use iso_c_binding
  use qsort_module
  integer(c_int32_t) :: isize
  real   (c_TYPE)    :: A(isize)
  call Lsort_noidx_big_c(A, isize)
end subroutine Lsort_noidx_big

Python scripts for type replacements

data = """
<<c_template>>
"""
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) )
data = """
<<f_template>>
"""
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) )
data = """
<<f_template2>>
"""
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) )

Generated C file

#include <stdlib.h>
#include <stdint.h>
<<replaced()>>

Generated Fortran file

module qsort_module
  use iso_c_binding
  
  interface
     <<replaced_f()>>
  end interface

end module qsort_module

<<replaced_f2()>>