annotate src/fftw-3.3.3/genfft/c.ml @ 10:37bf6b4a2645

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