P822_T1_ProductTailExecution

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Lemma · Product suffix executes

Let q be a modeled I64, \ell a modeled U32, and L,H arbitrary modeled U64 words, with -342\le q.\mathrm{val}\le308 and \ell.\mathrm{val}\le63. The following source-ordered Result computation returns normally. All mantissa words and their literals have U64 type, arithmetic on the exponent and shift-count intermediates has I32 type, and the significand literal 52 has U32 type.
First compute I_8=H\gg63, lift its unsigned-to-I32 cast u, compute I_9=u+64, lift the unsigned-to-I32 cast I_{10} of 52, and compute I_{11}=I_9-I_{10}, s=I_{11}-3, and M=H\gg s. Lift the I64-to-I32 cast q_{32} of q and call num.imp.dec2flt.lemire.power on q_{32}, obtaining p_0. Compute I_{15}=p_0+u, lift the unsigned-to-I32 cast I_{16} of \ell, compute I_{17}=I_{15}-I_{16}, bind the normal I32 literal -1022 as I_{18}, and compute I_{19}=I_{17}-I_{18} and p=I_{19}+1. Every addition, subtraction, negation and shift described here is the checked scalar operation of the extracted model.
If p\le0, compute n=-p and t_1=n+1. If t_1\ge64, return (0,0). Otherwise recompute t_2=n+1, compute M_1=M\gg t_2, lift c=M_1\mathbin{\&}1, compute M_2=M_1+c and M_3=M_2\gg1, compute B=1\ll52, lift the Boolean-to-I32 cast p_1 of M_3\ge B, and return (M_3,p_1).
If p>0, obtain M' by the following ordered tie adjustment. If L\le1, bind the normal I32 literal -4, lift its cast to I64, and test q\ge-4. Only if that succeeds, bind the normal I32 literal 23, lift its cast to I64, and test q\le23. Only if that also succeeds, lift A=M\mathbin{\&}3 and test A=1. If this succeeds, lift the unsigned-to-I32 cast I_{25} of 52, compute I_{26}=I_9-I_{25} and I_{27}=I_{26}-3, and compute I_{28}=M\ll I_{27}. If I_{28}=H, lift C=\mathord{\sim}1 and return M'=M\mathbin{\&}C normally; otherwise return M'=M. Failure of any earlier guard also returns M'=M normally. Next lift c=M'\mathbin{\&}1, compute M_2=M'+c, M_3=M_2\gg1 and T=2\ll52. If M_3\ge T, compute K=1\ll52 and p_2=p+1 and return (M_4,p_1)=(K,p_2) to the next bind; otherwise return (M_4,p_1)=(M_3,p). Compute B=1\ll52, lift C=\mathord{\sim}B, and lift M_5=M_4\mathbin{\&}C. Return (0,2047) if p_1\ge2047, and (M_5,p_1) otherwise.
Pairs in this description denote BiasedFp values with mantissa first and biased exponent second. The conclusion is \mathrm{WP.spec} of exactly this monadic computation with the identically true postcondition: there is a normal \mathrm{ok} return, and neither failure nor divergence.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.P822_T1_NormalRoundingExecution
import Tablet.IntegerPowerTableCertificate

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P822_T1_ProductTailExecution]
theorem P822_T1_ProductTailExecution (q : Std.I64) (l : Std.U32)
    (lo hi : Std.U64) (hqlo : -342 ≤ q.val) (hqhi : q.val ≤ 308)
    (hl : l.val ≤ 63) :
    WP.spec (do
      let i8 ← hi >>> 63#i32
      let upperbit ← lift (UScalar.hcast .I32 i8)
      let i9 ← upperbit + 64#i32
      let i10 ← lift (UScalar.hcast .I32 52#u32)
      let i11 ← i9 - i10
      let i12 ← i11 - 3#i32
      let mantissa ← hi >>> i12
      let i13 ← lift (IScalar.cast .I32 q)
      let i14 ← num.imp.dec2flt.lemire.power i13
      let i15 ← i14 + upperbit
      let i16 ← lift (UScalar.hcast .I32 l)
      let i17 ← i15 - i16
      let i18 ← ok (-1022)#i32
      let i19 ← i17 - i18
      let power2 ← i19 + 1#i32
      if power2 <= 0#i32 then
        let i20 ← -. power2
        let i21 ← i20 + 1#i32
        if i21 >= 64#i32 then
          ok { m := 0#u64, p_biased := 0#i32 }
        else
          let i22 ← i20 + 1#i32
          let mantissa1 ← mantissa >>> i22
          let i23 ← lift (mantissa1 &&& 1#u64)
          let mantissa2 ← mantissa1 + i23
          let mantissa3 ← mantissa2 >>> 1#i32
          let i24 ← 1#u64 <<< 52#u32
          let power21 ← lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
          ok { m := mantissa3, p_biased := power21 }
      else
        let mantissa1 ←
          if lo <= 1#u64 then
            do
              let i20 ← ok (-4)#i32
              let i21 ← lift (IScalar.cast .I64 i20)
              if q >= i21 then
                let i22 ← ok 23#i32
                let i23 ← lift (IScalar.cast .I64 i22)
                if q <= i23 then
                  let i24 ← lift (mantissa &&& 3#u64)
                  if i24 = 1#u64 then
                    let i25 ← lift (UScalar.hcast .I32 52#u32)
                    let i26 ← i9 - i25
                    let i27 ← i26 - 3#i32
                    let i28 ← mantissa <<< i27
                    if i28 = hi then
                      let i29 ← lift (~~~ 1#u64)
                      ok (mantissa &&& i29)
                    else ok mantissa
                  else ok mantissa
                else ok mantissa
              else ok mantissa
          else ok mantissa
        do
          let i20 ← lift (mantissa1 &&& 1#u64)
          let mantissa2 ← mantissa1 + 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
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

A U64 word has value in [0,2^{64}-1]. The first right shift has nonnegative I32 count 63 below 64, so it returns normally with value \lfloor H.\mathrm{val}/2^{63}\rfloor\le1. Consequently its unsigned-to-I32 cast preserves that value and gives u.\mathrm{val}\in[0,1]. The checked addition u+64 is in [64,65]. The cast of U32 52 preserves 52. The next checked subtractions are in [12,13] and [9,10], respectively, so all these I32 operations succeed. In particular 9\le s.\mathrm{val}\le10, making H\gg s a legal shift. Its result satisfies
M.\mathrm{val}=\left\lfloor H.\mathrm{val}/2^{s.\mathrm{val}}\right\rfloor <2^{55},
since H.\mathrm{val}<2^{64} and 2^{64}\le2^{55}2^{s.\mathrm{val}}. Left shifts below use the 64-bit bitvector semantics; their legal counts, rather than a no-truncation multiplication claim, justify execution.
Apply IntegerPowerTableCertificate with exactly the two hypotheses on q. Its normal cast q_{32} and normal power result p_0 satisfy p_0.\mathrm{val}=\lfloor217706q.\mathrm{val}/65536\rfloor+63. The deterministic cast in this computation equals that certified cast. Monotonicity of integer division by the positive 65536 and evaluation at q=-342,308 give -1074\le p_0.\mathrm{val}\le1086. The next addition is in [-1074,1087]. The cast of \ell preserves its value because 0\le\ell.\mathrm{val}\le63, well inside I32. The subtraction of this cast is in [-1137,1087]; subtracting the normally bound -1022 is in [-115,2109]; the final addition of 1 is in [-114,2110]. Each interval lies in [-2^{31},2^{31}-1], so each individual checked operation succeeds in its source order, not merely their combined expression. Thus -114\le p.\mathrm{val}\le2110.
Consider first p\le0. Checked negation gives n\in[0,114], and the first addition of 1 gives t_1\in[1,115]; both fit I32. If t_1\ge64 the return of (0,0) is immediate. Otherwise the repeated checked addition gives the same value t_2=t_1\in[1,63], so M\gg t_2 succeeds. Right shift cannot increase a nonnegative word, hence M_1.\mathrm{val}\le M.\mathrm{val}<2^{55}. The lifted AND is total and gives c.\mathrm{val}\le1. Therefore M_1.\mathrm{val}+c.\mathrm{val}\le2^{55}<2^{64} and its checked addition succeeds. The right shift by 1 and the left shift by U32 52 have legal counts, so both succeed. The comparison is total and its Boolean-to-I32 cast returns 0 or 1, which fits I32, normally. The final biased pair is then returned normally. This checks both additions of 1 and every bind in the subnormal branch.
Now suppose p>0. Split on L\le1. Its false arm returns M without any constant calls. In its true arm the literal -4 is normally bound and its widening I64 cast preserves -4. A false q\ge-4 guard returns M. In the true arm the literal 23 and its widening I64 cast are likewise normal and exact. A false q\le23 guard returns M. In the true arm the lifted AND with 3 is total; a false A=1 guard again returns M. Only in the remaining arm is 52 recast to I32. That cast is exact; since I_9.\mathrm{val}\in[64,65], the checked differences are in [12,13] and [9,10]. Hence the reverse left shift has a nonnegative count below 64 and succeeds. Its equality test with H is total. If unequal it returns M; if equal, complementing U64 1 is total and the return of the AND is normal. An AND cannot increase the natural value of its left operand. Thus every arm returns a word M' with M'.\mathrm{val}<2^{55}.
The hypotheses for NormalRoundingExecution now hold: M'.\mathrm{val}<2^{55} and 0<p.\mathrm{val}\le2110. That node's statement is precisely the remaining sequence of lifted low-bit AND, checked addition and shifts, optional checked carry increment, mask construction, and final infinity-or-pair return. It proves normality in both carry arms and both final comparison arms. Combining it with each normally returning tie-adjustment arm proves the positive-exponent branch. Together with the subnormal branch this exhausts the total comparison p\le0. Substitution of the obtained normal values at each successive Result bind establishes the stated normality of the complete product suffix, for arbitrary L,H.

Lean proof


  let coreRound (power2 : Std.I32) (mantissa1 : Std.U64) :
      Result num.imp.dec2flt.common.BiasedFp := do
    let i20 ← lift (mantissa1 &&& 1#u64)
    let mantissa2 ← mantissa1 + 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 }
  let coreTail (lo hi : Std.U64) : Result num.imp.dec2flt.common.BiasedFp := do
    let i8 ← hi >>> 63#i32
    let upperbit ← lift (UScalar.hcast .I32 i8)
    let i9 ← upperbit + 64#i32
    let i10 ← lift (UScalar.hcast .I32 52#u32)
    let i11 ← i9 - i10
    let i12 ← i11 - 3#i32
    let mantissa ← hi >>> i12
    let i13 ← lift (IScalar.cast .I32 q)
    let i14 ← num.imp.dec2flt.lemire.power i13
    let i15 ← i14 + upperbit
    let i16 ← lift (UScalar.hcast .I32 l)
    let i17 ← i15 - i16
    let i18 ← ok (-1022)#i32
    let i19 ← i17 - i18
    let power2 ← i19 + 1#i32
    if power2 <= 0#i32 then
      let i20 ← -. power2
      let i21 ← i20 + 1#i32
      if i21 >= 64#i32 then
        ok { m := 0#u64, p_biased := 0#i32 }
      else
        let i22 ← i20 + 1#i32
        let mantissa1 ← mantissa >>> i22
        let i23 ← lift (mantissa1 &&& 1#u64)
        let mantissa2 ← mantissa1 + i23
        let mantissa3 ← mantissa2 >>> 1#i32
        let i24 ← 1#u64 <<< 52#u32
        let power21 ← lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
        ok { m := mantissa3, p_biased := power21 }
    else
      let mantissa1 ←
        if lo <= 1#u64 then
          do
            let i20 ← ok (-4)#i32
            let i21 ← lift (IScalar.cast .I64 i20)
            if q >= i21 then
              let i22 ← ok 23#i32
              let i23 ← lift (IScalar.cast .I64 i22)
              if q <= i23 then
                let i24 ← lift (mantissa &&& 3#u64)
                if i24 = 1#u64 then
                  let i25 ← lift (UScalar.hcast .I32 52#u32)
                  let i26 ← i9 - i25
                  let i27 ← i26 - 3#i32
                  let i28 ← mantissa <<< i27
                  if i28 = hi then
                    let i29 ← lift (~~~ 1#u64)
                    ok (mantissa &&& i29)
                  else ok mantissa
                else ok mantissa
              else ok mantissa
            else ok mantissa
        else ok mantissa
      coreRound power2 mantissa1

  obtain ⟨q32, p, delta, index, a, b, hq32, hp, hpval,
    hdelta, hindex, htable, _⟩ := IntegerPowerTableCertificate q hqlo hqhi
  have hpRange : -1074 ≤ p.val ∧ p.val ≤ 1086 := by
    rw [hpval]
    constructor <;> omega
  change WP.spec (coreTail lo hi) (fun _ => True)
  unfold coreTail
  step by scalar_tac
  step by scalar_tac
  have hi8le : i8.val ≤ 1 := by
    have hhi : hi.val < 2 ^ 64 := hi.bv.isLt
    have hdiv : hi.val / 2 ^ 63 < 2 := by
      apply (Nat.div_lt_iff_lt_mul (by decide : 0 < 2 ^ 63)).2
      have hbound : hi.val < 2 * 2 ^ 63 := by
        have : 2 ^ 64 = 2 ^ 63 * 2 := by norm_num [pow_succ]
        omega
      exact hbound
    rw [i8_post1, Nat.shiftRight_eq_div_pow]
    omega
  have hupval : upperbit.val = (i8.val : Int) := by
    have hs := UScalar.hcast_inBounds_spec .I32 i8 (by scalar_tac)
    simp only [lift, WP.spec_ok] at hs
    rw [upperbit_post]
    exact hs
  have hup : 0 ≤ upperbit.val ∧ upperbit.val ≤ 1 := by
    rw [hupval]
    constructor
    · exact_mod_cast (Nat.zero_le i8.val)
    · exact_mod_cast hi8le
  step by scalar_tac
  step by scalar_tac
  have hi9Range : 64 ≤ i9.val ∧ i9.val ≤ 65 := by
    rw [i9_post]
    omega
  have hi10Val : i10.val = 52 := by
    rw [i10_post]
    rfl
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  have hq32' : IScalar.cast .I32 q = q32 := by
    simp only [lift] at hq32
    exact Result.ok.inj hq32
  have hi13 : i13 = q32 := i13_post.trans hq32'
  rw [hi13, hp]
  simp only [bind_tc_ok]
  step by scalar_tac
  step by scalar_tac
  have hi15Range : -1074 ≤ i15.val ∧ i15.val ≤ 1087 := by
    rw [i15_post]
    omega
  have hi16val : i16.val = l.val := by
    have hs := UScalar.hcast_inBounds_spec .I32 l (by
      rw [IScalar.max_I32_eq]
      omega)
    simp only [lift, WP.spec_ok] at hs
    rw [i16_post]
    exact hs
  have hi16Range : 0 ≤ i16.val ∧ i16.val ≤ 63 := by
    rw [hi16val]
    omega
  have hi12Range : 9 ≤ i12.val ∧ i12.val ≤ 10 := by
    rw [i12_post, i11_post, hi10Val, i9_post]
    omega
  have hmantissaBound : mantissa.val < 2 ^ 55 := by
    rw [mantissa_post1, Nat.shiftRight_eq_div_pow]
    have hhi : hi.val < 2 ^ 64 := hi.bv.isLt
    have hpow : 2 ^ 64 ≤ 2 ^ 55 * 2 ^ i12.val.toNat := by
      have hexp : 64 ≤ 55 + i12.val.toNat := by
        have hnat : 9 ≤ i12.val.toNat := by omega
        omega
      rw [← Nat.pow_add]
      exact Nat.pow_le_pow_right (by decide : 1 ≤ 2) hexp
    have : hi.val / 2 ^ i12.val.toNat < 2 ^ 55 := by
      apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ i12.val.toNat)).2
      omega
    exact this
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  have hpower2Range : -114 ≤ power2.val ∧ power2.val ≤ 2110 := by
    rw [power2_post, i19_post, i17_post, i15_post]
    omega
  have hroundSpec : ∀ (p : Std.I32) (m1 : Std.U64),
      m1.val < 2 ^ 55 → -114 ≤ p.val → p.val ≤ 2110 →
      0 < p.val → WP.spec (coreRound p m1) (fun _ => True) := by
    intro p m1 hm _ hpHi hpPos
    exact P822_T1_NormalRoundingExecution p m1 hm hpPos hpHi
  by_cases hsub : power2 <= 0#i32
  · simp only [hsub, if_pos]
    step by scalar_tac
    step by scalar_tac
    by_cases hlarge : i21 >= 64#i32
    · simp only [hlarge, if_pos]
      simp [WP.spec_ok]
    · simp only [hlarge, if_neg]
      simp only [if_false]
      step by scalar_tac
      step by scalar_tac
      step by scalar_tac
      simp only [lift, IScalar.cast_fromBool, bind_tc_ok]
      have hmantissa1Bound : mantissa1.val ≤ mantissa.val := by
        rw [mantissa1_post1, Nat.shiftRight_eq_div_pow]
        exact Nat.div_le_self _ _
      have hmantissa1Lt : mantissa1.val < 2 ^ 55 :=
        lt_of_le_of_lt hmantissa1Bound hmantissaBound
      have hi23le : i23.val ≤ 1 := by
        rw [i23_post1]
        simp only [UScalar.val_and]
        exact Nat.and_le_right
      have haddMantissa : mantissa1.val + i23.val ≤ UScalar.max .U64 := by
        norm_num [UScalar.max, U64.max_eq, UScalarTy.numBits]
        omega
      have hmspec := UScalar.add_spec (x := mantissa1) (y := i23) haddMantissa
      obtain ⟨mantissa2', hmadd, hmval⟩ := WP.spec_imp_exists hmspec
      rw [hmadd]
      simp only [bind_tc_ok]
      step by scalar_tac
      step by scalar_tac
  · simp only [hsub, if_neg]
    simp only [if_false]
    have hpPos : 0 < power2.val := by
      have hnotle : ¬ power2.val ≤ 0 := by
        simpa only [IScalar.le_equiv] using hsub
      omega
    by_cases hlosmall : lo <= 1#u64
    · simp only [hlosmall, if_pos]
      have hcast4 : lift (IScalar.cast .I64 (-4)#i32) =
          ok (-4)#i64 := by rfl
      rw [hcast4]
      simp only [bind_tc_ok]
      by_cases hqge : q ≥ (-4)#i64
      · simp only [hqge, if_pos]
        have hcast23 : lift (IScalar.cast .I64 23#i32) =
            ok 23#i64 := by rfl
        rw [hcast23]
        simp only [bind_tc_ok]
        by_cases hqle : q ≤ 23#i64
        · simp only [hqle, if_pos]
          have handSpec := UScalar.and_spec mantissa 3#u64
          obtain ⟨i24', hand, _⟩ := WP.spec_imp_exists handSpec
          rw [hand]
          simp only [bind_tc_ok]
          by_cases htie : i24' = 1#u64
          · simp only [htie, if_pos]
            have hcast52 : lift (UScalar.hcast .I32 52#u32) =
                ok 52#i32 := by rfl
            rw [hcast52]
            simp only [bind_tc_ok]
            have hsub26lo : IScalar.min .I32 ≤ i9.val - 52 := by
              rw [IScalar.min_I32_eq, i9_post]
              omega
            have hsub26hi : i9.val - 52 ≤ IScalar.max .I32 := by
              rw [IScalar.max_I32_eq, i9_post]
              omega
            have hsub26Spec := IScalar.sub_spec (x := i9)
              (y := 52#i32) hsub26lo hsub26hi
            obtain ⟨i26', hi26, hi26val⟩ :=
              WP.spec_imp_exists hsub26Spec
            rw [hi26]
            simp only [bind_tc_ok]
            have hi26range : 12 ≤ i26'.val ∧ i26'.val ≤ 13 := by
              rw [hi26val, i9_post]
              have h52 : (52#i32).val = 52 := rfl
              rw [h52]
              omega
            have hsub27lo : IScalar.min .I32 ≤ i26'.val - 3 := by
              rw [IScalar.min_I32_eq]
              rcases hi26range with ⟨hlo, hhi⟩
              omega
            have hsub27hi : i26'.val - 3 ≤ IScalar.max .I32 := by
              rw [IScalar.max_I32_eq]
              rcases hi26range with ⟨hlo, hhi⟩
              omega
            have hsub27Spec := IScalar.sub_spec (x := i26')
              (y := 3#i32) hsub27lo hsub27hi
            obtain ⟨i27', hi27, hi27val⟩ :=
              WP.spec_imp_exists hsub27Spec
            rw [hi27]
            simp only [bind_tc_ok]
            have hi27range : 9 ≤ i27'.val ∧ i27'.val ≤ 10 := by
              rw [hi27val, hi26val, i9_post]
              have h52 : (52#i32).val = 52 := rfl
              have h3 : (3#i32).val = 3 := rfl
              rw [h52]
              rw [h3]
              omega
            have hshift28 := UScalar.ShiftLeft_IScalar_spec
              mantissa i27' (2 ^ 64) (by omega)
              (by change i27'.val < 64; omega)
              (by norm_num [U64.size_eq])
            obtain ⟨i28', hi28, _⟩ := WP.spec_imp_exists hshift28
            rw [hi28]
            simp only [bind_tc_ok]
            by_cases heq28 : i28' = hi
            · simp only [heq28, if_pos]
              have hnotSpec := UScalar.not_spec (1#u64)
              obtain ⟨i29', hnot, _⟩ :=
                WP.spec_imp_exists hnotSpec
              rw [hnot]
              simp only [bind_tc_ok]
              have hm1bound : (mantissa &&& i29').val < 2 ^ 55 := by
                rw [UScalar.val_and]
                exact lt_of_le_of_lt Nat.and_le_left hmantissaBound
              change WP.spec (coreRound power2 (mantissa &&& i29'))
                (fun _ => True)
              exact hroundSpec power2 _ hm1bound hpower2Range.1
                hpower2Range.2 hpPos
            · simp only [heq28, if_neg]
              change WP.spec (coreRound power2 mantissa)
                (fun _ => True)
              exact hroundSpec power2 _ hmantissaBound hpower2Range.1
                hpower2Range.2 hpPos
          · simp only [htie, if_neg]
            change WP.spec (coreRound power2 mantissa)
              (fun _ => True)
            exact hroundSpec power2 _ hmantissaBound hpower2Range.1
              hpower2Range.2 hpPos
        · simp only [hqle, if_neg]
          change WP.spec (coreRound power2 mantissa)
            (fun _ => True)
          exact hroundSpec power2 _ hmantissaBound hpower2Range.1
            hpower2Range.2 hpPos
      · simp only [hqge, if_neg]
        change WP.spec (coreRound power2 mantissa) (fun _ => True)
        exact hroundSpec power2 _ hmantissaBound hpower2Range.1
          hpower2Range.2 hpPos
    · simp only [hlosmall, if_neg]
      change WP.spec (coreRound power2 mantissa) (fun _ => True)
      exact hroundSpec power2 _ hmantissaBound hpower2Range.1
        hpower2Range.2 hpPos

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P822_T1_NormalRoundingExecution
import Tablet.IntegerPowerTableCertificate

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P822_T1_ProductTailExecution]
theorem P822_T1_ProductTailExecution (q : Std.I64) (l : Std.U32)
    (lo hi : Std.U64) (hqlo : -342 ≤ q.val) (hqhi : q.val ≤ 308)
    (hl : l.val ≤ 63) :
    WP.spec (do
      let i8 ← hi >>> 63#i32
      let upperbit ← lift (UScalar.hcast .I32 i8)
      let i9 ← upperbit + 64#i32
      let i10 ← lift (UScalar.hcast .I32 52#u32)
      let i11 ← i9 - i10
      let i12 ← i11 - 3#i32
      let mantissa ← hi >>> i12
      let i13 ← lift (IScalar.cast .I32 q)
      let i14 ← num.imp.dec2flt.lemire.power i13
      let i15 ← i14 + upperbit
      let i16 ← lift (UScalar.hcast .I32 l)
      let i17 ← i15 - i16
      let i18 ← ok (-1022)#i32
      let i19 ← i17 - i18
      let power2 ← i19 + 1#i32
      if power2 <= 0#i32 then
        let i20 ← -. power2
        let i21 ← i20 + 1#i32
        if i21 >= 64#i32 then
          ok { m := 0#u64, p_biased := 0#i32 }
        else
          let i22 ← i20 + 1#i32
          let mantissa1 ← mantissa >>> i22
          let i23 ← lift (mantissa1 &&& 1#u64)
          let mantissa2 ← mantissa1 + i23
          let mantissa3 ← mantissa2 >>> 1#i32
          let i24 ← 1#u64 <<< 52#u32
          let power21 ← lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
          ok { m := mantissa3, p_biased := power21 }
      else
        let mantissa1 ←
          if lo <= 1#u64 then
            do
              let i20 ← ok (-4)#i32
              let i21 ← lift (IScalar.cast .I64 i20)
              if q >= i21 then
                let i22 ← ok 23#i32
                let i23 ← lift (IScalar.cast .I64 i22)
                if q <= i23 then
                  let i24 ← lift (mantissa &&& 3#u64)
                  if i24 = 1#u64 then
                    let i25 ← lift (UScalar.hcast .I32 52#u32)
                    let i26 ← i9 - i25
                    let i27 ← i26 - 3#i32
                    let i28 ← mantissa <<< i27
                    if i28 = hi then
                      let i29 ← lift (~~~ 1#u64)
                      ok (mantissa &&& i29)
                    else ok mantissa
                  else ok mantissa
                else ok mantissa
              else ok mantissa
          else ok mantissa
        do
          let i20 ← lift (mantissa1 &&& 1#u64)
          let mantissa2 ← mantissa1 + 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
  let coreRound (power2 : Std.I32) (mantissa1 : Std.U64) :
      Result num.imp.dec2flt.common.BiasedFp := do
    let i20 ← lift (mantissa1 &&& 1#u64)
    let mantissa2 ← mantissa1 + 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 }
  let coreTail (lo hi : Std.U64) : Result num.imp.dec2flt.common.BiasedFp := do
    let i8 ← hi >>> 63#i32
    let upperbit ← lift (UScalar.hcast .I32 i8)
    let i9 ← upperbit + 64#i32
    let i10 ← lift (UScalar.hcast .I32 52#u32)
    let i11 ← i9 - i10
    let i12 ← i11 - 3#i32
    let mantissa ← hi >>> i12
    let i13 ← lift (IScalar.cast .I32 q)
    let i14 ← num.imp.dec2flt.lemire.power i13
    let i15 ← i14 + upperbit
    let i16 ← lift (UScalar.hcast .I32 l)
    let i17 ← i15 - i16
    let i18 ← ok (-1022)#i32
    let i19 ← i17 - i18
    let power2 ← i19 + 1#i32
    if power2 <= 0#i32 then
      let i20 ← -. power2
      let i21 ← i20 + 1#i32
      if i21 >= 64#i32 then
        ok { m := 0#u64, p_biased := 0#i32 }
      else
        let i22 ← i20 + 1#i32
        let mantissa1 ← mantissa >>> i22
        let i23 ← lift (mantissa1 &&& 1#u64)
        let mantissa2 ← mantissa1 + i23
        let mantissa3 ← mantissa2 >>> 1#i32
        let i24 ← 1#u64 <<< 52#u32
        let power21 ← lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
        ok { m := mantissa3, p_biased := power21 }
    else
      let mantissa1 ←
        if lo <= 1#u64 then
          do
            let i20 ← ok (-4)#i32
            let i21 ← lift (IScalar.cast .I64 i20)
            if q >= i21 then
              let i22 ← ok 23#i32
              let i23 ← lift (IScalar.cast .I64 i22)
              if q <= i23 then
                let i24 ← lift (mantissa &&& 3#u64)
                if i24 = 1#u64 then
                  let i25 ← lift (UScalar.hcast .I32 52#u32)
                  let i26 ← i9 - i25
                  let i27 ← i26 - 3#i32
                  let i28 ← mantissa <<< i27
                  if i28 = hi then
                    let i29 ← lift (~~~ 1#u64)
                    ok (mantissa &&& i29)
                  else ok mantissa
                else ok mantissa
              else ok mantissa
            else ok mantissa
        else ok mantissa
      coreRound power2 mantissa1

  obtain ⟨q32, p, delta, index, a, b, hq32, hp, hpval,
    hdelta, hindex, htable, _⟩ := IntegerPowerTableCertificate q hqlo hqhi
  have hpRange : -1074 ≤ p.val ∧ p.val ≤ 1086 := by
    rw [hpval]
    constructor <;> omega
  change WP.spec (coreTail lo hi) (fun _ => True)
  unfold coreTail
  step by scalar_tac
  step by scalar_tac
  have hi8le : i8.val ≤ 1 := by
    have hhi : hi.val < 2 ^ 64 := hi.bv.isLt
    have hdiv : hi.val / 2 ^ 63 < 2 := by
      apply (Nat.div_lt_iff_lt_mul (by decide : 0 < 2 ^ 63)).2
      have hbound : hi.val < 2 * 2 ^ 63 := by
        have : 2 ^ 64 = 2 ^ 63 * 2 := by norm_num [pow_succ]
        omega
      exact hbound
    rw [i8_post1, Nat.shiftRight_eq_div_pow]
    omega
  have hupval : upperbit.val = (i8.val : Int) := by
    have hs := UScalar.hcast_inBounds_spec .I32 i8 (by scalar_tac)
    simp only [lift, WP.spec_ok] at hs
    rw [upperbit_post]
    exact hs
  have hup : 0 ≤ upperbit.val ∧ upperbit.val ≤ 1 := by
    rw [hupval]
    constructor
    · exact_mod_cast (Nat.zero_le i8.val)
    · exact_mod_cast hi8le
  step by scalar_tac
  step by scalar_tac
  have hi9Range : 64 ≤ i9.val ∧ i9.val ≤ 65 := by
    rw [i9_post]
    omega
  have hi10Val : i10.val = 52 := by
    rw [i10_post]
    rfl
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  have hq32' : IScalar.cast .I32 q = q32 := by
    simp only [lift] at hq32
    exact Result.ok.inj hq32
  have hi13 : i13 = q32 := i13_post.trans hq32'
  rw [hi13, hp]
  simp only [bind_tc_ok]
  step by scalar_tac
  step by scalar_tac
  have hi15Range : -1074 ≤ i15.val ∧ i15.val ≤ 1087 := by
    rw [i15_post]
    omega
  have hi16val : i16.val = l.val := by
    have hs := UScalar.hcast_inBounds_spec .I32 l (by
      rw [IScalar.max_I32_eq]
      omega)
    simp only [lift, WP.spec_ok] at hs
    rw [i16_post]
    exact hs
  have hi16Range : 0 ≤ i16.val ∧ i16.val ≤ 63 := by
    rw [hi16val]
    omega
  have hi12Range : 9 ≤ i12.val ∧ i12.val ≤ 10 := by
    rw [i12_post, i11_post, hi10Val, i9_post]
    omega
  have hmantissaBound : mantissa.val < 2 ^ 55 := by
    rw [mantissa_post1, Nat.shiftRight_eq_div_pow]
    have hhi : hi.val < 2 ^ 64 := hi.bv.isLt
    have hpow : 2 ^ 64 ≤ 2 ^ 55 * 2 ^ i12.val.toNat := by
      have hexp : 64 ≤ 55 + i12.val.toNat := by
        have hnat : 9 ≤ i12.val.toNat := by omega
        omega
      rw [← Nat.pow_add]
      exact Nat.pow_le_pow_right (by decide : 1 ≤ 2) hexp
    have : hi.val / 2 ^ i12.val.toNat < 2 ^ 55 := by
      apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ i12.val.toNat)).2
      omega
    exact this
  step by scalar_tac
  step by scalar_tac
  step by scalar_tac
  have hpower2Range : -114 ≤ power2.val ∧ power2.val ≤ 2110 := by
    rw [power2_post, i19_post, i17_post, i15_post]
    omega
  have hroundSpec : ∀ (p : Std.I32) (m1 : Std.U64),
      m1.val < 2 ^ 55 → -114 ≤ p.val → p.val ≤ 2110 →
      0 < p.val → WP.spec (coreRound p m1) (fun _ => True) := by
    intro p m1 hm _ hpHi hpPos
    exact P822_T1_NormalRoundingExecution p m1 hm hpPos hpHi
  by_cases hsub : power2 <= 0#i32
  · simp only [hsub, if_pos]
    step by scalar_tac
    step by scalar_tac
    by_cases hlarge : i21 >= 64#i32
    · simp only [hlarge, if_pos]
      simp [WP.spec_ok]
    · simp only [hlarge, if_neg]
      simp only [if_false]
      step by scalar_tac
      step by scalar_tac
      step by scalar_tac
      simp only [lift, IScalar.cast_fromBool, bind_tc_ok]
      have hmantissa1Bound : mantissa1.val ≤ mantissa.val := by
        rw [mantissa1_post1, Nat.shiftRight_eq_div_pow]
        exact Nat.div_le_self _ _
      have hmantissa1Lt : mantissa1.val < 2 ^ 55 :=
        lt_of_le_of_lt hmantissa1Bound hmantissaBound
      have hi23le : i23.val ≤ 1 := by
        rw [i23_post1]
        simp only [UScalar.val_and]
        exact Nat.and_le_right
      have haddMantissa : mantissa1.val + i23.val ≤ UScalar.max .U64 := by
        norm_num [UScalar.max, U64.max_eq, UScalarTy.numBits]
        omega
      have hmspec := UScalar.add_spec (x := mantissa1) (y := i23) haddMantissa
      obtain ⟨mantissa2', hmadd, hmval⟩ := WP.spec_imp_exists hmspec
      rw [hmadd]
      simp only [bind_tc_ok]
      step by scalar_tac
      step by scalar_tac
  · simp only [hsub, if_neg]
    simp only [if_false]
    have hpPos : 0 < power2.val := by
      have hnotle : ¬ power2.val ≤ 0 := by
        simpa only [IScalar.le_equiv] using hsub
      omega
    by_cases hlosmall : lo <= 1#u64
    · simp only [hlosmall, if_pos]
      have hcast4 : lift (IScalar.cast .I64 (-4)#i32) =
          ok (-4)#i64 := by rfl
      rw [hcast4]
      simp only [bind_tc_ok]
      by_cases hqge : q ≥ (-4)#i64
      · simp only [hqge, if_pos]
        have hcast23 : lift (IScalar.cast .I64 23#i32) =
            ok 23#i64 := by rfl
        rw [hcast23]
        simp only [bind_tc_ok]
        by_cases hqle : q ≤ 23#i64
        · simp only [hqle, if_pos]
          have handSpec := UScalar.and_spec mantissa 3#u64
          obtain ⟨i24', hand, _⟩ := WP.spec_imp_exists handSpec
          rw [hand]
          simp only [bind_tc_ok]
          by_cases htie : i24' = 1#u64
          · simp only [htie, if_pos]
            have hcast52 : lift (UScalar.hcast .I32 52#u32) =
                ok 52#i32 := by rfl
            rw [hcast52]
            simp only [bind_tc_ok]
            have hsub26lo : IScalar.min .I32 ≤ i9.val - 52 := by
              rw [IScalar.min_I32_eq, i9_post]
              omega
            have hsub26hi : i9.val - 52 ≤ IScalar.max .I32 := by
              rw [IScalar.max_I32_eq, i9_post]
              omega
            have hsub26Spec := IScalar.sub_spec (x := i9)
              (y := 52#i32) hsub26lo hsub26hi
            obtain ⟨i26', hi26, hi26val⟩ :=
              WP.spec_imp_exists hsub26Spec
            rw [hi26]
            simp only [bind_tc_ok]
            have hi26range : 12 ≤ i26'.val ∧ i26'.val ≤ 13 := by
              rw [hi26val, i9_post]
              have h52 : (52#i32).val = 52 := rfl
              rw [h52]
              omega
            have hsub27lo : IScalar.min .I32 ≤ i26'.val - 3 := by
              rw [IScalar.min_I32_eq]
              rcases hi26range with ⟨hlo, hhi⟩
              omega
            have hsub27hi : i26'.val - 3 ≤ IScalar.max .I32 := by
              rw [IScalar.max_I32_eq]
              rcases hi26range with ⟨hlo, hhi⟩
              omega
            have hsub27Spec := IScalar.sub_spec (x := i26')
              (y := 3#i32) hsub27lo hsub27hi
            obtain ⟨i27', hi27, hi27val⟩ :=
              WP.spec_imp_exists hsub27Spec
            rw [hi27]
            simp only [bind_tc_ok]
            have hi27range : 9 ≤ i27'.val ∧ i27'.val ≤ 10 := by
              rw [hi27val, hi26val, i9_post]
              have h52 : (52#i32).val = 52 := rfl
              have h3 : (3#i32).val = 3 := rfl
              rw [h52]
              rw [h3]
              omega
            have hshift28 := UScalar.ShiftLeft_IScalar_spec
              mantissa i27' (2 ^ 64) (by omega)
              (by change i27'.val < 64; omega)
              (by norm_num [U64.size_eq])
            obtain ⟨i28', hi28, _⟩ := WP.spec_imp_exists hshift28
            rw [hi28]
            simp only [bind_tc_ok]
            by_cases heq28 : i28' = hi
            · simp only [heq28, if_pos]
              have hnotSpec := UScalar.not_spec (1#u64)
              obtain ⟨i29', hnot, _⟩ :=
                WP.spec_imp_exists hnotSpec
              rw [hnot]
              simp only [bind_tc_ok]
              have hm1bound : (mantissa &&& i29').val < 2 ^ 55 := by
                rw [UScalar.val_and]
                exact lt_of_le_of_lt Nat.and_le_left hmantissaBound
              change WP.spec (coreRound power2 (mantissa &&& i29'))
                (fun _ => True)
              exact hroundSpec power2 _ hm1bound hpower2Range.1
                hpower2Range.2 hpPos
            · simp only [heq28, if_neg]
              change WP.spec (coreRound power2 mantissa)
                (fun _ => True)
              exact hroundSpec power2 _ hmantissaBound hpower2Range.1
                hpower2Range.2 hpPos
          · simp only [htie, if_neg]
            change WP.spec (coreRound power2 mantissa)
              (fun _ => True)
            exact hroundSpec power2 _ hmantissaBound hpower2Range.1
              hpower2Range.2 hpPos
        · simp only [hqle, if_neg]
          change WP.spec (coreRound power2 mantissa)
            (fun _ => True)
          exact hroundSpec power2 _ hmantissaBound hpower2Range.1
            hpower2Range.2 hpPos
      · simp only [hqge, if_neg]
        change WP.spec (coreRound power2 mantissa) (fun _ => True)
        exact hroundSpec power2 _ hmantissaBound hpower2Range.1
          hpower2Range.2 hpPos
    · simp only [hlosmall, if_neg]
      change WP.spec (coreRound power2 mantissa) (fun _ => True)
      exact hroundSpec power2 _ hmantissaBound hpower2Range.1
        hpower2Range.2 hpPos

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (46)
More program dependencies (13)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 8 entries
Technical checking records
Local closure summary
{
  "node": "P822_T1_ProductTailExecution",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "f0853cd957de5af99b13ab3ffd2c2af6f97c891638b47ff0cf8664c7efbded9c",
  "active_statement_hash": "7764c2ec7ab58abc0fb4ce81e3c18c155a14d79bd37500705ae54b91a5024c21",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-192",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "IntegerPowerTableCertificate": "c4b148e6d8bda215",
    "P822_T1_NormalRoundingExecution": "fdec535eda64e0d0"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_lemire_power": "56b7c3bd69ca3c6a",
    "num_imp_dec2flt_table_POWER_OF_FIVE_128": "bdd28e83b60fa362"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:945abe3b563af02a26bd08188294ffd72b2c5a42175a91931a6d5b428a558f3e",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}