annotate src/fftw-3.3.3/genfft/simd.ml @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 37bf6b4a2645
children
rev   line source
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 Expr
Chris@10 23 open List
Chris@10 24 open Printf
Chris@10 25 open Variable
Chris@10 26 open Annotate
Chris@10 27 open Simdmagic
Chris@10 28 open C
Chris@10 29
Chris@10 30 let realtype = "V"
Chris@10 31 let realtypep = realtype ^ " *"
Chris@10 32 let constrealtype = "const " ^ realtype
Chris@10 33 let constrealtypep = constrealtype ^ " *"
Chris@10 34 let alignment_mod = 2
Chris@10 35
Chris@10 36 (*
Chris@10 37 * SIMD C AST unparser
Chris@10 38 *)
Chris@10 39 let foldr_string_concat l = fold_right (^) l ""
Chris@10 40
Chris@10 41 let rec unparse_by_twiddle nam tw src =
Chris@10 42 sprintf "%s(&(%s),%s)" nam (Variable.unparse tw) (unparse_expr src)
Chris@10 43
Chris@10 44 and unparse_store dst = function
Chris@10 45 | Times (NaN MULTI_A, x) ->
Chris@10 46 sprintf "STM%d(&(%s),%s,%s,&(%s));\n"
Chris@10 47 !Simdmagic.store_multiple
Chris@10 48 (Variable.unparse dst) (unparse_expr x)
Chris@10 49 (Variable.vstride_of_locative dst)
Chris@10 50 (Variable.unparse_for_alignment alignment_mod dst)
Chris@10 51 | Times (NaN MULTI_B, Plus stuff) ->
Chris@10 52 sprintf "STN%d(&(%s)%s,%s);\n"
Chris@10 53 !Simdmagic.store_multiple
Chris@10 54 (Variable.unparse dst)
Chris@10 55 (List.fold_right (fun x a -> "," ^ (unparse_expr x) ^ a) stuff "")
Chris@10 56 (Variable.vstride_of_locative dst)
Chris@10 57 | src_expr ->
Chris@10 58 sprintf "ST(&(%s),%s,%s,&(%s));\n"
Chris@10 59 (Variable.unparse dst) (unparse_expr src_expr)
Chris@10 60 (Variable.vstride_of_locative dst)
Chris@10 61 (Variable.unparse_for_alignment alignment_mod dst)
Chris@10 62
Chris@10 63 and unparse_expr =
Chris@10 64 let rec unparse_plus = function
Chris@10 65 | [a] -> unparse_expr a
Chris@10 66
Chris@10 67 | (Uminus (Times (NaN I, b))) :: c :: d -> op2 "VFNMSI" [b] (c :: d)
Chris@10 68 | c :: (Uminus (Times (NaN I, b))) :: d -> op2 "VFNMSI" [b] (c :: d)
Chris@10 69 | (Uminus (Times (NaN CONJ, b))) :: c :: d -> op2 "VFNMSCONJ" [b] (c :: d)
Chris@10 70 | c :: (Uminus (Times (NaN CONJ, b))) :: d -> op2 "VFNMSCONJ" [b] (c :: d)
Chris@10 71 | (Times (NaN I, b)) :: c :: d -> op2 "VFMAI" [b] (c :: d)
Chris@10 72 | c :: (Times (NaN I, b)) :: d -> op2 "VFMAI" [b] (c :: d)
Chris@10 73 | (Times (NaN CONJ, b)) :: (Uminus c) :: d -> op2 "VFMSCONJ" [b] (c :: d)
Chris@10 74 | (Uminus c) :: (Times (NaN CONJ, b)) :: d -> op2 "VFMSCONJ" [b] (c :: d)
Chris@10 75 | (Times (NaN CONJ, b)) :: c :: d -> op2 "VFMACONJ" [b] (c :: d)
Chris@10 76 | c :: (Times (NaN CONJ, b)) :: d -> op2 "VFMACONJ" [b] (c :: d)
Chris@10 77 | (Times (NaN _, b)) :: (Uminus c) :: d -> failwith "VFMS NaN"
Chris@10 78 | (Uminus c) :: (Times (NaN _, b)) :: d -> failwith "VFMS NaN"
Chris@10 79
Chris@10 80 | (Uminus (Times (a, b))) :: c :: d -> op3 "VFNMS" a b (c :: d)
Chris@10 81 | c :: (Uminus (Times (a, b))) :: d -> op3 "VFNMS" a b (c :: d)
Chris@10 82 | (Times (a, b)) :: (Uminus c) :: d -> op3 "VFMS" a b (c :: negate d)
Chris@10 83 | (Uminus c) :: (Times (a, b)) :: d -> op3 "VFMS" a b (c :: negate d)
Chris@10 84 | (Times (a, b)) :: c :: d -> op3 "VFMA" a b (c :: d)
Chris@10 85 | c :: (Times (a, b)) :: d -> op3 "VFMA" a b (c :: d)
Chris@10 86
Chris@10 87 | (Uminus a :: b) -> op2 "VSUB" b [a]
Chris@10 88 | (b :: Uminus a :: c) -> op2 "VSUB" (b :: c) [a]
Chris@10 89 | (a :: b) -> op2 "VADD" [a] b
Chris@10 90 | [] -> failwith "unparse_plus"
Chris@10 91 and op3 nam a b c =
Chris@10 92 nam ^ "(" ^ (unparse_expr a) ^ ", " ^ (unparse_expr b) ^ ", " ^
Chris@10 93 (unparse_plus c) ^ ")"
Chris@10 94 and op2 nam a b =
Chris@10 95 nam ^ "(" ^ (unparse_plus a) ^ ", " ^ (unparse_plus b) ^ ")"
Chris@10 96 and op1 nam a =
Chris@10 97 nam ^ "(" ^ (unparse_expr a) ^ ")"
Chris@10 98 and negate = function
Chris@10 99 | [] -> []
Chris@10 100 | (Uminus x) :: y -> x :: negate y
Chris@10 101 | x :: y -> (Uminus x) :: negate y
Chris@10 102
Chris@10 103 in function
Chris@10 104 | CTimes(Load tw, src)
Chris@10 105 when Variable.is_constant tw && !Magic.generate_bytw ->
Chris@10 106 unparse_by_twiddle "BYTW" tw src
Chris@10 107 | CTimesJ(Load tw, src)
Chris@10 108 when Variable.is_constant tw && !Magic.generate_bytw ->
Chris@10 109 unparse_by_twiddle "BYTWJ" tw src
Chris@10 110 | Load v when is_locative(v) ->
Chris@10 111 sprintf "LD(&(%s), %s, &(%s))" (Variable.unparse v)
Chris@10 112 (Variable.vstride_of_locative v)
Chris@10 113 (Variable.unparse_for_alignment alignment_mod v)
Chris@10 114 | Load v when is_constant(v) -> sprintf "LDW(&(%s))" (Variable.unparse v)
Chris@10 115 | Load v -> Variable.unparse v
Chris@10 116 | Num n -> sprintf "LDK(%s)" (Number.to_konst n)
Chris@10 117 | NaN n -> failwith "NaN in unparse_expr"
Chris@10 118 | Plus [] -> "0.0 /* bug */"
Chris@10 119 | Plus [a] -> " /* bug */ " ^ (unparse_expr a)
Chris@10 120 | Plus a -> unparse_plus a
Chris@10 121 | Times(NaN I,b) -> op1 "VBYI" b
Chris@10 122 | Times(NaN CONJ,b) -> op1 "VCONJ" b
Chris@10 123 | Times(a,b) ->
Chris@10 124 sprintf "VMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@10 125 | CTimes(a,Times(NaN I, b)) ->
Chris@10 126 sprintf "VZMULI(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@10 127 | CTimes(a,b) ->
Chris@10 128 sprintf "VZMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@10 129 | CTimesJ(a,Times(NaN I, b)) ->
Chris@10 130 sprintf "VZMULIJ(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@10 131 | CTimesJ(a,b) ->
Chris@10 132 sprintf "VZMULJ(%s, %s)" (unparse_expr a) (unparse_expr b)
Chris@10 133 | Uminus a when !Magic.vneg -> op1 "VNEG" a
Chris@10 134 | Uminus a -> failwith "SIMD Uminus"
Chris@10 135 | _ -> failwith "unparse_expr"
Chris@10 136
Chris@10 137 and unparse_decl x = C.unparse_decl x
Chris@10 138
Chris@10 139 and unparse_ast ast =
Chris@10 140 let rec unparse_assignment = function
Chris@10 141 | Assign (v, x) when Variable.is_locative v ->
Chris@10 142 unparse_store v x
Chris@10 143 | Assign (v, x) ->
Chris@10 144 (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n"
Chris@10 145
Chris@10 146 and unparse_annotated force_bracket =
Chris@10 147 let rec unparse_code = function
Chris@10 148 | ADone -> ""
Chris@10 149 | AInstr i -> unparse_assignment i
Chris@10 150 | ASeq (a, b) ->
Chris@10 151 (unparse_annotated false a) ^ (unparse_annotated false b)
Chris@10 152 and declare_variables l =
Chris@10 153 let rec uvar = function
Chris@10 154 [] -> failwith "uvar"
Chris@10 155 | [v] -> (Variable.unparse v) ^ ";\n"
Chris@10 156 | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b)
Chris@10 157 in let rec vvar l =
Chris@10 158 let s = if !Magic.compact then 15 else 1 in
Chris@10 159 if (List.length l <= s) then
Chris@10 160 match l with
Chris@10 161 [] -> ""
Chris@10 162 | _ -> realtype ^ " " ^ (uvar l)
Chris@10 163 else
Chris@10 164 (vvar (Util.take s l)) ^ (vvar (Util.drop s l))
Chris@10 165 in vvar (List.filter Variable.is_temporary l)
Chris@10 166 in function
Chris@10 167 Annotate (_, _, decl, _, code) ->
Chris@10 168 if (not force_bracket) && (Util.null decl) then
Chris@10 169 unparse_code code
Chris@10 170 else "{\n" ^
Chris@10 171 (declare_variables decl) ^
Chris@10 172 (unparse_code code) ^
Chris@10 173 "}\n"
Chris@10 174
Chris@10 175 (* ---- *)
Chris@10 176 and unparse_plus = function
Chris@10 177 | [] -> ""
Chris@10 178 | (CUminus a :: b) -> " - " ^ (parenthesize a) ^ (unparse_plus b)
Chris@10 179 | (a :: b) -> " + " ^ (parenthesize a) ^ (unparse_plus b)
Chris@10 180 and parenthesize x = match x with
Chris@10 181 | (CVar _) -> unparse_ast x
Chris@10 182 | (CCall _) -> unparse_ast x
Chris@10 183 | (Integer _) -> unparse_ast x
Chris@10 184 | _ -> "(" ^ (unparse_ast x) ^ ")"
Chris@10 185
Chris@10 186 in match ast with
Chris@10 187 | Asch a -> (unparse_annotated true a)
Chris@10 188 | Return x -> "return " ^ unparse_ast x ^ ";"
Chris@10 189 | Simd_leavefun -> "VLEAVE();"
Chris@10 190 | For (a, b, c, d) ->
Chris@10 191 "for (" ^
Chris@10 192 unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c
Chris@10 193 ^ ")" ^ unparse_ast d
Chris@10 194 | If (a, d) ->
Chris@10 195 "if (" ^
Chris@10 196 unparse_ast a
Chris@10 197 ^ ")" ^ unparse_ast d
Chris@10 198 | Block (d, s) ->
Chris@10 199 if (s == []) then ""
Chris@10 200 else
Chris@10 201 "{\n" ^
Chris@10 202 foldr_string_concat (map unparse_decl d) ^
Chris@10 203 foldr_string_concat (map unparse_ast s) ^
Chris@10 204 "}\n"
Chris@10 205 | x -> C.unparse_ast x
Chris@10 206
Chris@10 207 and unparse_function = function
Chris@10 208 Fcn (typ, name, args, body) ->
Chris@10 209 let rec unparse_args = function
Chris@10 210 [Decl (a, b)] -> a ^ " " ^ b
Chris@10 211 | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", "
Chris@10 212 ^ unparse_args s
Chris@10 213 | [] -> ""
Chris@10 214 | _ -> failwith "unparse_function"
Chris@10 215 in
Chris@10 216 (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^
Chris@10 217 unparse_ast body)
Chris@10 218
Chris@10 219 let extract_constants f =
Chris@10 220 let constlist = flatten (map expr_to_constants (C.ast_to_expr_list f))
Chris@10 221 in map
Chris@10 222 (fun n ->
Chris@10 223 Tdecl
Chris@10 224 ("DVK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^
Chris@10 225 ");\n"))
Chris@10 226 (unique_constants constlist)