annotate src/fftw-3.3.5/genfft/oracle.ml @ 73:02caadb7509e

Rebuild with --disable-stack-protector for mingw32
author Chris Cannam
date Fri, 25 Jan 2019 14:31:07 +0000
parents 2cd0e3b3e1fd
children
rev   line source
Chris@42 1 (*
Chris@42 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@42 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42 5 *
Chris@42 6 * This program is free software; you can redistribute it and/or modify
Chris@42 7 * it under the terms of the GNU General Public License as published by
Chris@42 8 * the Free Software Foundation; either version 2 of the License, or
Chris@42 9 * (at your option) any later version.
Chris@42 10 *
Chris@42 11 * This program is distributed in the hope that it will be useful,
Chris@42 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@42 14 * GNU General Public License for more details.
Chris@42 15 *
Chris@42 16 * You should have received a copy of the GNU General Public License
Chris@42 17 * along with this program; if not, write to the Free Software
Chris@42 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@42 19 *
Chris@42 20 *)
Chris@42 21
Chris@42 22 (*
Chris@42 23 * the oracle decrees whether the sign of an expression should
Chris@42 24 * be changed.
Chris@42 25 *
Chris@42 26 * Say the expression (A - B) appears somewhere. Elsewhere in the
Chris@42 27 * expression dag the expression (B - A) may appear.
Chris@42 28 * The oracle determines which of the two forms is canonical.
Chris@42 29 *
Chris@42 30 * Algorithm: evaluate the expression at a random input, and
Chris@42 31 * keep the expression with the positive sign.
Chris@42 32 *)
Chris@42 33
Chris@42 34 let make_memoizer hash equal =
Chris@42 35 let table = ref Assoctable.empty
Chris@42 36 in
Chris@42 37 (fun f k ->
Chris@42 38 match Assoctable.lookup hash equal k !table with
Chris@42 39 Some value -> value
Chris@42 40 | None ->
Chris@42 41 let value = f k in
Chris@42 42 begin
Chris@42 43 table := Assoctable.insert hash k value !table;
Chris@42 44 value
Chris@42 45 end)
Chris@42 46
Chris@42 47 let almost_equal x y =
Chris@42 48 let epsilon = 1.0E-8 in
Chris@42 49 (abs_float (x -. y) < epsilon) ||
Chris@42 50 (abs_float (x -. y) < epsilon *. (abs_float x +. abs_float y))
Chris@42 51
Chris@42 52 let absid = make_memoizer
Chris@42 53 (fun x -> Expr.hash_float (abs_float x))
Chris@42 54 (fun a b -> almost_equal a b || almost_equal (-. a) b)
Chris@42 55 (fun x -> x)
Chris@42 56
Chris@42 57 let make_random_oracle () = make_memoizer
Chris@42 58 Variable.hash
Chris@42 59 Variable.same
Chris@42 60 (fun _ -> (float (Random.bits())) /. 1073741824.0)
Chris@42 61
Chris@42 62 let the_random_oracle = make_random_oracle ()
Chris@42 63
Chris@42 64 let sum_list l = List.fold_right (+.) l 0.0
Chris@42 65
Chris@42 66 let eval_aux random_oracle =
Chris@42 67 let memoizing = make_memoizer Expr.hash (==) in
Chris@42 68 let rec eval x =
Chris@42 69 memoizing
Chris@42 70 (function
Chris@42 71 | Expr.Num x -> Number.to_float x
Chris@42 72 | Expr.NaN x -> Expr.transcendent_to_float x
Chris@42 73 | Expr.Load v -> random_oracle v
Chris@42 74 | Expr.Store (v, x) -> eval x
Chris@42 75 | Expr.Plus l -> sum_list (List.map eval l)
Chris@42 76 | Expr.Times (a, b) -> (eval a) *. (eval b)
Chris@42 77 | Expr.CTimes (a, b) ->
Chris@42 78 1.098612288668109691395245236 +.
Chris@42 79 1.609437912434100374600759333 *. (eval a) *. (eval b)
Chris@42 80 | Expr.CTimesJ (a, b) ->
Chris@42 81 0.9102392266268373936142401657 +.
Chris@42 82 0.6213349345596118107071993881 *. (eval a) *. (eval b)
Chris@42 83 | Expr.Uminus x -> -. (eval x))
Chris@42 84 x
Chris@42 85 in eval
Chris@42 86
Chris@42 87 let eval = eval_aux the_random_oracle
Chris@42 88
Chris@42 89 let should_flip_sign node =
Chris@42 90 let v = eval node in
Chris@42 91 let v' = absid v in
Chris@42 92 not (almost_equal v v')
Chris@42 93
Chris@42 94 (*
Chris@42 95 * determine with high probability if two expressions are equal.
Chris@42 96 *
Chris@42 97 * The test is randomized: if the two expressions have the
Chris@42 98 * same value for NTESTS random inputs, then they are proclaimed
Chris@42 99 * equal. (Note that two distinct linear functions L1(x0, x1, ..., xn)
Chris@42 100 * and L2(x0, x1, ..., xn) have the same value with probability
Chris@42 101 * 0 for random x's, and thus this test is way more paranoid than
Chris@42 102 * necessary.)
Chris@42 103 *)
Chris@42 104 let likely_equal a b =
Chris@42 105 let tolerance = 1.0e-8
Chris@42 106 and ntests = 20
Chris@42 107 in
Chris@42 108 let rec loop n =
Chris@42 109 if n = 0 then
Chris@42 110 true
Chris@42 111 else
Chris@42 112 let r = make_random_oracle () in
Chris@42 113 let va = eval_aux r a
Chris@42 114 and vb = eval_aux r b
Chris@42 115 in
Chris@42 116 if (abs_float (va -. vb)) >
Chris@42 117 tolerance *. (abs_float va +. abs_float vb +. 0.0001)
Chris@42 118 then
Chris@42 119 false
Chris@42 120 else
Chris@42 121 loop (n - 1)
Chris@42 122 in
Chris@42 123 match (a, b) with
Chris@42 124
Chris@42 125 (*
Chris@42 126 * Because of the way eval is constructed, we have
Chris@42 127 * eval (Store (v, x)) == eval x
Chris@42 128 * However, we never consider the two expressions equal
Chris@42 129 *)
Chris@42 130 | (Expr.Store _, _) -> false
Chris@42 131 | (_, Expr.Store _) -> false
Chris@42 132
Chris@42 133 (*
Chris@42 134 * Expressions of the form ``Uminus (Store _)''
Chris@42 135 * are artifacts of algsimp
Chris@42 136 *)
Chris@42 137 | ((Expr.Uminus (Expr.Store _)), _) -> false
Chris@42 138 | (_, Expr.Uminus (Expr.Store _)) -> false
Chris@42 139
Chris@42 140 | _ -> loop ntests
Chris@42 141
Chris@42 142 let hash x =
Chris@42 143 let f = eval x in
Chris@42 144 truncate (f *. 65536.0)