import Tablet.Binary64FromU64Exact import Tablet.Binary64Pow10FastPathExact import Tablet.Binary64ExactPositiveMulRounds import Tablet.Binary64ZeroMulRounds import Tablet.P14_FiniteMagnitudeZero open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxRecDepth 1000000 set_option maxHeartbeats 2000000 namespace dec2flt_model -- [TABLET NODE: P14_NativeMultiplyRounding] theorem P14_NativeMultiplyRounding (v : Std.U64) (i : Std.Usize) (hv : v.val <= 2 ^ 53) (hi : i.val <= 22) : exists a p m : F64, ModelF64FromU64 v = ok a /\ F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.pow10_fast_path i = ok p /\ ModelF64Mul a p = ok m /\ m.val < 2 ^ 63 /\ DecimalGoalVocabularyDefault.roundsMagnitude ((v.val : Real) * (10 : Real) ^ (i.val : Int)) m := by -- BODY obtain ⟨a, ha, haFinite, haDecode, _⟩ := Binary64FromU64Exact v hv obtain ⟨p, hp, hpFinite, hpDecode⟩ := Binary64Pow10FastPathExact i hi have hpFinite' : p.val < 2047 * 2 ^ 52 := by norm_num at hpFinite ⊢; exact hpFinite have tenPositive : (0 : Rat) < 10 ^ i.val := by positivity have tenLower : (1 : Rat) <= 10 ^ i.val := one_le_pow₀ (by norm_num) have tenUpper : (10 : Rat) ^ i.val <= 10 ^ (22 : Nat) := pow_le_pow_right₀ (by norm_num) hi have vUpper : (v.val : Rat) <= 2 ^ (53 : Nat) := by exact_mod_cast hv by_cases hvZero : v.val = 0 · have aZero := P14_FiniteMagnitudeZero a haFinite (by simpa only [hvZero, Nat.cast_zero] using haDecode) obtain ⟨hm, hb, hr⟩ := Binary64ZeroMulRounds a p (10 ^ i.val) aZero hpFinite' hpDecode tenPositive refine ⟨a, p, a, ha, hp, hm, hb, ?_⟩ rw [hvZero, Nat.cast_zero, zero_mul] exact hr · have vLower : (1 : Rat) <= v.val := by exact_mod_cast (by omega : 1 <= v.val) have lo : (2 : Rat) ^ (-1022 : Int) <= (v.val : Rat) * 10 ^ i.val := by have unit : (1 : Rat) <= (v.val : Rat) * 10 ^ i.val := one_le_mul_of_one_le_of_one_le vLower tenLower have tiny : (2 : Rat) ^ (-1022 : Int) <= 1 := by calc (2 : Rat) ^ (-1022 : Int) <= (2 : Rat) ^ (0 : Int) := zpow_le_zpow_right₀ (by norm_num) (by norm_num) _ = 1 := zpow_zero _ exact tiny.trans unit have hiProduct : (v.val : Rat) * 10 ^ i.val < 2 ^ (128 : Nat) := by calc (v.val : Rat) * 10 ^ i.val <= (2 ^ (53 : Nat) : Rat) * 10 ^ (22 : Nat) := mul_le_mul vUpper tenUpper (by positivity) (by positivity) _ < 2 ^ (128 : Nat) := by norm_num obtain ⟨m, hm, hb, hr⟩ := Binary64ExactPositiveMulRounds a p (v.val : Rat) (10 ^ i.val) haFinite hpFinite' haDecode hpDecode lo hiProduct refine ⟨a, p, m, ha, hp, hm, hb, ?_⟩ have argEq : (((v.val : Rat) * (10 : Rat) ^ i.val : Rat) : Real) = (v.val : Real) * (10 : Real) ^ (i.val : Int) := by simp only [Rat.cast_mul, Rat.cast_pow, Rat.cast_natCast, Rat.cast_ofNat, zpow_natCast] rw [argEq] at hr exact hr end dec2flt_model