P873_T1_IntegerNormalRounds

Lean checked

Statement

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

Lemma

For every real x, natural number G, signed 32-bit scalars p,p_f, unsigned 64-bit scalars S,S_f,O, biased pair fp, and binary64 carrier t, use \operatorname{val} for scalar values and put
\begin{gathered} U=2^{52},\quad T=2047U,\quad H=2^{1024}-2^{970},\quad P=\operatorname{val}(p),\quad F=\operatorname{val}(p_f),\\ a=\lfloor G/2\rfloor,\quad \delta=2^{P-1076},\quad u=2^{P-1075},\quad L=P_{\mathbb N}U+(a-U),\\ \tau\ \Longleftrightarrow\ (x=G\delta\ \land\ G\bmod4=1),\qquad c\ \Longleftrightarrow\ (G\bmod2=1\ \land\ \neg\tau). \end{gathered}
Here P_{\mathbb N}=\max(P,0) viewed as a natural number, and subtraction in L is natural-number subtraction. Assume all of the following:
\begin{gathered} x>0,\qquad P>0,\qquad 2^{53}\le G<2^{54},\qquad \operatorname{val}(O)=U,\\ \bigl(\operatorname{val}(S)=2^{53}\ \land\ S_f=O\ \land\ F=P+1\bigr) \ \lor\ \bigl(\operatorname{val}(S)<2^{53}\ \land\ S_f=S\ \land\ p_f=p\bigr),\\ \operatorname{val}(S)= \begin{cases}a+1&c,\\a&\neg c,\end{cases} \qquad G\delta\le x<(G+1)\delta,\qquad au\le x\le(a+1)u,\\ \neg c\ \Longrightarrow\ |x-au|\le|x-(a+1)u|,\qquad c\ \Longrightarrow\ |x-(a+1)u|\le|x-au|,\\ \neg c\ \Longrightarrow\ \bigl(|x-au|=|x-(a+1)u|\ \Longrightarrow\ L\bmod2=0\bigr),\\ c\ \Longrightarrow\ \bigl(|x-(a+1)u|=|x-au|\ \Longrightarrow\ (L+1)\bmod2=0\bigr). \end{gathered}
Assume also that t is the unrestricted assembly of fp, in the sense of DecimalGoalVocabularyDefault, and that the final pair is
fp= \begin{cases} (0,2047),&F\ge2047,\\ (S_f\mathbin{\mathtt{\&}}\mathbin{\mathtt{\sim}}O,\ p_f),&F<2047. \end{cases}
The complement and conjunction here are the extracted 64-bit scalar operations, and the displayed pairs list mantissa followed by biased exponent. Then \texttt{roundsMagnitude}(x,t) holds, with exactly the meaning of DecimalGoalVocabularyDefault.

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.ModelU64BitAnd
import Tablet.F64

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1500000
set_option maxRecDepth 1000000

namespace dec2flt_model

-- [TABLET NODE: P873_T1_IntegerNormalRounds]
theorem P873_T1_IntegerNormalRounds
    (x : Real) (G : Nat) (p : Std.I32)
    (S64 Sf oneU : Std.U64) (pf : Std.I32)
    (fp : num.imp.dec2flt.common.BiasedFp) (target : F64)
    (hxpos : 0 < x) (hpPos : 0 < p.val)
    (hGLower : 2 ^ 53 ≤ G) (hGUpper : G < 2 ^ 54)
    (honeUval : oneU.val = 2 ^ 52)
    (hSfPf :
      (S64.val = 2 ^ 53 ∧ Sf = oneU ∧ pf.val = p.val + 1) ∨
      (S64.val < 2 ^ 53 ∧ Sf = S64 ∧ pf = p))
    (htargetAssembly : DecimalGoalVocabularyDefault.assembledWord fp target)
    (hfinal :
      (if pf ≥ 2047#i32 then
        ok { m := 0#u64, p_biased := 2047#i32 }
      else ok { m := Sf &&& ~~~oneU, p_biased := pf }) = ok fp) :
    let lowSig : Nat := G / 2
    let deltaR : Real := (2 : Real) ^ (p.val - 1076)
    let ulpR : Real := (2 : Real) ^ (p.val - 1075)
    let tieLow : Prop := x = (G : Real) * deltaR ∧ G % 4 = 1
    let chooseHigh : Prop := G % 2 = 1 ∧ ¬ tieLow
    let lowBits : Nat := p.val.toNat * 2 ^ 52 + (lowSig - 2 ^ 52)
    S64.val = (if chooseHigh then lowSig + 1 else lowSig) →
    (G : Real) * deltaR ≤ x →
    x < ((G + 1 : Nat) : Real) * deltaR →
    (lowSig : Real) * ulpR ≤ x →
    x ≤ ((lowSig + 1 : Nat) : Real) * ulpR →
    (¬ chooseHigh →
      abs (x - (lowSig : Real) * ulpR) ≤
        abs (x - ((lowSig + 1 : Nat) : Real) * ulpR)) →
    (chooseHigh →
      abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) ≤
        abs (x - (lowSig : Real) * ulpR)) →
    (¬ chooseHigh →
      abs (x - (lowSig : Real) * ulpR) =
        abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) →
      lowBits % 2 = 0) →
    (chooseHigh →
      abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) =
        abs (x - (lowSig : Real) * ulpR) →
      (lowBits + 1) % 2 = 0) →
    DecimalGoalVocabularyDefault.roundsMagnitude x target := 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

The bounds on G give U\le a<2U, and the selection formula gives U\le\operatorname{val}(S)\le2U. In either carry case F>0. Write V(n) for the finite-magnitude decoder in Binary64FiniteMagnitudeStrictMono. That lemma says that V is strictly increasing below T.
First record two bit-field calculations used in finite assembly. For U\le z<2U, clearing bit 52 gives z-U: the remainder of the 64-bit clearing mask modulo 2^{53} is U-1, so its conjunction with z is z\bmod U=z-U. This applies to S_f whenever F<2047, since the carry case has S_f=O and the noncarry case has S_f=S<2U. Also, if b<U, then b and eU have disjoint set bits, so b\mathbin{\mathtt{OR}}eU=b+eU. For 1\le F\le2046 the exponent shift is smaller than 2^{64} and the cast remainder is F itself. Consequently unrestricted assembly has value
\operatorname{val}(t)=F_{\mathbb N}U+ (\operatorname{val}(S_f)-U). \tag{1}
In the noncarry case, substituting the selection formula into (1) gives L+1 if c, and L otherwise. In the carry case, \operatorname{val}(S)=2U forces c and a=2U-1; hence (1) is (P_{\mathbb N}+1)U=L+1. Thus, in all finite cases,
\operatorname{val}(t)= \begin{cases}L+1&c,\\L&\neg c,\end{cases} \quad P_{\mathbb N}\le2046,\quad L+1\le T. \tag{2}
Now suppose F\ge2047. The final pair and unrestricted assembly give \operatorname{val}(t)=T. If there was a carry, then P\ge2046, c holds, and a=2U-1. Since G is odd under c, G=4U-1=2^{54}-1. Thus \delta\ge2^{970} and
H=(2^{54}-1)2^{970}\le G\delta\le x.
Without a carry, P=F\ge2047, so \delta\ge2^{971} and G\ge2^{53} imply x\ge2^{1024}\ge H. Together with x>0 these are precisely the infinity requirements of \texttt{roundsMagnitude}.
For the rest assume F<2047, so (1)--(2) hold and the assembled word is strictly below T. If L+1=T, the bounds on a force P_{\mathbb N}=2046 and a=2U-1. Finiteness and (2) force \neg c. If G were odd, \neg c would force \tau; but then G=2a+1=2^{54}-1 has remainder 3 modulo 4, contradicting \tau. Hence G=2a is even. Here \delta=2^{970}, and the strict cell upper bound gives
x<(G+1)\delta=H.
Direct decoding gives V(L)=a2^{971}=au\le x. Every distinct finite carrier has a smaller word than L=T-1 and hence, by strict monotonicity, a value strictly below V(L). Its distance to x is strictly larger than the distance of V(L) to x. This proves every finite comparison, and there is no distance equality requiring parity.
It remains that L+1<T. The two words decode to
V(L)=au,\qquad V(L+1)=(a+1)u. \tag{3}
For P_{\mathbb N}\le2045 this is Binary64AdjacentNormalValues. For P_{\mathbb N}=2046, L+1<T implies a+1<2U; division and remainder by U give the exponent 2046 and fractions a-U and a+1-U directly, which proves (3) at spacing u=2^{971}.
In this interior case x<H. Indeed, if P_{\mathbb N}\le2045, then u\le2^{970} and x\le(a+1)u\le2^{53}2^{970}=2^{1023}<H. If P_{\mathbb N}=2046, then a+1\le2^{53}-1 and
x\le(a+1)2^{971} \le(2^{53}-1)2^{971} =2^{1024}-2^{971}<H.
Finally, (3) and the hypotheses place x between the two adjacent decoded values. Formula (2) and the assumed local distance inequalities select a nearest endpoint, with even raw word in the equality case. For completeness, an arbitrary finite competing word below L has decoded value strictly below V(L), and a competing word above L+1 has decoded value strictly above V(L+1), by strict monotonicity. A word on the opposite side from the selected endpoint is at least as far from x as the opposite endpoint, so the assumed local comparison gives the required global comparison. A word beyond the selected endpoint on its own side is strictly farther, since x lies in the cell. If a global comparison is an equality, the comparison with the opposite endpoint must therefore also be an equality; the corresponding assumed parity implication makes the selected word even. Thus all quantified finite comparisons and their equality clauses hold. Together with 0<x, \operatorname{val}(t)<T, and x<H, these are exactly the remaining clauses of \texttt{roundsMagnitude}(x,t).

