import Tablet.Preamble open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: P822_T1_NormalRoundingExecution] theorem P822_T1_NormalRoundingExecution (power2 : Std.I32) (m1 : Std.U64) (hm : m1.val < 2 ^ 55) (hpPos : 0 < power2.val) (hpHi : power2.val ≤ 2110) : WP.spec (do let i20 ← lift (m1 &&& 1#u64) let mantissa2 ← m1 + i20 let mantissa3 ← mantissa2 >>> 1#i32 let i21 ← 2#u64 <<< 52#u32 let (mantissa4, power21) ← if mantissa3 >= i21 then do let mantissa5 ← 1#u64 <<< 52#u32 let power22 ← power2 + 1#i32 ok (mantissa5, power22) else ok (mantissa3, power2) let i22 ← 1#u64 <<< 52#u32 let i23 ← lift (~~~ i22) let mantissa5 ← lift (mantissa4 &&& i23) if power21 >= 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 } else ok { m := mantissa5, p_biased := power21 } : Result num.imp.dec2flt.common.BiasedFp) (fun _ => True) := by -- BODY have hpLo : -114 ≤ power2.val := by omega have handSpec := UScalar.and_spec m1 1#u64 obtain ⟨i20, hand, handval⟩ := WP.spec_imp_exists handSpec rw [hand] simp only [bind_tc_ok] have hlow : i20.val ≤ 1 := by rw [handval.1, UScalar.val_and] exact Nat.and_le_right have haddBound : m1.val + i20.val ≤ UScalar.max .U64 := by norm_num [UScalar.max, U64.max_eq, UScalarTy.numBits] omega have haddSpec := UScalar.add_spec (x := m1) (y := i20) haddBound obtain ⟨m2, hadd, _⟩ := WP.spec_imp_exists haddSpec rw [hadd] simp only [bind_tc_ok] have hshiftM3 := UScalar.ShiftRight_IScalar_spec m2 (1#i32) (by norm_num) (by change (1#i32).val < 64; norm_num) obtain ⟨m3, hm3, _⟩ := WP.spec_imp_exists hshiftM3 rw [hm3] simp only [bind_tc_ok] have hshift21 := UScalar.ShiftLeft_spec (2#u64) (52#u32) (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq]) obtain ⟨i21, hi21, _⟩ := WP.spec_imp_exists hshift21 rw [hi21] simp only [bind_tc_ok] by_cases hcarry : m3 >= i21 · simp only [hcarry, if_pos] have hshiftOne := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq]) obtain ⟨m5, hm5, _⟩ := WP.spec_imp_exists hshiftOne rw [hm5] simp only [bind_tc_ok] have hp22lo : IScalar.min .I32 ≤ power2.val + 1 := by rw [IScalar.min_I32_eq] omega have hp22hi : power2.val + 1 ≤ IScalar.max .I32 := by rw [IScalar.max_I32_eq] omega have hp22Spec := IScalar.add_spec (x := power2) (y := 1#i32) hp22lo hp22hi obtain ⟨p22, hp22, _⟩ := WP.spec_imp_exists hp22Spec rw [hp22] simp only [bind_tc_ok] change WP.spec (do let i23 ← lift (~~~ m5) let mantissa5 ← lift (m5 &&& i23) if p22 >= 2047#i32 then (ok { m := 0#u64, p_biased := 2047#i32 } : Result num.imp.dec2flt.common.BiasedFp) else (ok { m := mantissa5, p_biased := p22 } : Result num.imp.dec2flt.common.BiasedFp)) (fun _ => True) have hnotSpec := UScalar.not_spec m5 obtain ⟨i23, hnot, _⟩ := WP.spec_imp_exists hnotSpec rw [hnot] simp only [bind_tc_ok] have handFinal := UScalar.and_spec m5 i23 obtain ⟨mFinal, hand, _⟩ := WP.spec_imp_exists handFinal rw [hand] simp only [bind_tc_ok] by_cases hbig : p22 >= 2047#i32 · simp only [hbig, if_pos] simp [WP.spec_ok] · simp only [hbig, if_neg] simp [WP.spec_ok] · simp only [hcarry, if_neg] simp only [if_false] simp only [bind_tc_ok] step by scalar_tac step by scalar_tac step by scalar_tac by_cases hbig : power2 >= 2047#i32 · simp only [hbig, if_pos] simp [WP.spec_ok] · simp only [hbig, if_neg] simp [WP.spec_ok] end dec2flt_model