Commit 0d3d2818 by MARCHE Claude

### more on bitvectors and floats

parent 5155cf7e
 ... ... @@ -108,6 +108,36 @@ theory BitVector function to_nat (b:bv) : int = to_nat_aux b 0 (* generalization : (to_nat_sub b j i) returns the non-negative number represented by b[j..i] *) function to_nat_sub bv int int : int (* (to_nat_sub b j i) returns the non-negative integer whose binary repr is b[j..i] *) axiom to_nat_sub_zero : forall b:bv, j i:int. 0 <= i <= j -> nth b i = False -> to_nat_sub b j i = 2 * to_nat_sub b j (i+1) axiom to_nat_sub_one : forall b:bv, j i:int. 0 <= i <= j -> nth b i = True -> to_nat_sub b j i = 1 + 2 * to_nat_sub b j (i+1) axiom to_nat_sub_high : forall b:bv, j i:int. i > j -> to_nat_sub b j i = 0 lemma to_nat_sub_footprint: forall b1 b2:bv, j i:int. (forall k:int. i <= k <= j -> nth b1 k = nth b2 k) -> to_nat_sub b1 j i = to_nat_sub b2 j i (* 2-complement version *) function to_int_aux bv int : int (* (to_int_aux b i) returns the integer whose ... ... @@ -135,6 +165,23 @@ theory BitVector function to_int (b:bv) : int = to_int_aux b 0 (* (from_uint n) returns the bitvector representing the non-negative integer n on size bits. *) function from_int (n:int) : bv use import int.EuclideanDivision axiom nth_from_int_low_even: forall n:int. mod n 2 = 0 -> nth (from_int n) 0 = False axiom nth_from_int_low_odd: forall n:int. mod n 2 <> 0 -> nth (from_int n) 0 = True axiom nth_from_int_high: forall n i:int. i > 0 -> nth (from_int n) i = nth (from_int (div n 2)) (i-1) end ... ... @@ -147,6 +194,129 @@ theory BV32 end theory BV64 function size : int = 64 clone export BitVector with function size = size end theory BV32_64 use import int.Int use BV32 use BV64 function concat BV32.bv BV32.bv : BV64.bv axiom concat_low: forall b1 b2:BV32.bv. forall i:int. 0 <= i < 32 -> BV64.nth (concat b1 b2) i = BV32.nth b2 i axiom concat_high: forall b1 b2:BV32.bv. forall i:int. 32 <= i < 64 -> BV64.nth (concat b1 b2) i = BV32.nth b1 (i-32) end theory BV_double use import BV64 use import real.Real function double_of_bv64 (b:bv) : real (* TODO: axioms real represented by bv = sign . exp . mantissa sign on bit 63 exp on bits 62..52 mantissa on bits 51..0 si exp = 0 : zero or denormalized, we specify only zero si exp = 2047 : infinities or nan, we don't specify anything si 1 <= exp <= 2046 : the number represented is (-1)^sign * 2^(exp - bias) * (1 + mantissa * 2^(1-prec)) where bias = 1023, prec = 53 (e.g. the largest representable number is 2^(2046-1023) * (1+ (2^52-1)* 2^(-52)) = 2^1023 * (1 + 1 - 2^-52) = 2^1024 - 2^(1023-52) = 2^1024 - 2^971 ) *) function exp (b:bv) : int = BV64.to_nat_sub b 62 52 function mantissa (b:bv) : int= BV64.to_nat_sub b 51 0 function sign (b:bv) : bool = BV64.nth b 63 axiom zero : forall b:bv. exp(b) = 0 /\ mantissa(b) = 0 -> double_of_bv64(b) = 0.0 (* TODO *) end theory TestNegAsXOR use import BV64 use import BV_double use import int.Int use import real.RealInfix function j : bv = from_int 0x8000000000000000 lemma Nth_j: forall i:int. 0 <= i <= 62 -> nth j i = False lemma Exp_of_xor_j : forall x:bv. exp(bw_xor x j) = exp(x) lemma Mantissa_of_xor_j : forall x:bv. mantissa(bw_xor x j) = mantissa(x) lemma MainResultZero : forall x:bv. 0 = exp(x) /\ mantissa(x) = 0 -> double_of_bv64 (bw_xor x j) = -. double_of_bv64 x lemma MainResult : forall x:bv. 0 < exp(x) < 2047 -> double_of_bv64 (bw_xor x j) = -. double_of_bv64 x end theory TestDoubleOfInt use BV32 use BV64 use BV32_64 use import BV_double use import real.Real use import real.FromInt function j : BV32.bv = BV32.from_int 0x43300000 function j' : BV32.bv = BV32.from_int 0x80000000 function const : BV64.bv = BV32_64.concat j j' function const_as_double : real = double_of_bv64 const clone import int.Exponentiation with type t = real lemma L: const_as_double = power 2.0 52 + power 2.0 31 function double_of_int32 (i:int) : real = let var = BV32_64.concat j (BV32.bw_xor j' (BV32.from_int i)) in let v = double_of_bv64 var in v - const_as_double lemma MainResult: forall i:int. double_of_int32 i = from_int i end theory TestBv32 use import BV32 ... ...
This diff is collapsed.
 ... ... @@ -104,7 +104,7 @@ theory Double type double function max_double : real = 0x1.FFFFFFFFFFFFFp1023 function max_int : int = 9007199254740992 (* 2^23 *) function max_int : int = 9007199254740992 (* 2^53 *) clone export GenFloat with type t = double, ... ...
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!