import Tablet.Binary64NormalEncoderRounds import Tablet.Binary64AdjacentNormalValues import Tablet.Binary64ModelDefault import Tablet.Binary64NativeCallDomain import Tablet.DecimalGoalVocabularyDefault import Tablet.ModelF64FromU64 open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxRecDepth 1000000 set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: Binary64FromU64Exact] theorem Binary64FromU64Exact : ∀ v : Std.U64, v.val ≤ 2 ^ 53 → ∃ f : F64, ModelF64FromU64 v = ok f ∧ f.val < 2047 * 2 ^ 52 ∧ (let bits := f.val let e := bits / (2 ^ 52 : Nat) let m := bits % (2 ^ 52 : Nat) let decoded : Rat := if e = 0 then (m : Rat) / (2 : Rat) ^ (1074 : Nat) else if 1075 ≤ e then (((2 ^ 52 : Nat) + m : Nat) : Rat) * (2 : Rat) ^ (e - 1075) else (((2 ^ 52 : Nat) + m : Nat) : Rat) / (2 : Rat) ^ (1075 - e) decoded = (v.val : Rat)) ∧ DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := by -- BODY intro v hv obtain ⟨fd, hfd, hfdFinite⟩ := Binary64NativeCallDomain.1 v hv have hfdFinite' : fd.val < 2047 * 2 ^ 52 := by norm_num at hfdFinite ⊢ exact hfdFinite by_cases hv0 : v.val = 0 · have veq : v = 0#u64 := UScalar.eq_of_val_eq hv0 subst v let fz : F64 := ⟨BitVec.ofNat 64 0⟩ have fzval : fz.val = 0 := rfl have hmodel : ModelF64FromU64 0#u64 = ok fz := by set_option exponentiation.threshold 2048 in norm_num [ModelF64FromU64, Binary64ModelDefault, zpow_neg] apply UScalar.eq_of_val_eq change (BitVec.ofNat 64 0).toNat = 0 simp have hfdEq : fd = fz := by have hEq : ok fz = ok fd := hmodel.symm.trans hfd exact (Result.ok.inj hEq).symm refine ⟨fz, hmodel, ?_, ?_, ?_⟩ · rw [← hfdEq] exact hfdFinite' · simp [fzval] dsimp [DecimalGoalVocabularyDefault] rw [hv0, fzval] simp only [Nat.zero_div, Nat.zero_mod, Nat.cast_zero, zero_div, if_true] change 0 ≤ (0 : Real) ∧ 0 ≤ 9218868437227405312 ∧ (if (0 : Nat) = 9218868437227405312 then (2 : Real) ^ 1024 - (2 : Real) ^ 970 ≤ 0 else (0 : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 ∧ ∀ w : F64, w.val < 9218868437227405312 → w ≠ fz → abs (0 - 0) ≤ abs (0 - (let e := w.val / 4503599627370496 let m := w.val % 4503599627370496 if e = 0 then (m : Real) / (2 : Real) ^ 1074 else if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real) ^ (e - 1075) else ((4503599627370496 + m : Nat) : Real) / (2 : Real) ^ (1075 - e))) ∧ (abs (0 - 0) = abs (0 - (let e := w.val / 4503599627370496 let m := w.val % 4503599627370496 if e = 0 then (m : Real) / (2 : Real) ^ 1074 else if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real) ^ (e - 1075) else ((4503599627370496 + m : Nat) : Real) / (2 : Real) ^ (1075 - e))) → True)) refine ⟨by norm_num, by norm_num, ?_⟩ rw [if_neg (by norm_num)] refine ⟨?_, ?_⟩ · set_option exponentiation.threshold 2048 in norm_num · intro w hw hne have value_nonnegative : 0 ≤ (let e := w.val / 4503599627370496 let m := w.val % 4503599627370496 if e = 0 then (m : Real) / (2 : Real) ^ 1074 else if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real) ^ (e - 1075) else ((4503599627370496 + m : Nat) : Real) / (2 : Real) ^ (1075 - e)) := by dsimp only split <;> positivity constructor · simpa [abs_of_nonneg value_nonnegative] using value_nonnegative · intro _ trivial · let x : Rat := v.val have hxlow : (2 : Rat) ^ (-1022 : Int) ≤ x := by have hone : (1 : Rat) ≤ x := by change (1 : Rat) ≤ (v.val : Rat) exact_mod_cast (Nat.one_le_iff_ne_zero.mpr hv0) apply le_trans _ hone rw [zpow_neg] exact (inv_le_one₀ (pow_pos (by norm_num : (0 : Rat) < 2) _)).2 (one_le_pow₀ (by norm_num)) have hxhigh : x < (2 : Rat) ^ (128 : Nat) := by have hcast : x ≤ (2 ^ 53 : Rat) := by change (v.val : Rat) ≤ (2 ^ 53 : Rat) exact_mod_cast hv apply hcast.trans_lt exact pow_lt_pow_right₀ (by norm_num) (by omega) let step : Nat → Nat → Nat := fun old i => if ((2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023)) ≤ x then i + 1 else old let scan : Nat → Nat := fun n => List.foldl step 1 (List.range n) have scan_formula : ∀ n : Nat, scan (n + 1) = step (scan n) n := by intro n change List.foldl step 1 (List.range (n + 1)) = step (List.foldl step 1 (List.range n)) n rw [List.range_succ, List.foldl_append] rfl have scan_lower : ∀ n : Nat, 1 ≤ scan n := by intro n induction n with | zero => simp [scan] | succ n ih => rw [scan_formula] dsimp [step] split <;> omega have scan_upper : ∀ n : Nat, scan n ≤ n + 1 := by intro n induction n with | zero => simp [scan] | succ n ih => rw [scan_formula] dsimp [step] split <;> omega have scan_mono : ∀ n : Nat, scan n ≤ scan (n + 1) := by intro n rw [scan_formula] dsimp [step] split · exact scan_upper n · exact le_rfl have scan_monotone : Monotone scan := monotone_nat_of_le_succ scan_mono have scan_threshold : ∀ n : Nat, (2 : Rat) ^ ((scan n : Int) - 1023) ≤ x := by intro n induction n with | zero => simpa [scan] using hxlow | succ n ih => rw [scan_formula] dsimp [step] split · assumption · exact ih have scan_bound : ∀ n : Nat, 1 ≤ n → scan n ≤ n := by intro n hn induction n with | zero => omega | succ n ih => rw [scan_formula] by_cases hn0 : n = 0 · subst n simp [scan, step] · dsimp [step] split <;> omega have last_threshold : ¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x) := by intro h have hlarge : (2 : Rat) ^ (128 : Nat) < (2 : Rat) ^ (1023 : Nat) := pow_lt_pow_right₀ (by norm_num) (by omega) exact (not_lt_of_ge h) (hxhigh.trans hlarge) have exponent_upper : scan 2046 ≤ 2045 := by rw [show 2046 = 2045 + 1 by norm_num, scan_formula] change (if (2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x then 2046 else scan 2045) ≤ 2045 rw [if_neg last_threshold] exact scan_bound 2045 (by omega) have no_next_threshold : ¬ ((2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x) := by intro threshold have advanced : scan (scan 2046 + 1) = scan 2046 + 1 := by rw [scan_formula] change (if (2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x then scan 2046 + 1 else scan (scan 2046)) = scan 2046 + 1 rw [if_pos threshold] have future : scan (scan 2046 + 1) ≤ scan 2046 := by apply scan_monotone omega omega let e := scan 2046 let z : Rat := x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) have e_lower : 1 ≤ e := scan_lower 2046 have e_upper : e ≤ 2045 := exponent_upper have current_threshold : (2 : Rat) ^ ((e : Int) - 1023) ≤ x := scan_threshold 2046 have next_threshold : x < (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) := lt_of_not_ge no_next_threshold have e_small : e ≤ 1076 := by by_contra h have hp : (2 : Rat) ^ (54 : Int) ≤ (2 : Rat) ^ ((e : Int) - 1023) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hm : (2 : Rat) ^ (54 : Int) ≤ x := hp.trans current_threshold have hxle : x ≤ (2 : Rat) ^ (53 : Nat) := by change (v.val : Rat) ≤ (2 ^ 53 : Rat) exact_mod_cast hv have himpossible : (2 : Rat) ^ (54 : Nat) ≤ (2 : Rat) ^ (53 : Nat) := hm.trans hxle norm_num [pow_succ] at himpossible have z_exact : ∃ n : Nat, z = (n : Rat) := by by_cases he : e ≤ 1075 · refine ⟨v.val * 2 ^ (1075 - e), ?_⟩ dsimp [z, x] rw [show (1075 : Int) - (e : Int) = ((1075 - e : Nat) : Int) by omega, zpow_natCast] norm_num [Nat.cast_mul, Nat.cast_pow] · have ee : e = 1076 := by omega have hm : v.val = 2 ^ 53 := by have lower : (2 ^ 53 : Rat) ≤ x := by simpa [ee] using current_threshold have lower_nat : 2 ^ 53 ≤ v.val := by change (2 ^ 53 : Rat) ≤ (v.val : Rat) at lower exact_mod_cast lower exact le_antisymm hv lower_nat refine ⟨2 ^ 52, ?_⟩ dsimp [z, x] rw [ee, hm] norm_num [zpow_neg, div_eq_mul_inv] obtain ⟨n, hn⟩ := z_exact have z_lower : (2 ^ 52 : Rat) ≤ z := by dsimp [z] calc (2 ^ 52 : Rat) = (2 : Rat) ^ (52 : Int) := by norm_num _ = (2 : Rat) ^ ((e : Int) - 1023) * (2 : Rat) ^ ((1075 : Int) - (e : Int)) := by rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)] congr 1 omega _ ≤ x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) := mul_le_mul_of_nonneg_right current_threshold (zpow_pos (by norm_num) _).le have z_upper : z < (2 ^ 53 : Rat) := by dsimp [z] calc x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) < (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) * (2 : Rat) ^ ((1075 : Int) - (e : Int)) := mul_lt_mul_of_pos_right next_threshold (zpow_pos (by norm_num) _) _ = (2 : Rat) ^ (53 : Int) := by rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)] congr 1 push_cast omega _ = (2 ^ 53 : Rat) := by norm_num have n_lower : 2 ^ 52 ≤ n := by rw [hn] at z_lower exact_mod_cast z_lower have n_upper : n < 2 ^ 53 := by rw [hn] at z_upper exact_mod_cast z_upper let lowBits : Nat := e * 2 ^ 52 + (n - 2 ^ 52) let f : F64 := ⟨BitVec.ofNat 64 lowBits⟩ have flow : f.val = lowBits := by dsimp [f] change (BitVec.ofNat 64 lowBits).toNat = lowBits rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt] dsimp [lowBits] omega have adjacent := Binary64AdjacentNormalValues e n e_lower e_upper n_lower n_upper have decoded_real : (let bits := lowBits let ef := bits / (2 ^ 52 : Nat) let mf := bits % (2 ^ 52 : Nat) if ef = 0 then (mf : Real) / (2 : Real) ^ (1074 : Nat) else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Real) * (2 : Real) ^ (ef - 1075) else (((2 ^ 52 : Nat) + mf : Nat) : Real) / (2 : Real) ^ (1075 - ef)) = (x : Real) := by calc _ = (n : Real) * (2 : Real) ^ ((e : Int) - 1075) := adjacent.1 _ = (z : Real) * (2 : Real) ^ ((e : Int) - 1075) := by rw [hn] norm_num _ = (x : Real) := by dsimp [z] push_cast rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] have hz : ((1075 : Int) - (e : Int)) + ((e : Int) - 1075) = 0 := by ring rw [hz] norm_num have decoded : (let bits := f.val let ef := bits / (2 ^ 52 : Nat) let mf := bits % (2 ^ 52 : Nat) if ef = 0 then (mf : Rat) / (2 : Rat) ^ (1074 : Nat) else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Rat) * (2 : Rat) ^ (ef - 1075) else (((2 ^ 52 : Nat) + mf : Nat) : Rat) / (2 : Rat) ^ (1075 - ef)) = x := by apply Rat.cast_injective (α := Real) rw [flow] by_cases hez : lowBits / (2 ^ 52 : Nat) = 0 · rw [if_pos hez] at decoded_real ⊢ push_cast simpa only [Rat.cast_natCast] using decoded_real · rw [if_neg hez] at decoded_real ⊢ by_cases heh : 1075 ≤ lowBits / (2 ^ 52 : Nat) · rw [if_pos heh] at decoded_real ⊢ push_cast simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real · rw [if_neg heh] at decoded_real ⊢ push_cast simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real have hround := Binary64NormalEncoderRounds x hxlow hxhigh have hroundf : DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := by change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real) (⟨BitVec.ofNat 64 (if (if z.den < 2 * (z.num.natAbs % z.den) ∨ (z.den = 2 * (z.num.natAbs % z.den) ∧ (z.num.natAbs / z.den) % 2 = 1) then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53 then (e + 1) * 2 ^ 52 else e * 2 ^ 52 + ((if z.den < 2 * (z.num.natAbs % z.den) ∨ (z.den = 2 * (z.num.natAbs % z.den) ∧ (z.num.natAbs / z.den) % 2 = 1) then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩) at hround rw [hn] at hround have nne : n ≠ 2 ^ 53 := by omega simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast, Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero, false_and, false_or, if_false, nne] at hround have fw : (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f := rfl rw [fw] at hround change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real) f exact hround have hmodel : ModelF64FromU64 v = ok f := by simp only [ModelF64FromU64, Binary64ModelDefault] rw [if_pos hv] have rational_bound : (x : Rat) ≤ 2 ^ 53 := by dsimp [x] exact_mod_cast hv have overflow_false : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ x := by intro h have impossible : (2 ^ 1024 - 2 ^ 970 : Rat) ≤ 2 ^ 53 := h.trans rational_bound set_option exponentiation.threshold 2048 in set_option maxRecDepth 100000 in norm_num at impossible rw [if_neg overflow_false] rw [if_neg (not_lt_of_ge hxlow)] change ok (⟨BitVec.ofNat 64 (if (if z.den < 2 * (z.num.natAbs % z.den) ∨ (z.den = 2 * (z.num.natAbs % z.den) ∧ (z.num.natAbs / z.den) % 2 = 1) then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53 then (e + 1) * 2 ^ 52 else e * 2 ^ 52 + ((if z.den < 2 * (z.num.natAbs % z.den) ∨ (z.den = 2 * (z.num.natAbs % z.den) ∧ (z.num.natAbs / z.den) % 2 = 1) then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩ : F64) = ok f rw [hn] have nne' : n ≠ 2 ^ 53 := by omega simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast, Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero, false_and, false_or, if_false, nne'] apply congrArg ok change (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f rfl have hfdEq : fd = f := by have hEq : ok f = ok fd := hmodel.symm.trans hfd exact (Result.ok.inj hEq).symm refine ⟨f, hmodel, ?_, ?_, hroundf⟩ · rw [← hfdEq] exact hfdFinite' · simpa only [x] using decoded end dec2flt_model