annotate src/fftw-3.3.5/genfft/simd.ml @ 168:ceec0dd9ec9c

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