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