annotate src/fftw-3.3.5/genfft/twiddle.ml @ 148:b4bfdf10c4b3

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