Lean proof


  dsimp only
  let lowSig : Nat := G / 2
  let deltaR : Real := (2 : Real) ^ (p.val - 1076)
  let ulpR : Real := (2 : Real) ^ (p.val - 1075)
  let tieLow : Prop := x = (G : Real) * deltaR ∧ G % 4 = 1
  let chooseHigh : Prop := G % 2 = 1 ∧ ¬ tieLow
  let lowBits : Nat := p.val.toNat * 2 ^ 52 + (lowSig - 2 ^ 52)
  intro hSchoice hxcellLo' hxcellHi' hLowX hXHigh hLowNear hHighNear
    hTieLowEven hTieHighEven
  have hLowSigLower : 2 ^ 52 ≤ lowSig := by
    dsimp only [lowSig]
    omega
  have hLowSigUpper : lowSig < 2 ^ 53 := by
    dsimp only [lowSig]
    omega
  have hSBounds : 2 ^ 52 ≤ S64.val ∧ S64.val ≤ 2 ^ 53 := by
    rw [hSchoice]
    split <;> omega
  have hGdecomp : G = 2 * lowSig + G % 2 := by
    dsimp only [lowSig]
    omega
  have hrem2 : G % 2 ≤ 1 := Nat.le_pred_of_lt (Nat.mod_lt _ (by omega))
  let pNat : Nat := p.val.toNat
  have hpNatCast : (pNat : Int) = p.val := Int.toNat_of_nonneg hpPos.le
  let selectedBits : Nat := if chooseHigh then lowBits + 1 else lowBits
  have clearHidden (z one : Std.U64) (hone : one.val = 2 ^ 52)
      (hlo : 2 ^ 52 ≤ z.val) (hhi : z.val < 2 ^ 53) :
      (z &&& ~~~one).val = z.val - 2 ^ 52 := by as_aux_lemma =>
    have clear52Nat (n : Nat) (hnlo : 2 ^ 52 ≤ n) (hnhi : n < 2 ^ 53) :
        n &&& (2 ^ 64 - 1 - 2 ^ 52) = n - 2 ^ 52 := by
      let mask := 2 ^ 64 - 1 - 2 ^ 52
      have hlandlt : n &&& mask < 2 ^ 53 :=
        lt_of_le_of_lt Nat.and_le_left hnhi
      have hm : mask % 2 ^ 53 = 2 ^ 52 - 1 := by norm_num [mask]
      have hc := Nat.and_mod_two_pow (a := n) (b := mask) (n := 53)
      rw [Nat.mod_eq_of_lt hlandlt, Nat.mod_eq_of_lt hnhi, hm,
        Nat.and_two_pow_sub_one_eq_mod] at hc
      have hloDiv : 1 ≤ n / 2 ^ 52 :=
        (Nat.le_div_iff_mul_le (by positivity)).2 (by simpa using hnlo)
      have hhiDiv : n / 2 ^ 52 < 2 :=
        (Nat.div_lt_iff_lt_mul (by positivity)).2 (by simpa using hnhi)
      have hdiv : n / 2 ^ 52 = 1 := by omega
      have hd := Nat.div_add_mod n (2 ^ 52)
      have hnmod : n % 2 ^ 52 = n - 2 ^ 52 := by omega
      exact hc.trans hnmod
    have oneEq : one = 4503599627370496#u64 := by
      apply UScalar.eq_of_val_eq
      norm_num at hone ⊢
      exact hone
    rw [oneEq]
    change (z.bv &&& ~~~(BitVec.ofNat 64 4503599627370496)).toNat =
      z.bv.toNat - 2 ^ 52
    rw [BitVec.toNat_and, BitVec.toNat_not, BitVec.toNat_ofNat]
    norm_num
    exact clear52Nat z.val hlo hhi
  have fieldOr (a ef : Nat) (ha : a < 2 ^ 52)
      (hef : ef * 2 ^ 52 < 2 ^ 64) :
      a ||| (ef * 2 ^ 52) = a + ef * 2 ^ 52 := by as_aux_lemma =>
    have hland : a &&& (ef * 2 ^ 52) = 0 := by
      apply Nat.eq_of_testBit_eq
      intro j
      rw [Nat.testBit_land, Nat.testBit_mul_two_pow]
      by_cases hj : j < 52
      · simp [hj]
      · have haj : a.testBit j = false := by
          apply Nat.testBit_eq_false_of_lt
          exact lt_of_lt_of_le ha (Nat.pow_le_pow_right (by omega) (by omega))
        simp [haj]
    let av : BitVec 64 := BitVec.ofNat 64 a
    let ev : BitVec 64 := BitVec.ofNat 64 (ef * 2 ^ 52)
    have hlandBV : av &&& ev = 0#64 := by
      apply BitVec.eq_of_toNat_eq
      simp only [av, ev, BitVec.toNat_and, BitVec.toNat_ofNat]
      rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef,
        hland]
    calc
      a ||| (ef * 2 ^ 52) = (av ||| ev).toNat := by
        simp only [av, ev, BitVec.toNat_or, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
      _ = (av + ev).toNat := by
        rw [BitVec.add_eq_or_of_and_eq_zero av ev hlandBV]
      _ = a + ef * 2 ^ 52 := by
        rw [BitVec.toNat_add_of_and_eq_zero hlandBV]
        simp only [av, ev, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
  let unit : Nat := 2 ^ 52
  let top : Nat := 2047 * unit
  let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
  let value : Nat → Real := fun n =>
    let field := n / unit
    let fraction := n % unit
    if field = 0 then (fraction : Real) / (2 : Real) ^ (1074 : Nat)
    else if 1075 ≤ field then
      ((unit + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
    else ((unit + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
  have hvalueMono : ∀ a b : Nat, a < b → b < top → value a < value b := by
    simpa only [unit, top, value] using Binary64FiniteMagnitudeStrictMono
  have cellRounds : ∀ (t : F64) (low bits : Nat),
      t.val = bits → low + 1 < top →
      value low ≤ x → x ≤ value (low + 1) →
      (bits = low ∨ bits = low + 1) →
      (bits = low →
        abs (x - value low) ≤ abs (x - value (low + 1))) →
      (bits = low + 1 →
        abs (x - value (low + 1)) ≤ abs (x - value low)) →
      (bits = low →
        abs (x - value low) = abs (x - value (low + 1)) → bits % 2 = 0) →
      (bits = low + 1 →
        abs (x - value (low + 1)) = abs (x - value low) → bits % 2 = 0) →
      x < threshold →
      DecimalGoalVocabularyDefault.roundsMagnitude x t := by as_aux_lemma =>
    intro t low bits ht hlow hxl hxh hchoice hnearLow hnearHigh
      htieLow htieHigh hthreshold
    dsimp only [DecimalGoalVocabularyDefault]
    change 0 ≤ x ∧ t.val ≤ top ∧
      (if t.val = top then threshold ≤ x else x < threshold ∧
        ∀ v : F64, v.val < top → v ≠ t →
          abs (x - value t.val) ≤ abs (x - value v.val) ∧
          (abs (x - value t.val) = abs (x - value v.val) → t.val % 2 = 0))
    have hbits : bits < top := by rcases hchoice with h | h <;> omega
    have htBound : t.val < top := by omega
    refine ⟨hxpos.le, htBound.le, ?_⟩
    rw [if_neg (by omega)]
    refine ⟨hthreshold, ?_⟩
    intro v hv hne
    have hvne : v.val ≠ bits := by
      intro heq
      apply hne
      apply UScalar.eq_of_val_eq
      omega
    rcases hchoice with hbitsLow | hbitsHigh
    · have htlow : t.val = low := ht.trans hbitsLow
      rw [htlow]
      have hnear := hnearLow hbitsLow
      have htie := htieLow hbitsLow
      by_cases hvBelow : v.val < low
      · have hvValue : value v.val < value low :=
          hvalueMono _ _ hvBelow (by omega)
        have hvx : value v.val < x := hvValue.trans_le hxl
        have hstrict : abs (x - value low) < abs (x - value v.val) := by
          rw [abs_of_nonneg (sub_nonneg.mpr hxl),
            abs_of_nonneg (sub_nonneg.mpr hvx.le)]
          linarith
        exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
      · have hvAbove : low < v.val := by omega
        have hhighLe : low + 1 ≤ v.val := by omega
        have hhighValue : value (low + 1) ≤ value v.val := by
          rcases hhighLe.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (hvalueMono _ _ hlt hv).le
        have hxv : x ≤ value v.val := hxh.trans hhighValue
        have hhighDist : abs (x - value (low + 1)) ≤
            abs (x - value v.val) := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxh),
            abs_of_nonpos (sub_nonpos.mpr hxv)]
          linarith
        constructor
        · exact hnear.trans hhighDist
        · intro heq
          have hreverse : abs (x - value (low + 1)) ≤
              abs (x - value low) := hhighDist.trans_eq heq.symm
          have heqCell := le_antisymm hnear hreverse
          simpa only [hbitsLow] using htie heqCell
    · have hthigh : t.val = low + 1 := ht.trans hbitsHigh
      rw [hthigh]
      have hnear := hnearHigh hbitsHigh
      have htie := htieHigh hbitsHigh
      by_cases hvAbove : low + 1 < v.val
      · have hvValue : value (low + 1) < value v.val :=
          hvalueMono _ _ hvAbove hv
        have hxv : x < value v.val := hxh.trans_lt hvValue
        have hstrict : abs (x - value (low + 1)) < abs (x - value v.val) := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxh),
            abs_of_nonpos (sub_nonpos.mpr hxv.le)]
          linarith
        exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
      · have hvBelow : v.val < low + 1 := by omega
        have hvLow : v.val ≤ low := by omega
        have hvValue : value v.val ≤ value low := by
          rcases hvLow.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (hvalueMono _ _ hlt (by omega)).le
        have hvx : value v.val ≤ x := hvValue.trans hxl
        have hlowDist : abs (x - value low) ≤ abs (x - value v.val) := by
          rw [abs_of_nonneg (sub_nonneg.mpr hxl),
            abs_of_nonneg (sub_nonneg.mpr hvx)]
          linarith
        constructor
        · exact hnear.trans hlowDist
        · intro heq
          have hreverse : abs (x - value low) ≤
              abs (x - value (low + 1)) := hlowDist.trans_eq heq.symm
          have heqCell := le_antisymm hnear hreverse
          simpa only [hbitsHigh] using htie heqCell
  have hfpResult :
      (pf ≥ 2047#i32 → fp = { m := 0#u64, p_biased := 2047#i32 }) ∧
      (¬ pf ≥ 2047#i32 →
        fp = { m := Sf &&& ~~~oneU, p_biased := pf }) := by as_aux_lemma =>
    constructor
    · intro hover
      have hr := hfinal
      simp only [hover, if_true] at hr
      exact (Result.ok.inj hr).symm
    · intro hover
      have hr := hfinal
      simp only [hover, if_false] at hr
      exact (Result.ok.inj hr).symm
  by_cases hover : pf ≥ 2047#i32
  · as_aux_lemma =>
    have hfpEq : fp = { m := 0#u64, p_biased := 2047#i32 } := hfpResult.1 hover
    have htargetTop : target.val = top := by
      subst fp
      dsimp only [DecimalGoalVocabularyDefault] at htargetAssembly
      dsimp only [top, unit]
      norm_num [Int.toNat] at htargetAssembly ⊢
      exact htargetAssembly
    have hpfGe : 2047 ≤ pf.val := by
      simpa only [IScalar.le_equiv] using hover
    have hthreshold : threshold ≤ x := by
      rcases hSfPf with hcarryCase | hplainCase
      · have hpGe : 2046 ≤ p.val := by omega
        have hchoose : chooseHigh := by
          by_contra hn
          rw [hSchoice, if_neg hn] at hcarryCase
          omega
        have hlowMax : lowSig = 2 ^ 53 - 1 := by
          rw [hSchoice, if_pos hchoose] at hcarryCase
          omega
        have hGmax : G = 2 ^ 54 - 1 := by
          have hodd := hchoose.1
          omega
        have hdelta970 : (2 : Real) ^ (970 : Nat) ≤ deltaR := by
          dsimp only [deltaR]
          rw [← zpow_natCast]
          exact zpow_le_zpow_right₀ (by norm_num) (by omega)
        have hbase : threshold = ((2 ^ 54 - 1 : Nat) : Real) *
            (2 : Real) ^ (970 : Nat) := by
          dsimp only [threshold]
          rw [show (2 : Real) ^ (1024 : Nat) =
              (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) by
            rw [← pow_add]]
          rw [Nat.cast_sub (by norm_num : 1 ≤ (2 : Nat) ^ 54),
            Nat.cast_pow, Nat.cast_ofNat, Nat.cast_one, sub_mul, one_mul]
        rw [hbase, ← hGmax]
        exact (mul_le_mul_of_nonneg_left hdelta970 (by positivity)).trans hxcellLo'
      · have hpGe : 2047 ≤ p.val := by
          rcases hplainCase with ⟨_, _, heq⟩
          have := congrArg IScalar.val heq
          omega
        have hdelta971 : (2 : Real) ^ (971 : Nat) ≤ deltaR := by
          dsimp only [deltaR]
          rw [← zpow_natCast]
          exact zpow_le_zpow_right₀ (by norm_num) (by omega)
        calc
          threshold ≤ (2 : Real) ^ (1024 : Nat) := by
            dsimp only [threshold]
            exact sub_le_self _ (by positivity)
          _ = (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) := by
            rw [← pow_add]
          _ ≤ (G : Real) * deltaR := by
            exact mul_le_mul (by exact_mod_cast hGLower) hdelta971
              (by positivity) (by positivity)
          _ ≤ x := hxcellLo'
    dsimp only [DecimalGoalVocabularyDefault]
    change 0 ≤ x ∧ target.val ≤ top ∧
      (if target.val = top then threshold ≤ x else _)
    rw [htargetTop, if_pos rfl]
    exact ⟨hxpos.le, le_rfl, hthreshold⟩
  · have hfiniteData :
        target.val = pf.val.toNat * unit + (Sf.val - unit) ∧
        target.val = selectedBits ∧ Sf.val < 2 ^ 53 ∧
        pf.val.toNat ≤ 2046 ∧ pNat ≤ 2046 ∧
        lowBits + 1 ≤ top := by as_aux_lemma =>
      have hfpEq : fp = { m := Sf &&& ~~~oneU, p_biased := pf } :=
        hfpResult.2 hover
      have hpfLt : pf.val < 2047 := by
        have hn : ¬2047 ≤ pf.val := by
          simpa only [IScalar.le_equiv] using hover
        omega
      have hpfPos : 0 < pf.val := by
        rcases hSfPf with hcarryCase | hplainCase
        · omega
        · have heq := congrArg IScalar.val hplainCase.2.2
          omega
      have hSfRange : 2 ^ 52 ≤ Sf.val ∧ Sf.val < 2 ^ 53 := by as_aux_lemma =>
        rcases hSfPf with hcarryCase | hplainCase
        · rw [hcarryCase.2.1, honeUval]
          norm_num
        · rw [hplainCase.2.1]
          exact ⟨hSBounds.1, hplainCase.1⟩
      have hmVal : (Sf &&& ~~~oneU).val = Sf.val - 2 ^ 52 :=
        clearHidden Sf oneU honeUval hSfRange.1 hSfRange.2
      have hpfEmod : pf.val.emod (2 ^ 64) = pf.val := by
        apply Int.emod_eq_of_lt hpfPos.le
        omega
      have hpfNatCast : (pf.val.toNat : Int) = pf.val :=
        Int.toNat_of_nonneg hpfPos.le
      have hpfNatUpper : pf.val.toNat ≤ 2046 := by
        rw [Int.toNat_le]
        omega
      have htargetFormula : target.val =
        pf.val.toNat * unit + (Sf.val - unit) := by
        have ha := htargetAssembly
        dsimp only [DecimalGoalVocabularyDefault] at ha
        rw [hfpEq, hpfEmod] at ha
        have hmBound : Sf.val - unit < 2 ^ 52 := by
          dsimp only [unit]
          omega
        have heBound : pf.val.toNat * 2 ^ 52 < 2 ^ 64 := by omega
        have hor := fieldOr (Sf.val - unit) pf.val.toNat hmBound heBound
        dsimp only [unit] at ha hor ⊢
        rw [Nat.mod_eq_of_lt heBound, hmVal] at ha
        exact ha.trans (hor.trans (Nat.add_comm _ _))
      have htargetSelected : target.val = selectedBits := by as_aux_lemma =>
        rw [htargetFormula]
        rcases hSfPf with hcarryCase | hplainCase
        · have hchoose : chooseHigh := by
            by_contra hn
            rw [hSchoice, if_neg hn] at hcarryCase
            omega
          have hlowMax : lowSig = 2 ^ 53 - 1 := by
            rw [hSchoice, if_pos hchoose] at hcarryCase
            omega
          rw [hcarryCase.2.1, honeUval]
          have hpfEq : pf.val.toNat = pNat + 1 := by
            apply Nat.cast_injective (R := Int)
            rw [hpfNatCast, Nat.cast_add, Nat.cast_one, hpNatCast]
            omega
          rw [hpfEq]
          dsimp only [selectedBits, lowBits, unit]
          rw [if_pos hchoose]
          omega
        · have hpfEq : pf.val.toNat = pNat := by
            apply Nat.cast_injective (R := Int)
            rw [hpfNatCast, hpNatCast]
            have heq := congrArg IScalar.val hplainCase.2.2
            omega
          rw [hplainCase.2.1, hSchoice, hpfEq]
          dsimp only [selectedBits, lowBits, unit]
          split <;> omega
      have hpNatUpper : pNat ≤ 2046 := by as_aux_lemma =>
        rcases hSfPf with hcarryCase | hplainCase
        · have hi : (pNat : Int) ≤ 2046 := by
            rw [hpNatCast]
            omega
          exact_mod_cast hi
        · have heq := congrArg IScalar.val hplainCase.2.2
          have hi : (pNat : Int) ≤ 2046 := by
            rw [hpNatCast]
            omega
          exact_mod_cast hi
      have hlowSuccLe : lowBits + 1 ≤ top := by
        dsimp only [lowBits, top, unit]
        omega
      exact ⟨htargetFormula, htargetSelected, hSfRange.2, hpfNatUpper, hpNatUpper,
        hlowSuccLe⟩
    rcases hfiniteData with
      ⟨htargetFormula, htargetSelected, hSfUpper, hpfNatUpper, hpNatUpper,
        hlowSuccLe⟩
    by_cases hboundary : lowBits + 1 = top
    · as_aux_lemma =>
      have hpEq : pNat = 2046 := by
        dsimp only [lowBits, top, unit] at hboundary
        omega
      have hlowMax : lowSig = 2 ^ 53 - 1 := by
        dsimp only [lowBits, top, unit] at hboundary
        omega
      have htargetLt : target.val < top := by
        rw [htargetFormula]
        dsimp only [top, unit]
        omega
      have hselectedLt : selectedBits < top := by omega
      have hnotChoose : ¬chooseHigh := by
        intro hc
        dsimp only [selectedBits] at hselectedLt
        rw [if_pos hc, hboundary] at hselectedLt
        omega
      have htargetLow : target.val = lowBits := by
        rw [htargetSelected]
        dsimp only [selectedBits]
        rw [if_neg hnotChoose]
      have hGremZero : G % 2 = 0 := by
        by_contra hnzero
        have hm := Nat.mod_lt G (by omega : 0 < 2)
        have hodd : G % 2 = 1 := by omega
        have ht : tieLow := by
          by_contra hnt
          exact hnotChoose ⟨hodd, hnt⟩
        have hG : G = 2 * lowSig + 1 := by omega
        dsimp only [tieLow] at ht
        rw [hG, hlowMax] at ht
        norm_num at ht
      have hGEq : G = 2 * lowSig := by omega
      have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
        dsimp only [ulpR]
        rw [← hpNatCast, hpEq]
        norm_num
        exact zpow_natCast (2 : Real) 971
      have hrem : lowSig - unit < unit := by
        dsimp only [unit]
        omega
      have hlowDiv : lowBits / unit = pNat := by
        dsimp only [lowBits]
        rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by
          dsimp only [unit]
          positivity), Nat.div_eq_of_lt hrem, Nat.add_zero]
      have hlowMod : lowBits % unit = lowSig - unit := by
        dsimp only [lowBits]
        rw [Nat.mul_comm pNat unit, Nat.mul_add_mod,
          Nat.mod_eq_of_lt hrem]
      have hvalueLowEq : value lowBits = (lowSig : Real) * ulpR := by
        dsimp only [value]
        rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega),
          hulp2046]
        have hsum : unit + (lowSig - unit) = lowSig := by
          dsimp only [unit]
          omega
        rw [hsum, hpEq]
      have hvalueLow : value lowBits ≤ x := by
        rw [hvalueLowEq]
        exact hLowX
      have hdelta2046 : deltaR = (2 : Real) ^ (970 : Nat) := by
        dsimp only [deltaR]
        rw [← hpNatCast, hpEq]
        norm_num
        exact zpow_natCast (2 : Real) 970
      have hbelow : x < threshold := by
        have htopCell : ((G + 1 : Nat) : Real) * deltaR = threshold := by
          rw [hGEq, hlowMax, hdelta2046]
          dsimp only [threshold]
          have heq : (2 : Real) ^ (1024 : Nat) =
              (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
            rw [← pow_add]
          rw [heq]
          have hcoeff : ((2 * (2 ^ 53 - 1) + 1 : Nat) : Real) =
              (2 : Real) ^ (54 : Nat) - 1 := by norm_num
          rw [hcoeff, sub_mul, one_mul]
        exact hxcellHi'.trans_eq htopCell
      dsimp only [DecimalGoalVocabularyDefault]
      change 0 ≤ x ∧ target.val ≤ top ∧
        (if target.val = top then threshold ≤ x else x < threshold ∧
          ∀ v : F64, v.val < top → v ≠ target →
            abs (x - value target.val) ≤ abs (x - value v.val) ∧
            (abs (x - value target.val) = abs (x - value v.val) →
              target.val % 2 = 0))
      refine ⟨hxpos.le, htargetLt.le, ?_⟩
      rw [if_neg (by omega)]
      refine ⟨hbelow, ?_⟩
      intro v hv hne
      have hvne : v.val ≠ lowBits := by
        intro heq
        apply hne
        apply UScalar.eq_of_val_eq
        omega
      have hvlt : v.val < lowBits := by
        dsimp only [top, unit, lowBits] at hv hboundary ⊢
        omega
      have hvValue : value v.val < value lowBits :=
        hvalueMono _ _ hvlt (by omega)
      have hvx : value v.val < x := hvValue.trans_le hvalueLow
      have hstrict : abs (x - value lowBits) < abs (x - value v.val) := by
        rw [abs_of_nonneg (sub_nonneg.mpr hvalueLow),
          abs_of_nonneg (sub_nonneg.mpr hvx.le)]
        linarith
      rw [htargetLow]
      exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
    · as_aux_lemma =>
      have hlowTop : lowBits + 1 < top := by omega
      have hAdjacent :
          value lowBits = (lowSig : Real) * ulpR ∧
          value (lowBits + 1) = ((lowSig + 1 : Nat) : Real) * ulpR := by
        by_cases hpSmall : pNat ≤ 2045
        · simpa only [unit, lowBits, value, ulpR, hpNatCast] using
            (Binary64AdjacentNormalValues pNat lowSig
              (by dsimp only [pNat]; omega) hpSmall hLowSigLower hLowSigUpper)
        · have hpEq : pNat = 2046 := by omega
          have hsigSucc : lowSig + 1 < 2 ^ 53 := by
            dsimp only [lowBits, top, unit] at hlowTop
            omega
          have hrem : lowSig - unit < unit := by
            dsimp only [unit]
            omega
          have hremSucc : lowSig + 1 - unit < unit := by
            refine (Nat.sub_lt_iff_lt_add ?_).2 ?_
            · exact (by simpa only [unit] using
                hLowSigLower.trans (Nat.le_add_right lowSig 1))
            · norm_num [unit] at hsigSucc ⊢
              exact hsigSucc
          have hlowDiv : lowBits / unit = pNat := by
            dsimp only [lowBits]
            rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by
              dsimp only [unit]
              positivity)]
            rw [Nat.div_eq_of_lt hrem, Nat.add_zero]
          have hlowMod : lowBits % unit = lowSig - unit := by
            dsimp only [lowBits]
            rw [Nat.mul_comm pNat unit, Nat.mul_add_mod,
              Nat.mod_eq_of_lt hrem]
          have hhighEq : lowBits + 1 =
              pNat * unit + (lowSig + 1 - unit) := by
            dsimp only [lowBits, unit]
            omega
          have hhighDiv : (lowBits + 1) / unit = pNat := by
            rw [hhighEq, Nat.mul_comm pNat unit,
              Nat.mul_add_div (by dsimp only [unit]; positivity),
              Nat.div_eq_of_lt hremSucc, Nat.add_zero]
          have hhighMod : (lowBits + 1) % unit = lowSig + 1 - unit := by
            rw [hhighEq, Nat.mul_comm pNat unit, Nat.mul_add_mod,
              Nat.mod_eq_of_lt hremSucc]
          have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
            dsimp only [ulpR]
            rw [← hpNatCast, hpEq]
            norm_num
            exact zpow_natCast (2 : Real) 971
          constructor
          · dsimp only [value]
            rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega),
              hulp2046]
            have hsum : unit + (lowSig - unit) = lowSig := by
              dsimp only [unit]
              omega
            rw [hsum]
            rw [hpEq]
          · dsimp only [value]
            rw [hhighDiv, hhighMod, if_neg (by omega), if_pos (by omega),
              hulp2046]
            have hsum : unit + (lowSig + 1 - unit) = lowSig + 1 := by
              dsimp only [unit]
              omega
            rw [hsum]
            rw [hpEq]
      have hvalueLow : value lowBits ≤ x := by
        rw [hAdjacent.1]
        exact hLowX
      have hvalueHigh : x ≤ value (lowBits + 1) := by
        rw [hAdjacent.2]
        exact hXHigh
      have hchoice : selectedBits = lowBits ∨ selectedBits = lowBits + 1 := by
        dsimp only [selectedBits]
        split <;> simp
      have hbelow : x < threshold := by
        by_cases hpSmall : pNat ≤ 2045
        · have hulpBound : ulpR ≤ (2 : Real) ^ (970 : Nat) := by
            dsimp only [ulpR]
            rw [← zpow_natCast]
            apply zpow_le_zpow_right₀ (by norm_num)
            rw [← hpNatCast]
            have hi : (pNat : Int) ≤ 2045 := by exact_mod_cast hpSmall
            omega
          have hpow1023 : (2 : Real) ^ (1023 : Nat) < threshold := by
            have hp : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) :=
              pow_lt_pow_right₀ (by norm_num) (by omega)
            have heq : (2 : Real) ^ (1024 : Nat) =
                2 * (2 : Real) ^ (1023 : Nat) := by
              rw [show 1024 = 1023 + 1 by omega, pow_succ]
              ring
            dsimp only [threshold]
            rw [heq]
            rw [show 2 * (2 : Real) ^ (1023 : Nat) -
                (2 : Real) ^ (970 : Nat) =
                (2 : Real) ^ (1023 : Nat) +
                  ((2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat)) by ring]
            exact lt_add_of_pos_right _ (sub_pos.mpr hp)
          calc
            x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh
            _ ≤ (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (970 : Nat) := by
              apply mul_le_mul
              · exact_mod_cast (show lowSig + 1 ≤ 2 ^ 53 by omega)
              · exact hulpBound
              · dsimp only [ulpR]
                exact (zpow_pos (by norm_num) _).le
              · positivity
            _ = (2 : Real) ^ (1023 : Nat) := by rw [← pow_add]
            _ < threshold := hpow1023
        · have hpEq : pNat = 2046 := by omega
          have hulpEq2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
            dsimp only [ulpR]
            rw [← hpNatCast, hpEq]
            norm_num
            exact zpow_natCast (2 : Real) 971
          have hsigBound : lowSig + 1 ≤ 2 ^ 53 - 1 := by
            dsimp only [lowBits, top, unit] at hlowTop
            omega
          have hmaxBelow : ((2 ^ 53 - 1 : Nat) : Real) *
              (2 : Real) ^ (971 : Nat) < threshold := by
            have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity
            have heq1024 : (2 : Real) ^ (1024 : Nat) =
                (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
              rw [← pow_add]
            have heq971 : (2 : Real) ^ (971 : Nat) =
                2 * (2 : Real) ^ (970 : Nat) := by
              rw [show 971 = 970 + 1 by omega, pow_succ]
              ring
            dsimp only [threshold]
            rw [heq1024, heq971]
            push_cast
            have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by
              norm_num
            rw [h54]
            calc
              (9007199254740991 : Real) * (2 * (2 : Real) ^ (970 : Nat)) =
                  18014398509481982 * (2 : Real) ^ (970 : Nat) := by ring
              _ < 18014398509481983 * (2 : Real) ^ (970 : Nat) :=
                mul_lt_mul_of_pos_right (by norm_num) hp
              _ = 18014398509481984 * (2 : Real) ^ (970 : Nat) -
                  (2 : Real) ^ (970 : Nat) := by ring
          calc
            x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh
            _ ≤ ((2 ^ 53 - 1 : Nat) : Real) *
                (2 : Real) ^ (971 : Nat) := by
              rw [hulpEq2046]
              exact mul_le_mul_of_nonneg_right (by exact_mod_cast hsigBound)
                (by positivity)
            _ < threshold := hmaxBelow
      apply cellRounds target lowBits selectedBits htargetSelected hlowTop
        hvalueLow hvalueHigh hchoice
      · intro hsel
        rw [hAdjacent.1, hAdjacent.2]
        exact hLowNear (by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_pos hc] at hsel
          omega)
      · intro hsel
        rw [hAdjacent.1, hAdjacent.2]
        exact hHighNear (by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_neg hc] at hsel
          omega)
      · intro hsel heq
        rw [hAdjacent.1, hAdjacent.2] at heq
        have hc : ¬chooseHigh := by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_pos hc] at hsel
          omega
        rw [hsel]
        exact hTieLowEven hc heq
      · intro hsel heq
        rw [hAdjacent.1, hAdjacent.2] at heq
        have hc : chooseHigh := by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_neg hc] at hsel
          omega
        rw [hsel]
        exact hTieHighEven hc heq
      · exact hbelow

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.ModelU64BitAnd
import Tablet.F64

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1500000
set_option maxRecDepth 1000000

