cannam@95: (* cannam@95: * Copyright (c) 1997-1999 Massachusetts Institute of Technology cannam@95: * Copyright (c) 2003, 2007-11 Matteo Frigo cannam@95: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: *) cannam@95: cannam@95: open Util cannam@95: open Genutil cannam@95: open C cannam@95: cannam@95: cannam@95: type ditdif = DIT | DIF cannam@95: let ditdif = ref DIT cannam@95: let usage = "Usage: " ^ Sys.argv.(0) ^ " -n [ -dit | -dif ]" cannam@95: cannam@95: let urs = ref Stride_variable cannam@95: let ums = ref Stride_variable cannam@95: cannam@95: let speclist = [ cannam@95: "-dit", cannam@95: Arg.Unit(fun () -> ditdif := DIT), cannam@95: " generate a DIT codelet"; cannam@95: cannam@95: "-dif", cannam@95: Arg.Unit(fun () -> ditdif := DIF), cannam@95: " generate a DIF codelet"; cannam@95: cannam@95: "-with-rs", cannam@95: Arg.String(fun x -> urs := arg_to_stride x), cannam@95: " specialize for given R-stride"; cannam@95: cannam@95: "-with-ms", cannam@95: Arg.String(fun x -> ums := arg_to_stride x), cannam@95: " specialize for given ms" cannam@95: ] cannam@95: cannam@95: let byi = Complex.times Complex.i cannam@95: let byui = Complex.times (Complex.uminus Complex.i) cannam@95: cannam@95: let shuffle_eo fe fo i = if i mod 2 == 0 then fe (i/2) else fo ((i-1)/2) cannam@95: cannam@95: let generate n = cannam@95: let rs = "rs" cannam@95: and twarray = "W" cannam@95: and m = "m" and mb = "mb" and me = "me" and ms = "ms" cannam@95: cannam@95: (* the array names are from the point of view of the complex array cannam@95: (output in R2C, input in C2R) *) cannam@95: and arp = "Rp" (* real, positive *) cannam@95: and aip = "Ip" (* imag, positive *) cannam@95: and arm = "Rm" (* real, negative *) cannam@95: and aim = "Im" (* imag, negative *) cannam@95: cannam@95: in cannam@95: cannam@95: let sign = !Genutil.sign cannam@95: and name = !Magic.codelet_name cannam@95: and byvl x = choose_simd x (ctimes (CVar "VL", x)) in cannam@95: cannam@95: let (bytwiddle, num_twiddles, twdesc) = Twiddle.twiddle_policy 1 false in cannam@95: let nt = num_twiddles n in cannam@95: cannam@95: let byw = bytwiddle n sign (twiddle_array nt twarray) in cannam@95: cannam@95: let vrs = either_stride (!urs) (C.SVar rs) in cannam@95: cannam@95: (* assume a single location. No point in doing alias analysis *) cannam@95: let the_location = (Unique.make (), Unique.make ()) in cannam@95: let locations _ = the_location in cannam@95: cannam@95: let rlocp = (locative_array_c n cannam@95: (C.array_subscript arp vrs) cannam@95: (C.array_subscript aip vrs) cannam@95: locations "BUG") cannam@95: and rlocm = (locative_array_c n cannam@95: (C.array_subscript arm vrs) cannam@95: (C.array_subscript aim vrs) cannam@95: locations "BUG") cannam@95: and clocp = (locative_array_c n cannam@95: (C.array_subscript arp vrs) cannam@95: (C.array_subscript aip vrs) cannam@95: locations "BUG") cannam@95: and clocm = (locative_array_c n cannam@95: (C.array_subscript arm vrs) cannam@95: (C.array_subscript aim vrs) cannam@95: locations "BUG") cannam@95: in cannam@95: let rloc i = if i mod 2 == 0 then rlocp (i/2) else rlocm ((i-1)/2) cannam@95: and cloc i = if i < n - i then clocp i else clocm (n-1-i) cannam@95: and sym n f i = if (i < n - i) then f i else Complex.conj (f i) cannam@95: and sym1 f i = cannam@95: if i mod 2 == 0 then cannam@95: Complex.plus [f i; Complex.conj (f (i+1))] cannam@95: else cannam@95: Complex.times (Complex.uminus Complex.i) cannam@95: (Complex.plus [f (i-1); Complex.uminus (Complex.conj (f i))]) cannam@95: and sym1i f i = cannam@95: if i mod 2 == 0 then cannam@95: Complex.plus [f i; Complex.times Complex.i (f (i+1))] cannam@95: else cannam@95: Complex.conj cannam@95: (Complex.plus [f (i-1); cannam@95: Complex.times (Complex.uminus Complex.i) (f i)]) cannam@95: in cannam@95: cannam@95: let asch = cannam@95: match !ditdif with cannam@95: | DIT -> cannam@95: let output = cannam@95: (Complex.times Complex.half) @@ cannam@95: (Fft.dft sign n (byw (sym1 (load_array_c n rloc)))) in cannam@95: let odag = store_array_c n cloc (sym n output) in cannam@95: standard_optimizer odag cannam@95: cannam@95: | DIF -> cannam@95: let output = cannam@95: byw (Fft.dft sign n (sym n (load_array_c n cloc))) cannam@95: in cannam@95: let odag = store_array_c n rloc (sym1i output) in cannam@95: standard_optimizer odag cannam@95: in cannam@95: cannam@95: let vms = CVar "ms" cannam@95: and varp = CVar arp cannam@95: and vaip = CVar aip cannam@95: and varm = CVar arm cannam@95: and vaim = CVar aim cannam@95: and vm = CVar m and vmb = CVar mb and vme = CVar me cannam@95: in cannam@95: let body = Block ( cannam@95: [Decl ("INT", m)], cannam@95: [For (list_to_comma cannam@95: [Expr_assign (vm, vmb); cannam@95: Expr_assign (CVar twarray, cannam@95: CPlus [CVar twarray; cannam@95: ctimes (CPlus [vmb; CUminus (Integer 1)], cannam@95: Integer nt)])], cannam@95: Binop (" < ", vm, vme), cannam@95: list_to_comma cannam@95: [Expr_assign (vm, CPlus [vm; byvl (Integer 1)]); cannam@95: Expr_assign (varp, CPlus [varp; byvl vms]); cannam@95: Expr_assign (vaip, CPlus [vaip; byvl vms]); cannam@95: Expr_assign (varm, CPlus [varm; CUminus (byvl vms)]); cannam@95: Expr_assign (vaim, CPlus [vaim; CUminus (byvl vms)]); cannam@95: Expr_assign (CVar twarray, CPlus [CVar twarray; cannam@95: byvl (Integer nt)]); cannam@95: make_volatile_stride (4*n) (CVar rs) cannam@95: ], cannam@95: Asch asch)] cannam@95: ) cannam@95: in cannam@95: cannam@95: let tree = cannam@95: Fcn ("static void", name, cannam@95: [Decl (C.realtypep, arp); cannam@95: Decl (C.realtypep, aip); cannam@95: Decl (C.realtypep, arm); cannam@95: Decl (C.realtypep, aim); cannam@95: Decl (C.constrealtypep, twarray); cannam@95: Decl (C.stridetype, rs); cannam@95: Decl ("INT", mb); cannam@95: Decl ("INT", me); cannam@95: Decl ("INT", ms)], cannam@95: finalize_fcn body) cannam@95: in cannam@95: let twinstr = cannam@95: Printf.sprintf "static const tw_instr twinstr[] = %s;\n\n" cannam@95: (twinstr_to_string "VL" (twdesc n)) cannam@95: and desc = cannam@95: Printf.sprintf cannam@95: "static const hc2c_desc desc = {%d, \"%s\", twinstr, &GENUS, %s};\n\n" cannam@95: n name (flops_of tree) cannam@95: and register = "X(khc2c_register)" cannam@95: cannam@95: in cannam@95: let init = cannam@95: "\n" ^ cannam@95: twinstr ^ cannam@95: desc ^ cannam@95: (declare_register_fcn name) ^ cannam@95: (Printf.sprintf "{\n%s(p, %s, &desc, HC2C_VIA_DFT);\n}" register name) cannam@95: in cannam@95: cannam@95: (unparse tree) ^ "\n" ^ init cannam@95: cannam@95: cannam@95: let main () = cannam@95: begin cannam@95: parse (speclist @ Twiddle.speclist) usage; cannam@95: print_string (generate (check_size ())); cannam@95: end cannam@95: cannam@95: let _ = main()