annotate fft/fftw/fftw-3.3.4/api/genf03.pl @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 #!/usr/bin/perl -w
Chris@19 2 # Generate Fortran 2003 interfaces from a sequence of C function declarations
Chris@19 3 # of the form (one per line):
Chris@19 4 # extern <type> <name>(...args...)
Chris@19 5 # extern <type> <name>(...args...)
Chris@19 6 # ...
Chris@19 7 # with no line breaks within a given function. (It's too much work to
Chris@19 8 # write a general parser, since we just have to handle FFTW's header files.)
Chris@19 9
Chris@19 10 sub canonicalize_type {
Chris@19 11 my($type);
Chris@19 12 ($type) = @_;
Chris@19 13 $type =~ s/ +/ /g;
Chris@19 14 $type =~ s/^ //;
Chris@19 15 $type =~ s/ $//;
Chris@19 16 $type =~ s/([^\* ])\*/$1 \*/g;
Chris@19 17 return $type;
Chris@19 18 }
Chris@19 19
Chris@19 20 # C->Fortran map of supported return types
Chris@19 21 %return_types = (
Chris@19 22 "int" => "integer(C_INT)",
Chris@19 23 "ptrdiff_t" => "integer(C_INTPTR_T)",
Chris@19 24 "size_t" => "integer(C_SIZE_T)",
Chris@19 25 "double" => "real(C_DOUBLE)",
Chris@19 26 "float" => "real(C_FLOAT)",
Chris@19 27 "long double" => "real(C_LONG_DOUBLE)",
Chris@19 28 "float128__" => "real(16)",
Chris@19 29 "fftw_plan" => "type(C_PTR)",
Chris@19 30 "fftwf_plan" => "type(C_PTR)",
Chris@19 31 "fftwl_plan" => "type(C_PTR)",
Chris@19 32 "fftwq_plan" => "type(C_PTR)",
Chris@19 33 "void *" => "type(C_PTR)",
Chris@19 34 "char *" => "type(C_PTR)",
Chris@19 35 "double *" => "type(C_PTR)",
Chris@19 36 "float *" => "type(C_PTR)",
Chris@19 37 "long double *" => "type(C_PTR)",
Chris@19 38 "float128__ *" => "type(C_PTR)",
Chris@19 39 "fftw_complex *" => "type(C_PTR)",
Chris@19 40 "fftwf_complex *" => "type(C_PTR)",
Chris@19 41 "fftwl_complex *" => "type(C_PTR)",
Chris@19 42 "fftwq_complex *" => "type(C_PTR)",
Chris@19 43 );
Chris@19 44
Chris@19 45 # C->Fortran map of supported argument types
Chris@19 46 %arg_types = (
Chris@19 47 "int" => "integer(C_INT), value",
Chris@19 48 "unsigned" => "integer(C_INT), value",
Chris@19 49 "size_t" => "integer(C_SIZE_T), value",
Chris@19 50 "ptrdiff_t" => "integer(C_INTPTR_T), value",
Chris@19 51
Chris@19 52 "fftw_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
Chris@19 53 "fftwf_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
Chris@19 54 "fftwl_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
Chris@19 55 "fftwq_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
Chris@19 56
Chris@19 57 "double" => "real(C_DOUBLE), value",
Chris@19 58 "float" => "real(C_FLOAT), value",
Chris@19 59 "long double" => "real(C_LONG_DOUBLE), value",
Chris@19 60 "__float128" => "real(16), value",
Chris@19 61
Chris@19 62 "fftw_complex" => "complex(C_DOUBLE_COMPLEX), value",
Chris@19 63 "fftwf_complex" => "complex(C_DOUBLE_COMPLEX), value",
Chris@19 64 "fftwl_complex" => "complex(C_LONG_DOUBLE), value",
Chris@19 65 "fftwq_complex" => "complex(16), value",
Chris@19 66
Chris@19 67 "fftw_plan" => "type(C_PTR), value",
Chris@19 68 "fftwf_plan" => "type(C_PTR), value",
Chris@19 69 "fftwl_plan" => "type(C_PTR), value",
Chris@19 70 "fftwq_plan" => "type(C_PTR), value",
Chris@19 71 "const fftw_plan" => "type(C_PTR), value",
Chris@19 72 "const fftwf_plan" => "type(C_PTR), value",
Chris@19 73 "const fftwl_plan" => "type(C_PTR), value",
Chris@19 74 "const fftwq_plan" => "type(C_PTR), value",
Chris@19 75
Chris@19 76 "const int *" => "integer(C_INT), dimension(*), intent(in)",
Chris@19 77 "ptrdiff_t *" => "integer(C_INTPTR_T), intent(out)",
Chris@19 78 "const ptrdiff_t *" => "integer(C_INTPTR_T), dimension(*), intent(in)",
Chris@19 79
Chris@19 80 "const fftw_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
Chris@19 81 "const fftwf_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
Chris@19 82 "const fftwl_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
Chris@19 83 "const fftwq_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
Chris@19 84
Chris@19 85 "double *" => "real(C_DOUBLE), dimension(*), intent(out)",
Chris@19 86 "float *" => "real(C_FLOAT), dimension(*), intent(out)",
Chris@19 87 "long double *" => "real(C_LONG_DOUBLE), dimension(*), intent(out)",
Chris@19 88 "__float128 *" => "real(16), dimension(*), intent(out)",
Chris@19 89
Chris@19 90 "fftw_complex *" => "complex(C_DOUBLE_COMPLEX), dimension(*), intent(out)",
Chris@19 91 "fftwf_complex *" => "complex(C_FLOAT_COMPLEX), dimension(*), intent(out)",
Chris@19 92 "fftwl_complex *" => "complex(C_LONG_DOUBLE_COMPLEX), dimension(*), intent(out)",
Chris@19 93 "fftwq_complex *" => "complex(16), dimension(*), intent(out)",
Chris@19 94
Chris@19 95 "const fftw_iodim *" => "type(fftw_iodim), dimension(*), intent(in)",
Chris@19 96 "const fftwf_iodim *" => "type(fftwf_iodim), dimension(*), intent(in)",
Chris@19 97 "const fftwl_iodim *" => "type(fftwl_iodim), dimension(*), intent(in)",
Chris@19 98 "const fftwq_iodim *" => "type(fftwq_iodim), dimension(*), intent(in)",
Chris@19 99
Chris@19 100 "const fftw_iodim64 *" => "type(fftw_iodim64), dimension(*), intent(in)",
Chris@19 101 "const fftwf_iodim64 *" => "type(fftwf_iodim64), dimension(*), intent(in)",
Chris@19 102 "const fftwl_iodim64 *" => "type(fftwl_iodim64), dimension(*), intent(in)",
Chris@19 103 "const fftwq_iodim64 *" => "type(fftwq_iodim64), dimension(*), intent(in)",
Chris@19 104
Chris@19 105 "void *" => "type(C_PTR), value",
Chris@19 106 "FILE *" => "type(C_PTR), value",
Chris@19 107
Chris@19 108 "const char *" => "character(C_CHAR), dimension(*), intent(in)",
Chris@19 109
Chris@19 110 "fftw_write_char_func" => "type(C_FUNPTR), value",
Chris@19 111 "fftwf_write_char_func" => "type(C_FUNPTR), value",
Chris@19 112 "fftwl_write_char_func" => "type(C_FUNPTR), value",
Chris@19 113 "fftwq_write_char_func" => "type(C_FUNPTR), value",
Chris@19 114 "fftw_read_char_func" => "type(C_FUNPTR), value",
Chris@19 115 "fftwf_read_char_func" => "type(C_FUNPTR), value",
Chris@19 116 "fftwl_read_char_func" => "type(C_FUNPTR), value",
Chris@19 117 "fftwq_read_char_func" => "type(C_FUNPTR), value",
Chris@19 118
Chris@19 119 # Although the MPI standard defines this type as simply "integer",
Chris@19 120 # if we use integer without a 'C_' kind in a bind(C) interface then
Chris@19 121 # gfortran complains. Instead, since MPI also requires the C type
Chris@19 122 # MPI_Fint to match Fortran integers, we use the size of this type
Chris@19 123 # (extracted by configure and substituted by the Makefile).
Chris@19 124 "MPI_Comm" => "integer(C_MPI_FINT), value"
Chris@19 125 );
Chris@19 126
Chris@19 127 while (<>) {
Chris@19 128 next if /^ *$/;
Chris@19 129 if (/^ *extern +([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *\((.*)\) *$/) {
Chris@19 130 $ret = &canonicalize_type($1);
Chris@19 131 $name = $2;
Chris@19 132
Chris@19 133 $args = $3;
Chris@19 134 $args =~ s/^ *void *$//;
Chris@19 135
Chris@19 136 $bad = ($ret ne "void") && !exists($return_types{$ret});
Chris@19 137 foreach $arg (split(/ *, */, $args)) {
Chris@19 138 $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/;
Chris@19 139 $argtype = &canonicalize_type($1);
Chris@19 140 $bad = 1 if !exists($arg_types{$argtype});
Chris@19 141 }
Chris@19 142 if ($bad) {
Chris@19 143 print "! Unable to generate Fortran interface for $name\n";
Chris@19 144 next;
Chris@19 145 }
Chris@19 146
Chris@19 147 # any function taking an MPI_Comm arg needs a C wrapper (grr).
Chris@19 148 if ($args =~ /MPI_Comm/) {
Chris@19 149 $cname = $name . "_f03";
Chris@19 150 }
Chris@19 151 else {
Chris@19 152 $cname = $name;
Chris@19 153 }
Chris@19 154
Chris@19 155 # Fortran has a 132-character line-length limit by default (grr)
Chris@19 156 $len = 0;
Chris@19 157
Chris@19 158 print " "; $len = $len + length(" ");
Chris@19 159 if ($ret eq "void") {
Chris@19 160 $kind = "subroutine"
Chris@19 161 }
Chris@19 162 else {
Chris@19 163 print "$return_types{$ret} ";
Chris@19 164 $len = $len + length("$return_types{$ret} ");
Chris@19 165 $kind = "function"
Chris@19 166 }
Chris@19 167 print "$kind $name("; $len = $len + length("$kind $name(");
Chris@19 168 $len0 = $len;
Chris@19 169
Chris@19 170 $argnames = $args;
Chris@19 171 $argnames =~ s/([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) */$2/g;
Chris@19 172 $comma = "";
Chris@19 173 foreach $argname (split(/ *, */, $argnames)) {
Chris@19 174 if ($len + length("$comma$argname") + 3 > 132) {
Chris@19 175 printf ", &\n%*s", $len0, "";
Chris@19 176 $len = $len0;
Chris@19 177 $comma = "";
Chris@19 178 }
Chris@19 179 print "$comma$argname";
Chris@19 180 $len = $len + length("$comma$argname");
Chris@19 181 $comma = ",";
Chris@19 182 }
Chris@19 183 print ") "; $len = $len + 2;
Chris@19 184
Chris@19 185 if ($len + length("bind(C, name='$cname')") > 132) {
Chris@19 186 printf "&\n%*s", $len0 - length("$name("), "";
Chris@19 187 }
Chris@19 188 print "bind(C, name='$cname')\n";
Chris@19 189
Chris@19 190 print " import\n";
Chris@19 191 foreach $arg (split(/ *, */, $args)) {
Chris@19 192 $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/;
Chris@19 193 $argtype = &canonicalize_type($1);
Chris@19 194 $argname = $2;
Chris@19 195 $ftype = $arg_types{$argtype};
Chris@19 196
Chris@19 197 # Various special cases for argument types:
Chris@19 198 if ($name =~ /_flops$/ && $argtype eq "double *") {
Chris@19 199 $ftype = "real(C_DOUBLE), intent(out)"
Chris@19 200 }
Chris@19 201 if ($name =~ /_execute/ && ($argname eq "ri" ||
Chris@19 202 $argname eq "ii" ||
Chris@19 203 $argname eq "in")) {
Chris@19 204 $ftype =~ s/intent\(out\)/intent(inout)/;
Chris@19 205 }
Chris@19 206
Chris@19 207 print " $ftype :: $argname\n"
Chris@19 208 }
Chris@19 209
Chris@19 210 print " end $kind $name\n";
Chris@19 211 print " \n";
Chris@19 212 }
Chris@19 213 }