namespace dec2flt_model

-- [TABLET NODE: P873_T1_IntegerNormalRounds]
theorem P873_T1_IntegerNormalRounds
    (x : Real) (G : Nat) (p : Std.I32)
    (S64 Sf oneU : Std.U64) (pf : Std.I32)
    (fp : num.imp.dec2flt.common.BiasedFp) (target : F64)
    (hxpos : 0 < x) (hpPos : 0 < p.val)
    (hGLower : 2 ^ 53 ≤ G) (hGUpper : G < 2 ^ 54)
    (honeUval : oneU.val = 2 ^ 52)
    (hSfPf :
      (S64.val = 2 ^ 53 ∧ Sf = oneU ∧ pf.val = p.val + 1) ∨
      (S64.val < 2 ^ 53 ∧ Sf = S64 ∧ pf = p))
    (htargetAssembly : DecimalGoalVocabularyDefault.assembledWord fp target)
    (hfinal :
      (if pf ≥ 2047#i32 then
        ok { m := 0#u64, p_biased := 2047#i32 }
      else ok { m := Sf &&& ~~~oneU, p_biased := pf }) = ok fp) :
    let lowSig : Nat := G / 2
    let deltaR : Real := (2 : Real) ^ (p.val - 1076)
    let ulpR : Real := (2 : Real) ^ (p.val - 1075)
    let tieLow : Prop := x = (G : Real) * deltaR ∧ G % 4 = 1
    let chooseHigh : Prop := G % 2 = 1 ∧ ¬ tieLow
    let lowBits : Nat := p.val.toNat * 2 ^ 52 + (lowSig - 2 ^ 52)
    S64.val = (if chooseHigh then lowSig + 1 else lowSig) →
    (G : Real) * deltaR ≤ x →
    x < ((G + 1 : Nat) : Real) * deltaR →
    (lowSig : Real) * ulpR ≤ x →
    x ≤ ((lowSig + 1 : Nat) : Real) * ulpR →
    (¬ chooseHigh →
      abs (x - (lowSig : Real) * ulpR) ≤
        abs (x - ((lowSig + 1 : Nat) : Real) * ulpR)) →
    (chooseHigh →
      abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) ≤
        abs (x - (lowSig : Real) * ulpR)) →
    (¬ chooseHigh →
      abs (x - (lowSig : Real) * ulpR) =
        abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) →
      lowBits % 2 = 0) →
    (chooseHigh →
      abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) =
        abs (x - (lowSig : Real) * ulpR) →
      (lowBits + 1) % 2 = 0) →
    DecimalGoalVocabularyDefault.roundsMagnitude x target := by
