ParserComputeFloatMantissaBound

Lean checked

Statement

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

Lemma

For every signed decimal exponent q, unsigned word w, and biased pair b, if the concrete binary64 invocation of compute_float on (q,w) returns b, then
b.m.\mathit{val}\leq 2^{52}.

Lean statement

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

import Tablet.ParserExponentConstants
import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Tablet.ModelU64Shr
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option pp.universes false
set_option maxHeartbeats 500000

namespace dec2flt_model

-- [TABLET NODE: ParserComputeFloatMantissaBound]
theorem ParserComputeFloatMantissaBound (q : Std.I64) (w : Std.U64)
    (b : num.imp.dec2flt.common.BiasedFp)
    (h : num.imp.dec2flt.lemire.compute_float
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok b) :
    b.m.val ≤ 2 ^ 52 := 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

Write U=2^{52}. By ParserExponentConstants, the concrete binary64 instance has SIG_BITS=52, infinite power 2047, smallest and largest supported decimal powers -342 and 308, exponent minimum -1022, and round-to-even cutoffs -4 and 23. Substitute these equalities while unfolding num imp dec2flt lemire compute float. The zero, infinity, and error pairs constructed by num imp dec2flt common BiasedFp zero pow2 all have mantissa zero. Consequently the w=0, q<-342, q>308, unsafe-approximation, severe underflow, and final exponent-saturation exits satisfy the claimed bound. Every checked operation mentioned below occurs before the corresponding return in the result monad, so failure of a cast, arithmetic operation, or shift is incompatible with the assumed successful result.
It remains to treat the two product blocks. They are syntactically identical after the approximation guard, and the Lean proof checks both copies. Let (lo,hi) be the successful result of num imp dec2flt lemire compute product approx. The preliminary normalization shift has succeeded. The high-word shift by 63 is legal and returns its top bit u\in\{0,1\}; its cast to i32 is exact. The checked calculation of the next shift count is
u+64-52-3=u+9.
If u=0, then hi<2^{63} and hi\mathbin{\mathtt{>>}}9<2^{54}. If u=1, the universal 64-bit bound gives hi\mathbin{\mathtt{>>}}10<2^{54}. Thus the local pre-rounding mantissa x is always strictly below 2^{54}. This argument applies both when the all-ones low word passes its safe-exponent test and when the low word is not all ones.
Suppose first that the calculated binary power is nonpositive. Successful negation and addition form the shift count k=-\texttt{power2}+1. If k\geq64, the result is the zero pair. Otherwise 1\leq k<64. By the exact right-shift semantics of ModelU64Shr, x_1=x\mathbin{\mathtt{>>}}k<2^{53}. By ModelU64BitAnd, e=x_1\mathbin{\mathtt{\&}}1 is at most one. The successful checked addition therefore has value at most 2^{53}, and the subsequent right shift by one returns a mantissa at most 2^{52}=U. The comparison against the legally formed word 1\mathbin{\mathtt{<<}}52 only chooses the returned exponent and does not alter this mantissa.
Now suppose the calculated power is positive. The nested round-to-even probe either preserves x or clears its low bit, so its output x_1 is at most x by ModelU64BitAnd. Its low bit is again at most one. The successful addition and right shift by one therefore produce x_3\leq2^{53}. The threshold 2\mathbin{\mathtt{<<}}52=2^{53} and the replacement 1\mathbin{\mathtt{<<}}52=U are legal by ModelU64Shl. In the carrying branch, x_3\geq2^{53} combines with the preceding upper bound to give equality and the code replaces it by U. In the noncarrying branch, x_3<2^{53}. Hence the value immediately before the final mask is always strictly below 2^{53}.
The final mask is the 64-bit complement of U, namely 2^{64}-1-U. Conjunction with it clears bit 52. A value below 2^{53} has no higher set bit, so the masked value is below U; the Lean proof establishes this bitwise by comparing bit 52 and all higher bits. The following saturation test either returns the already-treated zero mantissa infinity pair or this masked record. This exhausts every successful return in both duplicated product blocks and proves the stated uniform bound.

