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