import Tablet.Binary64FromU64Exact import Tablet.Binary64Pow10FastPathExact import Tablet.Binary64ExactPositiveDivRounds import Tablet.Binary64ZeroDivRounds 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_NativeDivideRounding] theorem P14_NativeDivideRounding (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 /\ ModelF64Div 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⟩ := Binary64ZeroDivRounds 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 vPositive : (0 : Rat) < v.val := lt_of_lt_of_le (by norm_num) vLower have lo : (2 : Rat) ^ (-1022 : Int) <= (v.val : Rat) / 10 ^ i.val := by calc (2 : Rat) ^ (-1022 : Int) <= (2 : Rat) ^ (-100 : Int) := zpow_le_zpow_right₀ (by norm_num) (by norm_num) _ <= 1 / 10 ^ (22 : Nat) := by norm_num [zpow_neg] _ <= (v.val : Rat) / 10 ^ i.val := div_le_div₀ (by norm_num) vLower tenPositive tenUpper have hiQuotient : (v.val : Rat) / 10 ^ i.val < 2 ^ (128 : Nat) := by calc (v.val : Rat) / 10 ^ i.val <= (v.val : Rat) := div_le_self vPositive.le tenLower _ <= 2 ^ (53 : Nat) := vUpper _ < 2 ^ (128 : Nat) := by norm_num obtain ⟨m, hm, hb, hr⟩ := Binary64ExactPositiveDivRounds a p (v.val : Rat) (10 ^ i.val) haFinite hpFinite' haDecode hpDecode vPositive tenPositive lo hiQuotient 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_inv, Rat.cast_pow, Rat.cast_natCast, Rat.cast_ofNat, zpow_neg, zpow_natCast, div_eq_mul_inv] rw [argEq] at hr exact hr end dec2flt_model