annotate src/fftw-3.3.5/genfft/c.ml @ 42:2cd0e3b3e1fd

Current fftw source
author Chris Cannam
date Tue, 18 Oct 2016 13:40:26 +0100
parents
children
rev   line source
Chris@42 1 (*
Chris@42 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@42 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42 5 *
Chris@42 6 * This program is free software; you can redistribute it and/or modify
Chris@42 7 * it under the terms of the GNU General Public License as published by
Chris@42 8 * the Free Software Foundation; either version 2 of the License, or
Chris@42 9 * (at your option) any later version.
Chris@42 10 *
Chris@42 11 * This program is distributed in the hope that it will be useful,
Chris@42 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@42 14 * GNU General Public License for more details.
Chris@42 15 *
Chris@42 16 * You should have received a copy of the GNU General Public License
Chris@42 17 * along with this program; if not, write to the Free Software
Chris@42 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@42 19 *
Chris@42 20 *)
Chris@42 21
Chris@42 22 (*
Chris@42 23 * This module contains the definition of a C-like abstract
Chris@42 24 * syntax tree, and functions to convert ML values into C
Chris@42 25 * programs
Chris@42 26 *)
Chris@42 27
Chris@42 28 open Expr
Chris@42 29 open Annotate
Chris@42 30 open List
Chris@42 31
Chris@42 32 let realtype = "R"
Chris@42 33 let realtypep = realtype ^ " *"
Chris@42 34 let extended_realtype = "E"
Chris@42 35 let constrealtype = "const " ^ realtype
Chris@42 36 let constrealtypep = constrealtype ^ " *"
Chris@42 37
Chris@42 38 let stridetype = "stride"
Chris@42 39
Chris@42 40 (***********************************
Chris@42 41 * C program structure
Chris@42 42 ***********************************)
Chris@42 43 type c_decl =
Chris@42 44 | Decl of string * string
Chris@42 45 | Tdecl of string (* arbitrary text declaration *)
Chris@42 46
Chris@42 47 and c_ast =
Chris@42 48 | Asch of annotated_schedule
Chris@42 49 | Simd_leavefun
Chris@42 50 | Return of c_ast
Chris@42 51 | For of c_ast * c_ast * c_ast * c_ast
Chris@42 52 | If of c_ast * c_ast
Chris@42 53 | Block of (c_decl list) * (c_ast list)
Chris@42 54 | Binop of string * c_ast * c_ast
Chris@42 55 | Expr_assign of c_ast * c_ast
Chris@42 56 | Stmt_assign of c_ast * c_ast
Chris@42 57 | Comma of c_ast * c_ast
Chris@42 58 | Integer of int
Chris@42 59 | CVar of string
Chris@42 60 | CCall of string * c_ast
Chris@42 61 | CPlus of c_ast list
Chris@42 62 | ITimes of c_ast * c_ast
Chris@42 63 | CUminus of c_ast
Chris@42 64 and c_fcn = Fcn of string * string * (c_decl list) * c_ast
Chris@42 65
Chris@42 66
Chris@42 67 let ctimes = function
Chris@42 68 | (Integer 1), a -> a
Chris@42 69 | a, (Integer 1) -> a
Chris@42 70 | a, b -> ITimes (a, b)
Chris@42 71
Chris@42 72 (*
Chris@42 73 * C AST unparser
Chris@42 74 *)
Chris@42 75 let foldr_string_concat l = fold_right (^) l ""
Chris@42 76
Chris@42 77 let rec unparse_expr_c =
Chris@42 78 let yes x = x and no x = "" in
Chris@42 79
Chris@42 80 let rec unparse_plus maybe =
Chris@42 81 let maybep = maybe " + " in
Chris@42 82 function
Chris@42 83 | [] -> ""
Chris@42 84 | (Uminus (Times (a, b))) :: (Uminus c) :: d ->
Chris@42 85 maybep ^ (op "FNMA" a b c) ^ (unparse_plus yes d)
Chris@42 86 | (Uminus c) :: (Uminus (Times (a, b))) :: d ->
Chris@42 87 maybep ^ (op "FNMA" a b c) ^ (unparse_plus yes d)
Chris@42 88 | (Uminus (Times (a, b))) :: c :: d ->
Chris@42 89 maybep ^ (op "FNMS" a b c) ^ (unparse_plus yes d)
Chris@42 90 | c :: (Uminus (Times (a, b))) :: d ->
Chris@42 91 maybep ^ (op "FNMS" a b c) ^ (unparse_plus yes d)
Chris@42 92 | (Times (a, b)) :: (Uminus c) :: d ->
Chris@42 93 maybep ^ (op "FMS" a b c) ^ (unparse_plus yes d)
Chris@42 94 | (Uminus c) :: (Times (a, b)) :: d ->
Chris@42 95 maybep ^ (op "FMS" a b c) ^ (unparse_plus yes d)
Chris@42 96 | (Times (a, b)) :: c :: d ->
Chris@42 97 maybep ^ (op "FMA" a b c) ^ (unparse_plus yes d)
Chris@42 98 | c :: (Times (a, b)) :: d ->
Chris@42 99 maybep ^ (op "FMA" a b c) ^ (unparse_plus yes d)
Chris@42 100 | (Uminus a :: b) ->
Chris@42 101 " - " ^ (parenthesize a) ^ (unparse_plus yes b)
Chris@42 102 | (a :: b) ->
Chris@42 103 maybep ^ (parenthesize a) ^ (unparse_plus yes b)
Chris@42 104 and parenthesize x = match x with
Chris@42 105 | (Load _) -> unparse_expr_c x
Chris@42 106 | (Num _) -> unparse_expr_c x
Chris@42 107 | _ -> "(" ^ (unparse_expr_c x) ^ ")"
Chris@42 108 and op nam a b c =
Chris@42 109 nam ^ "(" ^ (unparse_expr_c a) ^ ", " ^ (unparse_expr_c b) ^ ", " ^
Chris@42 110 (unparse_expr_c c) ^ ")"
Chris@42 111
Chris@42 112 in function
Chris@42 113 | Load v -> Variable.unparse v
Chris@42 114 | Num n -> Number.to_konst n
Chris@42 115 | Plus [] -> "0.0 /* bug */"
Chris@42 116 | Plus [a] -> " /* bug */ " ^ (unparse_expr_c a)
Chris@42 117 | Plus a -> (unparse_plus no a)
Chris@42 118 | Times (a, b) -> (parenthesize a) ^ " * " ^ (parenthesize b)
Chris@42 119 | Uminus (Plus [a; Uminus b]) -> unparse_plus no [b; Uminus a]
Chris@42 120 | Uminus a -> "- " ^ (parenthesize a)
Chris@42 121 | _ -> failwith "unparse_expr_c"
Chris@42 122
Chris@42 123 and unparse_expr_generic =
Chris@42 124 let rec u x = unparse_expr_generic x
Chris@42 125 and unary op a = Printf.sprintf "%s(%s)" op (u a)
Chris@42 126 and binary op a b = Printf.sprintf "%s(%s, %s)" op (u a) (u b)
Chris@42 127 and ternary op a b c = Printf.sprintf "%s(%s, %s, %s)" op (u a) (u b) (u c)
Chris@42 128 and quaternary op a b c d =
Chris@42 129 Printf.sprintf "%s(%s, %s, %s, %s)" op (u a) (u b) (u c) (u d)
Chris@42 130 and unparse_plus = function
Chris@42 131 | [(Uminus (Times (a, b))); Times (c, d)] -> quaternary "FNMMS" a b c d
Chris@42 132 | [Times (c, d); (Uminus (Times (a, b)))] -> quaternary "FNMMS" a b c d
Chris@42 133 | [Times (c, d); (Times (a, b))] -> quaternary "FMMA" a b c d
Chris@42 134 | [(Uminus (Times (a, b))); c] -> ternary "FNMS" a b c
Chris@42 135 | [c; (Uminus (Times (a, b)))] -> ternary "FNMS" a b c
Chris@42 136 | [(Uminus c); (Times (a, b))] -> ternary "FMS" a b c
Chris@42 137 | [(Times (a, b)); (Uminus c)] -> ternary "FMS" a b c
Chris@42 138 | [c; (Times (a, b))] -> ternary "FMA" a b c
Chris@42 139 | [(Times (a, b)); c] -> ternary "FMA" a b c
Chris@42 140 | [a; Uminus b] -> binary "SUB" a b
Chris@42 141 | [a; b] -> binary "ADD" a b
Chris@42 142 | a :: b :: c -> binary "ADD" a (Plus (b :: c))
Chris@42 143 | _ -> failwith "unparse_plus"
Chris@42 144 in function
Chris@42 145 | Load v -> Variable.unparse v
Chris@42 146 | Num n -> Number.to_konst n
Chris@42 147 | Plus a -> unparse_plus a
Chris@42 148 | Times (a, b) -> binary "MUL" a b
Chris@42 149 | Uminus a -> unary "NEG" a
Chris@42 150 | _ -> failwith "unparse_expr"
Chris@42 151
Chris@42 152 and unparse_expr x =
Chris@42 153 if !Magic.generic_arith then
Chris@42 154 unparse_expr_generic x
Chris@42 155 else
Chris@42 156 unparse_expr_c x
Chris@42 157
Chris@42 158 and unparse_assignment (Assign (v, x)) =
Chris@42 159 (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n"
Chris@42 160
Chris@42 161 and unparse_annotated force_bracket =
Chris@42 162 let rec unparse_code = function
Chris@42 163 ADone -> ""
Chris@42 164 | AInstr i -> unparse_assignment i
Chris@42 165 | ASeq (a, b) ->
Chris@42 166 (unparse_annotated false a) ^ (unparse_annotated false b)
Chris@42 167 and declare_variables l =
Chris@42 168 let rec uvar = function
Chris@42 169 [] -> failwith "uvar"
Chris@42 170 | [v] -> (Variable.unparse v) ^ ";\n"
Chris@42 171 | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b)
Chris@42 172 in let rec vvar l =
Chris@42 173 let s = if !Magic.compact then 15 else 1 in
Chris@42 174 if (List.length l <= s) then
Chris@42 175 match l with
Chris@42 176 [] -> ""
Chris@42 177 | _ -> extended_realtype ^ " " ^ (uvar l)
Chris@42 178 else
Chris@42 179 (vvar (Util.take s l)) ^ (vvar (Util.drop s l))
Chris@42 180 in vvar (List.filter Variable.is_temporary l)
Chris@42 181 in function
Chris@42 182 Annotate (_, _, decl, _, code) ->
Chris@42 183 if (not force_bracket) && (Util.null decl) then
Chris@42 184 unparse_code code
Chris@42 185 else "{\n" ^
Chris@42 186 (declare_variables decl) ^
Chris@42 187 (unparse_code code) ^
Chris@42 188 "}\n"
Chris@42 189
Chris@42 190 and unparse_decl = function
Chris@42 191 | Decl (a, b) -> a ^ " " ^ b ^ ";\n"
Chris@42 192 | Tdecl x -> x
Chris@42 193
Chris@42 194 and unparse_ast =
Chris@42 195 let rec unparse_plus = function
Chris@42 196 | [] -> ""
Chris@42 197 | (CUminus a :: b) -> " - " ^ (parenthesize a) ^ (unparse_plus b)
Chris@42 198 | (a :: b) -> " + " ^ (parenthesize a) ^ (unparse_plus b)
Chris@42 199 and parenthesize x = match x with
Chris@42 200 | (CVar _) -> unparse_ast x
Chris@42 201 | (CCall _) -> unparse_ast x
Chris@42 202 | (Integer _) -> unparse_ast x
Chris@42 203 | _ -> "(" ^ (unparse_ast x) ^ ")"
Chris@42 204
Chris@42 205 in
Chris@42 206 function
Chris@42 207 | Asch a -> (unparse_annotated true a)
Chris@42 208 | Simd_leavefun -> "" (* used only in SIMD code *)
Chris@42 209 | Return x -> "return " ^ unparse_ast x ^ ";"
Chris@42 210 | For (a, b, c, d) ->
Chris@42 211 "for (" ^
Chris@42 212 unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c
Chris@42 213 ^ ")" ^ unparse_ast d
Chris@42 214 | If (a, d) ->
Chris@42 215 "if (" ^
Chris@42 216 unparse_ast a
Chris@42 217 ^ ")" ^ unparse_ast d
Chris@42 218 | Block (d, s) ->
Chris@42 219 if (s == []) then ""
Chris@42 220 else
Chris@42 221 "{\n" ^
Chris@42 222 foldr_string_concat (map unparse_decl d) ^
Chris@42 223 foldr_string_concat (map unparse_ast s) ^
Chris@42 224 "}\n"
Chris@42 225 | Binop (op, a, b) -> (unparse_ast a) ^ op ^ (unparse_ast b)
Chris@42 226 | Expr_assign (a, b) -> (unparse_ast a) ^ " = " ^ (unparse_ast b)
Chris@42 227 | Stmt_assign (a, b) -> (unparse_ast a) ^ " = " ^ (unparse_ast b) ^ ";\n"
Chris@42 228 | Comma (a, b) -> (unparse_ast a) ^ ", " ^ (unparse_ast b)
Chris@42 229 | Integer i -> string_of_int i
Chris@42 230 | CVar s -> s
Chris@42 231 | CCall (s, x) -> s ^ "(" ^ (unparse_ast x) ^ ")"
Chris@42 232 | CPlus [] -> "0 /* bug */"
Chris@42 233 | CPlus [a] -> " /* bug */ " ^ (unparse_ast a)
Chris@42 234 | CPlus (a::b) -> (parenthesize a) ^ (unparse_plus b)
Chris@42 235 | ITimes (a, b) -> (parenthesize a) ^ " * " ^ (parenthesize b)
Chris@42 236 | CUminus a -> "- " ^ (parenthesize a)
Chris@42 237
Chris@42 238 and unparse_function = function
Chris@42 239 Fcn (typ, name, args, body) ->
Chris@42 240 let rec unparse_args = function
Chris@42 241 [Decl (a, b)] -> a ^ " " ^ b
Chris@42 242 | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", "
Chris@42 243 ^ unparse_args s
Chris@42 244 | [] -> ""
Chris@42 245 | _ -> failwith "unparse_function"
Chris@42 246 in
Chris@42 247 (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^
Chris@42 248 unparse_ast body)
Chris@42 249
Chris@42 250
Chris@42 251 (*************************************************************
Chris@42 252 * traverse a a function and return a list of all expressions,
Chris@42 253 * in the execution order
Chris@42 254 **************************************************************)
Chris@42 255 let rec fcn_to_expr_list = fun (Fcn (_, _, _, body)) -> ast_to_expr_list body
Chris@42 256 and acode_to_expr_list = function
Chris@42 257 AInstr (Assign (_, x)) -> [x]
Chris@42 258 | ASeq (a, b) ->
Chris@42 259 (asched_to_expr_list a) @ (asched_to_expr_list b)
Chris@42 260 | _ -> []
Chris@42 261 and asched_to_expr_list (Annotate (_, _, _, _, code)) =
Chris@42 262 acode_to_expr_list code
Chris@42 263 and ast_to_expr_list = function
Chris@42 264 Asch a -> asched_to_expr_list a
Chris@42 265 | Block (_, a) -> flatten (map ast_to_expr_list a)
Chris@42 266 | For (_, _, _, body) -> ast_to_expr_list body
Chris@42 267 | If (_, body) -> ast_to_expr_list body
Chris@42 268 | _ -> []
Chris@42 269
Chris@42 270 (***********************
Chris@42 271 * Extracting Constants
Chris@42 272 ***********************)
Chris@42 273
Chris@42 274 (* add a new key & value to a list of (key,value) pairs, where
Chris@42 275 the keys are floats and each key is unique up to almost_equal *)
Chris@42 276
Chris@42 277 let extract_constants f =
Chris@42 278 let constlist = flatten (map expr_to_constants (ast_to_expr_list f))
Chris@42 279 in map
Chris@42 280 (fun n ->
Chris@42 281 Tdecl
Chris@42 282 ("DK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^
Chris@42 283 ");\n"))
Chris@42 284 (unique_constants constlist)
Chris@42 285
Chris@42 286 (******************************
Chris@42 287 Extracting operation counts
Chris@42 288 ******************************)
Chris@42 289
Chris@42 290 let count_stack_vars =
Chris@42 291 let rec count_acode = function
Chris@42 292 | ASeq (a, b) -> max (count_asched a) (count_asched b)
Chris@42 293 | _ -> 0
Chris@42 294 and count_asched (Annotate (_, _, decl, _, code)) =
Chris@42 295 (length decl) + (count_acode code)
Chris@42 296 and count_ast = function
Chris@42 297 | Asch a -> count_asched a
Chris@42 298 | Block (d, a) -> (length d) + (Util.max_list (map count_ast a))
Chris@42 299 | For (_, _, _, body) -> count_ast body
Chris@42 300 | If (_, body) -> count_ast body
Chris@42 301 | _ -> 0
Chris@42 302 in function (Fcn (_, _, _, body)) -> count_ast body
Chris@42 303
Chris@42 304 let count_memory_acc f =
Chris@42 305 let rec count_var v =
Chris@42 306 if (Variable.is_locative v) then 1 else 0
Chris@42 307 and count_acode = function
Chris@42 308 | AInstr (Assign (v, _)) -> count_var v
Chris@42 309 | ASeq (a, b) -> (count_asched a) + (count_asched b)
Chris@42 310 | _ -> 0
Chris@42 311 and count_asched = function
Chris@42 312 Annotate (_, _, _, _, code) -> count_acode code
Chris@42 313 and count_ast = function
Chris@42 314 | Asch a -> count_asched a
Chris@42 315 | Block (_, a) -> (Util.sum_list (map count_ast a))
Chris@42 316 | Comma (a, b) -> (count_ast a) + (count_ast b)
Chris@42 317 | For (_, _, _, body) -> count_ast body
Chris@42 318 | If (_, body) -> count_ast body
Chris@42 319 | _ -> 0
Chris@42 320 and count_acc_expr_func acc = function
Chris@42 321 | Load v -> acc + (count_var v)
Chris@42 322 | Plus a -> fold_left count_acc_expr_func acc a
Chris@42 323 | Times (a, b) -> fold_left count_acc_expr_func acc [a; b]
Chris@42 324 | Uminus a -> count_acc_expr_func acc a
Chris@42 325 | _ -> acc
Chris@42 326 in let (Fcn (typ, name, args, body)) = f
Chris@42 327 in (count_ast body) +
Chris@42 328 fold_left count_acc_expr_func 0 (fcn_to_expr_list f)
Chris@42 329
Chris@42 330 let good_for_fma = To_alist.good_for_fma
Chris@42 331
Chris@42 332 let build_fma = function
Chris@42 333 | [a; Times (b, c)] when good_for_fma (b, c) -> Some (a, b, c)
Chris@42 334 | [Times (b, c); a] when good_for_fma (b, c) -> Some (a, b, c)
Chris@42 335 | [a; Uminus (Times (b, c))] when good_for_fma (b, c) -> Some (a, b, c)
Chris@42 336 | [Uminus (Times (b, c)); a] when good_for_fma (b, c) -> Some (a, b, c)
Chris@42 337 | _ -> None
Chris@42 338
Chris@42 339 let rec count_flops_expr_func (adds, mults, fmas) = function
Chris@42 340 | Plus [] -> (adds, mults, fmas)
Chris@42 341 | Plus ([_; _] as a) ->
Chris@42 342 begin
Chris@42 343 match build_fma a with
Chris@42 344 | None ->
Chris@42 345 fold_left count_flops_expr_func
Chris@42 346 (adds + (length a) - 1, mults, fmas) a
Chris@42 347 | Some (a, b, c) ->
Chris@42 348 fold_left count_flops_expr_func (adds, mults, fmas+1) [a; b; c]
Chris@42 349 end
Chris@42 350 | Plus (a :: b) ->
Chris@42 351 count_flops_expr_func (adds, mults, fmas) (Plus [a; Plus b])
Chris@42 352 | Times (NaN MULTI_A,_) -> (adds, mults, fmas)
Chris@42 353 | Times (NaN MULTI_B,_) -> (adds, mults, fmas)
Chris@42 354 | Times (NaN I,b) -> count_flops_expr_func (adds, mults, fmas) b
Chris@42 355 | Times (NaN CONJ,b) -> count_flops_expr_func (adds, mults, fmas) b
Chris@42 356 | Times (a,b) -> fold_left count_flops_expr_func (adds, mults+1, fmas) [a; b]
Chris@42 357 | CTimes (a,b) ->
Chris@42 358 fold_left count_flops_expr_func (adds+1, mults+2, fmas) [a; b]
Chris@42 359 | CTimesJ (a,b) ->
Chris@42 360 fold_left count_flops_expr_func (adds+1, mults+2, fmas) [a; b]
Chris@42 361 | Uminus a -> count_flops_expr_func (adds, mults, fmas) a
Chris@42 362 | _ -> (adds, mults, fmas)
Chris@42 363
Chris@42 364 let count_flops f =
Chris@42 365 fold_left count_flops_expr_func (0, 0, 0) (fcn_to_expr_list f)
Chris@42 366
Chris@42 367 let count_constants f =
Chris@42 368 length (unique_constants (flatten (map expr_to_constants (fcn_to_expr_list f))))
Chris@42 369
Chris@42 370 let arith_complexity f =
Chris@42 371 let (a, m, fmas) = count_flops f
Chris@42 372 and v = count_stack_vars f
Chris@42 373 and c = count_constants f
Chris@42 374 and mem = count_memory_acc f
Chris@42 375 in (a, m, fmas, v, c, mem)
Chris@42 376
Chris@42 377 (* print the operation costs *)
Chris@42 378 let print_cost f =
Chris@42 379 let Fcn (_, _, _, _) = f
Chris@42 380 and (a, m, fmas, v, c, mem) = arith_complexity f
Chris@42 381 in
Chris@42 382 "/*\n"^
Chris@42 383 " * This function contains " ^
Chris@42 384 (string_of_int (a + fmas)) ^ " FP additions, " ^
Chris@42 385 (string_of_int (m + fmas)) ^ " FP multiplications,\n" ^
Chris@42 386 " * (or, " ^
Chris@42 387 (string_of_int a) ^ " additions, " ^
Chris@42 388 (string_of_int m) ^ " multiplications, " ^
Chris@42 389 (string_of_int fmas) ^ " fused multiply/add),\n" ^
Chris@42 390 " * " ^ (string_of_int v) ^ " stack variables, " ^
Chris@42 391 (string_of_int c) ^ " constants, and " ^
Chris@42 392 (string_of_int mem) ^ " memory accesses\n" ^
Chris@42 393 " */\n"
Chris@42 394
Chris@42 395 (*****************************************
Chris@42 396 * functions that create C arrays
Chris@42 397 *****************************************)
Chris@42 398 type stride =
Chris@42 399 | SVar of string
Chris@42 400 | SConst of string
Chris@42 401 | SInteger of int
Chris@42 402 | SNeg of stride
Chris@42 403
Chris@42 404 type sstride =
Chris@42 405 | Simple of int
Chris@42 406 | Constant of (string * int)
Chris@42 407 | Composite of (string * int)
Chris@42 408 | Negative of sstride
Chris@42 409
Chris@42 410 let rec simplify_stride stride i =
Chris@42 411 match (stride, i) with
Chris@42 412 (_, 0) -> Simple 0
Chris@42 413 | (SInteger n, i) -> Simple (n * i)
Chris@42 414 | (SConst s, i) -> Constant (s, i)
Chris@42 415 | (SVar s, i) -> Composite (s, i)
Chris@42 416 | (SNeg x, i) ->
Chris@42 417 match (simplify_stride x i) with
Chris@42 418 | Negative y -> y
Chris@42 419 | y -> Negative y
Chris@42 420
Chris@42 421 let rec cstride_to_string = function
Chris@42 422 | Simple i -> string_of_int i
Chris@42 423 | Constant (s, i) ->
Chris@42 424 if !Magic.lisp_syntax then
Chris@42 425 "(* " ^ s ^ " " ^ (string_of_int i) ^ ")"
Chris@42 426 else
Chris@42 427 s ^ " * " ^ (string_of_int i)
Chris@42 428 | Composite (s, i) ->
Chris@42 429 if !Magic.lisp_syntax then
Chris@42 430 "(* " ^ s ^ " " ^ (string_of_int i) ^ ")"
Chris@42 431 else
Chris@42 432 "WS(" ^ s ^ ", " ^ (string_of_int i) ^ ")"
Chris@42 433 | Negative x -> "-" ^ cstride_to_string x
Chris@42 434
Chris@42 435 let aref name index =
Chris@42 436 if !Magic.lisp_syntax then
Chris@42 437 Printf.sprintf "(aref %s %s)" name index
Chris@42 438 else
Chris@42 439 Printf.sprintf "%s[%s]" name index
Chris@42 440
Chris@42 441 let array_subscript name stride k =
Chris@42 442 aref name (cstride_to_string (simplify_stride stride k))
Chris@42 443
Chris@42 444 let varray_subscript name vstride stride v i =
Chris@42 445 let vindex = simplify_stride vstride v
Chris@42 446 and iindex = simplify_stride stride i
Chris@42 447 in
Chris@42 448 let index =
Chris@42 449 match (vindex, iindex) with
Chris@42 450 (Simple vi, Simple ii) -> string_of_int (vi + ii)
Chris@42 451 | (Simple 0, x) -> cstride_to_string x
Chris@42 452 | (x, Simple 0) -> cstride_to_string x
Chris@42 453 | _ -> (cstride_to_string vindex) ^ " + " ^ (cstride_to_string iindex)
Chris@42 454 in aref name index
Chris@42 455
Chris@42 456 let real_of s = "c_re(" ^ s ^ ")"
Chris@42 457 let imag_of s = "c_im(" ^ s ^ ")"
Chris@42 458
Chris@42 459 let flops_of f =
Chris@42 460 let (add, mul, fma) = count_flops f in
Chris@42 461 Printf.sprintf "{ %d, %d, %d, 0 }" add mul fma