import Tablet.P661_T1_ProductApproximationExecution import Tablet.IntegerPowerTableCertificate import Tablet.ParserExponentConstants import Tablet.P9_T1_ActualDispatchOperations import Tablet.num_imp_dec2flt_lemire_compute_float import Tablet.P822_T1_ProductTailExecution open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P661_T1_IntegerExecution] theorem P661_T1_IntegerExecution (q : Std.I64) (w : Std.U64) : exists fp : num.imp.dec2flt.common.BiasedFp, P9_T1_ActualDispatchOperations.integer q w = ok fp := by -- BODY rcases ParserExponentConstants with ⟨_, _, hsig, _, _, hinf, _, hmin, hsmall, hlarge, hroundmin, hroundmax⟩ have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 = ok { m := 0#u64, p_biased := 0#i32 } := by rfl have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 = ok { m := 0#u64, p_biased := 2047#i32 } := by rfl have herr : num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32 = ok { m := 0#u64, p_biased := (-1)#i32 } := by rfl have hcastSmall : lift (IScalar.cast .I64 (-342)#i32) = ok (-342)#i64 := by rfl have hcastLarge : lift (IScalar.cast .I64 308#i32) = ok 308#i64 := by rfl change ∃ fp, num.imp.dec2flt.lemire.compute_float F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp unfold num.imp.dec2flt.lemire.compute_float rw [hzero, hinf] simp only [bind_tc_ok] rw [hinfFp, herr] simp only [bind_tc_ok] by_cases hw : w = 0#u64 · simp only [hw, if_true] exact ⟨_, rfl⟩ · simp only [hw, if_false] rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def, hcastSmall] simp only [bind_tc_ok] by_cases hqsmall : q < (-342)#i64 · simp only [hqsmall, if_true] exact ⟨_, rfl⟩ · simp only [hqsmall, if_false] rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def, hcastLarge] simp only [bind_tc_ok] by_cases hqlarge : q > 308#i64 · simp only [hqlarge, if_true] exact ⟨_, rfl⟩ · simp only [hqlarge, if_false] have hqlo : -342 ≤ q.val := by have hq : ¬ q.val < -342 := by simpa only [IScalar.lt_equiv] using hqsmall omega have hqhi : q.val ≤ 308 := by have hq : ¬ 308 < q.val := by simpa only [IScalar.lt_equiv] using hqlarge omega have hwpos : 0 < w.val := by have hne : w.val ≠ 0 := by intro hz apply hw exact UScalar.eq_of_val_eq hz omega let l : Std.U32 := core.num.U64.leading_zeros w have hl : l.val ≤ 63 := by have hwbv : w.bv ≠ 0 := by intro h apply hw apply UScalar.eq_of_val_eq change w.bv.toNat = 0 rw [h] rfl have hloglt : Nat.log 2 w.val < 64 := Nat.log_lt_of_lt_pow hwpos.ne' w.bv.isLt simp only [l, core.num.U64.leading_zeros, BitVec.leadingZeros, if_neg hwbv, UScalar.val] change (BitVec.ofNat 32 (64 - Nat.log 2 w.bv.toNat - 1)).toNat ≤ 63 rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] omega have hshift := UScalar.ShiftLeft_spec w l (2 ^ 64) (by change l.val < 64 omega) (by norm_num [U64.size_eq]) cases hW : w <<< l with | fail e => simp [hW] at hshift | div => simp [hW] at hshift | ok W => simp only [hW, WP.spec_ok] at hshift rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def] simp only [core.f64.F64.MANTISSA_DIGITS, ModelF64MantissaDigits, Binary64ModelDefault] rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def] simp only [bind_tc_ok] have hsigop : 53#u32 - 1#u32 = ok 52#u32 := by rfl rw [hsigop] simp only [bind_tc_ok] have hcastSigSpec := UScalar.cast_inBounds_spec .Usize 52#u32 (by scalar_tac) obtain ⟨sig, hcastSig, hcastSigVal⟩ := WP.spec_imp_exists hcastSigSpec have hcastSig' : lift (UScalar.cast .Usize 52#u32) = ok sig := by simpa only [lift] using hcastSig rw [hcastSig'] simp only [bind_tc_ok] have haddSigSpec := UScalar.add_spec (x := sig) (y := 3#usize) (by scalar_tac) obtain ⟨precision, haddSig, haddSigVal⟩ := WP.spec_imp_exists haddSigSpec have hprecision : precision = 55#usize := by apply UScalar.eq_of_val_eq norm_num at hcastSigVal haddSigVal ⊢ omega rw [haddSig] simp only [bind_tc_ok] rw [show core.num.U64.leading_zeros w = l from rfl] rw [show lift l = ok l from rfl] simp only [bind_tc_ok] rw [hW] simp only [bind_tc_ok] rw [hprecision] obtain ⟨lo, hi, hproduct⟩ := P661_T1_ProductApproximationExecution q W hqlo hqhi have h18calc : (do let i ← do let i ← do let i ← do let i2 ← 64#u32 - 52#u32 i2 - 1#u32 let i1 ← 1#u32 <<< i i1 - 1#u32 i >>> 1#i32 let i1 ← lift (UScalar.hcast .I32 i) let i2 ← i1 - 1#i32 -. i2) = ok (-1022)#i32 := by rfl rw [hproduct] simp only [bind_tc_ok, uncurry_apply_pair] by_cases hlo : lo = 18446744073709551615#u64 · simp only [hlo, if_true, bind_tc_ok] by_cases hqge : q ≥ (-27)#i64 · simp only [hqge, bind_tc_ok] by_cases hqle : q ≤ 55#i64 · simp only [hqle, bind_tc_ok] simp only [if_true, decide_true, bind_tc_ok] rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MIN_EXPONENT_ROUND_TO_EVEN.eq_def, F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MAX_EXPONENT_ROUND_TO_EVEN.eq_def] rw [h18calc] have htail := WP.spec_imp_exists (P822_T1_ProductTailExecution q l lo hi hqlo hqhi hl) simpa only [bind_tc_ok, hlo, and_true] using htail · simp only [hqle, bind_tc_ok] exact ⟨_, rfl⟩ · simp only [hqge, bind_tc_ok] exact ⟨_, rfl⟩ · simp only [hlo, if_false] rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MIN_EXPONENT_ROUND_TO_EVEN.eq_def, F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MAX_EXPONENT_ROUND_TO_EVEN.eq_def] rw [h18calc] have htail := WP.spec_imp_exists (P822_T1_ProductTailExecution q l lo hi hqlo hqhi hl) simpa only [bind_tc_ok, and_true] using htail end dec2flt_model