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