import Tablet.Binary64Model import Mathlib.Data.Rat.Defs open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxRecDepth 4096 namespace dec2flt_model -- [TABLET NODE: Binary64ModelDefault] def Binary64ModelDefault : Binary64Model := -- BODY let signBit : Nat := 2 ^ 63 let fractionUnit : Nat := 2 ^ 52 let infinityBits : Nat := 2047 * fractionUnit let quietNanBits : Nat := infinityBits + 2 ^ 51 let word := fun n : Nat => (⟨BitVec.ofNat 64 n⟩ : Std.U64) let sign := fun w : F64 => w.val / signBit let magnitude := fun w : F64 => w.val % signBit let exponent := fun w : F64 => magnitude w / fractionUnit let fraction := fun w : F64 => magnitude w % fractionUnit let categoryCode := fun w : F64 => if exponent w = 2047 then if fraction w = 0 then 1 else 0 else if exponent w = 0 then if fraction w = 0 then 2 else 3 else 4 let category := fun w : F64 => match categoryCode w with | 0 => core.num.FpCategory.Nan | 1 => core.num.FpCategory.Infinite | 2 => core.num.FpCategory.Zero | 3 => core.num.FpCategory.Subnormal | _ => core.num.FpCategory.Normal let finiteMagnitudeValue := fun bits : Nat => let e := bits / fractionUnit let m := bits % fractionUnit if e = 0 then (m : Rat) / ((2 : Rat) ^ (1074 : Nat)) else if 1075 <= e then ((fractionUnit + m : Nat) : Rat) * ((2 : Rat) ^ (e - 1075)) else ((fractionUnit + m : Nat) : Rat) / ((2 : Rat) ^ (1075 - e)) let roundEvenNat := fun x : Rat => let q := x.num.natAbs / x.den let r := x.num.natAbs % x.den if x.den < 2 * r ∨ (x.den = 2 * r ∧ q % 2 = 1) then q + 1 else q let normalExponent := fun x : Rat => (List.range 2046).foldl (fun (old : Nat) (i : Nat) => let e : Nat := i + 1 if ((2 : Rat) ^ ((e : Int) - 1023)) ≤ x then e else old) 1 let overflowThreshold : Rat := ((2 : Rat) ^ (1024 : Nat)) - ((2 : Rat) ^ (970 : Nat)) let roundMagnitude := fun x : Rat => if overflowThreshold <= x then infinityBits else if x < ((2 : Rat) ^ (-1022 : Int)) then roundEvenNat (x * ((2 : Rat) ^ (1074 : Nat))) else let e := normalExponent x let significand := roundEvenNat (x * ((2 : Rat) ^ ((1075 : Int) - (e : Int)))) if significand = 2 ^ 53 then (e + 1) * fractionUnit else e * fractionUnit + (significand - fractionUnit) let signedWord := fun negative : Bool => fun mag : Nat => word (mag + if negative then signBit else 0) let negativeResult := fun a b : F64 => decide (sign a != sign b) let mulImpl := fun a b : F64 => let ca := categoryCode a let cb := categoryCode b let negResult := negativeResult a b if ca = 0 ∨ cb = 0 then ok (word quietNanBits) else if (ca = 1 ∧ cb = 2) ∨ (ca = 2 ∧ cb = 1) then ok (word quietNanBits) else if ca = 1 ∨ cb = 1 then ok (signedWord negResult infinityBits) else let x := finiteMagnitudeValue (magnitude a) let y := finiteMagnitudeValue (magnitude b) ok (signedWord negResult (roundMagnitude (x * y))) let divImpl := fun a b : F64 => let ca := categoryCode a let cb := categoryCode b let negResult := negativeResult a b if ca = 0 ∨ cb = 0 then ok (word quietNanBits) else if (ca = 1 ∧ cb = 1) ∨ (ca = 2 ∧ cb = 2) then ok (word quietNanBits) else if ca = 1 ∨ cb = 2 then ok (signedWord negResult infinityBits) else if cb = 1 ∨ ca = 2 then ok (signedWord negResult 0) else let x := finiteMagnitudeValue (magnitude a) let y := finiteMagnitudeValue (magnitude b) ok (signedWord negResult (roundMagnitude (x / y))) { clone := fun x => ok x mantissaDigits := ok 53#u32 nan := ok (word quietNanBits) infinity := ok (word infinityBits) negInfinity := ok (word (signBit + infinityBits)) exponentMask := ok (word infinityBits) mantissaMask := ok (word (fractionUnit - 1)) classify := fun x => ok (category x) toBits := fun x => ok x fromBits := fun x => ok x mul := mulImpl div := divImpl neg := fun x => ok ⟨x.bv ^^^ BitVec.ofNat 64 signBit⟩ fromU64 := fun x => if x.val <= 2 ^ 53 then ok (word (roundMagnitude (x.val : Rat))) else fail .assertionFailure } end dec2flt_model