annotate fft/fftw/fftw-3.3.4/genfft/simd.ml @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 (*
Chris@19 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@19 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 5 *
Chris@19 6 * This program is free software; you can redistribute it and/or modify
Chris@19 7 * it under the terms of the GNU General Public License as published by
Chris@19 8 * the Free Software Foundation; either version 2 of the License, or
Chris@19 9 * (at your option) any later version.
Chris@19 10 *
Chris@19 11 * This program is distributed in the hope that it will be useful,
Chris@19 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 14 * GNU General Public License for more details.
Chris@19 15 *
Chris@19 16 * You should have received a copy of the GNU General Public License
Chris@19 17 * along with this program; if not, write to the Free Software
Chris@19 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 19 *
Chris@19 20 *)
Chris@19 21
Chris@19 22 open Expr
Chris@19 23 open List
Chris@19 24 open Printf
Chris@19 25 open Variable
Chris@19 26 open Annotate
Chris@19 27 open Simdmagic
Chris@19 28 open C
Chris@19 29
Chris@19 30 let realtype = "V"
Chris@19 31 let realtypep = realtype ^ " *"
Chris@19 32 let constrealtype = "const " ^ realtype
Chris@19 33 let constrealtypep = constrealtype ^ " *"
Chris@19 34 let alignment_mod = 2
Chris@19 35
Chris@19 36 (*
Chris@19 37 * SIMD C AST unparser
Chris@19 38 *)
Chris@19 39 let foldr_string_concat l = fold_right (^) l ""
Chris@19 40
Chris@19 41 let rec unparse_by_twiddle nam tw src =
Chris@19 42 sprintf "%s(&(%s),%s)" nam (Variable.unparse tw) (unparse_expr src)
Chris@19 43
Chris@19 44 and unparse_store dst = function
Chris@19 45 | Times (NaN MULTI_A, x) ->
Chris@19 46 sprintf "STM%d(&(%s),%s,%s,&(%s));\n"
Chris@19 47 !Simdmagic.store_multiple
Chris@19 48 (Variable.unparse dst) (unparse_expr x)
Chris@19 49 (Variable.vstride_of_locative dst)
Chris@19 50 (Variable.unparse_for_alignment alignment_mod dst)
Chris@19 51 | Times (NaN MULTI_B, Plus stuff) ->
Chris@19 52 sprintf "STN%d(&(%s)%s,%s);\n"
Chris@19 53 !Simdmagic.store_multiple
Chris@19 54 (Variable.unparse dst)
Chris@19 55 (List.fold_right (fun x a -> "," ^ (unparse_expr x) ^ a) stuff "")
Chris@19 56 (Variable.vstride_of_locative dst)
Chris@19 57 | src_expr ->
Chris@19 58 sprintf "ST(&(%s),%s,%s,&(%s));\n"
Chris@19 59 (Variable.unparse dst) (unparse_expr src_expr)
Chris@19 60 (Variable.vstride_of_locative dst)
Chris@19 61 (Variable.unparse_for_alignment alignment_mod dst)
Chris@19 62
Chris@19 63 and unparse_expr =
Chris@19 64 let rec unparse_plus = function
Chris@19 65 | [a] -> unparse_expr a
Chris@19 66
Chris@19 67 | (Uminus (Times (NaN I, b))) :: c :: d -> op2 "VFNMSI" [b] (c :: d)
Chris@19 68 | c :: (Uminus (Times (NaN I, b))) :: d -> op2 "VFNMSI" [b] (c :: d)
Chris@19 69 | (Uminus (Times (NaN CONJ, b))) :: c :: d -> op2 "VFNMSCONJ" [b] (c :: d)
Chris@19 70 | c :: (Uminus (Times (NaN CONJ, b))) :: d -> op2 "VFNMSCONJ" [b] (c :: d)
Chris@19 71 | (Times (NaN I, b)) :: c :: d -> op2 "VFMAI" [b] (c :: d)
Chris@19 72 | c :: (Times (NaN I, b)) :: d -> op2 "VFMAI" [b] (c :: d)
Chris@19 73 | (Times (NaN CONJ, b)) :: (Uminus c) :: d -> op2 "VFMSCONJ" [b] (c :: d)
Chris@19 74 | (Uminus c) :: (Times (NaN CONJ, b)) :: d -> op2 "VFMSCONJ" [b] (c :: d)
Chris@19 75 | (Times (NaN CONJ, b)) :: c :: d -> op2 "VFMACONJ" [b] (c :: d)
Chris@19 76 | c :: (Times (NaN CONJ, b)) :: d -> op2 "VFMACONJ" [b] (c :: d)
Chris@19 77 | (Times (NaN _, b)) :: (Uminus c) :: d -> failwith "VFMS NaN"
Chris@19 78 | (Uminus c) :: (Times (NaN _, b)) :: d -> failwith "VFMS NaN"
Chris@19 79
Chris@19 80 | (Uminus (Times (a, b))) :: c :: d -> op3 "VFNMS" a b (c :: d)
Chris@19 81 | c :: (Uminus (Times (a, b))) :: d -> op3 "VFNMS" a b (c :: d)
Chris@19 82 | (Times (a, b)) :: (Uminus c) :: d -> op3 "VFMS" a b (c :: negate d)
Chris@19 83 | (Uminus c) :: (Times (a, b)) :: d -> op3 "VFMS" a b (c :: negate d)
Chris@19 84 | (Times (a, b)) :: c :: d -> op3 "VFMA" a b (c :: d)
Chris@19 85 | c :: (Times (a, b)) :: d -> op3 "VFMA" a b (c :: d)
Chris@19 86
Chris@19 87 | (Uminus a :: b) -> op2 "VSUB" b [a]
Chris@19 88 | (b :: Uminus a :: c) -> op2 "VSUB" (b :: c) [a]
Chris@19 89 | (a :: b) -> op2 "VADD" [a] b
Chris@19 90 | [] -> failwith "unparse_plus"
Chris@19 91 and op3 nam a b c =
Chris@19 92 nam ^ "(" ^ (unparse_expr a) ^ ", " ^ (unparse_expr b) ^ ", " ^
Chris@19 93 (unparse_plus c) ^ ")"
Chris@19 94 and op2 nam a b =
Chris@19 95 nam ^ "(" ^ (unparse_plus a) ^ ", " ^ (unparse_plus b) ^ ")"
Chris@19 96 and op1 nam a =
Chris@19 97 nam ^ "(" ^ (unparse_expr a) ^ ")"
Chris@19 98 and negate = function
Chris@19 99 | [] -> []
Chris@19 100 | (Uminus x) :: y -> x :: negate y
Chris@19 101 | x :: y -> (Uminus x) :: negate y
Chris@19 102
Chris@19 103 in function
Chris@19 104 | CTimes(Load tw, src)
Chris@19 105 when Variable.is_constant tw && !Magic.generate_bytw ->
Chris@19 106 unparse_by_twiddle "BYTW" tw src
Chris@19 107 | CTimesJ(Load tw, src)
Chris@19 108 when Variable.is_constant tw && !Magic.generate_bytw ->
Chris@19 109 unparse_by_twiddle "BYTWJ" tw src
Chris@19 110 | Load v when is_locative(v) ->
Chris@19 111 sprintf "LD(&(%s), %s, &(%s))" (Variable.unparse v)
Chris@19 112 (Variable.vstride_of_locative v)
Chris@19 113 (Variable.unparse_for_alignment alignment_mod v)
Chris@19 114 | Load v when is_constant(v) -> sprintf "LDW(&(%s))" (Variable.unparse v)
Chris@19 115 | Load v -> Variable.unparse v
Chris@19 116 | Num n -> sprintf "LDK(%s)" (Number.to_konst n)
Chris@19 117 | NaN n -> failwith "NaN in unparse_expr"
Chris@19 118 | Plus [] -> "0.0 /* bug */"
Chris@19 119 | Plus [a] -> " /* bug */ " ^ (unparse_expr a)
Chris@19 120 | Plus a -> unparse_plus a
Chris@19 121 | Times(NaN I,b) -> op1 "VBYI" b
Chris@19 122 | Times(NaN CONJ,b) -> op1 "VCONJ" b
Chris@19 123 | Times(a,b) ->
Chris@19 124 sprintf "VMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@19 125 | CTimes(a,Times(NaN I, b)) ->
Chris@19 126 sprintf "VZMULI(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@19 127 | CTimes(a,b) ->
Chris@19 128 sprintf "VZMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@19 129 | CTimesJ(a,Times(NaN I, b)) ->
Chris@19 130 sprintf "VZMULIJ(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@19 131 | CTimesJ(a,b) ->
Chris@19 132 sprintf "VZMULJ(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@19 133 | Uminus a when !Magic.vneg -> op1 "VNEG" a
Chris@19 134 | Uminus a -> failwith "SIMD Uminus"
Chris@19 135 | _ -> failwith "unparse_expr"
Chris@19 136
Chris@19 137 and unparse_decl x = C.unparse_decl x
Chris@19 138
Chris@19 139 and unparse_ast ast =
Chris@19 140 let rec unparse_assignment = function
Chris@19 141 | Assign (v, x) when Variable.is_locative v ->
Chris@19 142 unparse_store v x
Chris@19 143 | Assign (v, x) ->
Chris@19 144 (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n"
Chris@19 145
Chris@19 146 and unparse_annotated force_bracket =
Chris@19 147 let rec unparse_code = function
Chris@19 148 | ADone -> ""
Chris@19 149 | AInstr i -> unparse_assignment i
Chris@19 150 | ASeq (a, b) ->
Chris@19 151 (unparse_annotated false a) ^ (unparse_annotated false b)
Chris@19 152 and declare_variables l =
Chris@19 153 let rec uvar = function
Chris@19 154 [] -> failwith "uvar"
Chris@19 155 | [v] -> (Variable.unparse v) ^ ";\n"
Chris@19 156 | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b)
Chris@19 157 in let rec vvar l =
Chris@19 158 let s = if !Magic.compact then 15 else 1 in
Chris@19 159 if (List.length l <= s) then
Chris@19 160 match l with
Chris@19 161 [] -> ""
Chris@19 162 | _ -> realtype ^ " " ^ (uvar l)
Chris@19 163 else
Chris@19 164 (vvar (Util.take s l)) ^ (vvar (Util.drop s l))
Chris@19 165 in vvar (List.filter Variable.is_temporary l)
Chris@19 166 in function
Chris@19 167 Annotate (_, _, decl, _, code) ->
Chris@19 168 if (not force_bracket) && (Util.null decl) then
Chris@19 169 unparse_code code
Chris@19 170 else "{\n" ^
Chris@19 171 (declare_variables decl) ^
Chris@19 172 (unparse_code code) ^
Chris@19 173 "}\n"
Chris@19 174
Chris@19 175 (* ---- *)
Chris@19 176 and unparse_plus = function
Chris@19 177 | [] -> ""
Chris@19 178 | (CUminus a :: b) -> " - " ^ (parenthesize a) ^ (unparse_plus b)
Chris@19 179 | (a :: b) -> " + " ^ (parenthesize a) ^ (unparse_plus b)
Chris@19 180 and parenthesize x = match x with
Chris@19 181 | (CVar _) -> unparse_ast x
Chris@19 182 | (CCall _) -> unparse_ast x
Chris@19 183 | (Integer _) -> unparse_ast x
Chris@19 184 | _ -> "(" ^ (unparse_ast x) ^ ")"
Chris@19 185
Chris@19 186 in match ast with
Chris@19 187 | Asch a -> (unparse_annotated true a)
Chris@19 188 | Return x -> "return " ^ unparse_ast x ^ ";"
Chris@19 189 | Simd_leavefun -> "VLEAVE();"
Chris@19 190 | For (a, b, c, d) ->
Chris@19 191 "for (" ^
Chris@19 192 unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c
Chris@19 193 ^ ")" ^ unparse_ast d
Chris@19 194 | If (a, d) ->
Chris@19 195 "if (" ^
Chris@19 196 unparse_ast a
Chris@19 197 ^ ")" ^ unparse_ast d
Chris@19 198 | Block (d, s) ->
Chris@19 199 if (s == []) then ""
Chris@19 200 else
Chris@19 201 "{\n" ^
Chris@19 202 foldr_string_concat (map unparse_decl d) ^
Chris@19 203 foldr_string_concat (map unparse_ast s) ^
Chris@19 204 "}\n"
Chris@19 205 | x -> C.unparse_ast x
Chris@19 206
Chris@19 207 and unparse_function = function
Chris@19 208 Fcn (typ, name, args, body) ->
Chris@19 209 let rec unparse_args = function
Chris@19 210 [Decl (a, b)] -> a ^ " " ^ b
Chris@19 211 | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", "
Chris@19 212 ^ unparse_args s
Chris@19 213 | [] -> ""
Chris@19 214 | _ -> failwith "unparse_function"
Chris@19 215 in
Chris@19 216 (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^
Chris@19 217 unparse_ast body)
Chris@19 218
Chris@19 219 let extract_constants f =
Chris@19 220 let constlist = flatten (map expr_to_constants (C.ast_to_expr_list f))
Chris@19 221 in map
Chris@19 222 (fun n ->
Chris@19 223 Tdecl
Chris@19 224 ("DVK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^
Chris@19 225 ");\n"))
Chris@19 226 (unique_constants constlist)