Lean proof


  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
      Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by
    intro α β r f y hr
    cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
  have mask52_bound (x : U64) (hx : x.val < 2 ^ 53) :
      (x &&& 18442240474082181119#u64).val < 2 ^ 52 := by
    change (x.bv &&& (18442240474082181119#u64).bv).toNat < 2 ^ 52
    rw [BitVec.toNat_and]
    apply Nat.lt_of_testBit 52
    · rw [Nat.testBit_land]
      have hmask : (18442240474082181119#u64).val = 18442240474082181119 := by
        rfl
      change (Nat.testBit x.val 52 && Nat.testBit (18442240474082181119#u64).val 52) = false
      rw [hmask]
      norm_num [Nat.testBit, Nat.shiftRight_eq_div_pow]
    · norm_num [Nat.testBit, Nat.shiftRight_eq_div_pow]
    · intro j hj
      have h53 : 53 ≤ j := by omega
      have hpow : 2 ^ 53 ≤ 2 ^ j := Nat.pow_le_pow_right (by omega) h53
      have hland : x.bv.toNat &&& (18442240474082181119#u64).bv.toNat < 2 ^ j :=
        lt_of_le_of_lt Nat.and_le_left (lt_of_lt_of_le hx hpow)
      rw [Nat.testBit_eq_false_of_lt hland]
      rw [Nat.testBit_two_pow_of_ne (by omega)]
  
  have shifted_hi_bound (hi : U64) (i8 : U64) (upperbit shift : I32)
      (h63 : hi >>> 63#i32 = ok i8)
      (hcast : lift (UScalar.hcast .I32 i8) = ok upperbit)
      (hshift : (do
        let a ← upperbit + 64#i32
        let b ← a - 52#i32
        b - 3#i32) = ok shift)
      (mantissa : U64) (hm : hi >>> shift = ok mantissa) :
      mantissa.val < 2 ^ 54 := by
    have hs63 := UScalar.ShiftRight_IScalar_spec hi (63#i32) (by norm_num) (by norm_num)
    rw [h63] at hs63
    have hi8val : i8.val = hi.val / 2 ^ 63 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hs63.1
    have hi_lt : hi.val < 2 ^ 64 := hi.bv.isLt
    have hi8_lt : i8.val < 2 := by
      rw [hi8val]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt ⊢
      exact hi_lt
    have hcastSpec := UScalar.hcast_inBounds_spec .I32 i8 (by
      scalar_tac)
    rw [hcast] at hcastSpec
    have hupper : upperbit.val = (i8.val : Int) := by
      simpa [WP.spec_ok] using hcastSpec
    have hi8_le : i8.val ≤ 1 := by
      omega
    have hu_nonneg : 0 ≤ upperbit.val := by rw [hupper]; omega
    have hu_le : upperbit.val ≤ 1 := by rw [hupper]; exact_mod_cast hi8_le
    have hsval : shift.val = upperbit.val + 9 := by
      cases ha : upperbit + 64#i32 with
      | fail e => simp [ha] at hshift
      | div => simp [ha] at hshift
      | ok a =>
        simp only [ha, bind_tc_ok] at hshift
        cases hb : a - 52#i32 with
        | fail e => simp [hb] at hshift
        | div => simp [hb] at hshift
        | ok b =>
          simp only [hb, bind_tc_ok] at hshift
          have haeq := IScalar.add_equiv upperbit 64#i32
          rw [ha] at haeq
          have hbeq := IScalar.sub_equiv a 52#i32
          rw [hb] at hbeq
          have hseq := IScalar.sub_equiv b 3#i32
          rw [hshift] at hseq
          norm_num at haeq hbeq hseq
          omega
    have hs_nonneg : 0 ≤ shift.val := by omega
    have hs_lt : shift.val < 64 := by omega
    have hmspec := UScalar.ShiftRight_IScalar_spec hi shift hs_nonneg hs_lt
    rw [hm] at hmspec
    have hmval : mantissa.val = hi.val / 2 ^ shift.toNat := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hmspec.1
    have hu : upperbit.val = 0 ∨ upperbit.val = 1 := by omega
    rcases hu with hu | hu
    · have hi_lt63 : hi.val < 2 ^ 63 := by
        have hz : hi.val / 2 ^ 63 = 0 := by omega
        exact (Nat.div_eq_zero_iff).mp hz |>.resolve_left (by norm_num)
      have hshift9 : shift.toNat = 9 := by
        change Int.toNat shift.val = 9
        have hsv : shift.val = 9 := by omega
        rw [hsv]
        rfl
      rw [hshift9] at hmval
      norm_num at hmval ⊢
      rw [hmval]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt63 ⊢
      exact hi_lt63
    · have hshift10 : shift.toNat = 10 := by
        change Int.toNat shift.val = 10
        have hsv : shift.val = 10 := by omega
        rw [hsv]
        rfl
      rw [hshift10] at hmval
      norm_num at hmval ⊢
      rw [hmval]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt ⊢
      exact hi_lt
  
  have subnormal_round_bound (x : U64) (k : I32)
      (x1 bit x2 x3 : U64)
      (hx : x.val < 2 ^ 54)
      (hk1 : 1 ≤ k.val) (hk64 : k.val < 64)
      (h1 : x >>> k = ok x1)
      (hb : lift (x1 &&& 1#u64) = ok bit)
      (h2 : x1 + bit = ok x2)
      (h3 : x2 >>> 1#i32 = ok x3) :
      x3.val ≤ 2 ^ 52 := by
    have hk0 : 0 ≤ k.val := by omega
    have hspec1 := UScalar.ShiftRight_IScalar_spec x k hk0 hk64
    rw [h1] at hspec1
    have hx1 : x1.val = x.val / 2 ^ k.toNat := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec1.1
    have hkNat : 1 ≤ k.toNat := by
      change 1 ≤ Int.toNat k.val
      have hc := Int.toNat_of_nonneg hk0
      omega
    have hpow : 2 ≤ 2 ^ k.toNat := by
      have := Nat.pow_le_pow_right (by omega : 0 < 2) hkNat
      norm_num at this ⊢
      exact this
    have hx1lt : x1.val < 2 ^ 53 := by
      rw [hx1]
      apply (Nat.div_lt_iff_lt_mul (by positivity)).2
      norm_num at hx ⊢
      nlinarith
    have hbval : bit.val ≤ 1 := by
      simp only [lift] at hb
      cases hb
      change (x1.bv &&& (1#u64).bv).toNat ≤ 1
      rw [BitVec.toNat_and]
      change x1.val &&& (1#u64).val ≤ 1
      have hone : (1#u64).val = 1 := by norm_num
      rw [hone]
      exact Nat.and_le_right
    have h2eq := UScalar.add_equiv x1 bit
    rw [h2] at h2eq
    have hx2 : x2.val ≤ 2 ^ 53 := by
      omega
    have hspec3 := UScalar.ShiftRight_IScalar_spec x2 (1#i32) (by norm_num) (by norm_num)
    rw [h3] at hspec3
    have hx3 : x3.val = x2.val / 2 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec3.1
    omega
  
  have normal_round_bound (x x1 bit x2 x3 : U64)
      (hx : x.val < 2 ^ 54) (hx1 : x1.val ≤ x.val)
      (hb : lift (x1 &&& 1#u64) = ok bit)
      (h2 : x1 + bit = ok x2)
      (h3 : x2 >>> 1#i32 = ok x3) :
      x3.val ≤ 2 ^ 53 := by
    have hx1lt : x1.val < 2 ^ 54 := lt_of_le_of_lt hx1 hx
    have hbval : bit.val ≤ 1 := by
      simp only [lift] at hb
      cases hb
      change (x1.bv &&& (1#u64).bv).toNat ≤ 1
      rw [BitVec.toNat_and]
      change x1.val &&& (1#u64).val ≤ 1
      have hone : (1#u64).val = 1 := by norm_num
      rw [hone]
      exact Nat.and_le_right
    have h2eq := UScalar.add_equiv x1 bit
    rw [h2] at h2eq
    have hx2 : x2.val ≤ 2 ^ 54 := by omega
    have hspec3 := UScalar.ShiftRight_IScalar_spec x2 (1#i32) (by norm_num) (by norm_num)
    rw [h3] at hspec3
    have hx3 : x3.val = x2.val / 2 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec3.1
    omega
  
  have adjust_le {F Clause : Type} (traits : num.imp.traits.Float F Clause)
      (lo hi mantissa : U64) (q : I64) (i9 : I32) (i5 : U32) (out : U64)
      (h : (if lo ≤ 1#u64 then do
        let i20 ← traits.MIN_EXPONENT_ROUND_TO_EVEN
        let i21 ← lift (IScalar.cast .I64 i20)
        if q ≥ i21 then
          let i22 ← traits.MAX_EXPONENT_ROUND_TO_EVEN
          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 i5)
              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) = ok out) : out.val ≤ mantissa.val := by
    have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
        Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by
      intro α β r f y hr
      cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
    by_cases hlo : lo ≤ 1#u64
    · simp only [hlo, if_true] at h
      obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
      by_cases hqmin : q ≥ i21
      · simp only [hqmin, if_true] at h
        obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i23, hi23, h⟩ := bind_ok_inv _ _ _ h
        by_cases hqmax : q ≤ i23
        · simp only [hqmax, if_true] at h
          obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
          by_cases hlow : i24 = 1#u64
          · simp only [hlow, if_true] at h
            obtain ⟨i25, hi25, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i26, hi26, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i27, hi27, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i28, hi28, h⟩ := bind_ok_inv _ _ _ h
            by_cases heq : i28 = hi
            · simp only [heq, if_true] at h
              obtain ⟨i29, hi29, h⟩ := bind_ok_inv _ _ _ h
              cases h
              change (mantissa.bv &&& i29.bv).toNat ≤ mantissa.bv.toNat
              rw [BitVec.toNat_and]
              exact Nat.and_le_left
            · simp only [heq, if_false] at h
              cases h
              exact le_rfl
          · simp only [hlow, if_false] at h
            cases h
            exact le_rfl
        · simp only [hqmax, if_false] at h
          cases h
          exact le_rfl
      · simp only [hqmin, if_false] at h
        cases h
        exact le_rfl
    · simp only [hlo, if_false] at h
      cases h
      exact le_rfl
  
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, 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
  have hsigop : (53#u32 - 1#u32) = ok 52#u32 := by rfl
  unfold num.imp.dec2flt.lemire.compute_float at h
  rw [hzero, hinf] at h
  simp only [bind_tc_ok] at h
  rw [hinfFp, herr] at h
  simp only [bind_tc_ok] at h
  by_cases hw : w = 0#u64
  · simp only [hw, if_true] at h
    cases h
    norm_num
  simp only [hw, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def] at h
  rw [hcastSmall] at h
  simp only [bind_tc_ok] at h
  by_cases hqsmall : q < (-342)#i64
  · simp only [hqsmall, if_true] at h
    cases h
    norm_num
  simp only [hqsmall, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def] at h
  rw [hcastLarge] at h
  simp only [bind_tc_ok] at h
  by_cases hqlarge : q > 308#i64
  · simp only [hqlarge, if_true] at h
    cases h
    norm_num
  simp only [hqlarge, if_false] at h
  simp only [lift, bind_tc_ok] at h
  cases hwshift : (w <<< core.num.U64.leading_zeros w) with
  | fail e => simp only [hwshift, bind_tc_fail] at h; contradiction
  | div => simp only [hwshift, bind_tc_div] at h; contradiction
  | ok w1 =>
    simp only [hwshift, bind_tc_ok] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def] at h
    simp only [core.f64.F64.MANTISSA_DIGITS, ModelF64MantissaDigits,
      Binary64ModelDefault] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def] at h
    simp only [bind_tc_ok] at h
    rw [hsigop] at h
    simp only [bind_tc_ok] at h
    obtain ⟨i7, hi7, h⟩ := bind_ok_inv _ _ _ h
    obtain ⟨pair, hpair, h⟩ := bind_ok_inv _ _ _ h
    rcases pair with ⟨lo, hi⟩
    simp only [Aeneas.Std.uncurry_apply_pair] at h
    by_cases hlo : lo = 18446744073709551615#u64
    · simp only [hlo, if_true] at h
      by_cases hqneg : q ≥ (-27)#i64
      · simp only [hqneg, if_true, bind_tc_ok] at h
        by_cases hq55 : q ≤ 55#i64
        · have hbtrue : decide (q ≤ 55#i64) = true := by simpa using hq55
          simp only [hbtrue, if_true, bind_tc_ok] at h
          obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
          let upperbit : I32 := UScalar.hcast .I32 i8
          have hshift : (do
              let a ← upperbit + 64#i32
              let b ← a - 52#i32
              b - 3#i32) = ok i12 := by
            dsimp [upperbit]
            rw [hi9]
            simp only [bind_tc_ok]
            have hi11' : i9 - 52#i32 = ok i11 := by simpa using hi11
            rw [hi11']
            simp only [bind_tc_ok]
            exact hi12
          have hmantBound : mantissa.val < 2 ^ 54 :=
            shifted_hi_bound hi i8 upperbit i12 hi8 rfl hshift mantissa hmantissa
          by_cases hp2 : power2 ≤ 0#i32
          · simp only [hp2, if_true] at h
            obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            by_cases hz : i21 ≥ 64#i32
            · simp only [hz, if_true] at h
              cases h
              norm_num
            · simp only [hz, if_false] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
              cases h
              have hneg := IScalar.tryMk_eq .I32 (-power2.val)
              change IScalar.tryMk .I32 (-power2.val) = ok i20 at hi20
              rw [hi20] at hneg
              have hi20v : i20.val = -power2.val := by
                exact hneg.1
              have hi21v : i21.val = i20.val + 1 := by
                have he := IScalar.add_equiv i20 (1#i32)
                rw [hi21] at he
                exact he.2.1
              have hi22eq : i22 = i21 := by rw [hi21] at hi22; cases hi22; rfl
              subst i22
              have hk1 : 1 ≤ i21.val := by
                change power2.val ≤ 0 at hp2
                omega
              have hk64 : i21.val < 64 := by
                change ¬ i21.val ≥ 64 at hz
                omega
              exact subnormal_round_bound mantissa i21 mantissa1
                (mantissa1 &&& 1#u64) mantissa2 mantissa3 hmantBound
                hk1 hk64 hm1 rfl hm2 hm3
          · simp only [hp2, if_false] at h
            obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            have hm1le : mantissa1.val ≤ mantissa.val := by
              cases hm1
              exact le_rfl
            have hm3bound : mantissa3.val ≤ 2 ^ 53 :=
              normal_round_bound mantissa mantissa1 (mantissa1 &&& 1#u64)
                mantissa2 mantissa3 hmantBound hm1le rfl hm2 hm3
            by_cases hcarry : mantissa3 ≥ i21
            · simp only [hcarry, if_true] at h
              obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
              rcases pair2 with ⟨mantissa4, power21⟩
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa5, hm5, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              obtain ⟨power22, hp22, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              cases hpair2
              by_cases hsat : power21 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                norm_num
              · simp only [hsat, if_false] at h
                cases h
                have hm4 : mantissa4 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hm5
                  cases hm5
                  rfl
                have hi22eq : i22 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hi22
                  cases hi22
                  rfl
                subst mantissa4
                subst i22
                change ((4503599627370496#u64).bv &&&
                  (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
                rw [BitVec.toNat_and]
                exact Nat.and_le_left
            · simp only [hcarry, if_false, bind_tc_ok,
                Aeneas.Std.uncurry_apply_pair] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              by_cases hsat : power2 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                norm_num
              · simp only [hsat, if_false] at h
                cases h
                have hi21eq : i21 = 9007199254740992#u64 := by
                  have hs : (2#u64 <<< 52#u32) =
                      ok 9007199254740992#u64 := by rfl
                  rw [hs] at hi21
                  cases hi21
                  rfl
                have hm3lt : mantissa3.val < 2 ^ 53 := by
                  change ¬ mantissa3.val ≥ i21.val at hcarry
                  rw [hi21eq] at hcarry
                  norm_num at hcarry ⊢
                  omega
                have hi22eq : i22 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hi22
                  cases hi22
                  rfl
                subst i22
                change (mantissa3.bv &&&
                  (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
                rw [BitVec.toNat_and]
                have hnotScalar : (~~~4503599627370496#u64) =
                    18442240474082181119#u64 := by rfl
                change mantissa3.val &&& (~~~4503599627370496#u64).val ≤ 2 ^ 52
                rw [hnotScalar]
                change mantissa3.val &&& 18442240474082181119 ≤ 2 ^ 52
                exact Nat.le_of_lt (mask52_bound mantissa3 hm3lt)
        · have hbfalse : decide (q ≤ 55#i64) = false := by simpa using hq55
          simp only [hbfalse, if_false, bind_tc_ok] at h
          cases h
          norm_num
      · simp only [hqneg, if_false, bind_tc_ok] at h
        cases h
        norm_num
    · simp only [hlo, if_false] at h
      obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
      let upperbit : I32 := UScalar.hcast .I32 i8
      have hshift : (do
          let a ← upperbit + 64#i32
          let b ← a - 52#i32
          b - 3#i32) = ok i12 := by
        dsimp [upperbit]
        rw [hi9]
        simp only [bind_tc_ok]
        have hi11' : i9 - 52#i32 = ok i11 := by simpa using hi11
        rw [hi11']
        simp only [bind_tc_ok]
        exact hi12
      have hmantBound : mantissa.val < 2 ^ 54 :=
        shifted_hi_bound hi i8 upperbit i12 hi8 rfl hshift mantissa hmantissa
      by_cases hp2 : power2 ≤ 0#i32
      · simp only [hp2, if_true] at h
        obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        by_cases hz : i21 ≥ 64#i32
        · simp only [hz, if_true] at h
          cases h
          norm_num
        · simp only [hz, if_false] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
          cases h
          have hneg := IScalar.tryMk_eq .I32 (-power2.val)
          change IScalar.tryMk .I32 (-power2.val) = ok i20 at hi20
          rw [hi20] at hneg
          have hi20v : i20.val = -power2.val := hneg.1
          have hi21v : i21.val = i20.val + 1 := by
            have he := IScalar.add_equiv i20 (1#i32)
            rw [hi21] at he
            exact he.2.1
          have hi22eq : i22 = i21 := by rw [hi21] at hi22; cases hi22; rfl
          subst i22
          have hk1 : 1 ≤ i21.val := by
            change power2.val ≤ 0 at hp2
            omega
          have hk64 : i21.val < 64 := by
            change ¬ i21.val ≥ 64 at hz
            omega
          exact subnormal_round_bound mantissa i21 mantissa1
            (mantissa1 &&& 1#u64) mantissa2 mantissa3 hmantBound
            hk1 hk64 hm1 rfl hm2 hm3
      · simp only [hp2, if_false] at h
        obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        have hm1le : mantissa1.val ≤ mantissa.val := by
          apply adjust_le
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 lo hi mantissa
            q i9 52#u32 mantissa1
          simpa [lift, bind_tc_ok] using hm1
        have hm3bound : mantissa3.val ≤ 2 ^ 53 :=
          normal_round_bound mantissa mantissa1 (mantissa1 &&& 1#u64)
            mantissa2 mantissa3 hmantBound hm1le rfl hm2 hm3
        by_cases hcarry : mantissa3 ≥ i21
        · simp only [hcarry, if_true] at h
          obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
          rcases pair2 with ⟨mantissa4, power21⟩
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa5, hm5, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          obtain ⟨power22, hp22, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          cases hpair2
          by_cases hsat : power21 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            norm_num
          · simp only [hsat, if_false] at h
            cases h
            have hm4 : mantissa4 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hm5
              cases hm5
              rfl
            have hi22eq : i22 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hi22
              cases hi22
              rfl
            subst mantissa4
            subst i22
            change ((4503599627370496#u64).bv &&&
              (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
            rw [BitVec.toNat_and]
            exact Nat.and_le_left
        · simp only [hcarry, if_false, bind_tc_ok,
            Aeneas.Std.uncurry_apply_pair] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          by_cases hsat : power2 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            norm_num
          · simp only [hsat, if_false] at h
            cases h
            have hi21eq : i21 = 9007199254740992#u64 := by
              have hs : (2#u64 <<< 52#u32) =
                  ok 9007199254740992#u64 := by rfl
              rw [hs] at hi21
              cases hi21
              rfl
            have hm3lt : mantissa3.val < 2 ^ 53 := by
              change ¬ mantissa3.val ≥ i21.val at hcarry
              rw [hi21eq] at hcarry
              norm_num at hcarry ⊢
              omega
            have hi22eq : i22 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hi22
              cases hi22
              rfl
            subst i22
            change (mantissa3.bv &&&
              (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
            rw [BitVec.toNat_and]
            have hnotScalar : (~~~4503599627370496#u64) =
                18442240474082181119#u64 := by rfl
            change mantissa3.val &&& (~~~4503599627370496#u64).val ≤ 2 ^ 52
            rw [hnotScalar]
            change mantissa3.val &&& 18442240474082181119 ≤ 2 ^ 52
            exact Nat.le_of_lt (mask52_bound mantissa3 hm3lt)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ParserExponentConstants
import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Tablet.ModelU64Shr
import Tablet.num_imp_dec2flt_common_BiasedFp_zero_pow2
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_lemire_compute_product_approx

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option pp.universes false
set_option maxHeartbeats 500000

namespace dec2flt_model

-- [TABLET NODE: ParserComputeFloatMantissaBound]
theorem ParserComputeFloatMantissaBound (q : Std.I64) (w : Std.U64)
    (b : num.imp.dec2flt.common.BiasedFp)
    (h : num.imp.dec2flt.lemire.compute_float
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok b) :
    b.m.val ≤ 2 ^ 52 := by
-- BODY
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
      Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by
    intro α β r f y hr
    cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
  have mask52_bound (x : U64) (hx : x.val < 2 ^ 53) :
      (x &&& 18442240474082181119#u64).val < 2 ^ 52 := by
    change (x.bv &&& (18442240474082181119#u64).bv).toNat < 2 ^ 52
    rw [BitVec.toNat_and]
    apply Nat.lt_of_testBit 52
    · rw [Nat.testBit_land]
      have hmask : (18442240474082181119#u64).val = 18442240474082181119 := by
        rfl
      change (Nat.testBit x.val 52 && Nat.testBit (18442240474082181119#u64).val 52) = false
      rw [hmask]
      norm_num [Nat.testBit, Nat.shiftRight_eq_div_pow]
    · norm_num [Nat.testBit, Nat.shiftRight_eq_div_pow]
    · intro j hj
      have h53 : 53 ≤ j := by omega
      have hpow : 2 ^ 53 ≤ 2 ^ j := Nat.pow_le_pow_right (by omega) h53
      have hland : x.bv.toNat &&& (18442240474082181119#u64).bv.toNat < 2 ^ j :=
        lt_of_le_of_lt Nat.and_le_left (lt_of_lt_of_le hx hpow)
      rw [Nat.testBit_eq_false_of_lt hland]
      rw [Nat.testBit_two_pow_of_ne (by omega)]
  
  have shifted_hi_bound (hi : U64) (i8 : U64) (upperbit shift : I32)
      (h63 : hi >>> 63#i32 = ok i8)
      (hcast : lift (UScalar.hcast .I32 i8) = ok upperbit)
      (hshift : (do
        let a ← upperbit + 64#i32
        let b ← a - 52#i32
        b - 3#i32) = ok shift)
      (mantissa : U64) (hm : hi >>> shift = ok mantissa) :
      mantissa.val < 2 ^ 54 := by
    have hs63 := UScalar.ShiftRight_IScalar_spec hi (63#i32) (by norm_num) (by norm_num)
    rw [h63] at hs63
    have hi8val : i8.val = hi.val / 2 ^ 63 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hs63.1
    have hi_lt : hi.val < 2 ^ 64 := hi.bv.isLt
    have hi8_lt : i8.val < 2 := by
      rw [hi8val]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt ⊢
      exact hi_lt
    have hcastSpec := UScalar.hcast_inBounds_spec .I32 i8 (by
      scalar_tac)
    rw [hcast] at hcastSpec
    have hupper : upperbit.val = (i8.val : Int) := by
      simpa [WP.spec_ok] using hcastSpec
    have hi8_le : i8.val ≤ 1 := by
      omega
    have hu_nonneg : 0 ≤ upperbit.val := by rw [hupper]; omega
    have hu_le : upperbit.val ≤ 1 := by rw [hupper]; exact_mod_cast hi8_le
    have hsval : shift.val = upperbit.val + 9 := by
      cases ha : upperbit + 64#i32 with
      | fail e => simp [ha] at hshift
      | div => simp [ha] at hshift
      | ok a =>
        simp only [ha, bind_tc_ok] at hshift
        cases hb : a - 52#i32 with
        | fail e => simp [hb] at hshift
        | div => simp [hb] at hshift
        | ok b =>
          simp only [hb, bind_tc_ok] at hshift
          have haeq := IScalar.add_equiv upperbit 64#i32
          rw [ha] at haeq
          have hbeq := IScalar.sub_equiv a 52#i32
          rw [hb] at hbeq
          have hseq := IScalar.sub_equiv b 3#i32
          rw [hshift] at hseq
          norm_num at haeq hbeq hseq
          omega
    have hs_nonneg : 0 ≤ shift.val := by omega
    have hs_lt : shift.val < 64 := by omega
    have hmspec := UScalar.ShiftRight_IScalar_spec hi shift hs_nonneg hs_lt
    rw [hm] at hmspec
    have hmval : mantissa.val = hi.val / 2 ^ shift.toNat := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hmspec.1
    have hu : upperbit.val = 0 ∨ upperbit.val = 1 := by omega
    rcases hu with hu | hu
    · have hi_lt63 : hi.val < 2 ^ 63 := by
        have hz : hi.val / 2 ^ 63 = 0 := by omega
        exact (Nat.div_eq_zero_iff).mp hz |>.resolve_left (by norm_num)
      have hshift9 : shift.toNat = 9 := by
        change Int.toNat shift.val = 9
        have hsv : shift.val = 9 := by omega
        rw [hsv]
        rfl
      rw [hshift9] at hmval
      norm_num at hmval ⊢
      rw [hmval]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt63 ⊢
      exact hi_lt63
    · have hshift10 : shift.toNat = 10 := by
        change Int.toNat shift.val = 10
        have hsv : shift.val = 10 := by omega
        rw [hsv]
        rfl
      rw [hshift10] at hmval
      norm_num at hmval ⊢
      rw [hmval]
      apply (Nat.div_lt_iff_lt_mul (by norm_num)).2
      norm_num at hi_lt ⊢
      exact hi_lt
  
  have subnormal_round_bound (x : U64) (k : I32)
      (x1 bit x2 x3 : U64)
      (hx : x.val < 2 ^ 54)
      (hk1 : 1 ≤ k.val) (hk64 : k.val < 64)
      (h1 : x >>> k = ok x1)
      (hb : lift (x1 &&& 1#u64) = ok bit)
      (h2 : x1 + bit = ok x2)
      (h3 : x2 >>> 1#i32 = ok x3) :
      x3.val ≤ 2 ^ 52 := by
    have hk0 : 0 ≤ k.val := by omega
    have hspec1 := UScalar.ShiftRight_IScalar_spec x k hk0 hk64
    rw [h1] at hspec1
    have hx1 : x1.val = x.val / 2 ^ k.toNat := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec1.1
    have hkNat : 1 ≤ k.toNat := by
      change 1 ≤ Int.toNat k.val
      have hc := Int.toNat_of_nonneg hk0
      omega
    have hpow : 2 ≤ 2 ^ k.toNat := by
      have := Nat.pow_le_pow_right (by omega : 0 < 2) hkNat
      norm_num at this ⊢
      exact this
    have hx1lt : x1.val < 2 ^ 53 := by
      rw [hx1]
      apply (Nat.div_lt_iff_lt_mul (by positivity)).2
      norm_num at hx ⊢
      nlinarith
    have hbval : bit.val ≤ 1 := by
      simp only [lift] at hb
      cases hb
      change (x1.bv &&& (1#u64).bv).toNat ≤ 1
      rw [BitVec.toNat_and]
      change x1.val &&& (1#u64).val ≤ 1
      have hone : (1#u64).val = 1 := by norm_num
      rw [hone]
      exact Nat.and_le_right
    have h2eq := UScalar.add_equiv x1 bit
    rw [h2] at h2eq
    have hx2 : x2.val ≤ 2 ^ 53 := by
      omega
    have hspec3 := UScalar.ShiftRight_IScalar_spec x2 (1#i32) (by norm_num) (by norm_num)
    rw [h3] at hspec3
    have hx3 : x3.val = x2.val / 2 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec3.1
    omega
  
  have normal_round_bound (x x1 bit x2 x3 : U64)
      (hx : x.val < 2 ^ 54) (hx1 : x1.val ≤ x.val)
      (hb : lift (x1 &&& 1#u64) = ok bit)
      (h2 : x1 + bit = ok x2)
      (h3 : x2 >>> 1#i32 = ok x3) :
      x3.val ≤ 2 ^ 53 := by
    have hx1lt : x1.val < 2 ^ 54 := lt_of_le_of_lt hx1 hx
    have hbval : bit.val ≤ 1 := by
      simp only [lift] at hb
      cases hb
      change (x1.bv &&& (1#u64).bv).toNat ≤ 1
      rw [BitVec.toNat_and]
      change x1.val &&& (1#u64).val ≤ 1
      have hone : (1#u64).val = 1 := by norm_num
      rw [hone]
      exact Nat.and_le_right
    have h2eq := UScalar.add_equiv x1 bit
    rw [h2] at h2eq
    have hx2 : x2.val ≤ 2 ^ 54 := by omega
    have hspec3 := UScalar.ShiftRight_IScalar_spec x2 (1#i32) (by norm_num) (by norm_num)
    rw [h3] at hspec3
    have hx3 : x3.val = x2.val / 2 := by
      simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow] using hspec3.1
    omega
  
  have adjust_le {F Clause : Type} (traits : num.imp.traits.Float F Clause)
      (lo hi mantissa : U64) (q : I64) (i9 : I32) (i5 : U32) (out : U64)
      (h : (if lo ≤ 1#u64 then do
        let i20 ← traits.MIN_EXPONENT_ROUND_TO_EVEN
        let i21 ← lift (IScalar.cast .I64 i20)
        if q ≥ i21 then
          let i22 ← traits.MAX_EXPONENT_ROUND_TO_EVEN
          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 i5)
              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) = ok out) : out.val ≤ mantissa.val := by
    have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
        Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by
      intro α β r f y hr
      cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
    by_cases hlo : lo ≤ 1#u64
    · simp only [hlo, if_true] at h
      obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
      by_cases hqmin : q ≥ i21
      · simp only [hqmin, if_true] at h
        obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i23, hi23, h⟩ := bind_ok_inv _ _ _ h
        by_cases hqmax : q ≤ i23
        · simp only [hqmax, if_true] at h
          obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
          by_cases hlow : i24 = 1#u64
          · simp only [hlow, if_true] at h
            obtain ⟨i25, hi25, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i26, hi26, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i27, hi27, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i28, hi28, h⟩ := bind_ok_inv _ _ _ h
            by_cases heq : i28 = hi
            · simp only [heq, if_true] at h
              obtain ⟨i29, hi29, h⟩ := bind_ok_inv _ _ _ h
              cases h
              change (mantissa.bv &&& i29.bv).toNat ≤ mantissa.bv.toNat
              rw [BitVec.toNat_and]
              exact Nat.and_le_left
            · simp only [heq, if_false] at h
              cases h
              exact le_rfl
          · simp only [hlow, if_false] at h
            cases h
            exact le_rfl
        · simp only [hqmax, if_false] at h
          cases h
          exact le_rfl
      · simp only [hqmin, if_false] at h
        cases h
        exact le_rfl
    · simp only [hlo, if_false] at h
      cases h
      exact le_rfl
  
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, 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
  have hsigop : (53#u32 - 1#u32) = ok 52#u32 := by rfl
  unfold num.imp.dec2flt.lemire.compute_float at h
  rw [hzero, hinf] at h
  simp only [bind_tc_ok] at h
  rw [hinfFp, herr] at h
  simp only [bind_tc_ok] at h
  by_cases hw : w = 0#u64
  · simp only [hw, if_true] at h
    cases h
    norm_num
  simp only [hw, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def] at h
  rw [hcastSmall] at h
  simp only [bind_tc_ok] at h
  by_cases hqsmall : q < (-342)#i64
  · simp only [hqsmall, if_true] at h
    cases h
    norm_num
  simp only [hqsmall, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def] at h
  rw [hcastLarge] at h
  simp only [bind_tc_ok] at h
  by_cases hqlarge : q > 308#i64
  · simp only [hqlarge, if_true] at h
    cases h
    norm_num
  simp only [hqlarge, if_false] at h
  simp only [lift, bind_tc_ok] at h
  cases hwshift : (w <<< core.num.U64.leading_zeros w) with
  | fail e => simp only [hwshift, bind_tc_fail] at h; contradiction
  | div => simp only [hwshift, bind_tc_div] at h; contradiction
  | ok w1 =>
    simp only [hwshift, bind_tc_ok] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def] at h
    simp only [core.f64.F64.MANTISSA_DIGITS, ModelF64MantissaDigits,
      Binary64ModelDefault] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def] at h
    simp only [bind_tc_ok] at h
    rw [hsigop] at h
    simp only [bind_tc_ok] at h
    obtain ⟨i7, hi7, h⟩ := bind_ok_inv _ _ _ h
    obtain ⟨pair, hpair, h⟩ := bind_ok_inv _ _ _ h
    rcases pair with ⟨lo, hi⟩
    simp only [Aeneas.Std.uncurry_apply_pair] at h
    by_cases hlo : lo = 18446744073709551615#u64
    · simp only [hlo, if_true] at h
      by_cases hqneg : q ≥ (-27)#i64
      · simp only [hqneg, if_true, bind_tc_ok] at h
        by_cases hq55 : q ≤ 55#i64
        · have hbtrue : decide (q ≤ 55#i64) = true := by simpa using hq55
          simp only [hbtrue, if_true, bind_tc_ok] at h
          obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
          let upperbit : I32 := UScalar.hcast .I32 i8
          have hshift : (do
              let a ← upperbit + 64#i32
              let b ← a - 52#i32
              b - 3#i32) = ok i12 := by
            dsimp [upperbit]
            rw [hi9]
            simp only [bind_tc_ok]
            have hi11' : i9 - 52#i32 = ok i11 := by simpa using hi11
            rw [hi11']
            simp only [bind_tc_ok]
            exact hi12
          have hmantBound : mantissa.val < 2 ^ 54 :=
            shifted_hi_bound hi i8 upperbit i12 hi8 rfl hshift mantissa hmantissa
          by_cases hp2 : power2 ≤ 0#i32
          · simp only [hp2, if_true] at h
            obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            by_cases hz : i21 ≥ 64#i32
            · simp only [hz, if_true] at h
              cases h
              norm_num
            · simp only [hz, if_false] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
              cases h
              have hneg := IScalar.tryMk_eq .I32 (-power2.val)
              change IScalar.tryMk .I32 (-power2.val) = ok i20 at hi20
              rw [hi20] at hneg
              have hi20v : i20.val = -power2.val := by
                exact hneg.1
              have hi21v : i21.val = i20.val + 1 := by
                have he := IScalar.add_equiv i20 (1#i32)
                rw [hi21] at he
                exact he.2.1
              have hi22eq : i22 = i21 := by rw [hi21] at hi22; cases hi22; rfl
              subst i22
              have hk1 : 1 ≤ i21.val := by
                change power2.val ≤ 0 at hp2
                omega
              have hk64 : i21.val < 64 := by
                change ¬ i21.val ≥ 64 at hz
                omega
              exact subnormal_round_bound mantissa i21 mantissa1
                (mantissa1 &&& 1#u64) mantissa2 mantissa3 hmantBound
                hk1 hk64 hm1 rfl hm2 hm3
          · simp only [hp2, if_false] at h
            obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            have hm1le : mantissa1.val ≤ mantissa.val := by
              cases hm1
              exact le_rfl
            have hm3bound : mantissa3.val ≤ 2 ^ 53 :=
              normal_round_bound mantissa mantissa1 (mantissa1 &&& 1#u64)
                mantissa2 mantissa3 hmantBound hm1le rfl hm2 hm3
            by_cases hcarry : mantissa3 ≥ i21
            · simp only [hcarry, if_true] at h
              obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
              rcases pair2 with ⟨mantissa4, power21⟩
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa5, hm5, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              obtain ⟨power22, hp22, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              cases hpair2
              by_cases hsat : power21 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                norm_num
              · simp only [hsat, if_false] at h
                cases h
                have hm4 : mantissa4 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hm5
                  cases hm5
                  rfl
                have hi22eq : i22 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hi22
                  cases hi22
                  rfl
                subst mantissa4
                subst i22
                change ((4503599627370496#u64).bv &&&
                  (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
                rw [BitVec.toNat_and]
                exact Nat.and_le_left
            · simp only [hcarry, if_false, bind_tc_ok,
                Aeneas.Std.uncurry_apply_pair] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              by_cases hsat : power2 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                norm_num
              · simp only [hsat, if_false] at h
                cases h
                have hi21eq : i21 = 9007199254740992#u64 := by
                  have hs : (2#u64 <<< 52#u32) =
                      ok 9007199254740992#u64 := by rfl
                  rw [hs] at hi21
                  cases hi21
                  rfl
                have hm3lt : mantissa3.val < 2 ^ 53 := by
                  change ¬ mantissa3.val ≥ i21.val at hcarry
                  rw [hi21eq] at hcarry
                  norm_num at hcarry ⊢
                  omega
                have hi22eq : i22 = 4503599627370496#u64 := by
                  have hs : (1#u64 <<< 52#u32) =
                      ok 4503599627370496#u64 := by rfl
                  rw [hs] at hi22
                  cases hi22
                  rfl
                subst i22
                change (mantissa3.bv &&&
                  (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
                rw [BitVec.toNat_and]
                have hnotScalar : (~~~4503599627370496#u64) =
                    18442240474082181119#u64 := by rfl
                change mantissa3.val &&& (~~~4503599627370496#u64).val ≤ 2 ^ 52
                rw [hnotScalar]
                change mantissa3.val &&& 18442240474082181119 ≤ 2 ^ 52
                exact Nat.le_of_lt (mask52_bound mantissa3 hm3lt)
        · have hbfalse : decide (q ≤ 55#i64) = false := by simpa using hq55
          simp only [hbfalse, if_false, bind_tc_ok] at h
          cases h
          norm_num
      · simp only [hqneg, if_false, bind_tc_ok] at h
        cases h
        norm_num
    · simp only [hlo, if_false] at h
      obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
      let upperbit : I32 := UScalar.hcast .I32 i8
      have hshift : (do
          let a ← upperbit + 64#i32
          let b ← a - 52#i32
          b - 3#i32) = ok i12 := by
        dsimp [upperbit]
        rw [hi9]
        simp only [bind_tc_ok]
        have hi11' : i9 - 52#i32 = ok i11 := by simpa using hi11
        rw [hi11']
        simp only [bind_tc_ok]
        exact hi12
      have hmantBound : mantissa.val < 2 ^ 54 :=
        shifted_hi_bound hi i8 upperbit i12 hi8 rfl hshift mantissa hmantissa
      by_cases hp2 : power2 ≤ 0#i32
      · simp only [hp2, if_true] at h
        obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        by_cases hz : i21 ≥ 64#i32
        · simp only [hz, if_true] at h
          cases h
          norm_num
        · simp only [hz, if_false] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
          cases h
          have hneg := IScalar.tryMk_eq .I32 (-power2.val)
          change IScalar.tryMk .I32 (-power2.val) = ok i20 at hi20
          rw [hi20] at hneg
          have hi20v : i20.val = -power2.val := hneg.1
          have hi21v : i21.val = i20.val + 1 := by
            have he := IScalar.add_equiv i20 (1#i32)
            rw [hi21] at he
            exact he.2.1
          have hi22eq : i22 = i21 := by rw [hi21] at hi22; cases hi22; rfl
          subst i22
          have hk1 : 1 ≤ i21.val := by
            change power2.val ≤ 0 at hp2
            omega
          have hk64 : i21.val < 64 := by
            change ¬ i21.val ≥ 64 at hz
            omega
          exact subnormal_round_bound mantissa i21 mantissa1
            (mantissa1 &&& 1#u64) mantissa2 mantissa3 hmantBound
            hk1 hk64 hm1 rfl hm2 hm3
      · simp only [hp2, if_false] at h
        obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        have hm1le : mantissa1.val ≤ mantissa.val := by
          apply adjust_le
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 lo hi mantissa
            q i9 52#u32 mantissa1
          simpa [lift, bind_tc_ok] using hm1
        have hm3bound : mantissa3.val ≤ 2 ^ 53 :=
          normal_round_bound mantissa mantissa1 (mantissa1 &&& 1#u64)
            mantissa2 mantissa3 hmantBound hm1le rfl hm2 hm3
        by_cases hcarry : mantissa3 ≥ i21
        · simp only [hcarry, if_true] at h
          obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
          rcases pair2 with ⟨mantissa4, power21⟩
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa5, hm5, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          obtain ⟨power22, hp22, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          cases hpair2
          by_cases hsat : power21 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            norm_num
          · simp only [hsat, if_false] at h
            cases h
            have hm4 : mantissa4 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hm5
              cases hm5
              rfl
            have hi22eq : i22 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hi22
              cases hi22
              rfl
            subst mantissa4
            subst i22
            change ((4503599627370496#u64).bv &&&
              (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
            rw [BitVec.toNat_and]
            exact Nat.and_le_left
        · simp only [hcarry, if_false, bind_tc_ok,
            Aeneas.Std.uncurry_apply_pair] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          by_cases hsat : power2 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            norm_num
          · simp only [hsat, if_false] at h
            cases h
            have hi21eq : i21 = 9007199254740992#u64 := by
              have hs : (2#u64 <<< 52#u32) =
                  ok 9007199254740992#u64 := by rfl
              rw [hs] at hi21
              cases hi21
              rfl
            have hm3lt : mantissa3.val < 2 ^ 53 := by
              change ¬ mantissa3.val ≥ i21.val at hcarry
              rw [hi21eq] at hcarry
              norm_num at hcarry ⊢
              omega
            have hi22eq : i22 = 4503599627370496#u64 := by
              have hs : (1#u64 <<< 52#u32) =
                  ok 4503599627370496#u64 := by rfl
              rw [hs] at hi22
              cases hi22
              rfl
            subst i22
            change (mantissa3.bv &&&
              (~~~4503599627370496#u64).bv).toNat ≤ 2 ^ 52
            rw [BitVec.toNat_and]
            have hnotScalar : (~~~4503599627370496#u64) =
                18442240474082181119#u64 := by rfl
            change mantissa3.val &&& (~~~4503599627370496#u64).val ≤ 2 ^ 52
            rw [hnotScalar]
            change mantissa3.val &&& 18442240474082181119 ≤ 2 ^ 52
            exact Nat.le_of_lt (mask52_bound mantissa3 hm3lt)

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (87)
More program dependencies (45)
Standard-library dependencies (39)

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 83 entries
Technical checking records
Local closure summary
{
  "node": "ParserComputeFloatMantissaBound",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "38414f742e82b53dbc404364e5e74ccabd51fd0812936459e1305679f0147832",
  "active_statement_hash": "692031ef154b6cf475794e720b9f2325eca44dbd25cc1d88bab39c5ce687c330",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-133",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64ModelDefault": "c722a2c00805b545",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS": "cfedcf26d6dae341",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN": "2532cbe447dc07c7",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN": "8a9e3dfa7e9aa5ec",
    "ParserExponentConstants": "c1391b605f3cfebb",
    "num_imp_dec2flt_lemire_compute_float": "7539bc2a998f5c1c",
    "num_imp_traits_Float_EXP_BIAS_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_BITS_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_EXP_MIN_default": "39417be39e20ce5d",
    "num_imp_traits_Float_EXP_SAT_default": "9ab622d85ef04d94",
    "num_imp_traits_Float_SIG_BITS_default": "9ab622d85ef04d94"
  },
  "strict_definition_deps": {
    "Binary64Model": "c43614ff2149ecf2",
    "Binary64ModelDefault": "dc548d1e27696ba4",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_BITS": "d9fd91641cb78c5e",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_LARGEST_POWER_OF_TEN": "e23324c94fa5d1b5",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MAX_EXPONENT_ROUND_TO_EVEN": "3dbd3f15a918cb2c",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_MIN_EXPONENT_ROUND_TO_EVEN": "e508b987ade7bf71",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SMALLEST_POWER_OF_TEN": "0c461c0c4f5aa880",
    "Preamble": "1f712bede6a5dae6",
    "core_num_FpCategory": "f500de642a5d5148",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_common_BiasedFp_zero_pow2": "0f99ae12c6707137",
    "num_imp_dec2flt_lemire_compute_float": "39d89e3f0a8a9d4c",
    "num_imp_dec2flt_lemire_compute_product_approx": "af91b9cbef7779f3",
    "num_imp_dec2flt_lemire_power": "56b7c3bd69ca3c6a",
    "num_imp_traits_Float": "4442e69083aa891d"
  }
}
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:5df0218d27fcbb0f48a3f2de21dbeb2020088d349fd0c3954807767ec4eb2a01",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}