-- BODY
  dsimp only
  let lowSig : Nat := G / 2
  let deltaR : Real := (2 : Real) ^ (p.val - 1076)
  let ulpR : Real := (2 : Real) ^ (p.val - 1075)
  let tieLow : Prop := x = (G : Real) * deltaR ∧ G % 4 = 1
  let chooseHigh : Prop := G % 2 = 1 ∧ ¬ tieLow
  let lowBits : Nat := p.val.toNat * 2 ^ 52 + (lowSig - 2 ^ 52)
  intro hSchoice hxcellLo' hxcellHi' hLowX hXHigh hLowNear hHighNear
    hTieLowEven hTieHighEven
  have hLowSigLower : 2 ^ 52 ≤ lowSig := by
    dsimp only [lowSig]
    omega
  have hLowSigUpper : lowSig < 2 ^ 53 := by
    dsimp only [lowSig]
    omega
  have hSBounds : 2 ^ 52 ≤ S64.val ∧ S64.val ≤ 2 ^ 53 := by
    rw [hSchoice]
    split <;> omega
  have hGdecomp : G = 2 * lowSig + G % 2 := by
    dsimp only [lowSig]
    omega
  have hrem2 : G % 2 ≤ 1 := Nat.le_pred_of_lt (Nat.mod_lt _ (by omega))
  let pNat : Nat := p.val.toNat
  have hpNatCast : (pNat : Int) = p.val := Int.toNat_of_nonneg hpPos.le
  let selectedBits : Nat := if chooseHigh then lowBits + 1 else lowBits
  have clearHidden (z one : Std.U64) (hone : one.val = 2 ^ 52)
      (hlo : 2 ^ 52 ≤ z.val) (hhi : z.val < 2 ^ 53) :
      (z &&& ~~~one).val = z.val - 2 ^ 52 := by as_aux_lemma =>
    have clear52Nat (n : Nat) (hnlo : 2 ^ 52 ≤ n) (hnhi : n < 2 ^ 53) :
        n &&& (2 ^ 64 - 1 - 2 ^ 52) = n - 2 ^ 52 := by
      let mask := 2 ^ 64 - 1 - 2 ^ 52
      have hlandlt : n &&& mask < 2 ^ 53 :=
        lt_of_le_of_lt Nat.and_le_left hnhi
      have hm : mask % 2 ^ 53 = 2 ^ 52 - 1 := by norm_num [mask]
      have hc := Nat.and_mod_two_pow (a := n) (b := mask) (n := 53)
      rw [Nat.mod_eq_of_lt hlandlt, Nat.mod_eq_of_lt hnhi, hm,
        Nat.and_two_pow_sub_one_eq_mod] at hc
      have hloDiv : 1 ≤ n / 2 ^ 52 :=
        (Nat.le_div_iff_mul_le (by positivity)).2 (by simpa using hnlo)
      have hhiDiv : n / 2 ^ 52 < 2 :=
        (Nat.div_lt_iff_lt_mul (by positivity)).2 (by simpa using hnhi)
      have hdiv : n / 2 ^ 52 = 1 := by omega
      have hd := Nat.div_add_mod n (2 ^ 52)
      have hnmod : n % 2 ^ 52 = n - 2 ^ 52 := by omega
      exact hc.trans hnmod
    have oneEq : one = 4503599627370496#u64 := by
      apply UScalar.eq_of_val_eq
      norm_num at hone ⊢
      exact hone
    rw [oneEq]
    change (z.bv &&& ~~~(BitVec.ofNat 64 4503599627370496)).toNat =
      z.bv.toNat - 2 ^ 52
    rw [BitVec.toNat_and, BitVec.toNat_not, BitVec.toNat_ofNat]
    norm_num
    exact clear52Nat z.val hlo hhi
  have fieldOr (a ef : Nat) (ha : a < 2 ^ 52)
      (hef : ef * 2 ^ 52 < 2 ^ 64) :
      a ||| (ef * 2 ^ 52) = a + ef * 2 ^ 52 := by as_aux_lemma =>
    have hland : a &&& (ef * 2 ^ 52) = 0 := by
      apply Nat.eq_of_testBit_eq
      intro j
      rw [Nat.testBit_land, Nat.testBit_mul_two_pow]
      by_cases hj : j < 52
      · simp [hj]
      · have haj : a.testBit j = false := by
          apply Nat.testBit_eq_false_of_lt
          exact lt_of_lt_of_le ha (Nat.pow_le_pow_right (by omega) (by omega))
        simp [haj]
    let av : BitVec 64 := BitVec.ofNat 64 a
    let ev : BitVec 64 := BitVec.ofNat 64 (ef * 2 ^ 52)
    have hlandBV : av &&& ev = 0#64 := by
      apply BitVec.eq_of_toNat_eq
      simp only [av, ev, BitVec.toNat_and, BitVec.toNat_ofNat]
      rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef,
        hland]
    calc
      a ||| (ef * 2 ^ 52) = (av ||| ev).toNat := by
        simp only [av, ev, BitVec.toNat_or, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
      _ = (av + ev).toNat := by
        rw [BitVec.add_eq_or_of_and_eq_zero av ev hlandBV]
      _ = a + ef * 2 ^ 52 := by
        rw [BitVec.toNat_add_of_and_eq_zero hlandBV]
        simp only [av, ev, BitVec.toNat_ofNat]
        rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef]
  let unit : Nat := 2 ^ 52
  let top : Nat := 2047 * unit
  let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
  let value : Nat → Real := fun n =>
    let field := n / unit
    let fraction := n % unit
    if field = 0 then (fraction : Real) / (2 : Real) ^ (1074 : Nat)
    else if 1075 ≤ field then
      ((unit + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
    else ((unit + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
  have hvalueMono : ∀ a b : Nat, a < b → b < top → value a < value b := by
    simpa only [unit, top, value] using Binary64FiniteMagnitudeStrictMono
  have cellRounds : ∀ (t : F64) (low bits : Nat),
      t.val = bits → low + 1 < top →
      value low ≤ x → x ≤ value (low + 1) →
      (bits = low ∨ bits = low + 1) →
      (bits = low →
        abs (x - value low) ≤ abs (x - value (low + 1))) →
      (bits = low + 1 →
        abs (x - value (low + 1)) ≤ abs (x - value low)) →
      (bits = low →
        abs (x - value low) = abs (x - value (low + 1)) → bits % 2 = 0) →
      (bits = low + 1 →
        abs (x - value (low + 1)) = abs (x - value low) → bits % 2 = 0) →
      x < threshold →
      DecimalGoalVocabularyDefault.roundsMagnitude x t := by as_aux_lemma =>
    intro t low bits ht hlow hxl hxh hchoice hnearLow hnearHigh
      htieLow htieHigh hthreshold
    dsimp only [DecimalGoalVocabularyDefault]
    change 0 ≤ x ∧ t.val ≤ top ∧
      (if t.val = top then threshold ≤ x else x < threshold ∧
        ∀ v : F64, v.val < top → v ≠ t →
          abs (x - value t.val) ≤ abs (x - value v.val) ∧
          (abs (x - value t.val) = abs (x - value v.val) → t.val % 2 = 0))
    have hbits : bits < top := by rcases hchoice with h | h <;> omega
    have htBound : t.val < top := by omega
    refine ⟨hxpos.le, htBound.le, ?_⟩
    rw [if_neg (by omega)]
    refine ⟨hthreshold, ?_⟩
    intro v hv hne
    have hvne : v.val ≠ bits := by
      intro heq
      apply hne
      apply UScalar.eq_of_val_eq
      omega
    rcases hchoice with hbitsLow | hbitsHigh
    · have htlow : t.val = low := ht.trans hbitsLow
      rw [htlow]
      have hnear := hnearLow hbitsLow
      have htie := htieLow hbitsLow
      by_cases hvBelow : v.val < low
      · have hvValue : value v.val < value low :=
          hvalueMono _ _ hvBelow (by omega)
        have hvx : value v.val < x := hvValue.trans_le hxl
        have hstrict : abs (x - value low) < abs (x - value v.val) := by
          rw [abs_of_nonneg (sub_nonneg.mpr hxl),
            abs_of_nonneg (sub_nonneg.mpr hvx.le)]
          linarith
        exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
      · have hvAbove : low < v.val := by omega
        have hhighLe : low + 1 ≤ v.val := by omega
        have hhighValue : value (low + 1) ≤ value v.val := by
          rcases hhighLe.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (hvalueMono _ _ hlt hv).le
        have hxv : x ≤ value v.val := hxh.trans hhighValue
        have hhighDist : abs (x - value (low + 1)) ≤
            abs (x - value v.val) := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxh),
            abs_of_nonpos (sub_nonpos.mpr hxv)]
          linarith
        constructor
        · exact hnear.trans hhighDist
        · intro heq
          have hreverse : abs (x - value (low + 1)) ≤
              abs (x - value low) := hhighDist.trans_eq heq.symm
          have heqCell := le_antisymm hnear hreverse
          simpa only [hbitsLow] using htie heqCell
    · have hthigh : t.val = low + 1 := ht.trans hbitsHigh
      rw [hthigh]
      have hnear := hnearHigh hbitsHigh
      have htie := htieHigh hbitsHigh
      by_cases hvAbove : low + 1 < v.val
      · have hvValue : value (low + 1) < value v.val :=
          hvalueMono _ _ hvAbove hv
        have hxv : x < value v.val := hxh.trans_lt hvValue
        have hstrict : abs (x - value (low + 1)) < abs (x - value v.val) := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxh),
            abs_of_nonpos (sub_nonpos.mpr hxv.le)]
          linarith
        exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
      · have hvBelow : v.val < low + 1 := by omega
        have hvLow : v.val ≤ low := by omega
        have hvValue : value v.val ≤ value low := by
          rcases hvLow.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (hvalueMono _ _ hlt (by omega)).le
        have hvx : value v.val ≤ x := hvValue.trans hxl
        have hlowDist : abs (x - value low) ≤ abs (x - value v.val) := by
          rw [abs_of_nonneg (sub_nonneg.mpr hxl),
            abs_of_nonneg (sub_nonneg.mpr hvx)]
          linarith
        constructor
        · exact hnear.trans hlowDist
        · intro heq
          have hreverse : abs (x - value low) ≤
              abs (x - value (low + 1)) := hlowDist.trans_eq heq.symm
          have heqCell := le_antisymm hnear hreverse
          simpa only [hbitsHigh] using htie heqCell
  have hfpResult :
      (pf ≥ 2047#i32 → fp = { m := 0#u64, p_biased := 2047#i32 }) ∧
      (¬ pf ≥ 2047#i32 →
        fp = { m := Sf &&& ~~~oneU, p_biased := pf }) := by as_aux_lemma =>
    constructor
    · intro hover
      have hr := hfinal
      simp only [hover, if_true] at hr
      exact (Result.ok.inj hr).symm
    · intro hover
      have hr := hfinal
      simp only [hover, if_false] at hr
      exact (Result.ok.inj hr).symm
  by_cases hover : pf ≥ 2047#i32
  · as_aux_lemma =>
    have hfpEq : fp = { m := 0#u64, p_biased := 2047#i32 } := hfpResult.1 hover
    have htargetTop : target.val = top := by
      subst fp
      dsimp only [DecimalGoalVocabularyDefault] at htargetAssembly
      dsimp only [top, unit]
      norm_num [Int.toNat] at htargetAssembly ⊢
      exact htargetAssembly
    have hpfGe : 2047 ≤ pf.val := by
      simpa only [IScalar.le_equiv] using hover
    have hthreshold : threshold ≤ x := by
      rcases hSfPf with hcarryCase | hplainCase
      · have hpGe : 2046 ≤ p.val := by omega
        have hchoose : chooseHigh := by
          by_contra hn
          rw [hSchoice, if_neg hn] at hcarryCase
          omega
        have hlowMax : lowSig = 2 ^ 53 - 1 := by
          rw [hSchoice, if_pos hchoose] at hcarryCase
          omega
        have hGmax : G = 2 ^ 54 - 1 := by
          have hodd := hchoose.1
          omega
        have hdelta970 : (2 : Real) ^ (970 : Nat) ≤ deltaR := by
          dsimp only [deltaR]
          rw [← zpow_natCast]
          exact zpow_le_zpow_right₀ (by norm_num) (by omega)
        have hbase : threshold = ((2 ^ 54 - 1 : Nat) : Real) *
            (2 : Real) ^ (970 : Nat) := by
          dsimp only [threshold]
          rw [show (2 : Real) ^ (1024 : Nat) =
              (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) by
            rw [← pow_add]]
          rw [Nat.cast_sub (by norm_num : 1 ≤ (2 : Nat) ^ 54),
            Nat.cast_pow, Nat.cast_ofNat, Nat.cast_one, sub_mul, one_mul]
        rw [hbase, ← hGmax]
        exact (mul_le_mul_of_nonneg_left hdelta970 (by positivity)).trans hxcellLo'
      · have hpGe : 2047 ≤ p.val := by
          rcases hplainCase with ⟨_, _, heq⟩
          have := congrArg IScalar.val heq
          omega
        have hdelta971 : (2 : Real) ^ (971 : Nat) ≤ deltaR := by
          dsimp only [deltaR]
          rw [← zpow_natCast]
          exact zpow_le_zpow_right₀ (by norm_num) (by omega)
        calc
          threshold ≤ (2 : Real) ^ (1024 : Nat) := by
            dsimp only [threshold]
            exact sub_le_self _ (by positivity)
          _ = (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) := by
            rw [← pow_add]
          _ ≤ (G : Real) * deltaR := by
            exact mul_le_mul (by exact_mod_cast hGLower) hdelta971
              (by positivity) (by positivity)
          _ ≤ x := hxcellLo'
    dsimp only [DecimalGoalVocabularyDefault]
    change 0 ≤ x ∧ target.val ≤ top ∧
      (if target.val = top then threshold ≤ x else _)
    rw [htargetTop, if_pos rfl]
    exact ⟨hxpos.le, le_rfl, hthreshold⟩
  · have hfiniteData :
        target.val = pf.val.toNat * unit + (Sf.val - unit) ∧
        target.val = selectedBits ∧ Sf.val < 2 ^ 53 ∧
        pf.val.toNat ≤ 2046 ∧ pNat ≤ 2046 ∧
        lowBits + 1 ≤ top := by as_aux_lemma =>
      have hfpEq : fp = { m := Sf &&& ~~~oneU, p_biased := pf } :=
        hfpResult.2 hover
      have hpfLt : pf.val < 2047 := by
        have hn : ¬2047 ≤ pf.val := by
          simpa only [IScalar.le_equiv] using hover
        omega
      have hpfPos : 0 < pf.val := by
        rcases hSfPf with hcarryCase | hplainCase
        · omega
        · have heq := congrArg IScalar.val hplainCase.2.2
          omega
      have hSfRange : 2 ^ 52 ≤ Sf.val ∧ Sf.val < 2 ^ 53 := by as_aux_lemma =>
        rcases hSfPf with hcarryCase | hplainCase
        · rw [hcarryCase.2.1, honeUval]
          norm_num
        · rw [hplainCase.2.1]
          exact ⟨hSBounds.1, hplainCase.1⟩
      have hmVal : (Sf &&& ~~~oneU).val = Sf.val - 2 ^ 52 :=
        clearHidden Sf oneU honeUval hSfRange.1 hSfRange.2
      have hpfEmod : pf.val.emod (2 ^ 64) = pf.val := by
        apply Int.emod_eq_of_lt hpfPos.le
        omega
      have hpfNatCast : (pf.val.toNat : Int) = pf.val :=
        Int.toNat_of_nonneg hpfPos.le
      have hpfNatUpper : pf.val.toNat ≤ 2046 := by
        rw [Int.toNat_le]
        omega
      have htargetFormula : target.val =
        pf.val.toNat * unit + (Sf.val - unit) := by
        have ha := htargetAssembly
        dsimp only [DecimalGoalVocabularyDefault] at ha
        rw [hfpEq, hpfEmod] at ha
        have hmBound : Sf.val - unit < 2 ^ 52 := by
          dsimp only [unit]
          omega
        have heBound : pf.val.toNat * 2 ^ 52 < 2 ^ 64 := by omega
        have hor := fieldOr (Sf.val - unit) pf.val.toNat hmBound heBound
        dsimp only [unit] at ha hor ⊢
        rw [Nat.mod_eq_of_lt heBound, hmVal] at ha
        exact ha.trans (hor.trans (Nat.add_comm _ _))
      have htargetSelected : target.val = selectedBits := by as_aux_lemma =>
        rw [htargetFormula]
        rcases hSfPf with hcarryCase | hplainCase
        · have hchoose : chooseHigh := by
            by_contra hn
            rw [hSchoice, if_neg hn] at hcarryCase
            omega
          have hlowMax : lowSig = 2 ^ 53 - 1 := by
            rw [hSchoice, if_pos hchoose] at hcarryCase
            omega
          rw [hcarryCase.2.1, honeUval]
          have hpfEq : pf.val.toNat = pNat + 1 := by
            apply Nat.cast_injective (R := Int)
            rw [hpfNatCast, Nat.cast_add, Nat.cast_one, hpNatCast]
            omega
          rw [hpfEq]
          dsimp only [selectedBits, lowBits, unit]
          rw [if_pos hchoose]
          omega
        · have hpfEq : pf.val.toNat = pNat := by
            apply Nat.cast_injective (R := Int)
            rw [hpfNatCast, hpNatCast]
            have heq := congrArg IScalar.val hplainCase.2.2
            omega
          rw [hplainCase.2.1, hSchoice, hpfEq]
          dsimp only [selectedBits, lowBits, unit]
          split <;> omega
      have hpNatUpper : pNat ≤ 2046 := by as_aux_lemma =>
        rcases hSfPf with hcarryCase | hplainCase
        · have hi : (pNat : Int) ≤ 2046 := by
            rw [hpNatCast]
            omega
          exact_mod_cast hi
        · have heq := congrArg IScalar.val hplainCase.2.2
          have hi : (pNat : Int) ≤ 2046 := by
            rw [hpNatCast]
            omega
          exact_mod_cast hi
      have hlowSuccLe : lowBits + 1 ≤ top := by
        dsimp only [lowBits, top, unit]
        omega
      exact ⟨htargetFormula, htargetSelected, hSfRange.2, hpfNatUpper, hpNatUpper,
        hlowSuccLe⟩
    rcases hfiniteData with
      ⟨htargetFormula, htargetSelected, hSfUpper, hpfNatUpper, hpNatUpper,
        hlowSuccLe⟩
    by_cases hboundary : lowBits + 1 = top
    · as_aux_lemma =>
      have hpEq : pNat = 2046 := by
        dsimp only [lowBits, top, unit] at hboundary
        omega
      have hlowMax : lowSig = 2 ^ 53 - 1 := by
        dsimp only [lowBits, top, unit] at hboundary
        omega
      have htargetLt : target.val < top := by
        rw [htargetFormula]
        dsimp only [top, unit]
        omega
      have hselectedLt : selectedBits < top := by omega
      have hnotChoose : ¬chooseHigh := by
        intro hc
        dsimp only [selectedBits] at hselectedLt
        rw [if_pos hc, hboundary] at hselectedLt
        omega
      have htargetLow : target.val = lowBits := by
        rw [htargetSelected]
        dsimp only [selectedBits]
        rw [if_neg hnotChoose]
      have hGremZero : G % 2 = 0 := by
        by_contra hnzero
        have hm := Nat.mod_lt G (by omega : 0 < 2)
        have hodd : G % 2 = 1 := by omega
        have ht : tieLow := by
          by_contra hnt
          exact hnotChoose ⟨hodd, hnt⟩
        have hG : G = 2 * lowSig + 1 := by omega
        dsimp only [tieLow] at ht
        rw [hG, hlowMax] at ht
        norm_num at ht
      have hGEq : G = 2 * lowSig := by omega
      have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
        dsimp only [ulpR]
        rw [← hpNatCast, hpEq]
        norm_num
        exact zpow_natCast (2 : Real) 971
      have hrem : lowSig - unit < unit := by
        dsimp only [unit]
        omega
      have hlowDiv : lowBits / unit = pNat := by
        dsimp only [lowBits]
        rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by
          dsimp only [unit]
          positivity), Nat.div_eq_of_lt hrem, Nat.add_zero]
      have hlowMod : lowBits % unit = lowSig - unit := by
        dsimp only [lowBits]
        rw [Nat.mul_comm pNat unit, Nat.mul_add_mod,
          Nat.mod_eq_of_lt hrem]
      have hvalueLowEq : value lowBits = (lowSig : Real) * ulpR := by
        dsimp only [value]
        rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega),
          hulp2046]
        have hsum : unit + (lowSig - unit) = lowSig := by
          dsimp only [unit]
          omega
        rw [hsum, hpEq]
      have hvalueLow : value lowBits ≤ x := by
        rw [hvalueLowEq]
        exact hLowX
      have hdelta2046 : deltaR = (2 : Real) ^ (970 : Nat) := by
        dsimp only [deltaR]
        rw [← hpNatCast, hpEq]
        norm_num
        exact zpow_natCast (2 : Real) 970
      have hbelow : x < threshold := by
        have htopCell : ((G + 1 : Nat) : Real) * deltaR = threshold := by
          rw [hGEq, hlowMax, hdelta2046]
          dsimp only [threshold]
          have heq : (2 : Real) ^ (1024 : Nat) =
              (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
            rw [← pow_add]
          rw [heq]
          have hcoeff : ((2 * (2 ^ 53 - 1) + 1 : Nat) : Real) =
              (2 : Real) ^ (54 : Nat) - 1 := by norm_num
          rw [hcoeff, sub_mul, one_mul]
        exact hxcellHi'.trans_eq htopCell
      dsimp only [DecimalGoalVocabularyDefault]
      change 0 ≤ x ∧ target.val ≤ top ∧
        (if target.val = top then threshold ≤ x else x < threshold ∧
          ∀ v : F64, v.val < top → v ≠ target →
            abs (x - value target.val) ≤ abs (x - value v.val) ∧
            (abs (x - value target.val) = abs (x - value v.val) →
              target.val % 2 = 0))
      refine ⟨hxpos.le, htargetLt.le, ?_⟩
      rw [if_neg (by omega)]
      refine ⟨hbelow, ?_⟩
      intro v hv hne
      have hvne : v.val ≠ lowBits := by
        intro heq
        apply hne
        apply UScalar.eq_of_val_eq
        omega
      have hvlt : v.val < lowBits := by
        dsimp only [top, unit, lowBits] at hv hboundary ⊢
        omega
      have hvValue : value v.val < value lowBits :=
        hvalueMono _ _ hvlt (by omega)
      have hvx : value v.val < x := hvValue.trans_le hvalueLow
      have hstrict : abs (x - value lowBits) < abs (x - value v.val) := by
        rw [abs_of_nonneg (sub_nonneg.mpr hvalueLow),
          abs_of_nonneg (sub_nonneg.mpr hvx.le)]
        linarith
      rw [htargetLow]
      exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
    · as_aux_lemma =>
      have hlowTop : lowBits + 1 < top := by omega
      have hAdjacent :
          value lowBits = (lowSig : Real) * ulpR ∧
          value (lowBits + 1) = ((lowSig + 1 : Nat) : Real) * ulpR := by
        by_cases hpSmall : pNat ≤ 2045
        · simpa only [unit, lowBits, value, ulpR, hpNatCast] using
            (Binary64AdjacentNormalValues pNat lowSig
              (by dsimp only [pNat]; omega) hpSmall hLowSigLower hLowSigUpper)
        · have hpEq : pNat = 2046 := by omega
          have hsigSucc : lowSig + 1 < 2 ^ 53 := by
            dsimp only [lowBits, top, unit] at hlowTop
            omega
          have hrem : lowSig - unit < unit := by
            dsimp only [unit]
            omega
          have hremSucc : lowSig + 1 - unit < unit := by
            refine (Nat.sub_lt_iff_lt_add ?_).2 ?_
            · exact (by simpa only [unit] using
                hLowSigLower.trans (Nat.le_add_right lowSig 1))
            · norm_num [unit] at hsigSucc ⊢
              exact hsigSucc
          have hlowDiv : lowBits / unit = pNat := by
            dsimp only [lowBits]
            rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by
              dsimp only [unit]
              positivity)]
            rw [Nat.div_eq_of_lt hrem, Nat.add_zero]
          have hlowMod : lowBits % unit = lowSig - unit := by
            dsimp only [lowBits]
            rw [Nat.mul_comm pNat unit, Nat.mul_add_mod,
              Nat.mod_eq_of_lt hrem]
          have hhighEq : lowBits + 1 =
              pNat * unit + (lowSig + 1 - unit) := by
            dsimp only [lowBits, unit]
            omega
          have hhighDiv : (lowBits + 1) / unit = pNat := by
            rw [hhighEq, Nat.mul_comm pNat unit,
              Nat.mul_add_div (by dsimp only [unit]; positivity),
              Nat.div_eq_of_lt hremSucc, Nat.add_zero]
          have hhighMod : (lowBits + 1) % unit = lowSig + 1 - unit := by
            rw [hhighEq, Nat.mul_comm pNat unit, Nat.mul_add_mod,
              Nat.mod_eq_of_lt hremSucc]
          have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
            dsimp only [ulpR]
            rw [← hpNatCast, hpEq]
            norm_num
            exact zpow_natCast (2 : Real) 971
          constructor
          · dsimp only [value]
            rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega),
              hulp2046]
            have hsum : unit + (lowSig - unit) = lowSig := by
              dsimp only [unit]
              omega
            rw [hsum]
            rw [hpEq]
          · dsimp only [value]
            rw [hhighDiv, hhighMod, if_neg (by omega), if_pos (by omega),
              hulp2046]
            have hsum : unit + (lowSig + 1 - unit) = lowSig + 1 := by
              dsimp only [unit]
              omega
            rw [hsum]
            rw [hpEq]
      have hvalueLow : value lowBits ≤ x := by
        rw [hAdjacent.1]
        exact hLowX
      have hvalueHigh : x ≤ value (lowBits + 1) := by
        rw [hAdjacent.2]
        exact hXHigh
      have hchoice : selectedBits = lowBits ∨ selectedBits = lowBits + 1 := by
        dsimp only [selectedBits]
        split <;> simp
      have hbelow : x < threshold := by
        by_cases hpSmall : pNat ≤ 2045
        · have hulpBound : ulpR ≤ (2 : Real) ^ (970 : Nat) := by
            dsimp only [ulpR]
            rw [← zpow_natCast]
            apply zpow_le_zpow_right₀ (by norm_num)
            rw [← hpNatCast]
            have hi : (pNat : Int) ≤ 2045 := by exact_mod_cast hpSmall
            omega
          have hpow1023 : (2 : Real) ^ (1023 : Nat) < threshold := by
            have hp : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) :=
              pow_lt_pow_right₀ (by norm_num) (by omega)
            have heq : (2 : Real) ^ (1024 : Nat) =
                2 * (2 : Real) ^ (1023 : Nat) := by
              rw [show 1024 = 1023 + 1 by omega, pow_succ]
              ring
            dsimp only [threshold]
            rw [heq]
            rw [show 2 * (2 : Real) ^ (1023 : Nat) -
                (2 : Real) ^ (970 : Nat) =
                (2 : Real) ^ (1023 : Nat) +
                  ((2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat)) by ring]
            exact lt_add_of_pos_right _ (sub_pos.mpr hp)
          calc
            x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh
            _ ≤ (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (970 : Nat) := by
              apply mul_le_mul
              · exact_mod_cast (show lowSig + 1 ≤ 2 ^ 53 by omega)
              · exact hulpBound
              · dsimp only [ulpR]
                exact (zpow_pos (by norm_num) _).le
              · positivity
            _ = (2 : Real) ^ (1023 : Nat) := by rw [← pow_add]
            _ < threshold := hpow1023
        · have hpEq : pNat = 2046 := by omega
          have hulpEq2046 : ulpR = (2 : Real) ^ (971 : Nat) := by
            dsimp only [ulpR]
            rw [← hpNatCast, hpEq]
            norm_num
            exact zpow_natCast (2 : Real) 971
          have hsigBound : lowSig + 1 ≤ 2 ^ 53 - 1 := by
            dsimp only [lowBits, top, unit] at hlowTop
            omega
          have hmaxBelow : ((2 ^ 53 - 1 : Nat) : Real) *
              (2 : Real) ^ (971 : Nat) < threshold := by
            have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity
            have heq1024 : (2 : Real) ^ (1024 : Nat) =
                (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
              rw [← pow_add]
            have heq971 : (2 : Real) ^ (971 : Nat) =
                2 * (2 : Real) ^ (970 : Nat) := by
              rw [show 971 = 970 + 1 by omega, pow_succ]
              ring
            dsimp only [threshold]
            rw [heq1024, heq971]
            push_cast
            have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by
              norm_num
            rw [h54]
            calc
              (9007199254740991 : Real) * (2 * (2 : Real) ^ (970 : Nat)) =
                  18014398509481982 * (2 : Real) ^ (970 : Nat) := by ring
              _ < 18014398509481983 * (2 : Real) ^ (970 : Nat) :=
                mul_lt_mul_of_pos_right (by norm_num) hp
              _ = 18014398509481984 * (2 : Real) ^ (970 : Nat) -
                  (2 : Real) ^ (970 : Nat) := by ring
          calc
            x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh
            _ ≤ ((2 ^ 53 - 1 : Nat) : Real) *
                (2 : Real) ^ (971 : Nat) := by
              rw [hulpEq2046]
              exact mul_le_mul_of_nonneg_right (by exact_mod_cast hsigBound)
                (by positivity)
            _ < threshold := hmaxBelow
      apply cellRounds target lowBits selectedBits htargetSelected hlowTop
        hvalueLow hvalueHigh hchoice
      · intro hsel
        rw [hAdjacent.1, hAdjacent.2]
        exact hLowNear (by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_pos hc] at hsel
          omega)
      · intro hsel
        rw [hAdjacent.1, hAdjacent.2]
        exact hHighNear (by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_neg hc] at hsel
          omega)
      · intro hsel heq
        rw [hAdjacent.1, hAdjacent.2] at heq
        have hc : ¬chooseHigh := by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_pos hc] at hsel
          omega
        rw [hsel]
        exact hTieLowEven hc heq
      · intro hsel heq
        rw [hAdjacent.1, hAdjacent.2] at heq
        have hc : chooseHigh := by
          dsimp only [selectedBits] at hsel
          by_contra hc
          rw [if_neg hc] at hsel
          omega
        rw [hsel]
        exact hTieHighEven hc heq
      · exact hbelow

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
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 9 entries
Technical checking records
Local closure summary
{
  "node": "P873_T1_IntegerNormalRounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "dabcbeb04ef40b5a514b778f10cad29f05bd2d629e6cf41d7709b9029d9a17f2",
  "active_statement_hash": "46e6fcf4224bf2c5fa19105ceaca9d24879f0fe112c917895e6931bc581b8f47",
  "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-202",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64AdjacentNormalValues": "8495001b4edafedb",
    "Binary64FiniteMagnitudeStrictMono": "ea630fa702fb7b61",
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2"
  }
}
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:c9788eb64afe01b8795094511bed09f31e602aeb5c17b9a2ff7304f46bcbd266",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass"
}