cannam@127: (* cannam@127: * Copyright (c) 1997-1999 Massachusetts Institute of Technology cannam@127: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@127: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@127: * cannam@127: * This program is free software; you can redistribute it and/or modify cannam@127: * it under the terms of the GNU General Public License as published by cannam@127: * the Free Software Foundation; either version 2 of the License, or cannam@127: * (at your option) any later version. cannam@127: * cannam@127: * This program is distributed in the hope that it will be useful, cannam@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@127: * GNU General Public License for more details. cannam@127: * cannam@127: * You should have received a copy of the GNU General Public License cannam@127: * along with this program; if not, write to the Free Software cannam@127: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@127: * cannam@127: *) cannam@127: cannam@127: (* cannam@127: * the oracle decrees whether the sign of an expression should cannam@127: * be changed. cannam@127: * cannam@127: * Say the expression (A - B) appears somewhere. Elsewhere in the cannam@127: * expression dag the expression (B - A) may appear. cannam@127: * The oracle determines which of the two forms is canonical. cannam@127: * cannam@127: * Algorithm: evaluate the expression at a random input, and cannam@127: * keep the expression with the positive sign. cannam@127: *) cannam@127: cannam@127: let make_memoizer hash equal = cannam@127: let table = ref Assoctable.empty cannam@127: in cannam@127: (fun f k -> cannam@127: match Assoctable.lookup hash equal k !table with cannam@127: Some value -> value cannam@127: | None -> cannam@127: let value = f k in cannam@127: begin cannam@127: table := Assoctable.insert hash k value !table; cannam@127: value cannam@127: end) cannam@127: cannam@127: let almost_equal x y = cannam@127: let epsilon = 1.0E-8 in cannam@127: (abs_float (x -. y) < epsilon) || cannam@127: (abs_float (x -. y) < epsilon *. (abs_float x +. abs_float y)) cannam@127: cannam@127: let absid = make_memoizer cannam@127: (fun x -> Expr.hash_float (abs_float x)) cannam@127: (fun a b -> almost_equal a b || almost_equal (-. a) b) cannam@127: (fun x -> x) cannam@127: cannam@127: let make_random_oracle () = make_memoizer cannam@127: Variable.hash cannam@127: Variable.same cannam@127: (fun _ -> (float (Random.bits())) /. 1073741824.0) cannam@127: cannam@127: let the_random_oracle = make_random_oracle () cannam@127: cannam@127: let sum_list l = List.fold_right (+.) l 0.0 cannam@127: cannam@127: let eval_aux random_oracle = cannam@127: let memoizing = make_memoizer Expr.hash (==) in cannam@127: let rec eval x = cannam@127: memoizing cannam@127: (function cannam@127: | Expr.Num x -> Number.to_float x cannam@127: | Expr.NaN x -> Expr.transcendent_to_float x cannam@127: | Expr.Load v -> random_oracle v cannam@127: | Expr.Store (v, x) -> eval x cannam@127: | Expr.Plus l -> sum_list (List.map eval l) cannam@127: | Expr.Times (a, b) -> (eval a) *. (eval b) cannam@127: | Expr.CTimes (a, b) -> cannam@127: 1.098612288668109691395245236 +. cannam@127: 1.609437912434100374600759333 *. (eval a) *. (eval b) cannam@127: | Expr.CTimesJ (a, b) -> cannam@127: 0.9102392266268373936142401657 +. cannam@127: 0.6213349345596118107071993881 *. (eval a) *. (eval b) cannam@127: | Expr.Uminus x -> -. (eval x)) cannam@127: x cannam@127: in eval cannam@127: cannam@127: let eval = eval_aux the_random_oracle cannam@127: cannam@127: let should_flip_sign node = cannam@127: let v = eval node in cannam@127: let v' = absid v in cannam@127: not (almost_equal v v') cannam@127: cannam@127: (* cannam@127: * determine with high probability if two expressions are equal. cannam@127: * cannam@127: * The test is randomized: if the two expressions have the cannam@127: * same value for NTESTS random inputs, then they are proclaimed cannam@127: * equal. (Note that two distinct linear functions L1(x0, x1, ..., xn) cannam@127: * and L2(x0, x1, ..., xn) have the same value with probability cannam@127: * 0 for random x's, and thus this test is way more paranoid than cannam@127: * necessary.) cannam@127: *) cannam@127: let likely_equal a b = cannam@127: let tolerance = 1.0e-8 cannam@127: and ntests = 20 cannam@127: in cannam@127: let rec loop n = cannam@127: if n = 0 then cannam@127: true cannam@127: else cannam@127: let r = make_random_oracle () in cannam@127: let va = eval_aux r a cannam@127: and vb = eval_aux r b cannam@127: in cannam@127: if (abs_float (va -. vb)) > cannam@127: tolerance *. (abs_float va +. abs_float vb +. 0.0001) cannam@127: then cannam@127: false cannam@127: else cannam@127: loop (n - 1) cannam@127: in cannam@127: match (a, b) with cannam@127: cannam@127: (* cannam@127: * Because of the way eval is constructed, we have cannam@127: * eval (Store (v, x)) == eval x cannam@127: * However, we never consider the two expressions equal cannam@127: *) cannam@127: | (Expr.Store _, _) -> false cannam@127: | (_, Expr.Store _) -> false cannam@127: cannam@127: (* cannam@127: * Expressions of the form ``Uminus (Store _)'' cannam@127: * are artifacts of algsimp cannam@127: *) cannam@127: | ((Expr.Uminus (Expr.Store _)), _) -> false cannam@127: | (_, Expr.Uminus (Expr.Store _)) -> false cannam@127: cannam@127: | _ -> loop ntests cannam@127: cannam@127: let hash x = cannam@127: let f = eval x in cannam@127: truncate (f *. 65536.0)