annotate src/fftw-3.3.5/genfft/oracle.ml @ 158:fa7c54aeb697

Rebuild with --disable-stack-protector for mingw32
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 25 Jan 2019 14:31:07 +0000
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 (*
cannam@127 23 * the oracle decrees whether the sign of an expression should
cannam@127 24 * be changed.
cannam@127 25 *
cannam@127 26 * Say the expression (A - B) appears somewhere. Elsewhere in the
cannam@127 27 * expression dag the expression (B - A) may appear.
cannam@127 28 * The oracle determines which of the two forms is canonical.
cannam@127 29 *
cannam@127 30 * Algorithm: evaluate the expression at a random input, and
cannam@127 31 * keep the expression with the positive sign.
cannam@127 32 *)
cannam@127 33
cannam@127 34 let make_memoizer hash equal =
cannam@127 35 let table = ref Assoctable.empty
cannam@127 36 in
cannam@127 37 (fun f k ->
cannam@127 38 match Assoctable.lookup hash equal k !table with
cannam@127 39 Some value -> value
cannam@127 40 | None ->
cannam@127 41 let value = f k in
cannam@127 42 begin
cannam@127 43 table := Assoctable.insert hash k value !table;
cannam@127 44 value
cannam@127 45 end)
cannam@127 46
cannam@127 47 let almost_equal x y =
cannam@127 48 let epsilon = 1.0E-8 in
cannam@127 49 (abs_float (x -. y) < epsilon) ||
cannam@127 50 (abs_float (x -. y) < epsilon *. (abs_float x +. abs_float y))
cannam@127 51
cannam@127 52 let absid = make_memoizer
cannam@127 53 (fun x -> Expr.hash_float (abs_float x))
cannam@127 54 (fun a b -> almost_equal a b || almost_equal (-. a) b)
cannam@127 55 (fun x -> x)
cannam@127 56
cannam@127 57 let make_random_oracle () = make_memoizer
cannam@127 58 Variable.hash
cannam@127 59 Variable.same
cannam@127 60 (fun _ -> (float (Random.bits())) /. 1073741824.0)
cannam@127 61
cannam@127 62 let the_random_oracle = make_random_oracle ()
cannam@127 63
cannam@127 64 let sum_list l = List.fold_right (+.) l 0.0
cannam@127 65
cannam@127 66 let eval_aux random_oracle =
cannam@127 67 let memoizing = make_memoizer Expr.hash (==) in
cannam@127 68 let rec eval x =
cannam@127 69 memoizing
cannam@127 70 (function
cannam@127 71 | Expr.Num x -> Number.to_float x
cannam@127 72 | Expr.NaN x -> Expr.transcendent_to_float x
cannam@127 73 | Expr.Load v -> random_oracle v
cannam@127 74 | Expr.Store (v, x) -> eval x
cannam@127 75 | Expr.Plus l -> sum_list (List.map eval l)
cannam@127 76 | Expr.Times (a, b) -> (eval a) *. (eval b)
cannam@127 77 | Expr.CTimes (a, b) ->
cannam@127 78 1.098612288668109691395245236 +.
cannam@127 79 1.609437912434100374600759333 *. (eval a) *. (eval b)
cannam@127 80 | Expr.CTimesJ (a, b) ->
cannam@127 81 0.9102392266268373936142401657 +.
cannam@127 82 0.6213349345596118107071993881 *. (eval a) *. (eval b)
cannam@127 83 | Expr.Uminus x -> -. (eval x))
cannam@127 84 x
cannam@127 85 in eval
cannam@127 86
cannam@127 87 let eval = eval_aux the_random_oracle
cannam@127 88
cannam@127 89 let should_flip_sign node =
cannam@127 90 let v = eval node in
cannam@127 91 let v' = absid v in
cannam@127 92 not (almost_equal v v')
cannam@127 93
cannam@127 94 (*
cannam@127 95 * determine with high probability if two expressions are equal.
cannam@127 96 *
cannam@127 97 * The test is randomized: if the two expressions have the
cannam@127 98 * same value for NTESTS random inputs, then they are proclaimed
cannam@127 99 * equal. (Note that two distinct linear functions L1(x0, x1, ..., xn)
cannam@127 100 * and L2(x0, x1, ..., xn) have the same value with probability
cannam@127 101 * 0 for random x's, and thus this test is way more paranoid than
cannam@127 102 * necessary.)
cannam@127 103 *)
cannam@127 104 let likely_equal a b =
cannam@127 105 let tolerance = 1.0e-8
cannam@127 106 and ntests = 20
cannam@127 107 in
cannam@127 108 let rec loop n =
cannam@127 109 if n = 0 then
cannam@127 110 true
cannam@127 111 else
cannam@127 112 let r = make_random_oracle () in
cannam@127 113 let va = eval_aux r a
cannam@127 114 and vb = eval_aux r b
cannam@127 115 in
cannam@127 116 if (abs_float (va -. vb)) >
cannam@127 117 tolerance *. (abs_float va +. abs_float vb +. 0.0001)
cannam@127 118 then
cannam@127 119 false
cannam@127 120 else
cannam@127 121 loop (n - 1)
cannam@127 122 in
cannam@127 123 match (a, b) with
cannam@127 124
cannam@127 125 (*
cannam@127 126 * Because of the way eval is constructed, we have
cannam@127 127 * eval (Store (v, x)) == eval x
cannam@127 128 * However, we never consider the two expressions equal
cannam@127 129 *)
cannam@127 130 | (Expr.Store _, _) -> false
cannam@127 131 | (_, Expr.Store _) -> false
cannam@127 132
cannam@127 133 (*
cannam@127 134 * Expressions of the form ``Uminus (Store _)''
cannam@127 135 * are artifacts of algsimp
cannam@127 136 *)
cannam@127 137 | ((Expr.Uminus (Expr.Store _)), _) -> false
cannam@127 138 | (_, Expr.Uminus (Expr.Store _)) -> false
cannam@127 139
cannam@127 140 | _ -> loop ntests
cannam@127 141
cannam@127 142 let hash x =
cannam@127 143 let f = eval x in
cannam@127 144 truncate (f *. 65536.0)