IntegerProductApproximation

Lean checked

Statement

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

Lemma

Let -342\le q\le308 and let W be a 64-bit word with 2^{63}\le W<2^{64}. Suppose the extracted product approximation at precision 55 returns limbs (lo,hi), and suppose either lo\ne2^{64}-1 or -27\le q\le55. Let (a_q,b_q) be the accessed table entry and use e_q,D_q,T_q from IntegerPowerTableCertificate. Put
R=WT_q/2^{64},\quad Y=hi\,2^{64}+lo,\quad u=\lfloor hi/2^{63}\rfloor,\quad s=73+u, \quad G=\lfloor hi/2^{9+u}\rfloor.
Then u\le1, and Y is either Wa_q or Wa_q+\lfloor Wb_q/2^{64}\rfloor. Moreover
G2^s\le R<(G+1)2^s, \tag{1}
and the exact lower-even midpoint test is
R=G2^s\ \land\ G\bmod4=1 \quad\Longleftrightarrow\quad \begin{array}{l} -4\le q\le23,\quad G\bmod4=1,\quad lo\le1,\\ G2^{9+u}=hi. \end{array} \tag{2}
The table index, cast, and access in this description all return normally.

Lean statement

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

import Tablet.IntegerPowerTableCertificate
import Tablet.ModelU64BitAnd
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Mathlib.Data.Nat.Factorization.Basic
import Mathlib.Data.Nat.MaxPowDiv
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.IntervalCases

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

namespace dec2flt_model

