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