cannam@95: (* cannam@95: * Copyright (c) 1997-1999 Massachusetts Institute of Technology cannam@95: * Copyright (c) 2003, 2007-11 Matteo Frigo cannam@95: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: *) cannam@95: cannam@95: (* policies for loading/computing twiddle factors *) cannam@95: open Complex cannam@95: open Util cannam@95: cannam@95: type twop = TW_FULL | TW_CEXP | TW_NEXT cannam@95: cannam@95: let optostring = function cannam@95: | TW_CEXP -> "TW_CEXP" cannam@95: | TW_NEXT -> "TW_NEXT" cannam@95: | TW_FULL -> "TW_FULL" cannam@95: cannam@95: type twinstr = (twop * int * int) cannam@95: cannam@95: let rec unroll_twfull l = match l with cannam@95: | [] -> [] cannam@95: | (TW_FULL, v, n) :: b -> cannam@95: (forall [] cons 1 n (fun i -> (TW_CEXP, v, i))) cannam@95: @ unroll_twfull b cannam@95: | a :: b -> a :: unroll_twfull b cannam@95: cannam@95: let twinstr_to_c_string l = cannam@95: let one (op, a, b) = Printf.sprintf "{ %s, %d, %d }" (optostring op) a b cannam@95: in let rec loop first = function cannam@95: | [] -> "" cannam@95: | a :: b -> (if first then "\n" else ",\n") ^ (one a) ^ (loop false b) cannam@95: in "{" ^ (loop true l) ^ "}" cannam@95: cannam@95: let twinstr_to_simd_string vl l = cannam@95: let one sep = function cannam@95: | (TW_NEXT, 1, 0) -> sep ^ "{TW_NEXT, " ^ vl ^ ", 0}" cannam@95: | (TW_NEXT, _, _) -> failwith "twinstr_to_simd_string" cannam@95: | (TW_CEXP, v, b) -> sep ^ (Printf.sprintf "VTW(%d,%d)" v b) cannam@95: | _ -> failwith "twinstr_to_simd_string" cannam@95: in let rec loop first = function cannam@95: | [] -> "" cannam@95: | a :: b -> (one (if first then "\n" else ",\n") a) ^ (loop false b) cannam@95: in "{" ^ (loop true (unroll_twfull l)) ^ "}" cannam@95: cannam@95: let rec pow m n = cannam@95: if (n = 0) then 1 cannam@95: else m * pow m (n - 1) cannam@95: cannam@95: let rec is_pow m n = cannam@95: n = 1 || ((n mod m) = 0 && is_pow m (n / m)) cannam@95: cannam@95: let rec log m n = if n = 1 then 0 else 1 + log m (n / m) cannam@95: cannam@95: let rec largest_power_smaller_than m i = cannam@95: if (is_pow m i) then i cannam@95: else largest_power_smaller_than m (i - 1) cannam@95: cannam@95: let rec smallest_power_larger_than m i = cannam@95: if (is_pow m i) then i cannam@95: else smallest_power_larger_than m (i + 1) cannam@95: cannam@95: let rec_array n f = cannam@95: let g = ref (fun i -> Complex.zero) in cannam@95: let a = Array.init n (fun i -> lazy (!g i)) in cannam@95: let h i = f (fun i -> Lazy.force a.(i)) i in cannam@95: begin cannam@95: g := h; cannam@95: h cannam@95: end cannam@95: cannam@95: cannam@95: let ctimes use_complex_arith a b = cannam@95: if use_complex_arith then cannam@95: Complex.ctimes a b cannam@95: else cannam@95: Complex.times a b cannam@95: cannam@95: let ctimesj use_complex_arith a b = cannam@95: if use_complex_arith then cannam@95: Complex.ctimesj a b cannam@95: else cannam@95: Complex.times (Complex.conj a) b cannam@95: cannam@95: let make_bytwiddle sign use_complex_arith g f i = cannam@95: if i = 0 then cannam@95: f i cannam@95: else if sign = 1 then cannam@95: ctimes use_complex_arith (g i) (f i) cannam@95: else cannam@95: ctimesj use_complex_arith (g i) (f i) cannam@95: cannam@95: (* various policies for computing/loading twiddle factors *) cannam@95: cannam@95: let twiddle_policy_load_all v use_complex_arith = cannam@95: let bytwiddle n sign w f = cannam@95: make_bytwiddle sign use_complex_arith (fun i -> w (i - 1)) f cannam@95: and twidlen n = 2 * (n - 1) cannam@95: and twdesc r = [(TW_FULL, v, r);(TW_NEXT, 1, 0)] cannam@95: in bytwiddle, twidlen, twdesc cannam@95: cannam@95: (* cannam@95: * if i is a power of two, then load w (log i) cannam@95: * else let x = largest power of 2 less than i in cannam@95: * let y = i - x in cannam@95: * compute w^{x+y} = w^x * w^y cannam@95: *) cannam@95: let twiddle_policy_log2 v use_complex_arith = cannam@95: let bytwiddle n sign w f = cannam@95: let g = rec_array n (fun self i -> cannam@95: if i = 0 then Complex.one cannam@95: else if is_pow 2 i then w (log 2 i) cannam@95: else let x = largest_power_smaller_than 2 i in cannam@95: let y = i - x in cannam@95: ctimes use_complex_arith (self x) (self y)) cannam@95: in make_bytwiddle sign use_complex_arith g f cannam@95: and twidlen n = 2 * (log 2 (largest_power_smaller_than 2 (2 * n - 1))) cannam@95: and twdesc n = cannam@95: (List.flatten cannam@95: (List.map cannam@95: (fun i -> cannam@95: if i > 0 && is_pow 2 i then cannam@95: [TW_CEXP, v, i] cannam@95: else cannam@95: []) cannam@95: (iota n))) cannam@95: @ [(TW_NEXT, 1, 0)] cannam@95: in bytwiddle, twidlen, twdesc cannam@95: cannam@95: let twiddle_policy_log3 v use_complex_arith = cannam@95: let rec terms_needed i pi s n = cannam@95: if (s >= n - 1) then i cannam@95: else terms_needed (i + 1) (3 * pi) (s + pi) n cannam@95: in cannam@95: let rec bytwiddle n sign w f = cannam@95: let nterms = terms_needed 0 1 0 n in cannam@95: let maxterm = pow 3 (nterms - 1) in cannam@95: let g = rec_array (3 * n) (fun self i -> cannam@95: if i = 0 then Complex.one cannam@95: else if is_pow 3 i then w (log 3 i) cannam@95: else if i = (n - 1) && maxterm >= n then cannam@95: w (nterms - 1) cannam@95: else let x = smallest_power_larger_than 3 i in cannam@95: if (i + i >= x) then cannam@95: let x = min x (n - 1) in cannam@95: ctimesj use_complex_arith (self (x - i)) (self x) cannam@95: else let x = largest_power_smaller_than 3 i in cannam@95: ctimes use_complex_arith (self (i - x)) (self x)) cannam@95: in make_bytwiddle sign use_complex_arith g f cannam@95: and twidlen n = 2 * (terms_needed 0 1 0 n) cannam@95: and twdesc n = cannam@95: (List.map cannam@95: (fun i -> cannam@95: let x = min (pow 3 i) (n - 1) in cannam@95: TW_CEXP, v, x) cannam@95: (iota ((twidlen n) / 2))) cannam@95: @ [(TW_NEXT, 1, 0)] cannam@95: in bytwiddle, twidlen, twdesc cannam@95: cannam@95: let current_twiddle_policy = ref twiddle_policy_load_all cannam@95: cannam@95: let twiddle_policy use_complex_arith = cannam@95: !current_twiddle_policy use_complex_arith cannam@95: cannam@95: let set_policy x = Arg.Unit (fun () -> current_twiddle_policy := x) cannam@95: let set_policy_int x = Arg.Int (fun i -> current_twiddle_policy := x i) cannam@95: cannam@95: let undocumented = " Undocumented twiddle policy" cannam@95: cannam@95: let speclist = [ cannam@95: "-twiddle-load-all", set_policy twiddle_policy_load_all, undocumented; cannam@95: "-twiddle-log2", set_policy twiddle_policy_log2, undocumented; cannam@95: "-twiddle-log3", set_policy twiddle_policy_log3, undocumented; cannam@95: ]