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