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