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