-- [TABLET NODE: IntegerProductApproximation]
theorem IntegerProductApproximation :
    ∀ (q : Std.I64) (w lo hi : Std.U64),
      -342 ≤ q.val → q.val ≤ 308 →
      2 ^ 63 ≤ w.val →
      num.imp.dec2flt.lemire.compute_product_approx q w 55#usize =
        ok (lo, hi) →
      (lo.val ≠ 2 ^ 64 - 1 ∨ (-27 ≤ q.val ∧ q.val ≤ 55)) →
      ∃ (delta : Std.I64) (index : Std.Usize) (a b : Std.U64),
        q - (-342)#i64 = ok delta ∧
        lift (IScalar.hcast .Usize delta) = ok index ∧
        Array.index_usize num.imp.dec2flt.table.POWER_OF_FIVE_128 index =
          ok (a, b) ∧
        let e : Int := 217706 * q.val / 65536 - q.val
        let D : Nat := a.val * 2 ^ 64 + b.val
        let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
        let R : Real := (w.val : Real) * T / (2 : Real) ^ (64 : Nat)
        let Y : Nat := hi.val * 2 ^ 64 + lo.val
        let u : Nat := hi.val / 2 ^ 63
        let s : Nat := 73 + u
        let G : Nat := hi.val / 2 ^ (9 + u)
        (u ≤ 1 ∧
          (Y = w.val * a.val ∨
            Y = w.val * a.val + (w.val * b.val) / 2 ^ 64) ∧
          (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s ∧
          ((R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1) ↔
            (-4 ≤ q.val ∧ q.val ≤ 23 ∧ G % 4 = 1 ∧
              lo.val ≤ 1 ∧ G * 2 ^ (9 + u) = hi.val))) := 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

By num imp dec2flt table SMALLEST POWER OF FIVE and num imp dec2flt table LARGEST POWER OF FIVE, the extracted signed 32-bit endpoint constants have the defining values -342 and 308, respectively. Unfold num imp dec2flt lemire compute product approx. Casting these two values to signed 64 bits succeeds with values -342 and 308, so the first two extracted assertions are exactly -342\le q and q\le308 and follow from the hypotheses. The routine then casts the smallest endpoint a second time, again obtaining the signed 64-bit value -342, so its checked subtraction is exactly q-(-342). By IntegerPowerTableCertificate, that subtraction returns the certificate's \mathit{delta}, its following checked cast to a 64-bit usize returns the certificate's index, and access to the same extracted table at that index returns the certificate's pair (a_q,b_q). Thus the routine uses exactly the index and pair supplied by the certificate.
Set B=2^{64}. The same certificate gives D_q=a_qB+b_q, together with all of its normalization, negative-index, exactness, and directed-error clauses. Since 55<64, the precision assertion succeeds and the mask is (B-1)\mathbin{\mathtt{>>>}}55=511.
Unfold num imp dec2flt lemire full multiplication. Widening two 64-bit words to 128 bits is exact and their product is below 2^{128}; narrowing returns its remainder modulo B, and shifting right by 64 returns its quotient by B. Thus the first call returns the two limbs of Wa_q. The mask comparison, whose bitwise operation is total and has its ordinary bit meaning by ModelU64BitAnd, tests whether the low nine bits of the first high limb are all one. If not, the routine returns those limbs and
Y=Wa_q. \tag{4}
If so, the high limb of the second multiplication is
c=\left\lfloor\frac{Wb_q}{B}\right\rfloor.
The wrapping addition returns the low limb of Wa_q+c, and comparison with c detects exactly whether that addition carried. The first high limb is strictly below a_q<B, so its carried increment cannot overflow. Therefore the refining branch returns
Y=Wa_q+c =Wa_q+\left\lfloor\frac{Wb_q}{B}\right\rfloor =\left\lfloor\frac{WD_q}{B}\right\rfloor. \tag{5}
This proves the asserted two alternatives for Y directly from the extracted control flow.
Because hi<B, one has u=\lfloor hi/2^{63}\rfloor\le1. Put d=9+u and K=B2^d=2^{73+u}=2^s. Euclidean division of Y=hiB+lo by K gives
\left\lfloor Y/K\right\rfloor =\left\lfloor hi/2^d\right\rfloor=G. \tag{6}
The certificate's 2^{127}\le T_q<2^{128} and 2^{63}\le W<B imply
2^{126}\le R<2^{128}. \tag{7}
We next prove that R and Y lie in the same K-cell.
First suppose the routine does not refine, so (4) holds. On a floor entry, write T_q=D_q+\epsilon with 0\le\epsilon<1. Then
R-Y=\frac{W(b_q+\epsilon)}B,\qquad 0\le R-Y<B. \tag{8}
On a ceiling entry, the certificate supplies q=-n, positive z_n, and P=5^n, N=2^{z_n+127} with T_q=N/P. Dividing N=kP+r gives 0<r<P: P is odd and greater than one, so it cannot divide the power of two N. The strict interval D_q-1<T_q<D_q therefore gives
D_q=k+1=\frac{N+\delta}{P},\qquad \delta=P-r,qquad 0<\delta<P. \tag{9}
Here 1\le n\le27, so P\le5^{27}<2^{63}<B. Also z_n\ge3, since 5\le P<2^{z_n}, and hence B\mid N. If b_q=0, then B\mid D_q and (9) would make B divide D_qP-N=\delta, contrary to 0<\delta<P<B. Thus b_q\ge1, and
R-Y=\frac{W(b_q-\delta/P)}B,\qquad 0<R-Y<B. \tag{10}
The failed mask test says hi\bmod512\ne511. Since d is 9 or 10, this implies hi\bmod2^d\le2^d-2, and consequently
Y\bmod K\le(2^d-2)B+(B-1)<K-B. \tag{11}
The positive correction of size less than B in (8) or (10) cannot cross the next K-boundary. Thus R and Y have the same quotient by K in the nonrefining branch.
Now suppose the routine refines, so (5) holds. For a floor entry, R is at least Y and less than Y+2. If it crossed the next integral K-boundary, that boundary would have to be Y+1, forcing Y\bmod B=B-1, hence lo=B-1. The continuing-sentinel hypothesis then forces -27\le q\le55. A floor entry in that interval has 0\le q\le55, and the certificate says D_q=T_q there. In that exact case R=WD_q/B<Y+1, so no boundary is crossed after all.
For a ceiling entry, use (9). Write WD_q=YB+\rho with 0\le\rho<B. Then
BP(R-Y)=WN-YBP=\rho P-W\delta. \tag{12}
Since 0<W\delta<BP and 0\le\rho P<BP, the absolute value of the integer on the right is below BP. Also Y-1<R<Y+1, so a downward K-boundary could be crossed only when K\mid Y. In that case WN is divisible by 2^{z_n+127}, with z_n+127\ge130, while YBP is divisible by 2^{s+64}, with s+64\ge137. Their difference in (12) is thus divisible by 2^{130}. But P<2^{63} makes its absolute value less than BP<2^{127}, so the difference is zero. Hence R=Y at the only possible downward boundary. This proves that R and Y share a K-cell in every branch. Together with (6), this is exactly (1).
It remains to prove (2). Write W=2^vh with h odd and 0\le v\le63. Assume first that R=G2^s and G\bmod4=1, so G is odd. If q=-n<0, the certificate's negative-index formula gives R=W2^{z_n+63}/5^n. Clearing the odd denominator in R=G2^s and comparing the power-of-two and odd parts gives
v+z_n+63=s,\qquad h=G5^n. \tag{13}
As v\ge0 and s\le74, this forces z_n\le11. Since 2^{11}<5^5, the certificate's inequality 5^n<2^{z_n} then forces n\le4. If q\ge0, the definition of T_q gives
v+63-e_q=s,\qquad h5^q=G. \tag{14}
The odd factors h5^q and G force first the displayed equality of 2-adic exponents and then the equality of odd parts. Here v\le63 and s\ge73, so e_q\le53. The normalization clause of the certificate is equivalent in this case to 2^{e_q}\le5^q<2^{e_q+1}; hence 5^q<2^{54}, while 5^{24}>2^{55}. Thus q\le23. Every odd midpoint therefore has -4\le q\le23.
We now finish the forward implication, including the product calculation at the four negative indices. If 0\le q\le23, the certificate says both D_q=T_q and b_q=0. Thus D_q=a_qB, R=Wa_q, and the correction c is zero. Whether or not the mask requests the second multiplication, the returned integer is Y=R=G2^s. Hence lo=0 and hi=R/B=G2^{9+u}.
If q=-n with 1\le n\le4, retain P,N,\delta from (9). At the assumed midpoint, R=WN/(BP)=G2^s is an integer, while
\frac{WD_q}{B}=\frac{WN}{BP}+\frac{W\delta}{BP} =R+\frac{W\delta}{BP}.
The bounds W<B and 0<\delta<P give 0<W\delta/(BP)<1, so independently of which product branch is taken,
\left\lfloor\frac{WD_q}{B}\right\rfloor=R,\qquad Wa_q+c=R,\quad c=\left\lfloor\frac{Wb_q}{B}\right\rfloor. \tag{15}
If c=0, the first product is already Wa_q=R, and a possible refinement adds zero. If c>0, then 0<c<B because W,b_q<B, and (15) gives Wa_q=R-c. Since R/B=G2^{9+u} is integral, the first product has high limb R/B-1 and low limb B-c. The low nine bits of that high limb are all one, because R/B is divisible by 2^9. The mask therefore forces the refining branch, whose correction is exactly c and whose carry returns exactly R. In both subcases the final limbs are lo=0 and hi=G2^{9+u}. This proves the forward implication of (2).
Conversely, assume the right side of (2), put j=lo\in\{0,1\}, and let L=G2^s. The reconstruction hypothesis gives
Y=L+j. \tag{16}
For 0\le q\le23, the certificate again gives D_q=T_q and b_q=0, so both product paths give Y=R=Wa_q. Moreover 5^q\le5^{23}<2^{54} and normalization give e_q\le53, while D_q=T_q=5^q2^{127-e_q}=a_qB gives a_q=5^q2^{63-e_q}. Hence 2^{10}\mid a_q and 2^{10}\mid Y. Since 2^{10}\mid L and j\in\{0,1\}, (16) forces j=0, so R=L.
It remains only q=-n for 1\le n\le4. The four inequalities defining z_n, followed by division of N=2^{z_n+127} by P=5^n, are
\begin{array}{c|r|r|r|r|r|r|r} n&P&z_n&r&\delta&a_q&b_q&\nu_2(a_q)\\ \hline 1&5&3&4&1&14757395258967641292&14757395258967641293&2\\ 2&25&5&21&4&11805916207174113034&4427218577690292388&1\\ 3&125&7&59&66&9444732965739290427&7231123676894144234&0\\ 4&625&10&472&153&15111572745182864683&15259146697772541097&0. \end{array} \tag{17}
Indeed, the certificate states T_q=N/P, D_q-1<T_q<D_q, and D_q=a_qB+b_q; hence D_q=\lfloor N/P\rfloor+1, so every entry of (17) is ordinary Euclidean division from statement-level facts.
If the extracted routine refined, (5), (9), and (16) show Y=\lfloor WD_q/B\rfloor=L+j and Y-1<R<Y+1; hence |R-L|<2. The integer WN-LBP=BP(R-L) is divisible by 2^{130}: N has at least that power of two by (17), and L contributes at least 2^{73} before multiplication by B. Its absolute value is below 2BP<2^{75}. It is therefore zero, and R=L.
If the routine did not refine, (4) and (16) give Wa_q=L+j. When j=0, (17) gives \nu_2(Wa_q)\le63+2<73\le s=\nu_2(L), a contradiction. When j=1, Wa_q=L+1 is odd, so (17) leaves only n=3,4. Because G\bmod4=1, reduction modulo 2^{s+2} gives Wa_q\equiv2^s+1\pmod {2^{s+2}}. Inverting the displayed odd values of a_q gives the following least nonnegative residues for W:
\begin{array}{c|c|r} n&u&W\bmod 2^{s+2}\\ \hline 3&0&36501965416058737238515\\ 3&1&27057232450319446811123\\ 4&0&35111753888106220892547\\ 4&1&63445952785324092174723. \end{array} \tag{18}
Each number in (18) exceeds B, whereas 0\le W<B and B<2^{s+2}, a contradiction. Thus the nonrefining case cannot occur, and the refining case already gave R=L. Combining this equality with the common assumption G\bmod4=1 proves the reverse implication and completes (2).

Lean proof


  intro q w lo hi hqlo hqhi hw hrun hsent
  rcases IntegerPowerTableCertificate q hqlo hqhi with
    ⟨q32, p, delta, index, a, b, hq32, hp, hpval,
      hdelta, hindex, htable, hcert⟩
  refine ⟨delta, index, a, b, hdelta, hindex, htable, ?_⟩
  dsimp only
  have full_spec (x y : Std.U64) :
      ∃ l h : Std.U64,
        num.imp.dec2flt.lemire.full_multiplication x y = ok (l, h) ∧
        h.val * 2 ^ 64 + l.val = x.val * y.val ∧
        h.val = (x.val * y.val) / 2 ^ 64 := by
    have hx64 : x.val < 2 ^ 64 := by exact x.bv.isLt
    have hy64 : y.val < 2 ^ 64 := by exact y.bv.isLt
    have hxy128 : x.val * y.val < 2 ^ 128 := by
      have hxy := Nat.mul_le_mul (Nat.le_pred_of_lt hx64)
        (Nat.le_pred_of_lt hy64)
      norm_num at hxy ⊢
      omega
    have hx128 : x.val < 2 ^ 128 := by omega
    have hy128 : y.val < 2 ^ 128 := by omega
    have hdiv64 : x.val * y.val / 2 ^ 64 < 2 ^ 64 := by
      rw [Nat.div_lt_iff_lt_mul (by positivity)]
      norm_num at hxy128 ⊢
      exact hxy128
    have hx128' : x.val < 340282366920938463463374607431768211456 := by
      norm_num at hx128 ⊢
      exact hx128
    have hy128' : y.val < 340282366920938463463374607431768211456 := by
      norm_num at hy128 ⊢
      exact hy128
    have hxy128' : x.val * y.val < 340282366920938463463374607431768211456 := by
      norm_num at hxy128 ⊢
      exact hxy128
    have hdiv64' : x.val * y.val / 18446744073709551616 <
        18446744073709551616 := by
      norm_num at hdiv64 ⊢
      exact hdiv64
    have hs : num.imp.dec2flt.lemire.full_multiplication x y
        ⦃ r => r.2.val * 2 ^ 64 + r.1.val = x.val * y.val ∧
          r.2.val = (x.val * y.val) / 2 ^ 64 ⦄ := by
      unfold num.imp.dec2flt.lemire.full_multiplication
      step
      step
      step
      step
      step
      step
      constructor
      · rw [i4_post, UScalar.cast_val_eq, i3_post1,
          i2_post, UScalar.cast_val_eq, r_post, i_post,
          i1_post, UScalar.cast_val_eq, UScalar.cast_val_eq]
        simp only [UScalarTy.numBits]
        norm_num [Nat.shiftRight_eq_div_pow]
        rw [Nat.mod_eq_of_lt hx128', Nat.mod_eq_of_lt hy128',
          Nat.mod_eq_of_lt hdiv64']
        simpa [Nat.mul_comm] using Nat.div_add_mod (x.val * y.val)
          18446744073709551616
      · rw [i4_post, UScalar.cast_val_eq, i3_post1, r_post,
          i_post, i1_post, UScalar.cast_val_eq, UScalar.cast_val_eq]
        simp only [UScalarTy.numBits]
        norm_num [Nat.shiftRight_eq_div_pow]
        rw [Nat.mod_eq_of_lt hx128', Nat.mod_eq_of_lt hy128',
          Nat.mod_eq_of_lt hdiv64']
    obtain ⟨r, hr, hrs⟩ := Aeneas.Std.WP.spec_imp_exists hs
    exact ⟨r.1, r.2, hr, hrs⟩
  have and_mask (x : Std.U64) : (x &&& 511#u64).val = x.val % 512 := by
    simp only [UScalar.val_and]
    change x.val &&& (2 ^ 9 - 1) = x.val % 2 ^ 9
    apply Nat.eq_of_testBit_eq
    intro i
    rw [Nat.testBit_land, Nat.testBit_two_pow_sub_one,
      Nat.testBit_mod_two_pow]
    simp [Bool.and_comm]
  have two_adic_unique {A B x y : Nat} (hx : Odd x) (hy : Odd y)
      (h : 2 ^ A * x = 2 ^ B * y) : A = B := by
    have hx0 : x ≠ 0 := by rintro rfl; simp at hx
    have hy0 : y ≠ 0 := by rintro rfl; simp at hy
    have hxnd : ¬ 2 ∣ x := by
      rintro ⟨k, hk⟩
      rcases hx with ⟨m, hm⟩
      omega
    have hynd : ¬ 2 ∣ y := by
      rintro ⟨k, hk⟩
      rcases hy with ⟨m, hm⟩
      omega
    have hA := Nat.maxPowDvdDiv_of_pow_mul_eq
      (Nat.mul_ne_zero (by positivity) hx0)
      (rfl : 2 ^ A * x = 2 ^ A * x) hxnd
    have hB := Nat.maxPowDvdDiv_of_pow_mul_eq
      (Nat.mul_ne_zero (by positivity) hy0)
      (rfl : 2 ^ B * y = 2 ^ B * y) hynd
    rw [h] at hA
    rw [hB] at hA
    exact (congrArg Prod.fst hA).symm
  simp only [num.imp.dec2flt.lemire.compute_product_approx,
    num.imp.dec2flt.table.SMALLEST_POWER_OF_FIVE.eq_def,
    Int.reduceNeg, ge_iff_le, IScalar.le_equiv,
    num.imp.dec2flt.table.LARGEST_POWER_OF_FIVE.eq_def, UScalar.le_equiv,
    UScalar.ofNatCore_val_eq, Nat.reduceLeDiff, massert_True,
    UScalar.lt_equiv, Nat.reduceLT, if_true, gt_iff_lt,
    bind_tc_ok] at hrun
  have hsmall : lift (IScalar.cast .I64 (-342)#i32) = ok (-342)#i64 := by
    rfl
  have hlarge : lift (IScalar.cast .I64 308#i32) = ok 308#i64 := by
    rfl
  have hmask : 18446744073709551615#u64 >>> 55#usize = ok 511#u64 := by
    rfl
  rw [hsmall] at hrun
  simp only [bind_tc_ok, IScalar.le_equiv] at hrun
  rw [show ((-342)#i64).val = -342 by rfl] at hrun
  simp only [hqlo, massert_True, bind_tc_ok] at hrun
  rw [hlarge] at hrun
  simp only [bind_tc_ok, IScalar.le_equiv] at hrun
  rw [show (308#i64).val = 308 by rfl] at hrun
  simp only [hqhi, massert_True, bind_tc_ok] at hrun
  rw [hmask, hdelta] at hrun
  simp only [bind_tc_ok] at hrun
  rw [hindex] at hrun
  simp only [bind_tc_ok] at hrun
  rw [htable] at hrun
  simp only [bind_tc_ok, uncurry_apply_pair] at hrun
  rcases full_spec w a with ⟨firstLo, firstHi, hfirst, hfirstProd,
    hfirstHi⟩
  rw [hfirst] at hrun
  simp only [bind_tc_ok, uncurry_apply_pair] at hrun
  have hand : lift (firstHi &&& 511#u64) = ok (firstHi &&& 511#u64) := by
    rfl
  rw [hand] at hrun
  simp only [bind_tc_ok] at hrun
  have hflow :
      (hi = firstHi ∧ lo = firstLo ∧
        firstHi.val % 512 ≠ 511 ∧
        hi.val * 2 ^ 64 + lo.val = w.val * a.val) ∨
      (hi.val * 2 ^ 64 + lo.val =
        w.val * a.val + (w.val * b.val) / 2 ^ 64) := by
    by_cases hrefine : firstHi &&& 511#u64 = 511#u64
    · rw [if_pos hrefine] at hrun
      rcases full_spec w b with ⟨secondLo, secondHi, hsecond,
        hsecondProd, hsecondHi⟩
      rw [hsecond] at hrun
      simp only [bind_tc_ok, uncurry_apply_pair] at hrun
      have hadd : lift (core.num.U64.wrapping_add firstLo secondHi) =
          ok (core.num.U64.wrapping_add firstLo secondHi) := by rfl
      rw [hadd] at hrun
      simp only [bind_tc_ok] at hrun
      let added := core.num.U64.wrapping_add firstLo secondHi
      have haddVal : added.val =
          (firstLo.val + secondHi.val) % 2 ^ 64 := by
        simpa [added, U64.size_eq] using
          core.num.U64.wrapping_add_val_eq firstLo secondHi
      by_cases hcarry : added.val < secondHi.val
      · rw [if_pos hcarry] at hrun
        cases hinc : firstHi + 1#u64 with
        | fail e =>
            rw [hinc] at hrun
            simp at hrun
        | div =>
            rw [hinc] at hrun
            simp at hrun
        | ok firstHi1 =>
            rw [hinc] at hrun
            simp only [bind_tc_ok] at hrun
            have hout : (added, firstHi1) = (lo, hi) := Result.ok.inj hrun
            have hlo : lo = added := (congrArg Prod.fst hout).symm
            have hhi : hi = firstHi1 := (congrArg Prod.snd hout).symm
            have hincVal : firstHi1.val = firstHi.val + 1 := by
              have hs := UScalar.add_equiv firstHi 1#u64
              rw [hinc] at hs
              exact hs.2.1
            right
            rw [hlo, hhi]
            have hfl : firstLo.val < 2 ^ 64 := by exact firstLo.bv.isLt
            have hsh : secondHi.val < 2 ^ 64 := by exact secondHi.bv.isLt
            have hsum2 : firstLo.val + secondHi.val < 2 * 2 ^ 64 := by
              omega
            have hsumge : 2 ^ 64 ≤ firstLo.val + secondHi.val := by
              by_contra hnot
              have hsumlt : firstLo.val + secondHi.val < 2 ^ 64 := by
                omega
              rw [haddVal, Nat.mod_eq_of_lt hsumlt] at hcarry
              omega
            have hdiv : (firstLo.val + secondHi.val) / 2 ^ 64 = 1 := by
              apply Nat.div_eq_of_lt_le <;> omega
            have hmod := Nat.mod_eq_sub_mul_div
              (x := firstLo.val + secondHi.val) (k := 2 ^ 64)
            rw [hdiv] at hmod
            have haddExact : added.val =
                firstLo.val + secondHi.val - 2 ^ 64 := by
              rw [haddVal, hmod]
              simp
            have haddPlus : added.val + 2 ^ 64 =
                firstLo.val + secondHi.val := by
              rw [haddExact, Nat.sub_add_cancel hsumge]
            calc
              firstHi1.val * 2 ^ 64 + added.val =
                  firstHi.val * 2 ^ 64 + (added.val + 2 ^ 64) := by
                    rw [hincVal]
                    ring
              _ = firstHi.val * 2 ^ 64 +
                  (firstLo.val + secondHi.val) := by rw [haddPlus]
              _ = (firstHi.val * 2 ^ 64 + firstLo.val) +
                  secondHi.val := by ring
              _ = w.val * a.val + secondHi.val := by rw [hfirstProd]
              _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
                rw [hsecondHi]
      · rw [if_neg hcarry] at hrun
        have hout : (added, firstHi) = (lo, hi) := Result.ok.inj hrun
        have hlo : lo = added := (congrArg Prod.fst hout).symm
        have hhi : hi = firstHi := (congrArg Prod.snd hout).symm
        right
        rw [hlo, hhi]
        have hfl : firstLo.val < 2 ^ 64 := by exact firstLo.bv.isLt
        have hsh : secondHi.val < 2 ^ 64 := by exact secondHi.bv.isLt
        have hsumlt : firstLo.val + secondHi.val < 2 ^ 64 := by
          by_contra hnot
          have hsum2 : firstLo.val + secondHi.val < 2 * 2 ^ 64 := by
            omega
          have hdiv : (firstLo.val + secondHi.val) / 2 ^ 64 = 1 := by
            apply Nat.div_eq_of_lt_le <;> omega
          have hmod := Nat.mod_eq_sub_mul_div
            (x := firstLo.val + secondHi.val) (k := 2 ^ 64)
          rw [hdiv] at hmod
          omega
        have haddExact : added.val = firstLo.val + secondHi.val := by
          rw [haddVal, Nat.mod_eq_of_lt hsumlt]
        calc
          firstHi.val * 2 ^ 64 + added.val =
              firstHi.val * 2 ^ 64 + (firstLo.val + secondHi.val) := by
                rw [haddExact]
          _ = (firstHi.val * 2 ^ 64 + firstLo.val) +
              secondHi.val := by ring
          _ = w.val * a.val + secondHi.val := by rw [hfirstProd]
          _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
            rw [hsecondHi]
    · rw [if_neg hrefine] at hrun
      have hout : (firstLo, firstHi) = (lo, hi) := Result.ok.inj hrun
      have hlo : lo = firstLo := (congrArg Prod.fst hout).symm
      have hhi : hi = firstHi := (congrArg Prod.snd hout).symm
      left
      refine ⟨hhi, hlo, ?_, ?_⟩
      · intro heq
        apply hrefine
        apply UScalar.eq_of_val_eq
        rw [and_mask, heq]
        rfl
      · simpa [hlo, hhi] using hfirstProd
  have hu : hi.val / 2 ^ 63 ≤ 1 := by
    have hh : hi.val < 2 ^ 64 := by exact hi.bv.isLt
    omega
  refine ⟨hu, ?_, ?_⟩
  · rcases hflow with hnr | href
    · exact Or.inl hnr.2.2.2
    · exact Or.inr href
  · let e : Int := 217706 * q.val / 65536 - q.val
    let D : Nat := a.val * 2 ^ 64 + b.val
    let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
    let R : Real := (w.val : Real) * T / (2 : Real) ^ (64 : Nat)
    let Y : Nat := hi.val * 2 ^ 64 + lo.val
    let u : Nat := hi.val / 2 ^ 63
    let s : Nat := 73 + u
    let d : Nat := 9 + u
    let G : Nat := hi.val / 2 ^ d
    let K : Nat := 2 ^ s
    change ((G : Real) * (2 : Real) ^ s ≤ R ∧
      R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s ∧
      ((R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1) ↔
        (-4 ≤ q.val ∧ q.val ≤ 23 ∧ G % 4 = 1 ∧
          lo.val ≤ 1 ∧ G * 2 ^ d = hi.val)))
    change (((2 : Real) ^ (127 : Nat) ≤ T ∧
        T < (2 : Real) ^ (128 : Nat)) ∧
      (q.val < 0 →
        ∃ n z : Nat,
          q.val = -(n : Int) ∧
          1 ≤ n ∧ 1 ≤ z ∧
          2 ^ (z - 1) < 5 ^ n ∧ 5 ^ n < 2 ^ z ∧
          e = -(z : Int) ∧
          T = (2 : Real) ^ (z + 127) / (5 : Real) ^ n) ∧
      ((0 ≤ q.val ∧ q.val ≤ 55) → (D : Real) = T) ∧
      ((0 ≤ q.val ∧ q.val ≤ 23) → b.val = 0) ∧
      ((q.val < -27 ∨ 0 ≤ q.val) →
        (D : Real) ≤ T ∧ T < (D : Real) + 1) ∧
      ((-27 ≤ q.val ∧ q.val < 0) →
        (D : Real) - 1 < T ∧ T < D)) at hcert
    rcases hcert with ⟨hTnorm, hneg, hexact, hbzero, hfloor, hceil⟩
    have hwB : w.val < 2 ^ 64 := by exact w.bv.isLt
    have hloB : lo.val < 2 ^ 64 := by exact lo.bv.isLt
    have hhiB : hi.val < 2 ^ 64 := by exact hi.bv.isLt
    have haB : a.val < 2 ^ 64 := by exact a.bv.isLt
    have hbB : b.val < 2 ^ 64 := by exact b.bv.isLt
    have hu' : u ≤ 1 := by simpa [u] using hu
    have hu_cases : u = 0 ∨ u = 1 := by omega
    have hYcell : G * K ≤ Y ∧ Y < (G + 1) * K := by
      rcases hu_cases with hu0 | hu1
      · have hdiv := Nat.div_add_mod hi.val 512
        have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 512)
        simp [G, d, u, hu0, K, s, Y] at *
        omega
      · have hdiv := Nat.div_add_mod hi.val 1024
        have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 1024)
        simp [G, d, u, hu1, K, s, Y] at *
        omega
    let X : Real := (w.val : Real) * (D : Real) / (2 : Real) ^ (64 : Nat)
    let F : Nat := (w.val * D) / 2 ^ 64
    have hF : F = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
      calc
        F = (2 ^ 64 * (w.val * a.val) + w.val * b.val) / 2 ^ 64 := by
          congr 1
          simp [F, D]
          ring
        _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
          exact Nat.mul_add_div (by positivity) _ _
    have div_bounds (N : Nat) :
        ((N / 2 ^ 64 : Nat) : Real) ≤
            (N : Real) / (2 : Real) ^ (64 : Nat) ∧
          (N : Real) / (2 : Real) ^ (64 : Nat) <
            ((N / 2 ^ 64 : Nat) : Real) + 1 := by
      let f := N / 2 ^ 64
      let r := N % 2 ^ 64
      have heq : 2 ^ 64 * f + r = N := by
        simpa [f, r] using Nat.div_add_mod N (2 ^ 64)
      have hr : r < 2 ^ 64 := Nat.mod_lt _ (by positivity)
      have heqR : ((2 ^ 64 * f + r : Nat) : Real) = (N : Real) := by
        exact_mod_cast heq
      have hrR : (r : Real) < (2 : Real) ^ (64 : Nat) := by
        exact_mod_cast hr
      have hrepr : (N : Real) / (2 : Real) ^ (64 : Nat) =
          (f : Real) + (r : Real) / (2 : Real) ^ (64 : Nat) := by
        rw [← heqR]
        push_cast
        norm_num
        ring_nf
      rw [hrepr]
      simp only [f]
      constructor
      · have hrnonneg : (0 : Real) ≤
            (r : Real) / (2 : Real) ^ (64 : Nat) := by positivity
        linarith
      · have hr0 : (0 : Real) ≤ r := by positivity
        have hp : (0 : Real) < (2 : Real) ^ (64 : Nat) := by positivity
        have := (div_lt_one hp).2 hrR
        linarith
    have hFX : (F : Real) ≤ X ∧ X < (F : Real) + 1 := by
      simpa [F, X] using div_bounds (w.val * D)
    have hw0R : (0 : Real) ≤ w.val := by positivity
    have hwposR : (0 : Real) < w.val := by
      exact_mod_cast (show 0 < w.val by omega)
    have hwratio : (w.val : Real) / (2 : Real) ^ (64 : Nat) < 1 := by
      rw [div_lt_one (by positivity)]
      exact_mod_cast hwB
    have hRfloor (hf : (D : Real) ≤ T ∧ T < (D : Real) + 1) :
        X ≤ R ∧
          R < X + (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
      have hmul1 := mul_le_mul_of_nonneg_left hf.1 hw0R
      have hmul2 := mul_lt_mul_of_pos_left hf.2 hwposR
      have hdiv1 := (div_le_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul1
      have hdiv2 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul2
      constructor
      · simpa [X, R] using hdiv1
      · have hid : (w.val : Real) * ((D : Real) + 1) /
              (2 : Real) ^ (64 : Nat) =
            X + (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
            simp [X]
            ring
        rw [hid] at hdiv2
        simpa [R] using hdiv2
    have hRceil (hc : (D : Real) - 1 < T ∧ T < D) :
        X - (w.val : Real) / (2 : Real) ^ (64 : Nat) < R ∧
          R < X := by
      have hmul1 := mul_lt_mul_of_pos_left hc.1 hwposR
      have hmul2 := mul_lt_mul_of_pos_left hc.2 hwposR
      have hdiv1 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul1
      have hdiv2 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul2
      have hid : (w.val : Real) * ((D : Real) - 1) /
              (2 : Real) ^ (64 : Nat) =
            X - (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
            simp [X]
            ring
      rw [hid] at hdiv1
      constructor
      · simpa [R] using hdiv1
      · simpa [X, R] using hdiv2
    have bpos_of_ceil (hq : -27 ≤ q.val ∧ q.val < 0) : 1 ≤ b.val := by
      rcases hneg hq.2 with
        ⟨n, z, hqn, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
      have hn27 : n ≤ 27 := by omega
      have hPbound : 5 ^ n < 2 ^ 64 := by
        have hp := Nat.pow_le_pow_right (by norm_num : 0 < 5) hn27
        exact lt_of_le_of_lt hp (by norm_num)
      by_contra hbnot
      have hb0 : b.val = 0 := by omega
      have hc := hceil hq
      have hpR : (0 : Real) < (5 : Real) ^ n := by positivity
      have hlowR : ((D : Real) - 1) * (5 : Real) ^ n <
          (2 : Real) ^ (z + 127) := by
        apply (lt_div_iff₀ hpR).mp
        simpa [hTrep] using hc.1
      have hhighR : (2 : Real) ^ (z + 127) <
          (D : Real) * (5 : Real) ^ n := by
        apply (div_lt_iff₀ hpR).mp
        simpa [hTrep] using hc.2
      have hlowR' : (D : Real) * (5 : Real) ^ n <
          (2 : Real) ^ (z + 127) + (5 : Real) ^ n := by
        nlinarith
      have hlowN : D * 5 ^ n < 2 ^ (z + 127) + 5 ^ n := by
        exact_mod_cast hlowR'
      have hhighN : 2 ^ (z + 127) < D * 5 ^ n := by
        exact_mod_cast hhighR
      have hzsum : z + 127 = 64 + (z + 63) := by omega
      have hNmul : 2 ^ (z + 127) = 2 ^ 64 * 2 ^ (z + 63) := by
        rw [hzsum, pow_add]
      have hDmul : D * 5 ^ n = 2 ^ 64 * (a.val * 5 ^ n) := by
        simp [D, hb0]
        ring
      rw [hNmul, hDmul] at hlowN hhighN
      omega
    have hKcast : (K : Real) = (2 : Real) ^ s := by simp [K]
    have hKfac : K = 2 ^ 64 * 2 ^ d := by
      have hsd : s = 64 + d := by simp [s, d]; omega
      change 2 ^ s = 2 ^ 64 * 2 ^ d
      rw [hsd, pow_add]
    have hYcellR : (G : Real) * (K : Real) ≤ (Y : Real) ∧
        (Y : Real) < ((G + 1 : Nat) : Real) * (K : Real) := by
      constructor
      · exact_mod_cast hYcell.1
      · exact_mod_cast hYcell.2
    have nonref_cell
        (hnr : hi = firstHi ∧ lo = firstLo ∧
          firstHi.val % 512 ≠ 511 ∧
          hi.val * 2 ^ 64 + lo.val = w.val * a.val) :
        (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      have hY : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
      have hYreal : (Y : Real) = ((w.val * a.val : Nat) : Real) := by
        exact_mod_cast hY
      have hXid : X = (Y : Real) +
          ((w.val * b.val : Nat) : Real) / (2 : Real) ^ (64 : Nat) := by
        calc
          X = ((w.val * a.val : Nat) : Real) +
              ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) := by
                simp [X, D]
                ring
          _ = (Y : Real) + ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) := by rw [hYreal]
      have hYR : (Y : Real) ≤ R ∧
          R < (Y : Real) + (2 : Real) ^ (64 : Nat) := by
        by_cases hqf : q.val < -27 ∨ 0 ≤ q.val
        · have hr := hRfloor (hfloor hqf)
          have hb1 : b.val + 1 ≤ 2 ^ 64 := by omega
          have hprodN : w.val * (b.val + 1) < 2 ^ 64 * 2 ^ 64 := by
            calc
              w.val * (b.val + 1) < 2 ^ 64 * (b.val + 1) :=
                Nat.mul_lt_mul_of_pos_right hwB (by omega)
              _ ≤ 2 ^ 64 * 2 ^ 64 := Nat.mul_le_mul_left _ hb1
          have hprodR : ((w.val * (b.val + 1) : Nat) : Real) <
              (2 : Real) ^ (64 : Nat) * (2 : Real) ^ (64 : Nat) := by
            exact_mod_cast hprodN
          have hcorr : ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) +
              (w.val : Real) / (2 : Real) ^ (64 : Nat) <
                (2 : Real) ^ (64 : Nat) := by
            rw [← add_div]
            apply (div_lt_iff₀ (show (0 : Real) <
              (2 : Real) ^ (64 : Nat) by positivity)).2
            norm_num at hprodR ⊢
            convert hprodR using 1 <;> ring
          rw [hXid] at hr
          constructor
          · have hnonneg : (0 : Real) ≤
                ((w.val * b.val : Nat) : Real) /
                  (2 : Real) ^ (64 : Nat) := by positivity
            linarith
          · linarith
        · have hqc : -27 ≤ q.val ∧ q.val < 0 := by omega
          have hbpos := bpos_of_ceil hqc
          have hr := hRceil (hceil hqc)
          have hwbN : w.val ≤ w.val * b.val := by
            have hm := Nat.mul_le_mul_left w.val hbpos
            simpa using hm
          have hwbR : (w.val : Real) ≤
              ((w.val * b.val : Nat) : Real) := by exact_mod_cast hwbN
          rw [hXid] at hr
          constructor
          · have hp : (0 : Real) < (2 : Real) ^ (64 : Nat) := by
              positivity
            have hdiv := (div_le_div_iff_of_pos_right hp).2 hwbR
            linarith
          · have hterm : ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) < (2 : Real) ^ (64 : Nat) := by
              apply (div_lt_iff₀ (show (0 : Real) <
                (2 : Real) ^ (64 : Nat) by positivity)).2
              have hprod : w.val * b.val < 2 ^ 64 * 2 ^ 64 := by
                calc
                  w.val * b.val < 2 ^ 64 * b.val :=
                    Nat.mul_lt_mul_of_pos_right hwB (by omega)
                  _ < 2 ^ 64 * 2 ^ 64 :=
                    Nat.mul_lt_mul_of_pos_left hbB (by positivity)
              exact_mod_cast hprod
            linarith
      have hmaskFail : hi.val % 512 ≠ 511 := by simpa [hnr.1] using hnr.2.2.1
      have hgap : Y + 2 ^ 64 ≤ (G + 1) * K := by
        rcases hu_cases with hu0 | hu1
        · have hdiv := Nat.div_add_mod hi.val 512
          have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 512)
          simp [G, d, u, hu0, K, s, Y]
          omega
        · have hdiv := Nat.div_add_mod hi.val 1024
          have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 1024)
          simp [G, d, u, hu1, K, s, Y]
          omega
      have hgapR : (Y : Real) + (2 : Real) ^ (64 : Nat) ≤
          ((G + 1 : Nat) : Real) * (K : Real) := by
        exact_mod_cast hgap
      rw [← hKcast]
      constructor <;> linarith [hYcellR.1]
    have refine_cell
        (href : hi.val * 2 ^ 64 + lo.val =
          w.val * a.val + (w.val * b.val) / 2 ^ 64) :
        (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      have hYF : Y = F := by
        rw [hF]
        simpa [Y] using href
      by_cases hqf : q.val < -27 ∨ 0 ≤ q.val
      · have hr := hRfloor (hfloor hqf)
        have hRlo : (Y : Real) ≤ R := by
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          linarith [hFX.1]
        have hRnear : R < (Y : Real) + 2 := by
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          linarith [hFX.2, hwratio]
        have hRhi : R < ((G + 1 : Nat) : Real) * (K : Real) := by
          by_contra hnot
          have hLle : ((G + 1 : Nat) : Real) * (K : Real) ≤ R :=
            le_of_not_gt hnot
          have hsuccN : (G + 1) * K = Y + 1 := by
            have h1 : Y < (G + 1) * K := hYcell.2
            have h2R : (((G + 1) * K : Nat) : Real) <
                ((Y + 2 : Nat) : Real) := by
              have hLle' : (((G + 1) * K : Nat) : Real) ≤ R := by
                push_cast
                simpa using hLle
              exact lt_of_le_of_lt hLle' (by norm_num; simpa using hRnear)
            have h2 : (G + 1) * K < Y + 2 := by exact_mod_cast h2R
            omega
          have hloMax : lo.val = 2 ^ 64 - 1 := by
            change (G + 1) * K = hi.val * 2 ^ 64 + lo.val + 1 at hsuccN
            have hsuccN' : 2 ^ 64 * ((G + 1) * 2 ^ d) =
                hi.val * 2 ^ 64 + lo.val + 1 := by
              rw [← hsuccN, hKfac]
              ring
            omega
          have hqsent : -27 ≤ q.val ∧ q.val ≤ 55 := by
            rcases hsent with hne | hs
            · exact False.elim (hne hloMax)
            · exact hs
          have hqnonneg : 0 ≤ q.val := by rcases hqf with h | h <;> omega
          have hTexact := hexact ⟨hqnonneg, hqsent.2⟩
          have hRX : R = X := by
            simp [R, X, hTexact]
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          have hL : (((G + 1) * K : Nat) : Real) = ((Y + 1 : Nat) : Real) := by
            exact_mod_cast hsuccN
          have hRlt : R < (Y : Real) + 1 := by linarith [hFX.2]
          have hL' : ((G + 1 : Nat) : Real) * (K : Real) =
              (Y : Real) + 1 := by
            push_cast at hL ⊢
            exact hL
          linarith
        rw [← hKcast]
        exact ⟨le_trans hYcellR.1 hRlo, hRhi⟩
      · have hqc : -27 ≤ q.val ∧ q.val < 0 := by omega
        have hr := hRceil (hceil hqc)
        have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
        have hRnear : (Y : Real) - 1 < R ∧ R < (Y : Real) + 1 := by
          constructor <;> linarith [hFX.1, hFX.2, hwratio]
        have hboundary (hbnd : Y = G * K) : R = (Y : Real) := by
          rcases hneg hqc.2 with
            ⟨n, z, hqn, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
          have hn27 : n ≤ 27 := by omega
          have hPbound : 5 ^ n < 2 ^ 64 := by
            have hp := Nat.pow_le_pow_right (by norm_num : 0 < 5) hn27
            exact lt_of_le_of_lt hp (by norm_num)
          have hdenposR : (0 : Real) <
              (((2 ^ 64) * 5 ^ n : Nat) : Real) := by positivity
          have hRrep : R = ((w.val * 2 ^ (z + 127) : Nat) : Real) /
              (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [hTrep]
            push_cast
            ring_nf
          by_contra hne
          rcases lt_or_gt_of_ne hne with hdown | hup
          · have hnumltR : ((w.val * 2 ^ (z + 127) : Nat) : Real) <
                ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) := by
              rw [hRrep] at hdown
              have hd := (div_lt_iff₀ hdenposR).mp hdown
              push_cast at hd ⊢
              nlinarith
            have hnumhiR : ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) <
                ((w.val * 2 ^ (z + 127) : Nat) : Real) +
                  (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
              have hnear := hRnear.1
              rw [hRrep] at hnear
              have hd := (lt_div_iff₀ hdenposR).mp hnear
              push_cast at hd ⊢
              nlinarith
            have hnumlt : w.val * 2 ^ (z + 127) <
                Y * 2 ^ 64 * 5 ^ n := by exact_mod_cast hnumltR
            have hnumhi : Y * 2 ^ 64 * 5 ^ n <
                w.val * 2 ^ (z + 127) + 2 ^ 64 * 5 ^ n := by
              exact_mod_cast hnumhiR
            have hzsum : z + 127 = 128 + (z - 1) := by omega
            have hNmul : 2 ^ (z + 127) = 2 ^ 128 * 2 ^ (z - 1) := by
              rw [hzsum, pow_add]
            have hBsq : 2 ^ 128 = 2 ^ 64 * 2 ^ 64 := by norm_num
            have hwfac : w.val * 2 ^ (z + 127) =
                (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) := by
              rw [hNmul, hBsq]
              ring
            have hYfac : Y * 2 ^ 64 * 5 ^ n =
                (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) := by
              rw [hbnd, hKfac]
              ring
            rw [hwfac, hYfac] at hnumlt hnumhi
            have hAC : w.val * 2 ^ (z - 1) < G * 2 ^ d * 5 ^ n := by
              exact (Nat.mul_lt_mul_left (by positivity :
                0 < 2 ^ 64 * 2 ^ 64)).mp hnumlt
            have hstep : w.val * 2 ^ (z - 1) + 1 ≤
                G * 2 ^ d * 5 ^ n := by omega
            have hBP : 2 ^ 64 * 5 ^ n < 2 ^ 64 * 2 ^ 64 :=
              Nat.mul_lt_mul_of_pos_left hPbound (by positivity)
            have hlower := Nat.mul_le_mul_left (2 ^ 64 * 2 ^ 64) hstep
            have hcontra : (2 ^ 64 * 2 ^ 64) *
                  (G * 2 ^ d * 5 ^ n) <
                (2 ^ 64 * 2 ^ 64) *
                  (G * 2 ^ d * 5 ^ n) := by
              calc
                _ < (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) +
                    2 ^ 64 * 5 ^ n := hnumhi
                _ < (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) +
                    2 ^ 64 * 2 ^ 64 := Nat.add_lt_add_left hBP _
                _ = (2 ^ 64 * 2 ^ 64) *
                    (w.val * 2 ^ (z - 1) + 1) := by ring
                _ ≤ _ := hlower
            exact (lt_irrefl _ hcontra)
          · have hnumgtR : ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) <
                ((w.val * 2 ^ (z + 127) : Nat) : Real) := by
              rw [hRrep] at hup
              have hd := (lt_div_iff₀ hdenposR).mp hup
              push_cast at hd ⊢
              nlinarith
            have hnumloR : ((w.val * 2 ^ (z + 127) : Nat) : Real) <
                ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) +
                  (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
              have hnear := hRnear.2
              rw [hRrep] at hnear
              have hd := (div_lt_iff₀ hdenposR).mp hnear
              push_cast at hd ⊢
              nlinarith
            have hnumgt : Y * 2 ^ 64 * 5 ^ n <
                w.val * 2 ^ (z + 127) := by exact_mod_cast hnumgtR
            have hnumlo : w.val * 2 ^ (z + 127) <
                Y * 2 ^ 64 * 5 ^ n + 2 ^ 64 * 5 ^ n := by
              exact_mod_cast hnumloR
            have hzsum : z + 127 = 128 + (z - 1) := by omega
            have hNmul : 2 ^ (z + 127) = 2 ^ 128 * 2 ^ (z - 1) := by
              rw [hzsum, pow_add]
            have hBsq : 2 ^ 128 = 2 ^ 64 * 2 ^ 64 := by norm_num
            have hwfac : w.val * 2 ^ (z + 127) =
                (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) := by
              rw [hNmul, hBsq]
              ring
            have hYfac : Y * 2 ^ 64 * 5 ^ n =
                (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) := by
              rw [hbnd, hKfac]
              ring
            rw [hwfac, hYfac] at hnumgt hnumlo
            have hCA : G * 2 ^ d * 5 ^ n < w.val * 2 ^ (z - 1) := by
              exact (Nat.mul_lt_mul_left (by positivity :
                0 < 2 ^ 64 * 2 ^ 64)).mp hnumgt
            have hstep : G * 2 ^ d * 5 ^ n + 1 ≤
                w.val * 2 ^ (z - 1) := by omega
            have hBP : 2 ^ 64 * 5 ^ n < 2 ^ 64 * 2 ^ 64 :=
              Nat.mul_lt_mul_of_pos_left hPbound (by positivity)
            have hlower := Nat.mul_le_mul_left (2 ^ 64 * 2 ^ 64) hstep
            have hcontra : (2 ^ 64 * 2 ^ 64) *
                  (w.val * 2 ^ (z - 1)) <
                (2 ^ 64 * 2 ^ 64) *
                  (w.val * 2 ^ (z - 1)) := by
              calc
                _ < (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) +
                    2 ^ 64 * 5 ^ n := hnumlo
                _ < (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) +
                    2 ^ 64 * 2 ^ 64 := Nat.add_lt_add_left hBP _
                _ = (2 ^ 64 * 2 ^ 64) *
                    (G * 2 ^ d * 5 ^ n + 1) := by ring
                _ ≤ _ := hlower
            exact (lt_irrefl _ hcontra)
        have hRlo : (G : Real) * (K : Real) ≤ R := by
          by_cases hbnd : Y = G * K
          · have heq := hboundary hbnd
            rw [heq]
            exact_mod_cast (show G * K ≤ Y by omega)
          · have hnext : G * K + 1 ≤ Y := by omega
            have hnextR : (G : Real) * (K : Real) + 1 ≤ (Y : Real) := by
              exact_mod_cast hnext
            linarith
        have hRhi : R < ((G + 1 : Nat) : Real) * (K : Real) := by
          have hnext : Y + 1 ≤ (G + 1) * K := by omega
          have hnextR : (Y : Real) + 1 ≤
              ((G + 1 : Nat) : Real) * (K : Real) := by
            exact_mod_cast hnext
          linarith
        rw [← hKcast]
        exact ⟨hRlo, hRhi⟩
    have hcell : (G : Real) * (2 : Real) ^ s ≤ R ∧
        R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      rcases hflow with hnr | href
      · exact nonref_cell hnr
      · exact refine_cell href
    refine ⟨hcell.1, hcell.2, ?_⟩
    constructor
    · rintro ⟨hmid, hGmod⟩
      have hGodd : Odd G := by
        refine ⟨G / 2, ?_⟩
        omega
      have hwne : w.val ≠ 0 := by omega
      rcases Nat.exists_eq_two_pow_mul_odd hwne with ⟨v, h, hhodd, hwfac⟩
      have hhpos : 1 ≤ h := by
        rcases hhodd with ⟨m, hm⟩
        omega
      have hv63 : v ≤ 63 := by
        by_contra hv
        have hp : 2 ^ 64 ≤ 2 ^ v :=
          Nat.pow_le_pow_right (by norm_num) (by omega)
        have hmul : 2 ^ v ≤ 2 ^ v * h := by
          have hm := Nat.mul_le_mul_left (2 ^ v) hhpos
          simpa using hm
        rw [← hwfac] at hmul
        omega
      have hqrange : -4 ≤ q.val ∧ q.val ≤ 23 := by
        by_cases hqn : q.val < 0
        · rcases hneg hqn with
            ⟨n, z, hqneg, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
          have hmidR : (w.val : Real) * T /
              (2 : Real) ^ (64 : Nat) =
                (G : Real) * (2 : Real) ^ s := by simpa [R] using hmid
          rw [hTrep] at hmidR
          have hrepr : (w.val : Real) *
                ((2 : Real) ^ (z + 127) / (5 : Real) ^ n) /
                (2 : Real) ^ (64 : Nat) =
              ((w.val * 2 ^ (z + 127) : Nat) : Real) /
                (((5 ^ n) * 2 ^ 64 : Nat) : Real) := by
            push_cast
            ring
          rw [hrepr] at hmidR
          have hcross := (div_eq_iff (by positivity :
            ((((5 ^ n) * 2 ^ 64 : Nat) : Real)) ≠ 0)).mp hmidR
          have hrhs : (G : Real) * (2 : Real) ^ s *
                (((5 ^ n) * 2 ^ 64 : Nat) : Real) =
              (((G * 2 ^ s) * (5 ^ n * 2 ^ 64) : Nat) : Real) := by
            push_cast
            ring
          rw [hrhs] at hcross
          have hnat : w.val * 2 ^ (z + 127) =
              (G * 2 ^ s) * (5 ^ n * 2 ^ 64) := by exact_mod_cast hcross
          have hnat' : 2 ^ (v + z + 127) * h =
              2 ^ (s + 64) * (G * 5 ^ n) := by
            calc
              2 ^ (v + z + 127) * h =
                  (2 ^ v * h) * 2 ^ (z + 127) := by
                    rw [show v + z + 127 = v + (z + 127) by omega,
                      pow_add]
                    ring
              _ = w.val * 2 ^ (z + 127) := by rw [← hwfac]
              _ = (G * 2 ^ s) * (5 ^ n * 2 ^ 64) := hnat
              _ = 2 ^ (s + 64) * (G * 5 ^ n) := by
                rw [pow_add]
                ring
          have hodd5 : Odd (5 ^ n) := (by decide : Odd (5 : Nat)).pow
          have hexp : v + z + 127 = s + 64 :=
            two_adic_unique hhodd (hGodd.mul hodd5) hnat'
          have hz11 : z ≤ 11 := by
            have hs74 : s ≤ 74 := by simp [s]; omega
            omega
          have hn4 : n ≤ 4 := by
            by_contra hn
            have hp5 : 5 ^ 5 ≤ 5 ^ n :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            have hp2 : 2 ^ z ≤ 2 ^ 11 :=
              Nat.pow_le_pow_right (by norm_num) hz11
            norm_num at hp5 hp2
            omega
          omega
        · have hqnonneg : 0 ≤ q.val := by omega
          rcases Int.eq_ofNat_of_zero_le hqnonneg with ⟨qn, hqnat⟩
          have hmidR : (w.val : Real) * T /
              (2 : Real) ^ (64 : Nat) =
                (G : Real) * (2 : Real) ^ s := by simpa [R] using hmid
          have he53 : e ≤ 53 := by
            by_cases hk : 0 ≤ 127 - e
            · rcases Int.eq_ofNat_of_zero_le hk with ⟨kn, hkn⟩
              have hTpos : T = (2 : Real) ^ kn * (5 : Real) ^ qn := by
                simp [T, hqnat, hkn, zpow_ofNat]
              rw [hTpos] at hmidR
              have hrepr : (w.val : Real) *
                    ((2 : Real) ^ kn * (5 : Real) ^ qn) /
                    (2 : Real) ^ (64 : Nat) =
                  (((2 ^ kn) * (w.val * 5 ^ qn) : Nat) : Real) /
                    ((2 ^ 64 : Nat) : Real) := by
                push_cast
                ring
              rw [hrepr] at hmidR
              have hcross := (div_eq_iff (by positivity :
                (((2 ^ 64 : Nat) : Real)) ≠ 0)).mp hmidR
              have hrhs : (G : Real) * (2 : Real) ^ s *
                    ((2 ^ 64 : Nat) : Real) =
                  ((2 ^ (s + 64) * G : Nat) : Real) := by
                push_cast
                rw [pow_add]
                ring
              rw [hrhs] at hcross
              have hnat : 2 ^ kn * (w.val * 5 ^ qn) =
                  2 ^ (s + 64) * G := by exact_mod_cast hcross
              have hnat' : 2 ^ (kn + v) * (h * 5 ^ qn) =
                  2 ^ (s + 64) * G := by
                calc
                  2 ^ (kn + v) * (h * 5 ^ qn) =
                      2 ^ kn * ((2 ^ v * h) * 5 ^ qn) := by
                        rw [pow_add]
                        ring
                  _ = 2 ^ kn * (w.val * 5 ^ qn) := by rw [← hwfac]
                  _ = 2 ^ (s + 64) * G := hnat
              have hodd5 : Odd (5 ^ qn) :=
                (by decide : Odd (5 : Nat)).pow
              have hexp : kn + v = s + 64 :=
                two_adic_unique (hhodd.mul hodd5) hGodd hnat'
              have hs73 : 73 ≤ s := by simp [s]
              omega
            · have hk' : 0 ≤ e - 127 := by omega
              rcases Int.eq_ofNat_of_zero_le hk' with ⟨kn, hkn⟩
              have hkneg : 127 - e = -(kn : Int) := by omega
              have hTneg : T = (2 : Real) ^ (-(kn : Int)) *
                  (5 : Real) ^ qn := by simp [T, hqnat, hkneg, zpow_ofNat]
              rw [hTneg] at hmidR
              have hpow : (2 : Real) ^ (-(kn : Int)) =
                  1 / (2 : Real) ^ kn := by
                rw [zpow_neg, zpow_natCast]
                simp
              rw [hpow] at hmidR
              have hrepr : (w.val : Real) *
                    (1 / (2 : Real) ^ kn * (5 : Real) ^ qn) /
                    (2 : Real) ^ (64 : Nat) =
                  ((w.val * 5 ^ qn : Nat) : Real) /
                    ((2 ^ (kn + 64) : Nat) : Real) := by
                push_cast
                rw [pow_add]
                ring
              rw [hrepr] at hmidR
              have hcross := (div_eq_iff (by positivity :
                (((2 ^ (kn + 64) : Nat) : Real)) ≠ 0)).mp hmidR
              have hrhs : (G : Real) * (2 : Real) ^ s *
                    ((2 ^ (kn + 64) : Nat) : Real) =
                  ((2 ^ (s + 64 + kn) * G : Nat) : Real) := by
                push_cast
                rw [show s + 64 + kn = s + (kn + 64) by omega, pow_add]
                ring
              rw [hrhs] at hcross
              have hnat : w.val * 5 ^ qn =
                  2 ^ (s + 64 + kn) * G := by exact_mod_cast hcross
              have hnat' : 2 ^ v * (h * 5 ^ qn) =
                  2 ^ (s + 64 + kn) * G := by
                calc
                  2 ^ v * (h * 5 ^ qn) = (2 ^ v * h) * 5 ^ qn := by ring
                  _ = w.val * 5 ^ qn := by rw [← hwfac]
                  _ = 2 ^ (s + 64 + kn) * G := hnat
              have hodd5 : Odd (5 ^ qn) :=
                (by decide : Odd (5 : Nat)).pow
              have hexp : v = s + 64 + kn :=
                two_adic_unique (hhodd.mul hodd5) hGodd hnat'
              have hs73 : 73 ≤ s := by simp [s]
              omega
          have heDef : e = 217706 * q.val / 65536 - q.val := rfl
          omega
      have hmidK : R = ((G * K : Nat) : Real) := by
        rw [hmid]
        push_cast
        rw [hKcast]
      have hYL : Y = G * K := by
        by_cases hqn : q.val < 0
        · have hceilq := hceil ⟨by omega, hqn⟩
          have hrc := hRceil hceilq
          have hFLt : F < G * K + 1 := by
            have hreal : (F : Real) < ((G * K : Nat) : Real) + 1 := by
              rw [← hmidK]
              linarith [hFX.1, hrc.1, hwratio]
            exact_mod_cast hreal
          have hLLt : G * K < F + 1 := by
            have hreal : ((G * K : Nat) : Real) < (F : Real) + 1 := by
              rw [← hmidK]
              linarith [hFX.2, hrc.2]
            exact_mod_cast hreal
          have hFL : F = G * K := by omega
          rcases hflow with hnr | href
          · have hYwa : Y = w.val * a.val := by
              simpa [Y] using hnr.2.2.2
            let c : Nat := w.val * b.val / 2 ^ 64
            have hFc : F = w.val * a.val + c := by simpa [c] using hF
            by_cases hc : c = 0
            · calc
                Y = w.val * a.val := hYwa
                _ = w.val * a.val + c := by simp [hc]
                _ = F := hFc.symm
                _ = G * K := hFL
            · have hcpos : 0 < c := Nat.pos_of_ne_zero hc
              have hcellLower : G * K ≤ Y := hYcell.1
              exfalso
              apply (Nat.not_lt_of_ge hcellLower)
              calc
                Y < Y + c := Nat.lt_add_of_pos_right hcpos
                _ = F := by rw [hYwa, hFc]
                _ = G * K := hFL
          · have hYF : Y = F := by
              simpa [Y, hF] using href
            exact hYF.trans hFL
        · have hqnonneg : 0 ≤ q.val := by omega
          have hTex := hexact ⟨hqnonneg, by omega⟩
          have hb0 := hbzero ⟨hqnonneg, hqrange.2⟩
          have hYwa : Y = w.val * a.val := by
            rcases hflow with hnr | href
            · simpa [Y] using hnr.2.2.2
            · simpa [Y, hb0] using href
          have hRwa : R = ((w.val * a.val : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [← hTex]
            simp [D, hb0]
            push_cast
            norm_num
            ring
          have hcast : (Y : Real) = ((G * K : Nat) : Real) := by
            rw [hYwa, ← hRwa, hmidK]
          exact_mod_cast hcast
      refine ⟨hqrange.1, hqrange.2, hGmod, ?_, ?_⟩
      · have hm := congrArg (fun x : Nat => x % (2 ^ 64)) hYL
        simp [Y, hKfac, Nat.add_mod, Nat.mul_mod,
          Nat.mod_eq_of_lt hloB] at hm
        omega
      · have hlo0 : lo.val = 0 := by
          have hm := congrArg (fun x : Nat => x % (2 ^ 64)) hYL
          simp [Y, hKfac, Nat.add_mod, Nat.mul_mod,
            Nat.mod_eq_of_lt hloB] at hm
          omega
        have heq : 2 ^ 64 * hi.val = 2 ^ 64 * (G * 2 ^ d) := by
          rw [show 2 ^ 64 * hi.val = Y by simp [Y, hlo0, mul_comm]]
          rw [hYL, hKfac]
          ring
        exact Nat.eq_of_mul_eq_mul_left (by positivity) heq.symm
    · rintro ⟨hqlo4, hqhi23, hGmod, hlo1, hrec⟩
      have hrec' : G * 2 ^ d = hi.val := by simpa [d] using hrec
      have hYLlo : Y = G * K + lo.val := by
        simp only [Y]
        rw [hKfac, ← hrec']
        ring
      by_cases hqn : q.val < 0
      · rcases hneg hqn with
          ⟨n, z, hqneg, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
        have hn4 : n ≤ 4 := by omega
        have hz11 : z ≤ 11 := by
          by_contra hz
          have hp2 : 2 ^ 11 ≤ 2 ^ (z - 1) :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hp5 : 5 ^ n ≤ 5 ^ 4 :=
            Nat.pow_le_pow_right (by norm_num) hn4
          norm_num at hp2 hp5
          omega
        have hceilq := hceil ⟨by omega, hqn⟩
        have hnz : (n = 1 ∧ z = 3) ∨ (n = 2 ∧ z = 5) ∨
            (n = 3 ∧ z = 7) ∨ (n = 4 ∧ z = 10) := by
          have hnCases : n = 1 ∨ n = 2 ∨ n = 3 ∨ n = 4 := by omega
          rcases hnCases with h1 | h2 | h3 | h4
          · left
            refine ⟨h1, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; left
            refine ⟨h2, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; right; left
            refine ⟨h3, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; right; right
            refine ⟨h4, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
        have finish_refined (delta : Nat) (hdelta0 : 0 < delta)
            (hdelta625 : delta ≤ 625)
            (hDrel : D * 5 ^ n = 2 ^ (z + 127) + delta)
            (href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64) :
            R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1 := by
          let c : Nat := w.val * b.val / 2 ^ 64
          let r : Nat := w.val * b.val % 2 ^ 64
          have hrB : r < 2 ^ 64 := Nat.mod_lt _ (by positivity)
          have hdivmod : w.val * b.val = c * 2 ^ 64 + r := by
            calc
              w.val * b.val = 2 ^ 64 * (w.val * b.val / 2 ^ 64) +
                  w.val * b.val % 2 ^ 64 := (Nat.div_add_mod _ _).symm
              _ = c * 2 ^ 64 + r := by simp only [c, r]; ring
          have hYref : Y = w.val * a.val + c := by simpa [Y, c] using href
          have hwD : w.val * D = 2 ^ 64 * (w.val * a.val + c) + r := by
            change w.val * (a.val * 2 ^ 64 + b.val) = _
            calc
              w.val * (a.val * 2 ^ 64 + b.val) =
                  2 ^ 64 * (w.val * a.val) + w.val * b.val := by ring
              _ = 2 ^ 64 * (w.val * a.val) + (c * 2 ^ 64 + r) := by
                rw [hdivmod]
              _ = 2 ^ 64 * (w.val * a.val + c) + r := by ring
          have hbalance :
              w.val * 2 ^ (z + 127) + w.val * delta =
                2 ^ 64 * (G * K) * 5 ^ n +
                  (2 ^ 64 * lo.val + r) * 5 ^ n := by
            calc
              w.val * 2 ^ (z + 127) + w.val * delta =
                  w.val * (2 ^ (z + 127) + delta) := by ring
              _ = w.val * (D * 5 ^ n) := by rw [hDrel]
              _ = (w.val * D) * 5 ^ n := by ring
              _ = (2 ^ 64 * (w.val * a.val + c) + r) * 5 ^ n := by
                rw [hwD]
              _ = (2 ^ 64 * Y + r) * 5 ^ n := by rw [hYref]
              _ = 2 ^ 64 * (G * K) * 5 ^ n +
                    (2 ^ 64 * lo.val + r) * 5 ^ n := by
                rw [hYLlo]
                ring
          have hP625 : 5 ^ n ≤ 625 := by
            have hp := Nat.pow_le_pow_right (by norm_num : 0 < (5 : Nat)) hn4
            norm_num at hp ⊢
            exact hp
          have hN128 : 2 ^ 128 ≤ 2 ^ (z + 127) :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hwd : w.val * delta < 2 ^ (z + 127) := by
            have h1 : w.val * delta < 2 ^ 64 * delta :=
              Nat.mul_lt_mul_of_pos_right hwB hdelta0
            have h2 : 2 ^ 64 * delta ≤ 2 ^ 64 * 625 :=
              Nat.mul_le_mul_left _ hdelta625
            have h3 : 2 ^ 64 * 625 < 2 ^ 128 := by norm_num
            omega
          have hcorr : (2 ^ 64 * lo.val + r) * 5 ^ n <
              2 ^ (z + 127) := by
            have ht : 2 ^ 64 * lo.val + r ≤ 2 * 2 ^ 64 := by omega
            have hm := Nat.mul_le_mul ht hP625
            have hnum : (2 * 2 ^ 64) * 625 < 2 ^ 128 := by norm_num
            omega
          have hNK : 2 ^ (z + 127) ∣ 2 ^ 64 * K := by
            have hp : 2 ^ (z + 127) ∣ 2 ^ (64 + s) :=
              pow_dvd_pow 2 (by simp [s]; omega)
            simpa [K, pow_add] using hp
          have hbase : 2 ^ (z + 127) ∣
              2 ^ 64 * (G * K) * 5 ^ n := by
            rcases hNK with ⟨j, hj⟩
            use G * j * 5 ^ n
            calc
              2 ^ 64 * (G * K) * 5 ^ n = G * (2 ^ 64 * K) * 5 ^ n := by ring
              _ = G * (2 ^ (z + 127) * j) * 5 ^ n := by rw [hj]
              _ = 2 ^ (z + 127) * (G * j * 5 ^ n) := by ring
          have hwN : 2 ^ (z + 127) ∣ w.val * 2 ^ (z + 127) := by
            exact dvd_mul_left _ _
          have hm : (w.val * delta) % 2 ^ (z + 127) =
              ((2 ^ 64 * lo.val + r) * 5 ^ n) % 2 ^ (z + 127) := by
            calc
              (w.val * delta) % 2 ^ (z + 127) =
                  (w.val * 2 ^ (z + 127) + w.val * delta) %
                    2 ^ (z + 127) := by
                rw [Nat.add_mod, Nat.mod_eq_zero_of_dvd hwN]
                simp
              _ = (2 ^ 64 * (G * K) * 5 ^ n +
                    (2 ^ 64 * lo.val + r) * 5 ^ n) %
                    2 ^ (z + 127) :=
                congrArg (fun x : Nat => x % 2 ^ (z + 127)) hbalance
              _ = ((2 ^ 64 * lo.val + r) * 5 ^ n) %
                    2 ^ (z + 127) := by
                rw [Nat.add_mod, Nat.mod_eq_zero_of_dvd hbase]
                simp
          rw [Nat.mod_eq_of_lt hwd, Nat.mod_eq_of_lt hcorr] at hm
          have hcorreq : w.val * delta =
              (2 ^ 64 * lo.val + r) * 5 ^ n := hm
          rw [hcorreq] at hbalance
          have hnat : w.val * 2 ^ (z + 127) =
              2 ^ 64 * (G * K) * 5 ^ n := Nat.add_right_cancel hbalance
          have hreal : R = ((G * K : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [hTrep]
            calc
              (w.val : Real) *
                    ((2 : Real) ^ (z + 127) / (5 : Real) ^ n) /
                    (2 : Real) ^ (64 : Nat) =
                  ((w.val * 2 ^ (z + 127) : Nat) : Real) /
                    ((5 ^ n * 2 ^ 64 : Nat) : Real) := by
                push_cast
                ring
              _ = ((G * K : Nat) : Real) := by
                rw [hnat]
                push_cast
                field_simp
          constructor
          · rw [hreal]
            push_cast
            rw [hKcast]
          · exact hGmod
        have ceil_unique (C : Nat) (hC : 0 < C)
            (hloT : ((C - 1 : Nat) : Real) < T)
            (hhiT : T < (C : Real)) : D = C := by
          have hloR : ((C - 1 : Nat) : Real) < (D : Real) :=
            lt_trans hloT hceilq.2
          have hloN : C - 1 < D := by exact_mod_cast hloR
          have hhiR : (D : Real) < (C : Real) + 1 := by
            linarith [hceilq.1, hhiT]
          have hhiN : D < C + 1 := by exact_mod_cast hhiR
          have hCD : C ≤ D := by
            rw [← Nat.succ_pred_eq_of_pos hC]
            exact hloN
          exact Nat.le_antisymm (Nat.lt_succ_iff.mp hhiN) hCD
        have parts_of_D (C A Bv : Nat) (hDC : D = C)
            (hcalc : C = A * 2 ^ 64 + Bv) (hBv : Bv < 2 ^ 64) :
            a.val = A ∧ b.val = Bv := by
          have hdef : D = a.val * 2 ^ 64 + b.val := rfl
          omega
        have odd_nonref_zero (haodd : Odd a.val) (hYwa : Y = w.val * a.val)
            (hlo0 : lo.val = 0) : False := by
          have hGodd : Odd G := by
            refine ⟨G / 2, ?_⟩
            omega
          have hwne : w.val ≠ 0 := by omega
          rcases Nat.exists_eq_two_pow_mul_odd hwne with ⟨v, h, hhodd, hwfac⟩
          have hhpos : 1 ≤ h := by
            rcases hhodd with ⟨m, hm⟩
            omega
          have hv63 : v ≤ 63 := by
            by_contra hv
            have hp : 2 ^ 64 ≤ 2 ^ v :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            have hmul : 2 ^ v ≤ 2 ^ v * h := by
              have hm := Nat.mul_le_mul_left (2 ^ v) hhpos
              simpa using hm
            rw [← hwfac] at hmul
            omega
          have heq : 2 ^ v * (h * a.val) = 2 ^ s * G := by
            calc
              2 ^ v * (h * a.val) = w.val * a.val := by rw [hwfac]; ring
              _ = Y := hYwa.symm
              _ = G * K := by rw [hYLlo, hlo0]; simp
              _ = 2 ^ s * G := by change G * 2 ^ s = _; ring
          have hexp : v = s := two_adic_unique (hhodd.mul haodd) hGodd heq
          simp [s] at hexp
          omega
        rcases hnz with hnz | hnz | hnz | hnz
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((272225893536750770770699685945414569165 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (272225893536750770770699685945414569165 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 272225893536750770770699685945414569165
            (by norm_num) hloT hhiT
          have hab := parts_of_D 272225893536750770770699685945414569165
            14757395258967641292 14757395258967641293 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              rcases hu_cases with hu0 | hu1
              · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
              · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
            · exact href
          apply finish_refined 1 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((217780714829400616616559748756331655332 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (217780714829400616616559748756331655332 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 217780714829400616616559748756331655332
            (by norm_num) hloT hhiT
          have hab := parts_of_D 217780714829400616616559748756331655332
            11805916207174113034 4427218577690292388 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              rcases hu_cases with hu0 | hu1
              · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
              · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
            · exact href
          apply finish_refined 4 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((174224571863520493293247799005065324266 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (174224571863520493293247799005065324266 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 174224571863520493293247799005065324266
            (by norm_num) hloT hhiT
          have hab := parts_of_D 174224571863520493293247799005065324266
            9444732965739290427 7231123676894144234 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              have hloCases : lo.val = 0 ∨ lo.val = 1 := by omega
              rcases hloCases with hlo0 | hlo1'
              · apply odd_nonref_zero (by rw [hab.1]; decide) hYwa hlo0
              · rcases hu_cases with hu0 | hu1
                · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                  have heqres : w.val * 9444732965739290427 =
                      G * 2 ^ 73 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 9444732965739290427 = H * 2 ^ 73 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
                · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                  have heqres : w.val * 9444732965739290427 =
                      G * 2 ^ 74 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 9444732965739290427 = H * 2 ^ 74 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
            · exact href
          apply finish_refined 66 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((278759314981632789269196478408104518825 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (278759314981632789269196478408104518825 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 278759314981632789269196478408104518825
            (by norm_num) hloT hhiT
          have hab := parts_of_D 278759314981632789269196478408104518825
            15111572745182864683 15259146697772541097 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              have hloCases : lo.val = 0 ∨ lo.val = 1 := by omega
              rcases hloCases with hlo0 | hlo1'
              · apply odd_nonref_zero (by rw [hab.1]; decide) hYwa hlo0
              · rcases hu_cases with hu0 | hu1
                · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                  have heqres : w.val * 15111572745182864683 =
                      G * 2 ^ 73 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 15111572745182864683 = H * 2 ^ 73 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
                · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                  have heqres : w.val * 15111572745182864683 =
                      G * 2 ^ 74 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 15111572745182864683 = H * 2 ^ 74 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
            · exact href
          apply finish_refined 153 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
      · have hqnonneg : 0 ≤ q.val := by omega
        have hTex := hexact ⟨hqnonneg, by omega⟩
        have hb0 := hbzero ⟨hqnonneg, hqhi23⟩
        have hYwa : Y = w.val * a.val := by
          rcases hflow with hnr | href
          · simpa [Y] using hnr.2.2.2
          · simpa [Y, hb0] using href
        have he53 : e ≤ 53 := by
          have heDef : e = 217706 * q.val / 65536 - q.val := rfl
          omega
        rcases Int.eq_ofNat_of_zero_le hqnonneg with ⟨qn, hqnat⟩
        have hk : 0 ≤ 127 - e := by omega
        rcases Int.eq_ofNat_of_zero_le hk with ⟨kn, hkn⟩
        have hkn74 : 74 ≤ kn := by omega
        have hTpos : T = (2 : Real) ^ kn * (5 : Real) ^ qn := by
          simp [T, hqnat, hkn]
        have hDnat : D = 2 ^ kn * 5 ^ qn := by
          have hc : (D : Real) = ((2 ^ kn * 5 ^ qn : Nat) : Real) := by
            rw [hTex, hTpos]
            push_cast
            rfl
          exact_mod_cast hc
        have hdiv : 2 ^ 65 ∣ D := by
          use 2 ^ (kn - 65) * 5 ^ qn
          calc
            D = 2 ^ kn * 5 ^ qn := hDnat
            _ = 2 ^ 65 * (2 ^ (kn - 65) * 5 ^ qn) := by
              conv_lhs => rw [show kn = 65 + (kn - 65) by omega, pow_add]
              ring
        rcases hdiv with ⟨c, hc⟩
        have hac : a.val = 2 * c := by
          have heq : 2 ^ 64 * a.val = 2 ^ 64 * (2 * c) := by
            rw [show 2 ^ 64 * a.val = D by simp [D, hb0, mul_comm]]
            rw [hc]
            norm_num [pow_succ]
            ring
          exact Nat.eq_of_mul_eq_mul_left (by positivity) heq
        have hlo0 : lo.val = 0 := by
          have hYrep : Y = 2 * (w.val * c) := by
            rw [hYwa, hac]
            ring
          have hLrep : G * K = 2 * (G * (2 ^ 63 * 2 ^ d)) := by
            rw [hKfac]
            norm_num [pow_succ]
            ring
          omega
        have hRwa : R = ((w.val * a.val : Nat) : Real) := by
          change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
          rw [← hTex]
          simp [D, hb0]
          push_cast
          norm_num
          ring
        constructor
        · calc
            R = (Y : Real) := by rw [hYwa, hRwa]
            _ = ((G * K : Nat) : Real) := by rw [hYLlo, hlo0]; simp
            _ = (G : Real) * (2 : Real) ^ (73 + u) := by
              push_cast
              rw [hKcast]
        · exact hGmod

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.IntegerPowerTableCertificate
import Tablet.ModelU64BitAnd
import Tablet.num_imp_dec2flt_lemire_compute_product_approx
import Tablet.num_imp_dec2flt_lemire_full_multiplication
import Tablet.num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE
import Tablet.num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE
import Mathlib.Data.Nat.Factorization.Basic
import Mathlib.Data.Nat.MaxPowDiv
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.IntervalCases

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

namespace dec2flt_model

-- [TABLET NODE: IntegerProductApproximation]
theorem IntegerProductApproximation :
    ∀ (q : Std.I64) (w lo hi : Std.U64),
      -342 ≤ q.val → q.val ≤ 308 →
      2 ^ 63 ≤ w.val →
      num.imp.dec2flt.lemire.compute_product_approx q w 55#usize =
        ok (lo, hi) →
      (lo.val ≠ 2 ^ 64 - 1 ∨ (-27 ≤ q.val ∧ q.val ≤ 55)) →
      ∃ (delta : Std.I64) (index : Std.Usize) (a b : Std.U64),
        q - (-342)#i64 = ok delta ∧
        lift (IScalar.hcast .Usize delta) = ok index ∧
        Array.index_usize num.imp.dec2flt.table.POWER_OF_FIVE_128 index =
          ok (a, b) ∧
        let e : Int := 217706 * q.val / 65536 - q.val
        let D : Nat := a.val * 2 ^ 64 + b.val
        let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
        let R : Real := (w.val : Real) * T / (2 : Real) ^ (64 : Nat)
        let Y : Nat := hi.val * 2 ^ 64 + lo.val
        let u : Nat := hi.val / 2 ^ 63
        let s : Nat := 73 + u
        let G : Nat := hi.val / 2 ^ (9 + u)
        (u ≤ 1 ∧
          (Y = w.val * a.val ∨
            Y = w.val * a.val + (w.val * b.val) / 2 ^ 64) ∧
          (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s ∧
          ((R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1) ↔
            (-4 ≤ q.val ∧ q.val ≤ 23 ∧ G % 4 = 1 ∧
              lo.val ≤ 1 ∧ G * 2 ^ (9 + u) = hi.val))) := by
-- BODY
  intro q w lo hi hqlo hqhi hw hrun hsent
  rcases IntegerPowerTableCertificate q hqlo hqhi with
    ⟨q32, p, delta, index, a, b, hq32, hp, hpval,
      hdelta, hindex, htable, hcert⟩
  refine ⟨delta, index, a, b, hdelta, hindex, htable, ?_⟩
  dsimp only
  have full_spec (x y : Std.U64) :
      ∃ l h : Std.U64,
        num.imp.dec2flt.lemire.full_multiplication x y = ok (l, h) ∧
        h.val * 2 ^ 64 + l.val = x.val * y.val ∧
        h.val = (x.val * y.val) / 2 ^ 64 := by
    have hx64 : x.val < 2 ^ 64 := by exact x.bv.isLt
    have hy64 : y.val < 2 ^ 64 := by exact y.bv.isLt
    have hxy128 : x.val * y.val < 2 ^ 128 := by
      have hxy := Nat.mul_le_mul (Nat.le_pred_of_lt hx64)
        (Nat.le_pred_of_lt hy64)
      norm_num at hxy ⊢
      omega
    have hx128 : x.val < 2 ^ 128 := by omega
    have hy128 : y.val < 2 ^ 128 := by omega
    have hdiv64 : x.val * y.val / 2 ^ 64 < 2 ^ 64 := by
      rw [Nat.div_lt_iff_lt_mul (by positivity)]
      norm_num at hxy128 ⊢
      exact hxy128
    have hx128' : x.val < 340282366920938463463374607431768211456 := by
      norm_num at hx128 ⊢
      exact hx128
    have hy128' : y.val < 340282366920938463463374607431768211456 := by
      norm_num at hy128 ⊢
      exact hy128
    have hxy128' : x.val * y.val < 340282366920938463463374607431768211456 := by
      norm_num at hxy128 ⊢
      exact hxy128
    have hdiv64' : x.val * y.val / 18446744073709551616 <
        18446744073709551616 := by
      norm_num at hdiv64 ⊢
      exact hdiv64
    have hs : num.imp.dec2flt.lemire.full_multiplication x y
        ⦃ r => r.2.val * 2 ^ 64 + r.1.val = x.val * y.val ∧
          r.2.val = (x.val * y.val) / 2 ^ 64 ⦄ := by
      unfold num.imp.dec2flt.lemire.full_multiplication
      step
      step
      step
      step
      step
      step
      constructor
      · rw [i4_post, UScalar.cast_val_eq, i3_post1,
          i2_post, UScalar.cast_val_eq, r_post, i_post,
          i1_post, UScalar.cast_val_eq, UScalar.cast_val_eq]
        simp only [UScalarTy.numBits]
        norm_num [Nat.shiftRight_eq_div_pow]
        rw [Nat.mod_eq_of_lt hx128', Nat.mod_eq_of_lt hy128',
          Nat.mod_eq_of_lt hdiv64']
        simpa [Nat.mul_comm] using Nat.div_add_mod (x.val * y.val)
          18446744073709551616
      · rw [i4_post, UScalar.cast_val_eq, i3_post1, r_post,
          i_post, i1_post, UScalar.cast_val_eq, UScalar.cast_val_eq]
        simp only [UScalarTy.numBits]
        norm_num [Nat.shiftRight_eq_div_pow]
        rw [Nat.mod_eq_of_lt hx128', Nat.mod_eq_of_lt hy128',
          Nat.mod_eq_of_lt hdiv64']
    obtain ⟨r, hr, hrs⟩ := Aeneas.Std.WP.spec_imp_exists hs
    exact ⟨r.1, r.2, hr, hrs⟩
  have and_mask (x : Std.U64) : (x &&& 511#u64).val = x.val % 512 := by
    simp only [UScalar.val_and]
    change x.val &&& (2 ^ 9 - 1) = x.val % 2 ^ 9
    apply Nat.eq_of_testBit_eq
    intro i
    rw [Nat.testBit_land, Nat.testBit_two_pow_sub_one,
      Nat.testBit_mod_two_pow]
    simp [Bool.and_comm]
  have two_adic_unique {A B x y : Nat} (hx : Odd x) (hy : Odd y)
      (h : 2 ^ A * x = 2 ^ B * y) : A = B := by
    have hx0 : x ≠ 0 := by rintro rfl; simp at hx
    have hy0 : y ≠ 0 := by rintro rfl; simp at hy
    have hxnd : ¬ 2 ∣ x := by
      rintro ⟨k, hk⟩
      rcases hx with ⟨m, hm⟩
      omega
    have hynd : ¬ 2 ∣ y := by
      rintro ⟨k, hk⟩
      rcases hy with ⟨m, hm⟩
      omega
    have hA := Nat.maxPowDvdDiv_of_pow_mul_eq
      (Nat.mul_ne_zero (by positivity) hx0)
      (rfl : 2 ^ A * x = 2 ^ A * x) hxnd
    have hB := Nat.maxPowDvdDiv_of_pow_mul_eq
      (Nat.mul_ne_zero (by positivity) hy0)
      (rfl : 2 ^ B * y = 2 ^ B * y) hynd
    rw [h] at hA
    rw [hB] at hA
    exact (congrArg Prod.fst hA).symm
  simp only [num.imp.dec2flt.lemire.compute_product_approx,
    num.imp.dec2flt.table.SMALLEST_POWER_OF_FIVE.eq_def,
    Int.reduceNeg, ge_iff_le, IScalar.le_equiv,
    num.imp.dec2flt.table.LARGEST_POWER_OF_FIVE.eq_def, UScalar.le_equiv,
    UScalar.ofNatCore_val_eq, Nat.reduceLeDiff, massert_True,
    UScalar.lt_equiv, Nat.reduceLT, if_true, gt_iff_lt,
    bind_tc_ok] at hrun
  have hsmall : lift (IScalar.cast .I64 (-342)#i32) = ok (-342)#i64 := by
    rfl
  have hlarge : lift (IScalar.cast .I64 308#i32) = ok 308#i64 := by
    rfl
  have hmask : 18446744073709551615#u64 >>> 55#usize = ok 511#u64 := by
    rfl
  rw [hsmall] at hrun
  simp only [bind_tc_ok, IScalar.le_equiv] at hrun
  rw [show ((-342)#i64).val = -342 by rfl] at hrun
  simp only [hqlo, massert_True, bind_tc_ok] at hrun
  rw [hlarge] at hrun
  simp only [bind_tc_ok, IScalar.le_equiv] at hrun
  rw [show (308#i64).val = 308 by rfl] at hrun
  simp only [hqhi, massert_True, bind_tc_ok] at hrun
  rw [hmask, hdelta] at hrun
  simp only [bind_tc_ok] at hrun
  rw [hindex] at hrun
  simp only [bind_tc_ok] at hrun
  rw [htable] at hrun
  simp only [bind_tc_ok, uncurry_apply_pair] at hrun
  rcases full_spec w a with ⟨firstLo, firstHi, hfirst, hfirstProd,
    hfirstHi⟩
  rw [hfirst] at hrun
  simp only [bind_tc_ok, uncurry_apply_pair] at hrun
  have hand : lift (firstHi &&& 511#u64) = ok (firstHi &&& 511#u64) := by
    rfl
  rw [hand] at hrun
  simp only [bind_tc_ok] at hrun
  have hflow :
      (hi = firstHi ∧ lo = firstLo ∧
        firstHi.val % 512 ≠ 511 ∧
        hi.val * 2 ^ 64 + lo.val = w.val * a.val) ∨
      (hi.val * 2 ^ 64 + lo.val =
        w.val * a.val + (w.val * b.val) / 2 ^ 64) := by
    by_cases hrefine : firstHi &&& 511#u64 = 511#u64
    · rw [if_pos hrefine] at hrun
      rcases full_spec w b with ⟨secondLo, secondHi, hsecond,
        hsecondProd, hsecondHi⟩
      rw [hsecond] at hrun
      simp only [bind_tc_ok, uncurry_apply_pair] at hrun
      have hadd : lift (core.num.U64.wrapping_add firstLo secondHi) =
          ok (core.num.U64.wrapping_add firstLo secondHi) := by rfl
      rw [hadd] at hrun
      simp only [bind_tc_ok] at hrun
      let added := core.num.U64.wrapping_add firstLo secondHi
      have haddVal : added.val =
          (firstLo.val + secondHi.val) % 2 ^ 64 := by
        simpa [added, U64.size_eq] using
          core.num.U64.wrapping_add_val_eq firstLo secondHi
      by_cases hcarry : added.val < secondHi.val
      · rw [if_pos hcarry] at hrun
        cases hinc : firstHi + 1#u64 with
        | fail e =>
            rw [hinc] at hrun
            simp at hrun
        | div =>
            rw [hinc] at hrun
            simp at hrun
        | ok firstHi1 =>
            rw [hinc] at hrun
            simp only [bind_tc_ok] at hrun
            have hout : (added, firstHi1) = (lo, hi) := Result.ok.inj hrun
            have hlo : lo = added := (congrArg Prod.fst hout).symm
            have hhi : hi = firstHi1 := (congrArg Prod.snd hout).symm
            have hincVal : firstHi1.val = firstHi.val + 1 := by
              have hs := UScalar.add_equiv firstHi 1#u64
              rw [hinc] at hs
              exact hs.2.1
            right
            rw [hlo, hhi]
            have hfl : firstLo.val < 2 ^ 64 := by exact firstLo.bv.isLt
            have hsh : secondHi.val < 2 ^ 64 := by exact secondHi.bv.isLt
            have hsum2 : firstLo.val + secondHi.val < 2 * 2 ^ 64 := by
              omega
            have hsumge : 2 ^ 64 ≤ firstLo.val + secondHi.val := by
              by_contra hnot
              have hsumlt : firstLo.val + secondHi.val < 2 ^ 64 := by
                omega
              rw [haddVal, Nat.mod_eq_of_lt hsumlt] at hcarry
              omega
            have hdiv : (firstLo.val + secondHi.val) / 2 ^ 64 = 1 := by
              apply Nat.div_eq_of_lt_le <;> omega
            have hmod := Nat.mod_eq_sub_mul_div
              (x := firstLo.val + secondHi.val) (k := 2 ^ 64)
            rw [hdiv] at hmod
            have haddExact : added.val =
                firstLo.val + secondHi.val - 2 ^ 64 := by
              rw [haddVal, hmod]
              simp
            have haddPlus : added.val + 2 ^ 64 =
                firstLo.val + secondHi.val := by
              rw [haddExact, Nat.sub_add_cancel hsumge]
            calc
              firstHi1.val * 2 ^ 64 + added.val =
                  firstHi.val * 2 ^ 64 + (added.val + 2 ^ 64) := by
                    rw [hincVal]
                    ring
              _ = firstHi.val * 2 ^ 64 +
                  (firstLo.val + secondHi.val) := by rw [haddPlus]
              _ = (firstHi.val * 2 ^ 64 + firstLo.val) +
                  secondHi.val := by ring
              _ = w.val * a.val + secondHi.val := by rw [hfirstProd]
              _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
                rw [hsecondHi]
      · rw [if_neg hcarry] at hrun
        have hout : (added, firstHi) = (lo, hi) := Result.ok.inj hrun
        have hlo : lo = added := (congrArg Prod.fst hout).symm
        have hhi : hi = firstHi := (congrArg Prod.snd hout).symm
        right
        rw [hlo, hhi]
        have hfl : firstLo.val < 2 ^ 64 := by exact firstLo.bv.isLt
        have hsh : secondHi.val < 2 ^ 64 := by exact secondHi.bv.isLt
        have hsumlt : firstLo.val + secondHi.val < 2 ^ 64 := by
          by_contra hnot
          have hsum2 : firstLo.val + secondHi.val < 2 * 2 ^ 64 := by
            omega
          have hdiv : (firstLo.val + secondHi.val) / 2 ^ 64 = 1 := by
            apply Nat.div_eq_of_lt_le <;> omega
          have hmod := Nat.mod_eq_sub_mul_div
            (x := firstLo.val + secondHi.val) (k := 2 ^ 64)
          rw [hdiv] at hmod
          omega
        have haddExact : added.val = firstLo.val + secondHi.val := by
          rw [haddVal, Nat.mod_eq_of_lt hsumlt]
        calc
          firstHi.val * 2 ^ 64 + added.val =
              firstHi.val * 2 ^ 64 + (firstLo.val + secondHi.val) := by
                rw [haddExact]
          _ = (firstHi.val * 2 ^ 64 + firstLo.val) +
              secondHi.val := by ring
          _ = w.val * a.val + secondHi.val := by rw [hfirstProd]
          _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
            rw [hsecondHi]
    · rw [if_neg hrefine] at hrun
      have hout : (firstLo, firstHi) = (lo, hi) := Result.ok.inj hrun
      have hlo : lo = firstLo := (congrArg Prod.fst hout).symm
      have hhi : hi = firstHi := (congrArg Prod.snd hout).symm
      left
      refine ⟨hhi, hlo, ?_, ?_⟩
      · intro heq
        apply hrefine
        apply UScalar.eq_of_val_eq
        rw [and_mask, heq]
        rfl
      · simpa [hlo, hhi] using hfirstProd
  have hu : hi.val / 2 ^ 63 ≤ 1 := by
    have hh : hi.val < 2 ^ 64 := by exact hi.bv.isLt
    omega
  refine ⟨hu, ?_, ?_⟩
  · rcases hflow with hnr | href
    · exact Or.inl hnr.2.2.2
    · exact Or.inr href
  · let e : Int := 217706 * q.val / 65536 - q.val
    let D : Nat := a.val * 2 ^ 64 + b.val
    let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
    let R : Real := (w.val : Real) * T / (2 : Real) ^ (64 : Nat)
    let Y : Nat := hi.val * 2 ^ 64 + lo.val
    let u : Nat := hi.val / 2 ^ 63
    let s : Nat := 73 + u
    let d : Nat := 9 + u
    let G : Nat := hi.val / 2 ^ d
    let K : Nat := 2 ^ s
    change ((G : Real) * (2 : Real) ^ s ≤ R ∧
      R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s ∧
      ((R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1) ↔
        (-4 ≤ q.val ∧ q.val ≤ 23 ∧ G % 4 = 1 ∧
          lo.val ≤ 1 ∧ G * 2 ^ d = hi.val)))
    change (((2 : Real) ^ (127 : Nat) ≤ T ∧
        T < (2 : Real) ^ (128 : Nat)) ∧
      (q.val < 0 →
        ∃ n z : Nat,
          q.val = -(n : Int) ∧
          1 ≤ n ∧ 1 ≤ z ∧
          2 ^ (z - 1) < 5 ^ n ∧ 5 ^ n < 2 ^ z ∧
          e = -(z : Int) ∧
          T = (2 : Real) ^ (z + 127) / (5 : Real) ^ n) ∧
      ((0 ≤ q.val ∧ q.val ≤ 55) → (D : Real) = T) ∧
      ((0 ≤ q.val ∧ q.val ≤ 23) → b.val = 0) ∧
      ((q.val < -27 ∨ 0 ≤ q.val) →
        (D : Real) ≤ T ∧ T < (D : Real) + 1) ∧
      ((-27 ≤ q.val ∧ q.val < 0) →
        (D : Real) - 1 < T ∧ T < D)) at hcert
    rcases hcert with ⟨hTnorm, hneg, hexact, hbzero, hfloor, hceil⟩
    have hwB : w.val < 2 ^ 64 := by exact w.bv.isLt
    have hloB : lo.val < 2 ^ 64 := by exact lo.bv.isLt
    have hhiB : hi.val < 2 ^ 64 := by exact hi.bv.isLt
    have haB : a.val < 2 ^ 64 := by exact a.bv.isLt
    have hbB : b.val < 2 ^ 64 := by exact b.bv.isLt
    have hu' : u ≤ 1 := by simpa [u] using hu
    have hu_cases : u = 0 ∨ u = 1 := by omega
    have hYcell : G * K ≤ Y ∧ Y < (G + 1) * K := by
      rcases hu_cases with hu0 | hu1
      · have hdiv := Nat.div_add_mod hi.val 512
        have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 512)
        simp [G, d, u, hu0, K, s, Y] at *
        omega
      · have hdiv := Nat.div_add_mod hi.val 1024
        have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 1024)
        simp [G, d, u, hu1, K, s, Y] at *
        omega
    let X : Real := (w.val : Real) * (D : Real) / (2 : Real) ^ (64 : Nat)
    let F : Nat := (w.val * D) / 2 ^ 64
    have hF : F = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
      calc
        F = (2 ^ 64 * (w.val * a.val) + w.val * b.val) / 2 ^ 64 := by
          congr 1
          simp [F, D]
          ring
        _ = w.val * a.val + (w.val * b.val) / 2 ^ 64 := by
          exact Nat.mul_add_div (by positivity) _ _
    have div_bounds (N : Nat) :
        ((N / 2 ^ 64 : Nat) : Real) ≤
            (N : Real) / (2 : Real) ^ (64 : Nat) ∧
          (N : Real) / (2 : Real) ^ (64 : Nat) <
            ((N / 2 ^ 64 : Nat) : Real) + 1 := by
      let f := N / 2 ^ 64
      let r := N % 2 ^ 64
      have heq : 2 ^ 64 * f + r = N := by
        simpa [f, r] using Nat.div_add_mod N (2 ^ 64)
      have hr : r < 2 ^ 64 := Nat.mod_lt _ (by positivity)
      have heqR : ((2 ^ 64 * f + r : Nat) : Real) = (N : Real) := by
        exact_mod_cast heq
      have hrR : (r : Real) < (2 : Real) ^ (64 : Nat) := by
        exact_mod_cast hr
      have hrepr : (N : Real) / (2 : Real) ^ (64 : Nat) =
          (f : Real) + (r : Real) / (2 : Real) ^ (64 : Nat) := by
        rw [← heqR]
        push_cast
        norm_num
        ring_nf
      rw [hrepr]
      simp only [f]
      constructor
      · have hrnonneg : (0 : Real) ≤
            (r : Real) / (2 : Real) ^ (64 : Nat) := by positivity
        linarith
      · have hr0 : (0 : Real) ≤ r := by positivity
        have hp : (0 : Real) < (2 : Real) ^ (64 : Nat) := by positivity
        have := (div_lt_one hp).2 hrR
        linarith
    have hFX : (F : Real) ≤ X ∧ X < (F : Real) + 1 := by
      simpa [F, X] using div_bounds (w.val * D)
    have hw0R : (0 : Real) ≤ w.val := by positivity
    have hwposR : (0 : Real) < w.val := by
      exact_mod_cast (show 0 < w.val by omega)
    have hwratio : (w.val : Real) / (2 : Real) ^ (64 : Nat) < 1 := by
      rw [div_lt_one (by positivity)]
      exact_mod_cast hwB
    have hRfloor (hf : (D : Real) ≤ T ∧ T < (D : Real) + 1) :
        X ≤ R ∧
          R < X + (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
      have hmul1 := mul_le_mul_of_nonneg_left hf.1 hw0R
      have hmul2 := mul_lt_mul_of_pos_left hf.2 hwposR
      have hdiv1 := (div_le_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul1
      have hdiv2 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul2
      constructor
      · simpa [X, R] using hdiv1
      · have hid : (w.val : Real) * ((D : Real) + 1) /
              (2 : Real) ^ (64 : Nat) =
            X + (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
            simp [X]
            ring
        rw [hid] at hdiv2
        simpa [R] using hdiv2
    have hRceil (hc : (D : Real) - 1 < T ∧ T < D) :
        X - (w.val : Real) / (2 : Real) ^ (64 : Nat) < R ∧
          R < X := by
      have hmul1 := mul_lt_mul_of_pos_left hc.1 hwposR
      have hmul2 := mul_lt_mul_of_pos_left hc.2 hwposR
      have hdiv1 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul1
      have hdiv2 := (div_lt_div_iff_of_pos_right
        (show (0 : Real) < (2 : Real) ^ (64 : Nat) by positivity)).2 hmul2
      have hid : (w.val : Real) * ((D : Real) - 1) /
              (2 : Real) ^ (64 : Nat) =
            X - (w.val : Real) / (2 : Real) ^ (64 : Nat) := by
            simp [X]
            ring
      rw [hid] at hdiv1
      constructor
      · simpa [R] using hdiv1
      · simpa [X, R] using hdiv2
    have bpos_of_ceil (hq : -27 ≤ q.val ∧ q.val < 0) : 1 ≤ b.val := by
      rcases hneg hq.2 with
        ⟨n, z, hqn, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
      have hn27 : n ≤ 27 := by omega
      have hPbound : 5 ^ n < 2 ^ 64 := by
        have hp := Nat.pow_le_pow_right (by norm_num : 0 < 5) hn27
        exact lt_of_le_of_lt hp (by norm_num)
      by_contra hbnot
      have hb0 : b.val = 0 := by omega
      have hc := hceil hq
      have hpR : (0 : Real) < (5 : Real) ^ n := by positivity
      have hlowR : ((D : Real) - 1) * (5 : Real) ^ n <
          (2 : Real) ^ (z + 127) := by
        apply (lt_div_iff₀ hpR).mp
        simpa [hTrep] using hc.1
      have hhighR : (2 : Real) ^ (z + 127) <
          (D : Real) * (5 : Real) ^ n := by
        apply (div_lt_iff₀ hpR).mp
        simpa [hTrep] using hc.2
      have hlowR' : (D : Real) * (5 : Real) ^ n <
          (2 : Real) ^ (z + 127) + (5 : Real) ^ n := by
        nlinarith
      have hlowN : D * 5 ^ n < 2 ^ (z + 127) + 5 ^ n := by
        exact_mod_cast hlowR'
      have hhighN : 2 ^ (z + 127) < D * 5 ^ n := by
        exact_mod_cast hhighR
      have hzsum : z + 127 = 64 + (z + 63) := by omega
      have hNmul : 2 ^ (z + 127) = 2 ^ 64 * 2 ^ (z + 63) := by
        rw [hzsum, pow_add]
      have hDmul : D * 5 ^ n = 2 ^ 64 * (a.val * 5 ^ n) := by
        simp [D, hb0]
        ring
      rw [hNmul, hDmul] at hlowN hhighN
      omega
    have hKcast : (K : Real) = (2 : Real) ^ s := by simp [K]
    have hKfac : K = 2 ^ 64 * 2 ^ d := by
      have hsd : s = 64 + d := by simp [s, d]; omega
      change 2 ^ s = 2 ^ 64 * 2 ^ d
      rw [hsd, pow_add]
    have hYcellR : (G : Real) * (K : Real) ≤ (Y : Real) ∧
        (Y : Real) < ((G + 1 : Nat) : Real) * (K : Real) := by
      constructor
      · exact_mod_cast hYcell.1
      · exact_mod_cast hYcell.2
    have nonref_cell
        (hnr : hi = firstHi ∧ lo = firstLo ∧
          firstHi.val % 512 ≠ 511 ∧
          hi.val * 2 ^ 64 + lo.val = w.val * a.val) :
        (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      have hY : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
      have hYreal : (Y : Real) = ((w.val * a.val : Nat) : Real) := by
        exact_mod_cast hY
      have hXid : X = (Y : Real) +
          ((w.val * b.val : Nat) : Real) / (2 : Real) ^ (64 : Nat) := by
        calc
          X = ((w.val * a.val : Nat) : Real) +
              ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) := by
                simp [X, D]
                ring
          _ = (Y : Real) + ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) := by rw [hYreal]
      have hYR : (Y : Real) ≤ R ∧
          R < (Y : Real) + (2 : Real) ^ (64 : Nat) := by
        by_cases hqf : q.val < -27 ∨ 0 ≤ q.val
        · have hr := hRfloor (hfloor hqf)
          have hb1 : b.val + 1 ≤ 2 ^ 64 := by omega
          have hprodN : w.val * (b.val + 1) < 2 ^ 64 * 2 ^ 64 := by
            calc
              w.val * (b.val + 1) < 2 ^ 64 * (b.val + 1) :=
                Nat.mul_lt_mul_of_pos_right hwB (by omega)
              _ ≤ 2 ^ 64 * 2 ^ 64 := Nat.mul_le_mul_left _ hb1
          have hprodR : ((w.val * (b.val + 1) : Nat) : Real) <
              (2 : Real) ^ (64 : Nat) * (2 : Real) ^ (64 : Nat) := by
            exact_mod_cast hprodN
          have hcorr : ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) +
              (w.val : Real) / (2 : Real) ^ (64 : Nat) <
                (2 : Real) ^ (64 : Nat) := by
            rw [← add_div]
            apply (div_lt_iff₀ (show (0 : Real) <
              (2 : Real) ^ (64 : Nat) by positivity)).2
            norm_num at hprodR ⊢
            convert hprodR using 1 <;> ring
          rw [hXid] at hr
          constructor
          · have hnonneg : (0 : Real) ≤
                ((w.val * b.val : Nat) : Real) /
                  (2 : Real) ^ (64 : Nat) := by positivity
            linarith
          · linarith
        · have hqc : -27 ≤ q.val ∧ q.val < 0 := by omega
          have hbpos := bpos_of_ceil hqc
          have hr := hRceil (hceil hqc)
          have hwbN : w.val ≤ w.val * b.val := by
            have hm := Nat.mul_le_mul_left w.val hbpos
            simpa using hm
          have hwbR : (w.val : Real) ≤
              ((w.val * b.val : Nat) : Real) := by exact_mod_cast hwbN
          rw [hXid] at hr
          constructor
          · have hp : (0 : Real) < (2 : Real) ^ (64 : Nat) := by
              positivity
            have hdiv := (div_le_div_iff_of_pos_right hp).2 hwbR
            linarith
          · have hterm : ((w.val * b.val : Nat) : Real) /
                (2 : Real) ^ (64 : Nat) < (2 : Real) ^ (64 : Nat) := by
              apply (div_lt_iff₀ (show (0 : Real) <
                (2 : Real) ^ (64 : Nat) by positivity)).2
              have hprod : w.val * b.val < 2 ^ 64 * 2 ^ 64 := by
                calc
                  w.val * b.val < 2 ^ 64 * b.val :=
                    Nat.mul_lt_mul_of_pos_right hwB (by omega)
                  _ < 2 ^ 64 * 2 ^ 64 :=
                    Nat.mul_lt_mul_of_pos_left hbB (by positivity)
              exact_mod_cast hprod
            linarith
      have hmaskFail : hi.val % 512 ≠ 511 := by simpa [hnr.1] using hnr.2.2.1
      have hgap : Y + 2 ^ 64 ≤ (G + 1) * K := by
        rcases hu_cases with hu0 | hu1
        · have hdiv := Nat.div_add_mod hi.val 512
          have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 512)
          simp [G, d, u, hu0, K, s, Y]
          omega
        · have hdiv := Nat.div_add_mod hi.val 1024
          have hrem := Nat.mod_lt hi.val (by norm_num : 0 < 1024)
          simp [G, d, u, hu1, K, s, Y]
          omega
      have hgapR : (Y : Real) + (2 : Real) ^ (64 : Nat) ≤
          ((G + 1 : Nat) : Real) * (K : Real) := by
        exact_mod_cast hgap
      rw [← hKcast]
      constructor <;> linarith [hYcellR.1]
    have refine_cell
        (href : hi.val * 2 ^ 64 + lo.val =
          w.val * a.val + (w.val * b.val) / 2 ^ 64) :
        (G : Real) * (2 : Real) ^ s ≤ R ∧
          R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      have hYF : Y = F := by
        rw [hF]
        simpa [Y] using href
      by_cases hqf : q.val < -27 ∨ 0 ≤ q.val
      · have hr := hRfloor (hfloor hqf)
        have hRlo : (Y : Real) ≤ R := by
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          linarith [hFX.1]
        have hRnear : R < (Y : Real) + 2 := by
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          linarith [hFX.2, hwratio]
        have hRhi : R < ((G + 1 : Nat) : Real) * (K : Real) := by
          by_contra hnot
          have hLle : ((G + 1 : Nat) : Real) * (K : Real) ≤ R :=
            le_of_not_gt hnot
          have hsuccN : (G + 1) * K = Y + 1 := by
            have h1 : Y < (G + 1) * K := hYcell.2
            have h2R : (((G + 1) * K : Nat) : Real) <
                ((Y + 2 : Nat) : Real) := by
              have hLle' : (((G + 1) * K : Nat) : Real) ≤ R := by
                push_cast
                simpa using hLle
              exact lt_of_le_of_lt hLle' (by norm_num; simpa using hRnear)
            have h2 : (G + 1) * K < Y + 2 := by exact_mod_cast h2R
            omega
          have hloMax : lo.val = 2 ^ 64 - 1 := by
            change (G + 1) * K = hi.val * 2 ^ 64 + lo.val + 1 at hsuccN
            have hsuccN' : 2 ^ 64 * ((G + 1) * 2 ^ d) =
                hi.val * 2 ^ 64 + lo.val + 1 := by
              rw [← hsuccN, hKfac]
              ring
            omega
          have hqsent : -27 ≤ q.val ∧ q.val ≤ 55 := by
            rcases hsent with hne | hs
            · exact False.elim (hne hloMax)
            · exact hs
          have hqnonneg : 0 ≤ q.val := by rcases hqf with h | h <;> omega
          have hTexact := hexact ⟨hqnonneg, hqsent.2⟩
          have hRX : R = X := by
            simp [R, X, hTexact]
          have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
          have hL : (((G + 1) * K : Nat) : Real) = ((Y + 1 : Nat) : Real) := by
            exact_mod_cast hsuccN
          have hRlt : R < (Y : Real) + 1 := by linarith [hFX.2]
          have hL' : ((G + 1 : Nat) : Real) * (K : Real) =
              (Y : Real) + 1 := by
            push_cast at hL ⊢
            exact hL
          linarith
        rw [← hKcast]
        exact ⟨le_trans hYcellR.1 hRlo, hRhi⟩
      · have hqc : -27 ≤ q.val ∧ q.val < 0 := by omega
        have hr := hRceil (hceil hqc)
        have hFY : (F : Real) = (Y : Real) := by exact_mod_cast hYF.symm
        have hRnear : (Y : Real) - 1 < R ∧ R < (Y : Real) + 1 := by
          constructor <;> linarith [hFX.1, hFX.2, hwratio]
        have hboundary (hbnd : Y = G * K) : R = (Y : Real) := by
          rcases hneg hqc.2 with
            ⟨n, z, hqn, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
          have hn27 : n ≤ 27 := by omega
          have hPbound : 5 ^ n < 2 ^ 64 := by
            have hp := Nat.pow_le_pow_right (by norm_num : 0 < 5) hn27
            exact lt_of_le_of_lt hp (by norm_num)
          have hdenposR : (0 : Real) <
              (((2 ^ 64) * 5 ^ n : Nat) : Real) := by positivity
          have hRrep : R = ((w.val * 2 ^ (z + 127) : Nat) : Real) /
              (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [hTrep]
            push_cast
            ring_nf
          by_contra hne
          rcases lt_or_gt_of_ne hne with hdown | hup
          · have hnumltR : ((w.val * 2 ^ (z + 127) : Nat) : Real) <
                ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) := by
              rw [hRrep] at hdown
              have hd := (div_lt_iff₀ hdenposR).mp hdown
              push_cast at hd ⊢
              nlinarith
            have hnumhiR : ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) <
                ((w.val * 2 ^ (z + 127) : Nat) : Real) +
                  (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
              have hnear := hRnear.1
              rw [hRrep] at hnear
              have hd := (lt_div_iff₀ hdenposR).mp hnear
              push_cast at hd ⊢
              nlinarith
            have hnumlt : w.val * 2 ^ (z + 127) <
                Y * 2 ^ 64 * 5 ^ n := by exact_mod_cast hnumltR
            have hnumhi : Y * 2 ^ 64 * 5 ^ n <
                w.val * 2 ^ (z + 127) + 2 ^ 64 * 5 ^ n := by
              exact_mod_cast hnumhiR
            have hzsum : z + 127 = 128 + (z - 1) := by omega
            have hNmul : 2 ^ (z + 127) = 2 ^ 128 * 2 ^ (z - 1) := by
              rw [hzsum, pow_add]
            have hBsq : 2 ^ 128 = 2 ^ 64 * 2 ^ 64 := by norm_num
            have hwfac : w.val * 2 ^ (z + 127) =
                (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) := by
              rw [hNmul, hBsq]
              ring
            have hYfac : Y * 2 ^ 64 * 5 ^ n =
                (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) := by
              rw [hbnd, hKfac]
              ring
            rw [hwfac, hYfac] at hnumlt hnumhi
            have hAC : w.val * 2 ^ (z - 1) < G * 2 ^ d * 5 ^ n := by
              exact (Nat.mul_lt_mul_left (by positivity :
                0 < 2 ^ 64 * 2 ^ 64)).mp hnumlt
            have hstep : w.val * 2 ^ (z - 1) + 1 ≤
                G * 2 ^ d * 5 ^ n := by omega
            have hBP : 2 ^ 64 * 5 ^ n < 2 ^ 64 * 2 ^ 64 :=
              Nat.mul_lt_mul_of_pos_left hPbound (by positivity)
            have hlower := Nat.mul_le_mul_left (2 ^ 64 * 2 ^ 64) hstep
            have hcontra : (2 ^ 64 * 2 ^ 64) *
                  (G * 2 ^ d * 5 ^ n) <
                (2 ^ 64 * 2 ^ 64) *
                  (G * 2 ^ d * 5 ^ n) := by
              calc
                _ < (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) +
                    2 ^ 64 * 5 ^ n := hnumhi
                _ < (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) +
                    2 ^ 64 * 2 ^ 64 := Nat.add_lt_add_left hBP _
                _ = (2 ^ 64 * 2 ^ 64) *
                    (w.val * 2 ^ (z - 1) + 1) := by ring
                _ ≤ _ := hlower
            exact (lt_irrefl _ hcontra)
          · have hnumgtR : ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) <
                ((w.val * 2 ^ (z + 127) : Nat) : Real) := by
              rw [hRrep] at hup
              have hd := (lt_div_iff₀ hdenposR).mp hup
              push_cast at hd ⊢
              nlinarith
            have hnumloR : ((w.val * 2 ^ (z + 127) : Nat) : Real) <
                ((Y * 2 ^ 64 * 5 ^ n : Nat) : Real) +
                  (((2 ^ 64) * 5 ^ n : Nat) : Real) := by
              have hnear := hRnear.2
              rw [hRrep] at hnear
              have hd := (div_lt_iff₀ hdenposR).mp hnear
              push_cast at hd ⊢
              nlinarith
            have hnumgt : Y * 2 ^ 64 * 5 ^ n <
                w.val * 2 ^ (z + 127) := by exact_mod_cast hnumgtR
            have hnumlo : w.val * 2 ^ (z + 127) <
                Y * 2 ^ 64 * 5 ^ n + 2 ^ 64 * 5 ^ n := by
              exact_mod_cast hnumloR
            have hzsum : z + 127 = 128 + (z - 1) := by omega
            have hNmul : 2 ^ (z + 127) = 2 ^ 128 * 2 ^ (z - 1) := by
              rw [hzsum, pow_add]
            have hBsq : 2 ^ 128 = 2 ^ 64 * 2 ^ 64 := by norm_num
            have hwfac : w.val * 2 ^ (z + 127) =
                (2 ^ 64 * 2 ^ 64) * (w.val * 2 ^ (z - 1)) := by
              rw [hNmul, hBsq]
              ring
            have hYfac : Y * 2 ^ 64 * 5 ^ n =
                (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) := by
              rw [hbnd, hKfac]
              ring
            rw [hwfac, hYfac] at hnumgt hnumlo
            have hCA : G * 2 ^ d * 5 ^ n < w.val * 2 ^ (z - 1) := by
              exact (Nat.mul_lt_mul_left (by positivity :
                0 < 2 ^ 64 * 2 ^ 64)).mp hnumgt
            have hstep : G * 2 ^ d * 5 ^ n + 1 ≤
                w.val * 2 ^ (z - 1) := by omega
            have hBP : 2 ^ 64 * 5 ^ n < 2 ^ 64 * 2 ^ 64 :=
              Nat.mul_lt_mul_of_pos_left hPbound (by positivity)
            have hlower := Nat.mul_le_mul_left (2 ^ 64 * 2 ^ 64) hstep
            have hcontra : (2 ^ 64 * 2 ^ 64) *
                  (w.val * 2 ^ (z - 1)) <
                (2 ^ 64 * 2 ^ 64) *
                  (w.val * 2 ^ (z - 1)) := by
              calc
                _ < (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) +
                    2 ^ 64 * 5 ^ n := hnumlo
                _ < (2 ^ 64 * 2 ^ 64) * (G * 2 ^ d * 5 ^ n) +
                    2 ^ 64 * 2 ^ 64 := Nat.add_lt_add_left hBP _
                _ = (2 ^ 64 * 2 ^ 64) *
                    (G * 2 ^ d * 5 ^ n + 1) := by ring
                _ ≤ _ := hlower
            exact (lt_irrefl _ hcontra)
        have hRlo : (G : Real) * (K : Real) ≤ R := by
          by_cases hbnd : Y = G * K
          · have heq := hboundary hbnd
            rw [heq]
            exact_mod_cast (show G * K ≤ Y by omega)
          · have hnext : G * K + 1 ≤ Y := by omega
            have hnextR : (G : Real) * (K : Real) + 1 ≤ (Y : Real) := by
              exact_mod_cast hnext
            linarith
        have hRhi : R < ((G + 1 : Nat) : Real) * (K : Real) := by
          have hnext : Y + 1 ≤ (G + 1) * K := by omega
          have hnextR : (Y : Real) + 1 ≤
              ((G + 1 : Nat) : Real) * (K : Real) := by
            exact_mod_cast hnext
          linarith
        rw [← hKcast]
        exact ⟨hRlo, hRhi⟩
    have hcell : (G : Real) * (2 : Real) ^ s ≤ R ∧
        R < ((G + 1 : Nat) : Real) * (2 : Real) ^ s := by
      rcases hflow with hnr | href
      · exact nonref_cell hnr
      · exact refine_cell href
    refine ⟨hcell.1, hcell.2, ?_⟩
    constructor
    · rintro ⟨hmid, hGmod⟩
      have hGodd : Odd G := by
        refine ⟨G / 2, ?_⟩
        omega
      have hwne : w.val ≠ 0 := by omega
      rcases Nat.exists_eq_two_pow_mul_odd hwne with ⟨v, h, hhodd, hwfac⟩
      have hhpos : 1 ≤ h := by
        rcases hhodd with ⟨m, hm⟩
        omega
      have hv63 : v ≤ 63 := by
        by_contra hv
        have hp : 2 ^ 64 ≤ 2 ^ v :=
          Nat.pow_le_pow_right (by norm_num) (by omega)
        have hmul : 2 ^ v ≤ 2 ^ v * h := by
          have hm := Nat.mul_le_mul_left (2 ^ v) hhpos
          simpa using hm
        rw [← hwfac] at hmul
        omega
      have hqrange : -4 ≤ q.val ∧ q.val ≤ 23 := by
        by_cases hqn : q.val < 0
        · rcases hneg hqn with
            ⟨n, z, hqneg, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
          have hmidR : (w.val : Real) * T /
              (2 : Real) ^ (64 : Nat) =
                (G : Real) * (2 : Real) ^ s := by simpa [R] using hmid
          rw [hTrep] at hmidR
          have hrepr : (w.val : Real) *
                ((2 : Real) ^ (z + 127) / (5 : Real) ^ n) /
                (2 : Real) ^ (64 : Nat) =
              ((w.val * 2 ^ (z + 127) : Nat) : Real) /
                (((5 ^ n) * 2 ^ 64 : Nat) : Real) := by
            push_cast
            ring
          rw [hrepr] at hmidR
          have hcross := (div_eq_iff (by positivity :
            ((((5 ^ n) * 2 ^ 64 : Nat) : Real)) ≠ 0)).mp hmidR
          have hrhs : (G : Real) * (2 : Real) ^ s *
                (((5 ^ n) * 2 ^ 64 : Nat) : Real) =
              (((G * 2 ^ s) * (5 ^ n * 2 ^ 64) : Nat) : Real) := by
            push_cast
            ring
          rw [hrhs] at hcross
          have hnat : w.val * 2 ^ (z + 127) =
              (G * 2 ^ s) * (5 ^ n * 2 ^ 64) := by exact_mod_cast hcross
          have hnat' : 2 ^ (v + z + 127) * h =
              2 ^ (s + 64) * (G * 5 ^ n) := by
            calc
              2 ^ (v + z + 127) * h =
                  (2 ^ v * h) * 2 ^ (z + 127) := by
                    rw [show v + z + 127 = v + (z + 127) by omega,
                      pow_add]
                    ring
              _ = w.val * 2 ^ (z + 127) := by rw [← hwfac]
              _ = (G * 2 ^ s) * (5 ^ n * 2 ^ 64) := hnat
              _ = 2 ^ (s + 64) * (G * 5 ^ n) := by
                rw [pow_add]
                ring
          have hodd5 : Odd (5 ^ n) := (by decide : Odd (5 : Nat)).pow
          have hexp : v + z + 127 = s + 64 :=
            two_adic_unique hhodd (hGodd.mul hodd5) hnat'
          have hz11 : z ≤ 11 := by
            have hs74 : s ≤ 74 := by simp [s]; omega
            omega
          have hn4 : n ≤ 4 := by
            by_contra hn
            have hp5 : 5 ^ 5 ≤ 5 ^ n :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            have hp2 : 2 ^ z ≤ 2 ^ 11 :=
              Nat.pow_le_pow_right (by norm_num) hz11
            norm_num at hp5 hp2
            omega
          omega
        · have hqnonneg : 0 ≤ q.val := by omega
          rcases Int.eq_ofNat_of_zero_le hqnonneg with ⟨qn, hqnat⟩
          have hmidR : (w.val : Real) * T /
              (2 : Real) ^ (64 : Nat) =
                (G : Real) * (2 : Real) ^ s := by simpa [R] using hmid
          have he53 : e ≤ 53 := by
            by_cases hk : 0 ≤ 127 - e
            · rcases Int.eq_ofNat_of_zero_le hk with ⟨kn, hkn⟩
              have hTpos : T = (2 : Real) ^ kn * (5 : Real) ^ qn := by
                simp [T, hqnat, hkn, zpow_ofNat]
              rw [hTpos] at hmidR
              have hrepr : (w.val : Real) *
                    ((2 : Real) ^ kn * (5 : Real) ^ qn) /
                    (2 : Real) ^ (64 : Nat) =
                  (((2 ^ kn) * (w.val * 5 ^ qn) : Nat) : Real) /
                    ((2 ^ 64 : Nat) : Real) := by
                push_cast
                ring
              rw [hrepr] at hmidR
              have hcross := (div_eq_iff (by positivity :
                (((2 ^ 64 : Nat) : Real)) ≠ 0)).mp hmidR
              have hrhs : (G : Real) * (2 : Real) ^ s *
                    ((2 ^ 64 : Nat) : Real) =
                  ((2 ^ (s + 64) * G : Nat) : Real) := by
                push_cast
                rw [pow_add]
                ring
              rw [hrhs] at hcross
              have hnat : 2 ^ kn * (w.val * 5 ^ qn) =
                  2 ^ (s + 64) * G := by exact_mod_cast hcross
              have hnat' : 2 ^ (kn + v) * (h * 5 ^ qn) =
                  2 ^ (s + 64) * G := by
                calc
                  2 ^ (kn + v) * (h * 5 ^ qn) =
                      2 ^ kn * ((2 ^ v * h) * 5 ^ qn) := by
                        rw [pow_add]
                        ring
                  _ = 2 ^ kn * (w.val * 5 ^ qn) := by rw [← hwfac]
                  _ = 2 ^ (s + 64) * G := hnat
              have hodd5 : Odd (5 ^ qn) :=
                (by decide : Odd (5 : Nat)).pow
              have hexp : kn + v = s + 64 :=
                two_adic_unique (hhodd.mul hodd5) hGodd hnat'
              have hs73 : 73 ≤ s := by simp [s]
              omega
            · have hk' : 0 ≤ e - 127 := by omega
              rcases Int.eq_ofNat_of_zero_le hk' with ⟨kn, hkn⟩
              have hkneg : 127 - e = -(kn : Int) := by omega
              have hTneg : T = (2 : Real) ^ (-(kn : Int)) *
                  (5 : Real) ^ qn := by simp [T, hqnat, hkneg, zpow_ofNat]
              rw [hTneg] at hmidR
              have hpow : (2 : Real) ^ (-(kn : Int)) =
                  1 / (2 : Real) ^ kn := by
                rw [zpow_neg, zpow_natCast]
                simp
              rw [hpow] at hmidR
              have hrepr : (w.val : Real) *
                    (1 / (2 : Real) ^ kn * (5 : Real) ^ qn) /
                    (2 : Real) ^ (64 : Nat) =
                  ((w.val * 5 ^ qn : Nat) : Real) /
                    ((2 ^ (kn + 64) : Nat) : Real) := by
                push_cast
                rw [pow_add]
                ring
              rw [hrepr] at hmidR
              have hcross := (div_eq_iff (by positivity :
                (((2 ^ (kn + 64) : Nat) : Real)) ≠ 0)).mp hmidR
              have hrhs : (G : Real) * (2 : Real) ^ s *
                    ((2 ^ (kn + 64) : Nat) : Real) =
                  ((2 ^ (s + 64 + kn) * G : Nat) : Real) := by
                push_cast
                rw [show s + 64 + kn = s + (kn + 64) by omega, pow_add]
                ring
              rw [hrhs] at hcross
              have hnat : w.val * 5 ^ qn =
                  2 ^ (s + 64 + kn) * G := by exact_mod_cast hcross
              have hnat' : 2 ^ v * (h * 5 ^ qn) =
                  2 ^ (s + 64 + kn) * G := by
                calc
                  2 ^ v * (h * 5 ^ qn) = (2 ^ v * h) * 5 ^ qn := by ring
                  _ = w.val * 5 ^ qn := by rw [← hwfac]
                  _ = 2 ^ (s + 64 + kn) * G := hnat
              have hodd5 : Odd (5 ^ qn) :=
                (by decide : Odd (5 : Nat)).pow
              have hexp : v = s + 64 + kn :=
                two_adic_unique (hhodd.mul hodd5) hGodd hnat'
              have hs73 : 73 ≤ s := by simp [s]
              omega
          have heDef : e = 217706 * q.val / 65536 - q.val := rfl
          omega
      have hmidK : R = ((G * K : Nat) : Real) := by
        rw [hmid]
        push_cast
        rw [hKcast]
      have hYL : Y = G * K := by
        by_cases hqn : q.val < 0
        · have hceilq := hceil ⟨by omega, hqn⟩
          have hrc := hRceil hceilq
          have hFLt : F < G * K + 1 := by
            have hreal : (F : Real) < ((G * K : Nat) : Real) + 1 := by
              rw [← hmidK]
              linarith [hFX.1, hrc.1, hwratio]
            exact_mod_cast hreal
          have hLLt : G * K < F + 1 := by
            have hreal : ((G * K : Nat) : Real) < (F : Real) + 1 := by
              rw [← hmidK]
              linarith [hFX.2, hrc.2]
            exact_mod_cast hreal
          have hFL : F = G * K := by omega
          rcases hflow with hnr | href
          · have hYwa : Y = w.val * a.val := by
              simpa [Y] using hnr.2.2.2
            let c : Nat := w.val * b.val / 2 ^ 64
            have hFc : F = w.val * a.val + c := by simpa [c] using hF
            by_cases hc : c = 0
            · calc
                Y = w.val * a.val := hYwa
                _ = w.val * a.val + c := by simp [hc]
                _ = F := hFc.symm
                _ = G * K := hFL
            · have hcpos : 0 < c := Nat.pos_of_ne_zero hc
              have hcellLower : G * K ≤ Y := hYcell.1
              exfalso
              apply (Nat.not_lt_of_ge hcellLower)
              calc
                Y < Y + c := Nat.lt_add_of_pos_right hcpos
                _ = F := by rw [hYwa, hFc]
                _ = G * K := hFL
          · have hYF : Y = F := by
              simpa [Y, hF] using href
            exact hYF.trans hFL
        · have hqnonneg : 0 ≤ q.val := by omega
          have hTex := hexact ⟨hqnonneg, by omega⟩
          have hb0 := hbzero ⟨hqnonneg, hqrange.2⟩
          have hYwa : Y = w.val * a.val := by
            rcases hflow with hnr | href
            · simpa [Y] using hnr.2.2.2
            · simpa [Y, hb0] using href
          have hRwa : R = ((w.val * a.val : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [← hTex]
            simp [D, hb0]
            push_cast
            norm_num
            ring
          have hcast : (Y : Real) = ((G * K : Nat) : Real) := by
            rw [hYwa, ← hRwa, hmidK]
          exact_mod_cast hcast
      refine ⟨hqrange.1, hqrange.2, hGmod, ?_, ?_⟩
      · have hm := congrArg (fun x : Nat => x % (2 ^ 64)) hYL
        simp [Y, hKfac, Nat.add_mod, Nat.mul_mod,
          Nat.mod_eq_of_lt hloB] at hm
        omega
      · have hlo0 : lo.val = 0 := by
          have hm := congrArg (fun x : Nat => x % (2 ^ 64)) hYL
          simp [Y, hKfac, Nat.add_mod, Nat.mul_mod,
            Nat.mod_eq_of_lt hloB] at hm
          omega
        have heq : 2 ^ 64 * hi.val = 2 ^ 64 * (G * 2 ^ d) := by
          rw [show 2 ^ 64 * hi.val = Y by simp [Y, hlo0, mul_comm]]
          rw [hYL, hKfac]
          ring
        exact Nat.eq_of_mul_eq_mul_left (by positivity) heq.symm
    · rintro ⟨hqlo4, hqhi23, hGmod, hlo1, hrec⟩
      have hrec' : G * 2 ^ d = hi.val := by simpa [d] using hrec
      have hYLlo : Y = G * K + lo.val := by
        simp only [Y]
        rw [hKfac, ← hrec']
        ring
      by_cases hqn : q.val < 0
      · rcases hneg hqn with
          ⟨n, z, hqneg, hn1, hz1, hzlo, hzhi, hez, hTrep⟩
        have hn4 : n ≤ 4 := by omega
        have hz11 : z ≤ 11 := by
          by_contra hz
          have hp2 : 2 ^ 11 ≤ 2 ^ (z - 1) :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hp5 : 5 ^ n ≤ 5 ^ 4 :=
            Nat.pow_le_pow_right (by norm_num) hn4
          norm_num at hp2 hp5
          omega
        have hceilq := hceil ⟨by omega, hqn⟩
        have hnz : (n = 1 ∧ z = 3) ∨ (n = 2 ∧ z = 5) ∨
            (n = 3 ∧ z = 7) ∨ (n = 4 ∧ z = 10) := by
          have hnCases : n = 1 ∨ n = 2 ∨ n = 3 ∨ n = 4 := by omega
          rcases hnCases with h1 | h2 | h3 | h4
          · left
            refine ⟨h1, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; left
            refine ⟨h2, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; right; left
            refine ⟨h3, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
          · right; right; right
            refine ⟨h4, ?_⟩
            subst n
            interval_cases z <;> norm_num at hzlo <;> norm_num at hzhi
            rfl
        have finish_refined (delta : Nat) (hdelta0 : 0 < delta)
            (hdelta625 : delta ≤ 625)
            (hDrel : D * 5 ^ n = 2 ^ (z + 127) + delta)
            (href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64) :
            R = (G : Real) * (2 : Real) ^ s ∧ G % 4 = 1 := by
          let c : Nat := w.val * b.val / 2 ^ 64
          let r : Nat := w.val * b.val % 2 ^ 64
          have hrB : r < 2 ^ 64 := Nat.mod_lt _ (by positivity)
          have hdivmod : w.val * b.val = c * 2 ^ 64 + r := by
            calc
              w.val * b.val = 2 ^ 64 * (w.val * b.val / 2 ^ 64) +
                  w.val * b.val % 2 ^ 64 := (Nat.div_add_mod _ _).symm
              _ = c * 2 ^ 64 + r := by simp only [c, r]; ring
          have hYref : Y = w.val * a.val + c := by simpa [Y, c] using href
          have hwD : w.val * D = 2 ^ 64 * (w.val * a.val + c) + r := by
            change w.val * (a.val * 2 ^ 64 + b.val) = _
            calc
              w.val * (a.val * 2 ^ 64 + b.val) =
                  2 ^ 64 * (w.val * a.val) + w.val * b.val := by ring
              _ = 2 ^ 64 * (w.val * a.val) + (c * 2 ^ 64 + r) := by
                rw [hdivmod]
              _ = 2 ^ 64 * (w.val * a.val + c) + r := by ring
          have hbalance :
              w.val * 2 ^ (z + 127) + w.val * delta =
                2 ^ 64 * (G * K) * 5 ^ n +
                  (2 ^ 64 * lo.val + r) * 5 ^ n := by
            calc
              w.val * 2 ^ (z + 127) + w.val * delta =
                  w.val * (2 ^ (z + 127) + delta) := by ring
              _ = w.val * (D * 5 ^ n) := by rw [hDrel]
              _ = (w.val * D) * 5 ^ n := by ring
              _ = (2 ^ 64 * (w.val * a.val + c) + r) * 5 ^ n := by
                rw [hwD]
              _ = (2 ^ 64 * Y + r) * 5 ^ n := by rw [hYref]
              _ = 2 ^ 64 * (G * K) * 5 ^ n +
                    (2 ^ 64 * lo.val + r) * 5 ^ n := by
                rw [hYLlo]
                ring
          have hP625 : 5 ^ n ≤ 625 := by
            have hp := Nat.pow_le_pow_right (by norm_num : 0 < (5 : Nat)) hn4
            norm_num at hp ⊢
            exact hp
          have hN128 : 2 ^ 128 ≤ 2 ^ (z + 127) :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hwd : w.val * delta < 2 ^ (z + 127) := by
            have h1 : w.val * delta < 2 ^ 64 * delta :=
              Nat.mul_lt_mul_of_pos_right hwB hdelta0
            have h2 : 2 ^ 64 * delta ≤ 2 ^ 64 * 625 :=
              Nat.mul_le_mul_left _ hdelta625
            have h3 : 2 ^ 64 * 625 < 2 ^ 128 := by norm_num
            omega
          have hcorr : (2 ^ 64 * lo.val + r) * 5 ^ n <
              2 ^ (z + 127) := by
            have ht : 2 ^ 64 * lo.val + r ≤ 2 * 2 ^ 64 := by omega
            have hm := Nat.mul_le_mul ht hP625
            have hnum : (2 * 2 ^ 64) * 625 < 2 ^ 128 := by norm_num
            omega
          have hNK : 2 ^ (z + 127) ∣ 2 ^ 64 * K := by
            have hp : 2 ^ (z + 127) ∣ 2 ^ (64 + s) :=
              pow_dvd_pow 2 (by simp [s]; omega)
            simpa [K, pow_add] using hp
          have hbase : 2 ^ (z + 127) ∣
              2 ^ 64 * (G * K) * 5 ^ n := by
            rcases hNK with ⟨j, hj⟩
            use G * j * 5 ^ n
            calc
              2 ^ 64 * (G * K) * 5 ^ n = G * (2 ^ 64 * K) * 5 ^ n := by ring
              _ = G * (2 ^ (z + 127) * j) * 5 ^ n := by rw [hj]
              _ = 2 ^ (z + 127) * (G * j * 5 ^ n) := by ring
          have hwN : 2 ^ (z + 127) ∣ w.val * 2 ^ (z + 127) := by
            exact dvd_mul_left _ _
          have hm : (w.val * delta) % 2 ^ (z + 127) =
              ((2 ^ 64 * lo.val + r) * 5 ^ n) % 2 ^ (z + 127) := by
            calc
              (w.val * delta) % 2 ^ (z + 127) =
                  (w.val * 2 ^ (z + 127) + w.val * delta) %
                    2 ^ (z + 127) := by
                rw [Nat.add_mod, Nat.mod_eq_zero_of_dvd hwN]
                simp
              _ = (2 ^ 64 * (G * K) * 5 ^ n +
                    (2 ^ 64 * lo.val + r) * 5 ^ n) %
                    2 ^ (z + 127) :=
                congrArg (fun x : Nat => x % 2 ^ (z + 127)) hbalance
              _ = ((2 ^ 64 * lo.val + r) * 5 ^ n) %
                    2 ^ (z + 127) := by
                rw [Nat.add_mod, Nat.mod_eq_zero_of_dvd hbase]
                simp
          rw [Nat.mod_eq_of_lt hwd, Nat.mod_eq_of_lt hcorr] at hm
          have hcorreq : w.val * delta =
              (2 ^ 64 * lo.val + r) * 5 ^ n := hm
          rw [hcorreq] at hbalance
          have hnat : w.val * 2 ^ (z + 127) =
              2 ^ 64 * (G * K) * 5 ^ n := Nat.add_right_cancel hbalance
          have hreal : R = ((G * K : Nat) : Real) := by
            change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
            rw [hTrep]
            calc
              (w.val : Real) *
                    ((2 : Real) ^ (z + 127) / (5 : Real) ^ n) /
                    (2 : Real) ^ (64 : Nat) =
                  ((w.val * 2 ^ (z + 127) : Nat) : Real) /
                    ((5 ^ n * 2 ^ 64 : Nat) : Real) := by
                push_cast
                ring
              _ = ((G * K : Nat) : Real) := by
                rw [hnat]
                push_cast
                field_simp
          constructor
          · rw [hreal]
            push_cast
            rw [hKcast]
          · exact hGmod
        have ceil_unique (C : Nat) (hC : 0 < C)
            (hloT : ((C - 1 : Nat) : Real) < T)
            (hhiT : T < (C : Real)) : D = C := by
          have hloR : ((C - 1 : Nat) : Real) < (D : Real) :=
            lt_trans hloT hceilq.2
          have hloN : C - 1 < D := by exact_mod_cast hloR
          have hhiR : (D : Real) < (C : Real) + 1 := by
            linarith [hceilq.1, hhiT]
          have hhiN : D < C + 1 := by exact_mod_cast hhiR
          have hCD : C ≤ D := by
            rw [← Nat.succ_pred_eq_of_pos hC]
            exact hloN
          exact Nat.le_antisymm (Nat.lt_succ_iff.mp hhiN) hCD
        have parts_of_D (C A Bv : Nat) (hDC : D = C)
            (hcalc : C = A * 2 ^ 64 + Bv) (hBv : Bv < 2 ^ 64) :
            a.val = A ∧ b.val = Bv := by
          have hdef : D = a.val * 2 ^ 64 + b.val := rfl
          omega
        have odd_nonref_zero (haodd : Odd a.val) (hYwa : Y = w.val * a.val)
            (hlo0 : lo.val = 0) : False := by
          have hGodd : Odd G := by
            refine ⟨G / 2, ?_⟩
            omega
          have hwne : w.val ≠ 0 := by omega
          rcases Nat.exists_eq_two_pow_mul_odd hwne with ⟨v, h, hhodd, hwfac⟩
          have hhpos : 1 ≤ h := by
            rcases hhodd with ⟨m, hm⟩
            omega
          have hv63 : v ≤ 63 := by
            by_contra hv
            have hp : 2 ^ 64 ≤ 2 ^ v :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            have hmul : 2 ^ v ≤ 2 ^ v * h := by
              have hm := Nat.mul_le_mul_left (2 ^ v) hhpos
              simpa using hm
            rw [← hwfac] at hmul
            omega
          have heq : 2 ^ v * (h * a.val) = 2 ^ s * G := by
            calc
              2 ^ v * (h * a.val) = w.val * a.val := by rw [hwfac]; ring
              _ = Y := hYwa.symm
              _ = G * K := by rw [hYLlo, hlo0]; simp
              _ = 2 ^ s * G := by change G * 2 ^ s = _; ring
          have hexp : v = s := two_adic_unique (hhodd.mul haodd) hGodd heq
          simp [s] at hexp
          omega
        rcases hnz with hnz | hnz | hnz | hnz
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((272225893536750770770699685945414569165 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (272225893536750770770699685945414569165 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 272225893536750770770699685945414569165
            (by norm_num) hloT hhiT
          have hab := parts_of_D 272225893536750770770699685945414569165
            14757395258967641292 14757395258967641293 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              rcases hu_cases with hu0 | hu1
              · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
              · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
            · exact href
          apply finish_refined 1 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((217780714829400616616559748756331655332 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (217780714829400616616559748756331655332 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 217780714829400616616559748756331655332
            (by norm_num) hloT hhiT
          have hab := parts_of_D 217780714829400616616559748756331655332
            11805916207174113034 4427218577690292388 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              rcases hu_cases with hu0 | hu1
              · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
              · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                rw [hYwa, hab.1, hK] at hYLlo
                omega
            · exact href
          apply finish_refined 4 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((174224571863520493293247799005065324266 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (174224571863520493293247799005065324266 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 174224571863520493293247799005065324266
            (by norm_num) hloT hhiT
          have hab := parts_of_D 174224571863520493293247799005065324266
            9444732965739290427 7231123676894144234 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              have hloCases : lo.val = 0 ∨ lo.val = 1 := by omega
              rcases hloCases with hlo0 | hlo1'
              · apply odd_nonref_zero (by rw [hab.1]; decide) hYwa hlo0
              · rcases hu_cases with hu0 | hu1
                · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                  have heqres : w.val * 9444732965739290427 =
                      G * 2 ^ 73 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 9444732965739290427 = H * 2 ^ 73 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
                · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                  have heqres : w.val * 9444732965739290427 =
                      G * 2 ^ 74 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 9444732965739290427 = H * 2 ^ 74 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
            · exact href
          apply finish_refined 66 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
        · rcases hnz with ⟨rfl, rfl⟩
          have hloT :
              ((278759314981632789269196478408104518825 - 1 : Nat) : Real) < T := by
            rw [hTrep]
            norm_num
          have hhiT : T <
              (278759314981632789269196478408104518825 : Nat) := by
            rw [hTrep]
            norm_num
          have hDC := ceil_unique 278759314981632789269196478408104518825
            (by norm_num) hloT hhiT
          have hab := parts_of_D 278759314981632789269196478408104518825
            15111572745182864683 15259146697772541097 hDC (by norm_num) (by norm_num)
          have href : hi.val * 2 ^ 64 + lo.val =
              w.val * a.val + w.val * b.val / 2 ^ 64 := by
            rcases hflow with hnr | href
            · exfalso
              have hYwa : Y = w.val * a.val := by simpa [Y] using hnr.2.2.2
              have hloCases : lo.val = 0 ∨ lo.val = 1 := by omega
              rcases hloCases with hlo0 | hlo1'
              · apply odd_nonref_zero (by rw [hab.1]; decide) hYwa hlo0
              · rcases hu_cases with hu0 | hu1
                · have hK : K = 2 ^ 73 := by simp [K, s, hu0]
                  have heqres : w.val * 15111572745182864683 =
                      G * 2 ^ 73 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 15111572745182864683 = H * 2 ^ 73 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
                · have hK : K = 2 ^ 74 := by simp [K, s, hu1]
                  have heqres : w.val * 15111572745182864683 =
                      G * 2 ^ 74 + 1 := by simpa [hYwa, hab.1, hK, hlo1'] using hYLlo
                  have nores : ∀ W H : Nat, 2 ^ 63 ≤ W → W < 2 ^ 64 →
                      H % 4 = 1 → W * 15111572745182864683 = H * 2 ^ 74 + 1 →
                      False := by
                    as_aux_lemma =>
                      intro W H hWlo hWhi hHm hEq
                      omega
                  exact nores w.val G hw hwB hGmod heqres
            · exact href
          apply finish_refined 153 (by norm_num) (by norm_num) _ href
          rw [hDC]
          norm_num
      · have hqnonneg : 0 ≤ q.val := by omega
        have hTex := hexact ⟨hqnonneg, by omega⟩
        have hb0 := hbzero ⟨hqnonneg, hqhi23⟩
        have hYwa : Y = w.val * a.val := by
          rcases hflow with hnr | href
          · simpa [Y] using hnr.2.2.2
          · simpa [Y, hb0] using href
        have he53 : e ≤ 53 := by
          have heDef : e = 217706 * q.val / 65536 - q.val := rfl
          omega
        rcases Int.eq_ofNat_of_zero_le hqnonneg with ⟨qn, hqnat⟩
        have hk : 0 ≤ 127 - e := by omega
        rcases Int.eq_ofNat_of_zero_le hk with ⟨kn, hkn⟩
        have hkn74 : 74 ≤ kn := by omega
        have hTpos : T = (2 : Real) ^ kn * (5 : Real) ^ qn := by
          simp [T, hqnat, hkn]
        have hDnat : D = 2 ^ kn * 5 ^ qn := by
          have hc : (D : Real) = ((2 ^ kn * 5 ^ qn : Nat) : Real) := by
            rw [hTex, hTpos]
            push_cast
            rfl
          exact_mod_cast hc
        have hdiv : 2 ^ 65 ∣ D := by
          use 2 ^ (kn - 65) * 5 ^ qn
          calc
            D = 2 ^ kn * 5 ^ qn := hDnat
            _ = 2 ^ 65 * (2 ^ (kn - 65) * 5 ^ qn) := by
              conv_lhs => rw [show kn = 65 + (kn - 65) by omega, pow_add]
              ring
        rcases hdiv with ⟨c, hc⟩
        have hac : a.val = 2 * c := by
          have heq : 2 ^ 64 * a.val = 2 ^ 64 * (2 * c) := by
            rw [show 2 ^ 64 * a.val = D by simp [D, hb0, mul_comm]]
            rw [hc]
            norm_num [pow_succ]
            ring
          exact Nat.eq_of_mul_eq_mul_left (by positivity) heq
        have hlo0 : lo.val = 0 := by
          have hYrep : Y = 2 * (w.val * c) := by
            rw [hYwa, hac]
            ring
          have hLrep : G * K = 2 * (G * (2 ^ 63 * 2 ^ d)) := by
            rw [hKfac]
            norm_num [pow_succ]
            ring
          omega
        have hRwa : R = ((w.val * a.val : Nat) : Real) := by
          change (w.val : Real) * T / (2 : Real) ^ (64 : Nat) = _
          rw [← hTex]
          simp [D, hb0]
          push_cast
          norm_num
          ring
        constructor
        · calc
            R = (Y : Real) := by rw [hYwa, hRwa]
            _ = ((G * K : Nat) : Real) := by rw [hYLlo, hlo0]; simp
            _ = (G : Real) * (2 : Real) ^ (73 + u) := by
              push_cast
              rw [hKcast]
        · exact hGmod

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (50)
More program dependencies (17)
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 10 entries
Technical checking records
Local closure summary
{
  "node": "IntegerProductApproximation",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "3660517bf1876804408c99bfbb202b87a10c78a4c3b43f03e5cc87f98858b642",
  "active_statement_hash": "75412ac4275f18fdd01cef90e7cc32e60d796270d05a399638a9f0032bec6813",
  "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-145",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "IntegerPowerTableCertificate": "c4b148e6d8bda215",
    "num_imp_dec2flt_lemire_compute_product_approx": "8e1d7c32e8a2a55b",
    "num_imp_dec2flt_lemire_full_multiplication": "e9ae367eb148ddf9",
    "num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE": "2532cbe447dc07c7",
    "num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE": "8a9e3dfa7e9aa5ec"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_lemire_compute_product_approx": "af91b9cbef7779f3",
    "num_imp_dec2flt_lemire_full_multiplication": "46682dcb8efbf576",
    "num_imp_dec2flt_lemire_power": "56b7c3bd69ca3c6a",
    "num_imp_dec2flt_table_LARGEST_POWER_OF_FIVE": "133d9b18714defd6",
    "num_imp_dec2flt_table_POWER_OF_FIVE_128": "bdd28e83b60fa362",
    "num_imp_dec2flt_table_SMALLEST_POWER_OF_FIVE": "5394ae3df8a14bc9"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:b1eb079c19eee51f27ca4a2348a106ca67cd01c95ccb2d8127f47e617d7a8734",
  "current_fingerprint": "sound-v2:b1eb079c19eee51f27ca4a2348a106ca67cd01c95ccb2d8127f47e617d7a8734",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}