1(*
2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
3 * Copyright (c) 2003, 2007-14 Matteo Frigo
4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 *
20 *)
21
22open Util
23open Genutil
24open C
25
26
27type ditdif = DIT | DIF
28let ditdif = ref DIT
29let usage = "Usage: " ^ Sys.argv.(0) ^ " -n <number> [ -dit | -dif ]"
30
31let urs = ref Stride_variable
32
33let speclist = [
34  "-dit",
35  Arg.Unit(fun () -> ditdif := DIT),
36  " generate a DIT codelet";
37
38  "-dif",
39  Arg.Unit(fun () -> ditdif := DIF),
40  " generate a DIF codelet";
41
42  "-with-rs",
43  Arg.String(fun x -> urs := arg_to_stride x),
44  " specialize for given R-stride";
45]
46
47let byi = Complex.times Complex.i
48let byui = Complex.times (Complex.uminus Complex.i)
49
50let sym n f i = if (i < n - i) then f i else Complex.conj (f i)
51
52let shuffle_eo fe fo i = if i mod 2 == 0 then fe (i/2) else fo ((i-1)/2)
53
54let generate n =
55  let rs = "rs"
56  and twarray = "W"
57  and m = "m" and mb = "mb" and me = "me" and ms = "ms"
58
59  (* the array names are from the point of view of the complex array
60     (output in R2C, input in C2R) *)
61  and arp = "Rp" (* real, positive *)
62  and aip = "Ip" (* imag, positive *)
63  and arm = "Rm" (* real, negative *)
64  and aim = "Im" (* imag, negative *)
65
66  in
67
68  let sign = !Genutil.sign
69  and name = !Magic.codelet_name
70  and byvl x = choose_simd x (ctimes (CVar "VL", x)) in
71
72  let (bytwiddle, num_twiddles, twdesc) = Twiddle.twiddle_policy 1 false in
73  let nt = num_twiddles n in
74
75  let byw = bytwiddle n sign (twiddle_array nt twarray) in
76
77  let vrs = either_stride (!urs) (C.SVar rs) in
78
79  (* assume a single location.  No point in doing alias analysis *)
80  let the_location = (Unique.make (), Unique.make ()) in
81  let locations _ = the_location in
82
83  let locr = (locative_array_c n
84		(C.array_subscript arp vrs)
85		(C.array_subscript arm vrs)
86		locations "BUG")
87  and loci = (locative_array_c n
88		(C.array_subscript aip vrs)
89		(C.array_subscript aim vrs)
90		locations "BUG")
91  and locp = (locative_array_c n
92		(C.array_subscript arp vrs)
93		(C.array_subscript aip vrs)
94		locations "BUG")
95  and locm = (locative_array_c n
96		(C.array_subscript arm vrs)
97		(C.array_subscript aim vrs)
98		locations "BUG")
99  in
100  let locri i = if i mod 2 == 0 then locr (i/2) else loci ((i-1)/2)
101  and locpm i = if i < n - i then locp i else locm (n-1-i)
102  in
103
104  let asch =
105    match !ditdif with
106    | DIT ->
107	let output = Fft.dft sign n (byw (load_array_c n locri)) in
108	let odag = store_array_c n locpm (sym n output) in
109	  standard_optimizer odag
110
111    | DIF ->
112	let output = byw (Fft.dft sign n (sym n (load_array_c n locpm))) in
113	let odag = store_array_c n locri output in
114	  standard_optimizer odag
115  in
116
117  let vms = CVar "ms"
118  and varp = CVar arp
119  and vaip = CVar aip
120  and varm = CVar arm
121  and vaim = CVar aim
122  and vm = CVar m and vmb = CVar mb and vme = CVar me
123  in
124  let body = Block (
125    [Decl ("INT", m)],
126    [For (list_to_comma
127	    [Expr_assign (vm, vmb);
128	     Expr_assign (CVar twarray,
129			  CPlus [CVar twarray;
130				 ctimes (CPlus [vmb; CUminus (Integer 1)],
131					 Integer nt)])],
132	  Binop (" < ", vm, vme),
133	  list_to_comma
134	    [Expr_assign (vm, CPlus [vm; byvl (Integer 1)]);
135	     Expr_assign (varp, CPlus [varp; byvl vms]);
136	     Expr_assign (vaip, CPlus [vaip; byvl vms]);
137	     Expr_assign (varm, CPlus [varm; CUminus (byvl vms)]);
138	     Expr_assign (vaim, CPlus [vaim; CUminus (byvl vms)]);
139	     Expr_assign (CVar twarray, CPlus [CVar twarray;
140					       byvl (Integer nt)]);
141	     make_volatile_stride (4*n) (CVar rs)
142	   ],
143	  Asch asch)])
144  in
145
146  let tree =
147    Fcn ("static void", name,
148	 [Decl (C.realtypep, arp);
149	  Decl (C.realtypep, aip);
150	  Decl (C.realtypep, arm);
151	  Decl (C.realtypep, aim);
152	  Decl (C.constrealtypep, twarray);
153	  Decl (C.stridetype, rs);
154	  Decl ("INT", mb);
155	  Decl ("INT", me);
156	  Decl ("INT", ms)],
157         finalize_fcn body)
158  in
159  let twinstr =
160    Printf.sprintf "static const tw_instr twinstr[] = %s;\n\n"
161      (twinstr_to_string "VL" (twdesc n))
162  and desc =
163    Printf.sprintf
164      "static const hc2c_desc desc = {%d, \"%s\", twinstr, &GENUS, %s};\n\n"
165      n name (flops_of tree)
166  and register = "X(khc2c_register)"
167
168  in
169  let init =
170    "\n" ^
171    twinstr ^
172    desc ^
173    (declare_register_fcn name) ^
174    (Printf.sprintf "{\n%s(p, %s, &desc, HC2C_VIA_RDFT);\n}" register name)
175  in
176
177  (unparse tree) ^ "\n" ^ init
178
179
180let main () =
181  begin
182    parse (speclist @ Twiddle.speclist) usage;
183    print_string (generate (check_size ()));
184  end
185
186let _ = main()
187