Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma
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
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.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.
Uses
Used by
Inspect related source (50)
- compute_product_approx · dec2flt/lemire.rs:134 Program Rust
- full_multiplication · dec2flt/lemire.rs:126 Program Rust
- LARGEST_POWER_OF_FIVE · dec2flt/table.rs:10 Program Rust
More program dependencies (17)
- SMALLEST_POWER_OF_FIVE · dec2flt/table.rs:9 Program Rust
- power · dec2flt/lemire.rs:121 Program Rust
- POWER_OF_FIVE_128 · dec2flt/table.rs:19 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:56 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:22 Program Rust
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
Standard-library dependencies (30)
- bitand · core/src/ops/bit.rs:178 Rust library
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
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
- Classical.choice
- Quot.sound
- propext
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
- IntegerPowerFiniteCertificate
- IntegerPowerRealBridge
- IntegerPowerTableCertificate
- IntegerProductApproximation
- num imp dec2flt lemire compute product approx
- num imp dec2flt lemire full multiplication
All 10 entries
- IntegerPowerFiniteCertificate
- IntegerPowerRealBridge
- IntegerPowerTableCertificate
- IntegerProductApproximation
- num imp dec2flt lemire compute product approx
- num imp dec2flt lemire full multiplication
- num imp dec2flt lemire power
- num imp dec2flt table LARGEST POWER OF FIVE
- num imp dec2flt table POWER OF FIVE 128
- num imp dec2flt table SMALLEST POWER OF FIVE
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": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-68",
"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: Pass",
"assessment_status": "Pass",
"fingerprint_relation": "unknown",
"assessment_fingerprint": null,
"current_fingerprint": "sound-v2:b1eb079c19eee51f27ca4a2348a106ca67cd01c95ccb2d8127f47e617d7a8734",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}