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