annotate fft/fftw/fftw-3.3.4/genfft/twiddle.ml @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 (*
Chris@19 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@19 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 5 *
Chris@19 6 * This program is free software; you can redistribute it and/or modify
Chris@19 7 * it under the terms of the GNU General Public License as published by
Chris@19 8 * the Free Software Foundation; either version 2 of the License, or
Chris@19 9 * (at your option) any later version.
Chris@19 10 *
Chris@19 11 * This program is distributed in the hope that it will be useful,
Chris@19 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 14 * GNU General Public License for more details.
Chris@19 15 *
Chris@19 16 * You should have received a copy of the GNU General Public License
Chris@19 17 * along with this program; if not, write to the Free Software
Chris@19 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 19 *
Chris@19 20 *)
Chris@19 21
Chris@19 22 (* policies for loading/computing twiddle factors *)
Chris@19 23 open Complex
Chris@19 24 open Util
Chris@19 25
Chris@19 26 type twop = TW_FULL | TW_CEXP | TW_NEXT
Chris@19 27
Chris@19 28 let optostring = function
Chris@19 29 | TW_CEXP -> "TW_CEXP"
Chris@19 30 | TW_NEXT -> "TW_NEXT"
Chris@19 31 | TW_FULL -> "TW_FULL"
Chris@19 32
Chris@19 33 type twinstr = (twop * int * int)
Chris@19 34
Chris@19 35 let rec unroll_twfull l = match l with
Chris@19 36 | [] -> []
Chris@19 37 | (TW_FULL, v, n) :: b ->
Chris@19 38 (forall [] cons 1 n (fun i -> (TW_CEXP, v, i)))
Chris@19 39 @ unroll_twfull b
Chris@19 40 | a :: b -> a :: unroll_twfull b
Chris@19 41
Chris@19 42 let twinstr_to_c_string l =
Chris@19 43 let one (op, a, b) = Printf.sprintf "{ %s, %d, %d }" (optostring op) a b
Chris@19 44 in let rec loop first = function
Chris@19 45 | [] -> ""
Chris@19 46 | a :: b -> (if first then "\n" else ",\n") ^ (one a) ^ (loop false b)
Chris@19 47 in "{" ^ (loop true l) ^ "}"
Chris@19 48
Chris@19 49 let twinstr_to_simd_string vl l =
Chris@19 50 let one sep = function
Chris@19 51 | (TW_NEXT, 1, 0) -> sep ^ "{TW_NEXT, " ^ vl ^ ", 0}"
Chris@19 52 | (TW_NEXT, _, _) -> failwith "twinstr_to_simd_string"
Chris@19 53 | (TW_CEXP, v, b) -> sep ^ (Printf.sprintf "VTW(%d,%d)" v b)
Chris@19 54 | _ -> failwith "twinstr_to_simd_string"
Chris@19 55 in let rec loop first = function
Chris@19 56 | [] -> ""
Chris@19 57 | a :: b -> (one (if first then "\n" else ",\n") a) ^ (loop false b)
Chris@19 58 in "{" ^ (loop true (unroll_twfull l)) ^ "}"
Chris@19 59
Chris@19 60 let rec pow m n =
Chris@19 61 if (n = 0) then 1
Chris@19 62 else m * pow m (n - 1)
Chris@19 63
Chris@19 64 let rec is_pow m n =
Chris@19 65 n = 1 || ((n mod m) = 0 && is_pow m (n / m))
Chris@19 66
Chris@19 67 let rec log m n = if n = 1 then 0 else 1 + log m (n / m)
Chris@19 68
Chris@19 69 let rec largest_power_smaller_than m i =
Chris@19 70 if (is_pow m i) then i
Chris@19 71 else largest_power_smaller_than m (i - 1)
Chris@19 72
Chris@19 73 let rec smallest_power_larger_than m i =
Chris@19 74 if (is_pow m i) then i
Chris@19 75 else smallest_power_larger_than m (i + 1)
Chris@19 76
Chris@19 77 let rec_array n f =
Chris@19 78 let g = ref (fun i -> Complex.zero) in
Chris@19 79 let a = Array.init n (fun i -> lazy (!g i)) in
Chris@19 80 let h i = f (fun i -> Lazy.force a.(i)) i in
Chris@19 81 begin
Chris@19 82 g := h;
Chris@19 83 h
Chris@19 84 end
Chris@19 85
Chris@19 86
Chris@19 87 let ctimes use_complex_arith a b =
Chris@19 88 if use_complex_arith then
Chris@19 89 Complex.ctimes a b
Chris@19 90 else
Chris@19 91 Complex.times a b
Chris@19 92
Chris@19 93 let ctimesj use_complex_arith a b =
Chris@19 94 if use_complex_arith then
Chris@19 95 Complex.ctimesj a b
Chris@19 96 else
Chris@19 97 Complex.times (Complex.conj a) b
Chris@19 98
Chris@19 99 let make_bytwiddle sign use_complex_arith g f i =
Chris@19 100 if i = 0 then
Chris@19 101 f i
Chris@19 102 else if sign = 1 then
Chris@19 103 ctimes use_complex_arith (g i) (f i)
Chris@19 104 else
Chris@19 105 ctimesj use_complex_arith (g i) (f i)
Chris@19 106
Chris@19 107 (* various policies for computing/loading twiddle factors *)
Chris@19 108
Chris@19 109 let twiddle_policy_load_all v use_complex_arith =
Chris@19 110 let bytwiddle n sign w f =
Chris@19 111 make_bytwiddle sign use_complex_arith (fun i -> w (i - 1)) f
Chris@19 112 and twidlen n = 2 * (n - 1)
Chris@19 113 and twdesc r = [(TW_FULL, v, r);(TW_NEXT, 1, 0)]
Chris@19 114 in bytwiddle, twidlen, twdesc
Chris@19 115
Chris@19 116 (*
Chris@19 117 * if i is a power of two, then load w (log i)
Chris@19 118 * else let x = largest power of 2 less than i in
Chris@19 119 * let y = i - x in
Chris@19 120 * compute w^{x+y} = w^x * w^y
Chris@19 121 *)
Chris@19 122 let twiddle_policy_log2 v use_complex_arith =
Chris@19 123 let bytwiddle n sign w f =
Chris@19 124 let g = rec_array n (fun self i ->
Chris@19 125 if i = 0 then Complex.one
Chris@19 126 else if is_pow 2 i then w (log 2 i)
Chris@19 127 else let x = largest_power_smaller_than 2 i in
Chris@19 128 let y = i - x in
Chris@19 129 ctimes use_complex_arith (self x) (self y))
Chris@19 130 in make_bytwiddle sign use_complex_arith g f
Chris@19 131 and twidlen n = 2 * (log 2 (largest_power_smaller_than 2 (2 * n - 1)))
Chris@19 132 and twdesc n =
Chris@19 133 (List.flatten
Chris@19 134 (List.map
Chris@19 135 (fun i ->
Chris@19 136 if i > 0 && is_pow 2 i then
Chris@19 137 [TW_CEXP, v, i]
Chris@19 138 else
Chris@19 139 [])
Chris@19 140 (iota n)))
Chris@19 141 @ [(TW_NEXT, 1, 0)]
Chris@19 142 in bytwiddle, twidlen, twdesc
Chris@19 143
Chris@19 144 let twiddle_policy_log3 v use_complex_arith =
Chris@19 145 let rec terms_needed i pi s n =
Chris@19 146 if (s >= n - 1) then i
Chris@19 147 else terms_needed (i + 1) (3 * pi) (s + pi) n
Chris@19 148 in
Chris@19 149 let rec bytwiddle n sign w f =
Chris@19 150 let nterms = terms_needed 0 1 0 n in
Chris@19 151 let maxterm = pow 3 (nterms - 1) in
Chris@19 152 let g = rec_array (3 * n) (fun self i ->
Chris@19 153 if i = 0 then Complex.one
Chris@19 154 else if is_pow 3 i then w (log 3 i)
Chris@19 155 else if i = (n - 1) && maxterm >= n then
Chris@19 156 w (nterms - 1)
Chris@19 157 else let x = smallest_power_larger_than 3 i in
Chris@19 158 if (i + i >= x) then
Chris@19 159 let x = min x (n - 1) in
Chris@19 160 ctimesj use_complex_arith (self (x - i)) (self x)
Chris@19 161 else let x = largest_power_smaller_than 3 i in
Chris@19 162 ctimes use_complex_arith (self (i - x)) (self x))
Chris@19 163 in make_bytwiddle sign use_complex_arith g f
Chris@19 164 and twidlen n = 2 * (terms_needed 0 1 0 n)
Chris@19 165 and twdesc n =
Chris@19 166 (List.map
Chris@19 167 (fun i ->
Chris@19 168 let x = min (pow 3 i) (n - 1) in
Chris@19 169 TW_CEXP, v, x)
Chris@19 170 (iota ((twidlen n) / 2)))
Chris@19 171 @ [(TW_NEXT, 1, 0)]
Chris@19 172 in bytwiddle, twidlen, twdesc
Chris@19 173
Chris@19 174 let current_twiddle_policy = ref twiddle_policy_load_all
Chris@19 175
Chris@19 176 let twiddle_policy use_complex_arith =
Chris@19 177 !current_twiddle_policy use_complex_arith
Chris@19 178
Chris@19 179 let set_policy x = Arg.Unit (fun () -> current_twiddle_policy := x)
Chris@19 180 let set_policy_int x = Arg.Int (fun i -> current_twiddle_policy := x i)
Chris@19 181
Chris@19 182 let undocumented = " Undocumented twiddle policy"
Chris@19 183
Chris@19 184 let speclist = [
Chris@19 185 "-twiddle-load-all", set_policy twiddle_policy_load_all, undocumented;
Chris@19 186 "-twiddle-log2", set_policy twiddle_policy_log2, undocumented;
Chris@19 187 "-twiddle-log3", set_policy twiddle_policy_log3, undocumented;
Chris@19 188 ]