import ContractExact /-! # Exact runtime, starting champion Every kernel is built on one exact dot product. Each finite product `±m · 2^idx` goes into 32-bit limbs (`Acc.add`), and the exact total is rounded once with FloatLib's own rounding. The rounding (`finishFast`) never builds the total as a big integer: it turns the nonzero limbs into 32-bit digits of `pos - neg` and rounds from the top two digits and a sticky bit, which rounds the same (`roundDyadic_jam`). A NaN or an infinity anywhere falls back to FloatLib's `dot`. `dotFn n fa fb` reads its operands through functions (`dotFn_eq`). `dot` reads two arrays and `sum` is a dot with ones. Every other dot-based kernel is the contract's own definition run with `dotFn` instead of the spec's `dotOf` (`linearWith dotFn`, `conv2dWith dotFn`, …), so it reads its inputs, weights and bias in place and its proof is one rewrite. Max pooling keeps its running maximum as a word and compares finite words by their exact values (`maxW`, proved against FloatLib's `maximum`). The residual add and batch norm use their specs. -/ namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats /-! ## The exact accumulator -/ /-- Positive and negative limbs, "a NaN or infinity was seen", and "every term so far was a zero of negative sign" (which decides the sign of a zero result). -/ structure Acc where pos : Array Nat neg : Array Nat special : Bool negZero : Bool /-- No terms yet: zero limbs, no special value, and "every term was −0" vacuously true. -/ def Acc.empty : Acc := ⟨Array.replicate 18 0, Array.replicate 18 0, false, true⟩ /-- The exact integer total held in the limbs. -/ def Acc.total (acc : Acc) : Int := (limbVal acc.pos.toList : Int) - limbVal acc.neg.toList /-- Add the exact term `±m · 2^idx`; a zero term only updates the zero-sign flag. -/ @[inline] def Acc.add (acc : Acc) (m idx : Nat) (neg : Bool) : Acc := match acc with | ⟨pos, ng, sp, nz⟩ => if m = 0 then ⟨pos, ng, sp, nz && neg⟩ else if neg then ⟨pos, addLimbs64 ng m.toUInt64 idx, sp, false⟩ else ⟨addLimbs64 pos m.toUInt64 idx, ng, sp, false⟩ /-- Record that a NaN or an infinity was seen. -/ @[inline] def Acc.markSpecial (acc : Acc) : Acc := match acc with | ⟨pos, ng, _, nz⟩ => ⟨pos, ng, true, nz⟩ /-- Everything the proof needs to know about one `Acc.add`. -/ theorem Acc.add_spec (acc : Acc) (m idx : Nat) (neg : Bool) (hp : acc.pos.size = 18) (hn : acc.neg.size = 18) (hm : m < 2 ^ 64) (hi : idx / 32 + 1 < 18) : (acc.add m idx neg).pos.size = 18 ∧ (acc.add m idx neg).neg.size = 18 ∧ (acc.add m idx neg).special = acc.special ∧ (acc.add m idx neg).negZero = (acc.negZero && (decide (m = 0) && neg)) ∧ (acc.add m idx neg).total = acc.total + (if neg then -(m : Int) else m) * 2 ^ idx := by obtain ⟨pos, ng, sp, nz⟩ := acc simp only at hp hn by_cases hm0 : m = 0 · simp [Acc.add, Acc.total, hm0, hp, hn] · cases neg · refine ⟨by simp [Acc.add, hm0, addLimbs64_size, hp], by simp [Acc.add, hm0, hn], by simp [Acc.add, hm0], by simp [Acc.add, hm0], ?_⟩ simp only [Acc.add, hm0, ite_false, Bool.false_eq_true, addLimbs64_eq _ _ _ hm, Acc.total] rw [addLimbs_val _ _ _ (by omega)] push_cast; ring · refine ⟨by simp [Acc.add, hm0, hp], by simp [Acc.add, hm0, addLimbs64_size, hn], by simp [Acc.add, hm0], by simp [Acc.add, hm0], ?_⟩ simp only [Acc.add, hm0, ite_false, ite_true, addLimbs64_eq _ _ _ hm, Acc.total] rw [addLimbs_val _ _ _ (by omega)] push_cast; ring theorem Acc.total_empty : Acc.empty.total = 0 := by decide /-! ## Rounding without big integers -/ section open FloatLib.Numerics /-- Jamming the low `j` bits into a sticky bit rounds the same, when at least 25 bits (the 24-bit significand and a guard bit) stay above them. -/ theorem roundDyadic_jam (s : Bool) (m : Nat) (e : Int) (j : Nat) (h : j + 25 ≤ m.log2) : Model.roundDyadic Fmt ⟨s, shiftRightJam m j, e + j⟩ = Model.roundDyadic Fmt ⟨s, m, e⟩ := by have hm0 : m ≠ 0 := by rintro rfl; simp at h have hpow : 2 ^ m.log2 ≤ m := Nat.log2_self_le hm0 have hm : 2 ^ (j + 1) ≤ m := le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) hpow have hlog : (shiftRightJam m j).log2 = m.log2 - j := log2_shiftRightJam m j hm have hne : shiftRightJam m j ≠ 0 := shiftRightJam_ne_zero m j (le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) hm) rw [← Model.roundDyadicGeneral_eq_roundDyadic, ← Model.roundDyadicGeneral_eq_roundDyadic] unfold Model.roundDyadicGeneral simp only [hne, hm0, beq_iff_eq, ite_false, hlog] have htot : ((m.log2 - j : Nat) : Int) + (e + j) = (m.log2 : Int) + e := by omega rw [htot] have hfw : Fmt.fracWidth = 23 := rfl have hsub : Fmt.minSubnormalExponent = -149 := rfl have hmin : Fmt.minNormalExponent = -126 := rfl rw [hfw, hsub, hmin] split_ifs with h1 h2 h3 h4 <;> try omega · rfl · congr 1 obtain ⟨a, ha⟩ : ∃ a : Nat, e - -149 = Int.negSucc a := ⟨(-(e + 149) - 1).toNat, by omega⟩ have hb : e + j - -149 = Int.negSucc (a - j) := by omega rw [ha, hb] dsimp only rw [show a - j + 1 = (a + 1) - j by omega] exact roundShiftRightEven_shiftRightJam m j (a + 1) (by omega) · congr 1 rw [show m.log2 - j - 23 = (m.log2 - 23) - j by omega] exact roundShiftRightEven_shiftRightJam m j _ (by omega) /-! ## Values of limb windows -/ /-- `k` limbs of `a` from index `i`: `a[i] + 2^32·a[i+1] + …`. -/ def winVal (a : Array Nat) (i : Nat) : Nat → Nat | 0 => 0 | k + 1 => a[i]! + 2 ^ 32 * winVal a (i + 1) k theorem winVal_split (a : Array Nat) (i k m : Nat) : winVal a i (k + m) = winVal a i k + 2 ^ (32 * k) * winVal a (i + k) m := by induction k generalizing i with | zero => simp [winVal] | succ k ih => rw [show k + 1 + m = (k + m) + 1 by omega, winVal, winVal, ih, show i + 1 + k = i + (k + 1) by omega] rw [show 32 * (k + 1) = 32 + 32 * k by ring, pow_add] ring theorem winVal_congr (a b : Array Nat) (i j k : Nat) (h : ∀ t, t < k → a[i + t]! = b[j + t]!) : winVal a i k = winVal b j k := by induction k generalizing i j with | zero => rfl | succ k ih => rw [winVal, winVal, ih (i + 1) (j + 1) (fun t ht => by rw [show i + 1 + t = i + (t + 1) by omega, show j + 1 + t = j + (t + 1) by omega] exact h (t + 1) (by omega)), show a[i]! = b[j]! from by simpa using h 0 (by omega)] theorem winVal_eq_zero_iff (a : Array Nat) (i k : Nat) : winVal a i k = 0 ↔ ∀ t, t < k → a[i + t]! = 0 := by induction k generalizing i with | zero => simp [winVal] | succ k ih => rw [winVal, Nat.add_eq_zero_iff, Nat.mul_eq_zero, ih (i + 1)] have hp : (2 : ℕ) ^ 32 ≠ 0 := by norm_num constructor · rintro ⟨h0, h1 | h1⟩ t ht · exact absurd h1 hp · rcases t with _ | t · simpa using h0 · rw [show i + (t + 1) = i + 1 + t by omega]; exact h1 t (by omega) · intro h refine ⟨by simpa using h 0 (by omega), Or.inr fun t ht => ?_⟩ rw [show i + 1 + t = i + (t + 1) by omega]; exact h (t + 1) (by omega) theorem winVal_lt (a : Array Nat) (i k : Nat) (h : ∀ t, t < k → a[i + t]! < 2 ^ 32) : winVal a i k < 2 ^ (32 * k) := by induction k generalizing i with | zero => simp [winVal] | succ k ih => have h0 := h 0 (by omega) have h1 := ih (i + 1) (fun t ht => by rw [show i + 1 + t = i + (t + 1) by omega]; exact h (t + 1) (by omega)) simp only [Nat.add_zero] at h0 rw [winVal, show 32 * (k + 1) = 32 + 32 * k by ring, pow_add] nlinarith theorem getElem!_size_le (a : Array Nat) (t : Nat) (h : a.size ≤ t) : a[t]! = 0 := by simp [getElem!_neg a t (by omega)] theorem winVal_eq_limbVal (a : Array Nat) (i k : Nat) (h : a.size ≤ i + k) : winVal a i k = limbVal (a.toList.drop i) := by induction k generalizing i with | zero => simp [winVal, List.drop_of_length_le (by simpa using h : a.toList.length ≤ i), limbVal] | succ k ih => rw [winVal, ih (i + 1) (by omega)] by_cases hi : i < a.size · have hl : i < a.toList.length := by simpa using hi simp only [List.drop_eq_getElem_cons hl, limbVal, getElem!_pos a i hi, Array.getElem_toList] · rw [getElem!_size_le a i (by omega), List.drop_of_length_le (by simp; omega), List.drop_of_length_le (by simp; omega)] simp [limbVal] /-- A limb array's value is its window between two runs of zero limbs. -/ theorem limbVal_eq_win (a : Array Nat) (lo n : Nat) (hlow : ∀ t, t < lo → a[t]! = 0) (hhigh : ∀ t, lo + n ≤ t → a[t]! = 0) : limbVal a.toList = 2 ^ (32 * lo) * winVal a lo n := by rw [← List.drop_zero (l := a.toList), ← winVal_eq_limbVal a 0 (lo + (n + a.size)) (by omega), winVal_split, winVal_split] rw [(winVal_eq_zero_iff a 0 lo).2 (fun t ht => by simpa using hlow t ht), (winVal_eq_zero_iff a (0 + lo + n) a.size).2 (fun t _ => hhigh _ (by omega))] simp /-! ## Digits of `A - B` -/ /-- One pass over limbs `i, i+1, …`: normalise `a` and `b` to 32-bit digits (carries `ca`, `cb`) and subtract them (borrow `br`), pushing the digits of the difference. -/ def diffGo (a b : Array Nat) (i ca cb br : Nat) (out : Array Nat) : Nat → Array Nat × Nat × Nat × Nat | 0 => (out, ca, cb, br) | k + 1 => let ta := a[i]! + ca let tb := b[i]! + cb let t := ta % 4294967296 + 4294967296 - tb % 4294967296 - br diffGo a b (i + 1) (ta / 4294967296) (tb / 4294967296) (1 - t / 4294967296) (out.push (t % 4294967296)) k theorem diffGo_spec (a b : Array Nat) (i ca cb br : Nat) (out : Array Nat) (k : Nat) (hbr : br ≤ 1) (hout : ∀ t, t < out.size → out[t]! < 2 ^ 32) : let r := diffGo a b i ca cb br out k r.1.size = out.size + k ∧ (∀ t, t < r.1.size → r.1[t]! < 2 ^ 32) ∧ (winVal r.1 0 r.1.size : Int) + 2 ^ (32 * (out.size + k)) * (r.2.1 - r.2.2.1 - r.2.2.2 : Int) = winVal out 0 out.size + 2 ^ (32 * out.size) * ((winVal a i k : Int) - winVal b i k + ca - cb - br) := by induction k generalizing i ca cb br out with | zero => exact ⟨by simp [diffGo], by simpa [diffGo] using hout, by simp [diffGo, winVal]⟩ | succ k ih => simp only [diffGo] set ta := a[i]! + ca set tb := b[i]! + cb set t := ta % 4294967296 + 4294967296 - tb % 4294967296 - br have htb : tb % 4294967296 < 4294967296 := Nat.mod_lt _ (by norm_num) have hta : ta % 4294967296 < 4294967296 := Nat.mod_lt _ (by norm_num) have ht2 : t < 2 * 4294967296 := by omega have hq : t / 4294967296 ≤ 1 := by omega have hd : t % 4294967296 < 2 ^ 32 := by have := Nat.mod_lt t (by norm_num : 4294967296 > 0); norm_num; omega have hout' : ∀ s, s < (out.push (t % 4294967296)).size → (out.push (t % 4294967296))[s]! < 2 ^ 32 := by intro s hs simp only [Array.size_push] at hs rcases Nat.lt_or_eq_of_le (Nat.lt_succ_iff.mp hs) with hs | rfl · rw [getElem!_pos _ s (by simp; omega), Array.getElem_push_lt hs, ← getElem!_pos out s hs] exact hout s hs · rw [getElem!_pos _ _ (by simp), Array.getElem_push_eq]; exact hd obtain ⟨h1, h2, h3⟩ := ih (i + 1) (ta / 4294967296) (tb / 4294967296) (1 - t / 4294967296) (out.push (t % 4294967296)) (by omega) hout' refine ⟨by simp [h1]; omega, h2, ?_⟩ have hpush : winVal (out.push (t % 4294967296)) 0 (out.size + 1) = winVal out 0 out.size + 2 ^ (32 * out.size) * (t % 4294967296) := by rw [winVal_split, winVal_congr _ out 0 0 out.size (fun s hs => by rw [getElem!_pos _ _ (by simp; omega), Array.getElem_push_lt (by omega), ← getElem!_pos out _ (by omega)])] simp only [winVal, Nat.zero_add, Nat.mul_zero, Nat.add_zero] rw [getElem!_pos _ _ (by simp), Array.getElem_push_eq] simp only [Array.size_push] at h3 ⊢ rw [show out.size + (k + 1) = out.size + 1 + k by omega] rw [h3, hpush, winVal, winVal] have hstep : ((t % 4294967296 : Nat) : Int) + 4294967296 * ((ta / 4294967296 : Nat) - (tb / 4294967296 : Nat) - (1 - t / 4294967296 : Nat)) = (a[i]! : Int) - b[i]! + ca - cb - br := by have e1 := Nat.mod_add_div ta 4294967296 have e2 := Nat.mod_add_div tb 4294967296 have e3 := Nat.mod_add_div t 4294967296 have e4 : (1 - t / 4294967296 : Nat) = 1 - (t / 4294967296 : Int) := by omega have e5 : (t : Int) = ta % 4294967296 + 4294967296 - tb % 4294967296 - br := by omega push_cast [e4] omega rw [show 32 * (out.size + 1) = 32 * out.size + 32 by ring, pow_add] simp only [Nat.cast_add, Nat.cast_mul, Nat.cast_pow, Nat.cast_ofNat] linear_combination (2 : Int) ^ (32 * out.size) * hstep /-! ## Rounding the digits from a 62-bit window -/ /-- The highest nonzero digit below `k`, or none. -/ def topGo (r : Array Nat) : Nat → Option Nat | 0 => none | k + 1 => if r[k]! != 0 then some k else topGo r k /-- "Some digit below `k` is nonzero." -/ def anyGo (r : Array Nat) : Nat → Bool | 0 => false | k + 1 => r[k]! != 0 || anyGo r k theorem topGo_none (r : Array Nat) (k : Nat) (h : topGo r k = none) : ∀ t, t < k → r[t]! = 0 := by induction k with | zero => intro t ht; omega | succ k ih => simp only [topGo] at h split at h · cases h · rename_i hk intro t ht rcases Nat.lt_or_eq_of_le (Nat.lt_succ_iff.mp ht) with ht | rfl · exact ih h t ht · simpa using hk theorem topGo_some (r : Array Nat) (k h : Nat) (hh : topGo r k = some h) : h < k ∧ r[h]! ≠ 0 ∧ ∀ t, h < t → t < k → r[t]! = 0 := by induction k with | zero => simp [topGo] at hh | succ k ih => simp only [topGo] at hh split at hh · rename_i hk cases hh exact ⟨by omega, by simpa using hk, fun t h1 h2 => by omega⟩ · rename_i hk obtain ⟨a, b, c⟩ := ih hh refine ⟨by omega, b, fun t h1 h2 => ?_⟩ rcases Nat.lt_or_eq_of_le (Nat.lt_succ_iff.mp h2) with h2 | rfl · exact c t h1 h2 · simpa using hk theorem anyGo_false (r : Array Nat) (k : Nat) : anyGo r k = false ↔ ∀ t, t < k → r[t]! = 0 := by induction k with | zero => simp [anyGo] | succ k ih => simp only [anyGo, Bool.or_eq_false_iff, bne_eq_false_iff_eq, ih] constructor · rintro ⟨h0, h1⟩ t ht rcases Nat.lt_or_eq_of_le (Nat.lt_succ_iff.mp ht) with ht | rfl · exact h1 t ht · exact h0 · intro h; exact ⟨h k (by omega), fun t ht => h t (by omega)⟩ theorem anyGo_eq (r : Array Nat) (k : Nat) : anyGo r k = (winVal r 0 k != 0) := by have hz := winVal_eq_zero_iff r 0 k simp only [Nat.zero_add] at hz cases h : anyGo r k · rw [hz.2 ((anyGo_false r k).1 h)]; rfl · have : winVal r 0 k ≠ 0 := fun h0 => by have := (anyGo_false r k).2 (hz.1 h0); simp_all simp [this] /-- A 62-bit window of the top two digits and a sticky bit for the rest is `shiftRightJam`. -/ theorem jam_window (low x y h : Nat) (hlow : low < 2 ^ (32 * h)) : shiftRightJam (low + 2 ^ (32 * h) * (x + 2 ^ 32 * y)) (32 * h + 2) = if x % 4 != 0 || low != 0 then (y * 1073741824 + x / 4) ||| 1 else y * 1073741824 + x / 4 := by have hX0 : 0 < 2 ^ (32 * h) := by positivity have hdiv : 2 ^ (32 * h + 2) = 4 * 2 ^ (32 * h) := by rw [pow_add]; ring have hs : x % 4 < 4 := Nat.mod_lt x (by norm_num) have hA : low + 2 ^ (32 * h) * (x % 4) < 4 * 2 ^ (32 * h) := by nlinarith have hsplit : low + 2 ^ (32 * h) * (x + 2 ^ 32 * y) = (low + 2 ^ (32 * h) * (x % 4)) + 4 * 2 ^ (32 * h) * (y * 1073741824 + x / 4) := by have h4 := Nat.mod_add_div x 4 rw [show (2 : Nat) ^ 32 = 4 * 1073741824 by norm_num] conv_lhs => rw [← h4] ring have hz : low + 2 ^ (32 * h) * (x % 4) = 0 ↔ ¬(x % 4 ≠ 0 ∨ low ≠ 0) := by constructor · intro h0 have h1 := Nat.eq_zero_of_add_eq_zero h0 rcases Nat.mul_eq_zero.mp h1.2 with h2 | h2 <;> omega · intro h0; simp only [not_or, not_not] at h0; simp [h0.1, h0.2] unfold shiftRightJam dsimp only rw [hdiv, hsplit, Nat.add_mul_div_left _ _ (by omega), Nat.div_eq_of_lt hA, Nat.zero_add, Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt hA] by_cases hc : x % 4 ≠ 0 ∨ low ≠ 0 · have : ¬ (low + 2 ^ (32 * h) * (x % 4) = 0) := fun h0 => hz.mp h0 hc have hb : (x % 4 != 0 || low != 0) = true := by simpa [bne_iff_ne] using hc simp only [beq_iff_eq, this, ite_false, hb, ite_true] · have : low + 2 ^ (32 * h) * (x % 4) = 0 := hz.mpr hc have hb : (x % 4 != 0 || low != 0) = false := by simpa [bne_iff_ne] using hc simp only [beq_iff_eq, this, ite_true, hb] rfl /-- Round `±R · 2^e`, `R` the value of the digits `r`: exactly while `R` has at most two digits, otherwise from the top two digits and a sticky bit. -/ def finishDigits (neg : Bool) (r : Array Nat) (e : Int) (negZero : Bool) : UInt32 := match topGo r r.size with | none => finishWord 0 e negZero | some 0 => finishWord (if neg then -(r[0]! : Int) else r[0]!) e negZero | some (h + 1) => let x := r[h]! let v := r[h + 1]! * 1073741824 + x / 4 let m := if x % 4 != 0 || anyGo r h then v ||| 1 else v toWord (Model.roundDyadic Fmt ⟨neg, m, e + ((32 * h + 2 : Nat) : Int)⟩) theorem finishWord_signed (neg : Bool) (R : Nat) (e : Int) (negZero : Bool) (hR : R ≠ 0) : finishWord (if neg then -(R : Int) else R) e negZero = toWord (Model.roundDyadic Fmt ⟨neg, R, e⟩) := by have hpos : (0 : Int) < R := by exact_mod_cast Nat.pos_of_ne_zero hR cases neg <;> simp [finishWord, ofIntDyadic, hR, not_lt.mpr hpos.le, Nat.pos_of_ne_zero hR] theorem finishDigits_eq (neg : Bool) (r : Array Nat) (e : Int) (negZero : Bool) (hr : ∀ t, t < r.size → r[t]! < 2 ^ 32) : finishDigits neg r e negZero = finishWord (if neg then -(winVal r 0 r.size : Int) else winVal r 0 r.size) e negZero := by unfold finishDigits split · rename_i hnone rw [(winVal_eq_zero_iff r 0 r.size).2 (fun t ht => by simpa using topGo_none r _ hnone t ht)] simp · rename_i hsome obtain ⟨h1, h2, h3⟩ := topGo_some r _ 0 hsome rw [show r.size = 1 + (r.size - 1) by omega, winVal_split, (winVal_eq_zero_iff r (0 + 1) (r.size - 1)).2 (fun t ht => h3 _ (by omega) (by omega))] simp [winVal] · rename_i h hsome obtain ⟨h1, h2, h3⟩ := topGo_some r _ (h + 1) hsome have hR : winVal r 0 r.size = winVal r 0 h + 2 ^ (32 * h) * (r[h]! + 2 ^ 32 * r[h + 1]!) := by rw [show r.size = h + (2 + (r.size - h - 2)) by omega, winVal_split, winVal_split, (winVal_eq_zero_iff r (0 + h + 2) _).2 (fun t ht => h3 _ (by omega) (by omega))] simp [winVal] have hlow : winVal r 0 h < 2 ^ (32 * h) := winVal_lt r 0 h (fun t ht => hr _ (by omega)) have hR0 : winVal r 0 r.size ≠ 0 := by rw [hR]; have : 0 < 2 ^ (32 * h) * (r[h]! + 2 ^ 32 * r[h + 1]!) := by have := Nat.pos_of_ne_zero h2; positivity omega have hlog : 32 * h + 2 + 25 ≤ (winVal r 0 r.size).log2 := by rw [Nat.le_log2 hR0, hR] have : 1 ≤ r[h + 1]! := Nat.pos_of_ne_zero h2 calc 2 ^ (32 * h + 2 + 25) ≤ 2 ^ (32 * h) * 2 ^ 32 := by rw [← pow_add]; exact Nat.pow_le_pow_right (by norm_num) (by omega) _ ≤ 2 ^ (32 * h) * (r[h]! + 2 ^ 32 * r[h + 1]!) := by gcongr; nlinarith _ ≤ _ := by omega rw [finishWord_signed neg _ e negZero hR0, ← roundDyadic_jam neg _ e _ hlog, hR, jam_window _ _ _ h hlow, anyGo_eq] /-! ## The fast final rounding -/ /-- One past the highest limb below `k` where either array is nonzero. -/ def highLimb (pos neg : Array Nat) : Nat → Nat | 0 => 0 | k + 1 => if pos[k]! != 0 || neg[k]! != 0 then k + 1 else highLimb pos neg k theorem highLimb_zero (pos neg : Array Nat) (k : Nat) : ∀ t, highLimb pos neg k ≤ t → t < k → pos[t]! = 0 ∧ neg[t]! = 0 := by induction k with | zero => intro t _ ht; omega | succ k ih => intro t h1 h2 simp only [highLimb] at h1 split at h1 · omega · rename_i hk rcases Nat.lt_or_eq_of_le (Nat.lt_succ_iff.mp h2) with h2 | rfl · exact ih t h1 h2 · simpa [Bool.or_eq_true, bne_iff_ne, not_or] using hk /-- `finishLimbs` without big integers: the limbs between the lowest and highest nonzero ones become 32-bit digits of `pos - neg` (one pass; a second with the operands swapped if the difference is negative), which are rounded from their top 62 bits and a sticky bit. A carry or borrow left over, which the accumulator never produces, falls back to `finishLimbs`. -/ def finishFast (pos neg : Array Nat) (e : Int) (negZero : Bool) : UInt32 := let lo := lowLimb pos neg pos.size 0 let n := highLimb pos neg (max pos.size neg.size) + 1 - lo let e' := e + ((32 * lo : Nat) : Int) let d := diffGo pos neg lo 0 0 0 (Array.emptyWithCapacity n) n if d.2.1 = 0 ∧ d.2.2.1 = 0 ∧ d.2.2.2 = 0 then finishDigits false d.1 e' negZero else let d' := diffGo neg pos lo 0 0 0 (Array.emptyWithCapacity n) n if d'.2.1 = 0 ∧ d'.2.2.1 = 0 ∧ d'.2.2.2 = 0 then finishDigits true d'.1 e' negZero else finishLimbs pos neg e negZero theorem finishFast_eq (pos neg : Array Nat) (e : Int) (negZero : Bool) : finishFast pos neg e negZero = finishWord ((limbVal pos.toList : Int) - limbVal neg.toList) e negZero := by unfold finishFast dsimp only set lo := lowLimb pos neg pos.size 0 set n := highLimb pos neg (max pos.size neg.size) + 1 - lo have hlow := lowLimb_zero pos neg pos.size 0 have hhigh := highLimb_zero pos neg (max pos.size neg.size) have hzero (a : Array Nat) (ha : a = pos ∨ a = neg) : limbVal a.toList = 2 ^ (32 * lo) * winVal a lo n := by apply limbVal_eq_win a lo n · intro t ht; rcases ha with rfl | rfl · exact (hlow t (by omega) ht).1 · exact (hlow t (by omega) ht).2 · intro t ht by_cases hts : t < max pos.size neg.size · rcases ha with rfl | rfl · exact (hhigh t (by omega) hts).1 · exact (hhigh t (by omega) hts).2 · rcases ha with rfl | rfl <;> exact getElem!_size_le _ t (by omega) have hP := hzero pos (Or.inl rfl) have hN := hzero neg (Or.inr rfl) rw [show (Array.emptyWithCapacity n : Array Nat) = #[] from rfl] have hd := diffGo_spec pos neg lo 0 0 0 #[] n (by omega) (by simp) have hd' := diffGo_spec neg pos lo 0 0 0 #[] n (by omega) (by simp) have hw0 : winVal (#[] : Array Nat) 0 0 = 0 := rfl simp only [List.size_toArray, List.length_nil, Nat.mul_zero, pow_zero, one_mul, hw0, Nat.cast_zero, zero_add] at hd hd' obtain ⟨-, hb, hv⟩ := hd obtain ⟨-, hb', hv'⟩ := hd' split_ifs with h1 h2 · rw [finishDigits_eq false _ _ _ hb, finishWord_scale, hP, hN] simp only [Bool.false_eq_true, ite_false] congr 1 rw [h1.1, h1.2.1, h1.2.2] at hv push_cast at hv ⊢ linear_combination (2 : Int) ^ (32 * lo) * hv · rw [finishDigits_eq true _ _ _ hb', finishWord_scale, hP, hN] simp only [ite_true] congr 1 rw [h2.1, h2.2.1, h2.2.2] at hv' push_cast at hv' ⊢ linear_combination (-(2 : Int) ^ (32 * lo)) * hv' · exact finishLimbs_eq pos neg e negZero end /-! ## The exact dot product -/ /-- One term: a NaN or infinity marks the accumulator, otherwise the exact product goes into the limbs. -/ @[inline] def dotStep (acc : Acc) (wa wb : UInt32) : Acc := if expOf wa = 255 ∨ expOf wb = 255 then acc.markSpecial else acc.add (mantOf wa * mantOf wb) (idxOf wa + idxOf wb) (Bool.xor (signOf wa) (signOf wb)) /-- The loop has FloatLib's `dotStateLoop` shape; the accumulator's fields are plain arguments so the compiled loop allocates nothing per term. -/ @[specialize] def dotLoop (fa fb : Nat → UInt32) (pos neg : Array Nat) (sp nz : Bool) (index : Nat) : Nat → Acc | 0 => ⟨pos, neg, sp, nz⟩ | count + 1 => match dotStep ⟨pos, neg, sp, nz⟩ (fa index) (fb index) with | ⟨p, n, s, z⟩ => dotLoop fa fb p n s z (index + 1) count /-- The exact dot product of `[fa 0, …, fa (n-1)]` and `[fb 0, …, fb (n-1)]`. -/ @[inline] def dotFn (n : Nat) (fa fb : Nat → UInt32) : UInt32 := let acc := dotLoop fa fb (Array.replicate 18 0) (Array.replicate 18 0) false true 0 n if acc.special then dotSpec (Array.ofFn (n := n) fun i => fa i) (Array.ofFn (n := n) fun i => fb i) else finishFast acc.pos acc.neg (-298) (n ≠ 0 && acc.negZero) theorem sv_mul (wa wb : UInt32) : (if Bool.xor (signOf wa) (signOf wb) then -((mantOf wa * mantOf wb : Nat) : Int) else ((mantOf wa * mantOf wb : Nat) : Int)) = sv wa * sv wb := by unfold sv; cases signOf wa <;> cases signOf wb <;> simp /-- A NaN or infinity, once seen, stays recorded. -/ theorem dotLoop_special (fa fb : Nat → UInt32) (pos neg : Array Nat) (nz : Bool) (index count : Nat) : (dotLoop fa fb pos neg true nz index count).special = true := by induction count generalizing pos neg nz index with | zero => rfl | succ count ih => rw [dotLoop] have hs : (dotStep ⟨pos, neg, true, nz⟩ (fa index) (fb index)).special = true := by unfold dotStep Acc.markSpecial Acc.add; split_ifs <;> rfl generalize dotStep ⟨pos, neg, true, nz⟩ (fa index) (fb index) = r at hs ⊢ obtain ⟨p, q, s, z⟩ := r simp only at hs; subst hs exact ih .. /-- What a loop without specials tells us, stated over arrays `A`, `B` that hold the operands (as the toolkit's FloatLib lemmas are). -/ theorem dotLoopFacts (fa fb : Nat → UInt32) (A B : Array UInt32) (n : Nat) (hA : ∀ k, k < n → A[k]! = fa k) (hB : ∀ k, k < n → B[k]! = fb k) (acc : Acc) (index remaining : Nat) (h : index + remaining ≤ n) (hp : acc.pos.size = 18) (hn : acc.neg.size = 18) (hfin : (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).special = false) : acc.special = false ∧ (∀ k, index ≤ k → k < index + remaining → expOf A[k]! ≠ 255 ∧ expOf B[k]! ≠ 255) ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).total = acc.total + dsum A B index remaining ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).negZero = (acc.negZero && dallNZ A B index remaining) := by induction remaining generalizing acc index with | zero => obtain ⟨p, n, s, z⟩ := acc; simp_all [dotLoop, dsum, dallNZ] | succ count ih => obtain ⟨p, q, s, z⟩ := acc simp only at hp hn have ha := hA index (by omega) have hb := hB index (by omega) by_cases he : expOf (fa index) = 255 ∨ expOf (fb index) = 255 · exfalso rw [dotLoop] at hfin have hs : (dotStep ⟨p, q, s, z⟩ (fa index) (fb index)).special = true := by simp [dotStep, he, Acc.markSpecial] generalize dotStep ⟨p, q, s, z⟩ (fa index) (fb index) = r at hs hfin obtain ⟨p', q', s', z'⟩ := r simp only at hs; subst hs rw [dotLoop_special] at hfin exact Bool.noConfusion hfin · have he' := not_or.mp he obtain ⟨h1, h2, h3, h4, h5⟩ := Acc.add_spec ⟨p, q, s, z⟩ (mantOf (fa index) * mantOf (fb index)) (idxOf (fa index) + idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) hp hn (mul_mant_lt _ _) (by have := idxOf_le _ he'.1; have := idxOf_le _ he'.2; omega) have hstep : dotStep ⟨p, q, s, z⟩ (fa index) (fb index) = Acc.add ⟨p, q, s, z⟩ (mantOf (fa index) * mantOf (fb index)) (idxOf (fa index) + idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) := by simp [dotStep, he] rw [dotLoop, hstep] at hfin ⊢ obtain ⟨hs, hall, hD, hz⟩ := ih _ (index + 1) (by omega) h1 h2 hfin refine ⟨by rw [← h3, hs], ?_, ?_, ?_⟩ · intro k k1 k2 rcases Nat.lt_or_ge k (index + 1) with hk | hk · have : k = index := by omega subst this; rw [ha, hb]; exact he' · exact hall k hk (by omega) · rw [hD, h5, dsum, ha, hb, sv_mul]; ring · rw [hz, h4, dallNZ, ha, hb]; simp [Bool.and_assoc] theorem getElem!_ofFn (n : Nat) (f : Nat → UInt32) (k : Nat) (hk : k < n) : (Array.ofFn (n := n) fun i => f i.val)[k]! = f k := by rw [getElem!_pos _ k (by simpa using hk), Array.getElem_ofFn] theorem dotFn_eq (n : Nat) (fa fb : Nat → UInt32) : dotFn n fa fb = dotSpec (Array.ofFn (n := n) fun i => fa i) (Array.ofFn (n := n) fun i => fb i) := by unfold dotFn set A := Array.ofFn (n := n) fun i => fa i with hA set B := Array.ofFn (n := n) fun i => fb i with hB set acc := dotLoop fa fb (Array.replicate 18 0) (Array.replicate 18 0) false true 0 n with hacc by_cases hsp : acc.special = true · simp [hsp] · have hsp' : acc.special = false := by simpa using hsp obtain ⟨-, hall, hD, hz⟩ := dotLoopFacts fa fb A B n (getElem!_ofFn n fa) (getElem!_ofFn n fb) Acc.empty 0 n (by omega) (by simp [Acc.empty]) (by simp [Acc.empty]) hsp' have hD : acc.total = dsum A B 0 n := by have := hD; simp only [Acc.total_empty, Int.zero_add] at this; exact this have hz : acc.negZero = dallNZ A B 0 n := by have := hz; simp only [Acc.empty, Bool.true_and] at this; exact this simp only [hsp', Bool.false_eq_true, ite_false] rw [finishFast_eq, show ((limbVal acc.pos.toList : Int) - limbVal acc.neg.toList) = acc.total from rfl] -- FloatLib's side: every word is finite, and its exact total is ours. have hAsize : A.size = n := by simp [hA] have hBsize : B.size = n := by simp [hB] set L := A.map (Model.ofUInt32 (fmt := Fmt)) with hL set R := B.map (Model.ofUInt32 (fmt := Fmt)) with hR have hLR : L.size = R.size := by simp [hL, hR, hAsize, hBsize] have hLsize : L.size = n := by simp [hL, hAsize] have hLfin : ∀ k (hk : k < L.size), Model.isFinite L[k] = true := by intro k hk have hk' : k < A.size := by simpa [hL] using hk have := (hall k (by omega) (by omega)).1 rw [getElem!_pos A k hk'] at this simp [hL, isFinite_ofUInt32, this] have hRfin : ∀ k (hk : k < R.size), Model.isFinite R[k] = true := by intro k hk have hk' : k < B.size := by simpa [hR] using hk have := (hall k (by omega) (by omega)).2 rw [getElem!_pos B k hk'] at this simp [hR, isFinite_ofUInt32, this] have hslice : Model.Reduction.Semantics.finiteDotSlice L R 0 L.size (by simp) (by simp [hLR]) = (acc.total : ℚ) * 2 ^ (-298 : ℤ) := by have := finiteDotSlice_eq A B 0 L.size (by simp [hL]) (by simp [hL, hAsize, hBsize]) (by intro k k1 k2; exact hall k k1 (by omega)) exact this.trans (by rw [hLsize, hD]) have hexact := Model.Reduction.Internal.dotState_exact_toRat (destination := Fmt) L R hLR hLfin hRfin unfold dotSpec Model.dot by_cases hS0 : acc.total = 0 · have hz0 : (Model.Reduction.Internal.dotState L R hLR : Model.Reduction.Internal.State Fmt).exact.significand = 0 := by rw [← FloatLib.Numerics.Dyadic.toRat_eq_zero_iff, hexact, hslice, hS0]; simp rw [Model.Reduction.dotWithStatus_eq_zero_of_finite Fmt L R .nearestEven hLR hLfin hRfin hz0, hS0, finishWord_zero] unfold Model.Reduction.Internal.dotState obtain ⟨h1, h2⟩ := dotStateLoop_flags A B {} 0 L.size (by simp [hL]) (by simp [hL, hAsize, hBsize]) (by intro k k1 k2; exact hall k k1 (by omega)) have h1' : (Model.Reduction.Internal.dotStateLoop L R ({} : Model.Reduction.Internal.State Fmt) 0 L.size (by simp) (by simp [hLR])).sawFiniteTerm = decide (L.size ≠ 0) := by rw [show decide (L.size ≠ 0) = (false || decide (L.size ≠ 0)) by simp]; exact h1 have h2' : (Model.Reduction.Internal.dotStateLoop L R ({} : Model.Reduction.Internal.State Fmt) 0 L.size (by simp) (by simp [hLR])).allTermsNegativeZero = dallNZ A B 0 L.size := by rw [show dallNZ A B 0 L.size = (true && dallNZ A B 0 L.size) by simp]; exact h2 simp only [Except.map, h1', h2'] rw [hz] simp [hLsize] · have hnz : Model.Reduction.Semantics.finiteDot L.toList R.toList ≠ .ok 0 := by rw [Model.Reduction.Semantics.finiteDot_toList_eq_slice L R hLR] intro h injection h with h rw [hslice] at h simp at h; exact hS0 h rw [Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L R .nearestEven hLR hLfin hRfin hnz] simp only [Except.map] exact finishWord_of_ne _ _ _ _ hS0 (by rw [hexact, hslice]) /-! ## Arrays as functions -/ theorem ofFn_getElem! (a : Array UInt32) (n : Nat) (h : n = a.size) : (Array.ofFn (n := n) fun i => a[i.val]!) = a := by subst h apply Array.ext (by simp) intro i h1 h2 rw [Array.getElem_ofFn, getElem!_pos a i h2] theorem ofFn_const (n : Nat) (c : UInt32) : (Array.ofFn (n := n) fun _ => c) = Array.replicate n c := by apply Array.ext (by simp) intro i h1 h2 simp /-! ## The kernels -/ /-- `dot`: `dotFn` over the two arrays; FloatLib's dot product for unequal sizes. -/ def dotX (a b : Array UInt32) : UInt32 := if a.size = b.size then dotFn a.size (fun i => a[i]!) (fun i => b[i]!) else dotSpec a b /-- `sum`: a dot product with ones. -/ def sumX (x : Array UInt32) : UInt32 := dotFn x.size (fun i => x[i]!) (fun _ => oneW) theorem dotX_eq_spec (a b : Array UInt32) : dotX a b = dotSpec a b := by unfold dotX split · rename_i h rw [dotFn_eq, ofFn_getElem! a _ rfl, ofFn_getElem! b _ h] · rfl theorem sumX_eq_spec (x : Array UInt32) : sumX x = sumSpec x := by rw [sumX, dotFn_eq, ofFn_getElem! x _ rfl, ofFn_const, sumSpec] /-- `dotFn` is the spec's dot product, so every `…With` definition run with `dotFn` is its spec. -/ theorem dotFn_eq_dotOf : dotFn = dotOf := funext fun n => funext fun fa => funext fun fb => dotFn_eq n fa fb /-! ## Max pooling on words -/ theorem toWord_ofW (w : Word) : toWord (ofW w) = w := by apply UInt32.toNat_inj.mp simp only [toWord, ofW, Model.toNatBits, ofUInt32_bits, UInt32.toNat_ofNat'] exact Nat.mod_eq_of_lt w.toBitVec.isLt theorem ofW_toWord (z : Model Fmt) : ofW (toWord z) = z := by have h : z.toNatBits < 2 ^ 32 := Model.toNatBits_lt_two_pow z simp only [ofW, toWord, Model.ofUInt32, UInt32.toNat_ofNat', Nat.mod_eq_of_lt h] exact Model.ofNatBits_toNatBits z /-- A finite word's exact value times `2^149`: `±mant · 2^idx`. -/ @[inline] def keyOf (w : Word) : Int := if signOf w then -((mantOf w <<< idxOf w : Nat) : Int) else ((mantOf w <<< idxOf w : Nat) : Int) theorem keyOf_eq (w : Word) : keyOf w = sv w * 2 ^ idxOf w := by unfold keyOf sv; split <;> simp [Nat.shiftLeft_eq] theorem toReal_decodeD (w : Word) : (decodeD w).toReal = (keyOf w : ℝ) * 2 ^ (-149 : Int) := by rw [← FloatLib.Numerics.Dyadic.cast_toRat, decodeD_toRat, keyOf_eq] push_cast rw [zpow_sub₀ two_ne_zero, zpow_natCast] ring /-- FloatLib's `maximum` on words: finite words compared by `keyOf`; the spec's own steps when the values are equal (signed zeros) or a NaN or infinity is involved. -/ @[inline] def maxW (a b : Word) : Word := if expOf a = 255 ∨ expOf b = 255 then toWord (Model.maximum (ofW a) (ofW b)) else match compare (keyOf a) (keyOf b) with | .lt => b | .gt => a | .eq => toWord (if Model.isZero (ofW a) && Model.isZero (ofW b) then Model.zero Fmt (Model.signBit (ofW a) && Model.signBit (ofW b)) else ofW a) theorem cmp_decodeD (a b : Word) : Model.cmpDyadic (decodeD a) (decodeD b) = compare (keyOf a) (keyOf b) := by have hs : (0 : ℝ) < 2 ^ (-149 : Int) := zpow_pos two_pos _ cases h : compare (keyOf a) (keyOf b) · rw [Model.cmpDyadic_lt_iff, toReal_decodeD, toReal_decodeD] exact mul_lt_mul_of_pos_right (by exact_mod_cast compare_lt_iff_lt.mp h) hs · rw [Model.cmpDyadic_eq_iff, toReal_decodeD, toReal_decodeD, compare_eq_iff_eq.mp h] · rw [Model.cmpDyadic_gt_iff, toReal_decodeD, toReal_decodeD] exact mul_lt_mul_of_pos_right (by exact_mod_cast compare_gt_iff_gt.mp h) hs theorem maxW_spec (a b : Word) : ofW (maxW a b) = Model.maximum (ofW a) (ofW b) := by unfold maxW split · exact ofW_toWord _ · rename_i hs have ha := toDyadic_ofUInt32 a (fun h => hs (Or.inl h)) have hb := toDyadic_ofUInt32 b (fun h => hs (Or.inr h)) have hna := Model.isNaN_eq_false_of_toDyadic?_some ha have hnb := Model.isNaN_eq_false_of_toDyadic?_some hb have hc : Model.compare (ofW a) (ofW b) = some _ := Model.compare_eq_some_cmpDyadic_of_toDyadic? _ _ ha hb rw [cmp_decodeD] at hc have hnone : Model.chooseNaN2 (ofW a) (ofW b) = none := (Model.chooseNaN2_eq_none_iff _ _).2 ⟨hna, hnb⟩ unfold Model.maximum rw [Model.withNaNSelection_of_none _ _ hnone] dsimp only rw [Model.compareNonNaN_eq_of_compare_eq_some _ _ _ _ hc] cases compare (keyOf a) (keyOf b) <;> simp only [ofW_toWord] /-- `maxpoolSpec` with the running maximum kept as a word. -/ def maxpoolW (c h w k : Nat) (inp : Array Word) : Array Word := Array.ofFn (n := c * (h / k * (w / k))) fun q => let ch := q.val / (h / k * (w / k)) let y := q.val / (w / k) % (h / k) let x := q.val % (w / k) (List.range (k * k)).foldl (fun acc t => maxW acc (poolAt h w k inp ch y x t)) (poolAt h w k inp ch y x 0) theorem foldl_maxW (p : Nat → Word) (l : List Nat) (acc : Word) : l.foldl (fun m t => Model.maximum m (ofW (p t))) (ofW acc) = ofW (l.foldl (fun a t => maxW a (p t)) acc) := by induction l generalizing acc with | nil => rfl | cons t l ih => simp only [List.foldl_cons, ← maxW_spec, ih] theorem maxpoolW_eq : maxpoolW = maxpoolSpec := by funext c h w k inp simp only [maxpoolW, maxpoolSpec, foldl_maxW, toWord_ofW] /-! ## The entry -/ /-- The entry: every dot-based kernel is the contract's definition run with `dotFn`. -/ theorem usz (t n : USize) (h : t < n) : (t + 1).toNat = t.toNat + 1 := by have h1 : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have h2 : n.toNat < USize.size := n.toNat_lt_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl rw [USize.toNat_add, USize.toNat_one] exact Nat.mod_eq_of_lt (by omega) theorem uadd (a b : USize) (h : a.toNat + b.toNat < 2 ^ 32) : (a + b).toNat = a.toNat + b.toNat := by have h1 : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl rw [USize.toNat_add]; exact Nat.mod_eq_of_lt (by omega) theorem uk (k : Nat) (h : k < 2 ^ 32) : (USize.ofNat k).toNat = k := by have h1 : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl rw [USize.toNat_ofNat']; exact Nat.mod_eq_of_lt (by omega) theorem lt_of_usz_f (a : FloatArray) (i : USize) (h : i < a.usize) : i.toNat < a.size := by have h1 := USize.lt_iff_toNat_lt.mp h have h2 : a.usize.toNat ≤ a.size := by simp only [FloatArray.usize, Nat.toUSize_eq, USize.toNat_ofNat'] exact Nat.mod_le _ _ omega theorem lt_of_usz_w (a : Array UInt32) (i : USize) (h : i < a.usize) : i.toNat < a.size := by have h1 := USize.lt_iff_toNat_lt.mp h have h2 : a.usize.toNat ≤ a.size := by simp only [Array.usize, Nat.toUSize_eq, USize.toNat_ofNat'] exact Nat.mod_le _ _ omega @[inline] def rdF (a : FloatArray) (i : USize) : Float := if h : i < a.usize then a.uget i (lt_of_usz_f a i h) else 0 @[inline] def rdW (a : Array UInt32) (i : USize) : UInt32 := if h : i < a.usize then a.uget i (lt_of_usz_w a i h) else 0 @[inline] def wrF (a : FloatArray) (i : USize) (v : Float) : FloatArray := if h : i < a.usize then a.uset i v (lt_of_usz_f a i h) else a @[inline] def wrW (a : Array UInt32) (i : USize) (v : UInt32) : Array UInt32 := if h : i < a.usize then a.uset i v (lt_of_usz_w a i h) else a /-- Signed scale for a word's sign and exponent: its value is mantissa * pw[w >>> 23]. -/ def pwEntry (i : Nat) : Float := let e := (i % 256).toUInt64 let s : UInt64 := if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then Float.ofBits s else Float.ofBits (s ||| ((e + 873) <<< (52 : UInt64))) def pw : FloatArray := (List.range 512).foldl (fun a i => a.push (pwEntry i)) (FloatArray.emptyWithCapacity 512) theorem pw_data : pw.data = (List.range 512).toArray.map pwEntry := by have key : ∀ (l : List Nat) (a : FloatArray), (l.foldl (fun a i => a.push (pwEntry i)) a).data = a.data ++ (l.toArray.map pwEntry) := by intro l induction l with | nil => intro a; simp | cons x l ih => intro a simp only [List.foldl_cons, ih] simp [FloatArray.push] simp only [pw, key] simp [FloatArray.emptyWithCapacity] theorem pw_size : pw.size = 512 := by simp [FloatArray.size, pw_data] theorem pw_idx (w : UInt32) : (w >>> (23 : UInt32)).toUSize.toNat < pw.size := by rw [pw_size] have h := w.toNat_lt_size simp only [UInt32.toNat_toUSize, UInt32.toNat_shiftRight] norm_num [Nat.shiftRight_eq_div_pow] at h ⊢ omega @[inline] def dv (w : UInt32) : Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat * pw.uget (w >>> (23 : UInt32)).toUSize (pw_idx w) /-- `v * dv w` with the power of two applied first: the same value, but arm64 keeps it scalar. -/ @[inline] def dvB (v : Float) (w : UInt32) : Float := (v * pw.uget (w >>> (23 : UInt32)).toUSize (pw_idx w)) * ((w &&& 0x7FFFFF) ||| 0x800000).toFloat /-- True for +0, -0 and finite normal words: the words the fast path decodes exactly. -/ @[inline] def good (w : UInt32) : Bool := let v := w &&& 0x7FFFFFFF v == 0 || v - (0x00800000 : UInt32) ≤ (0x7EFFFFFF : UInt32) /-- Tracks the smallest `|w| - 1` and largest `|w|`, so every word is scanned without a branch. The result is the largest magnitude, or all ones if some word is not good. -/ def scan (A : Array UInt32) (n i : USize) (hn : n.toNat ≤ A.size) (lo hi : UInt32) : UInt32 := if h : i < n then have ht := USize.lt_iff_toNat_lt.mp h let v := A.uget i (by omega) &&& 0x7FFFFFFF let u := v - 1 scan A n (i + 1) hn (if u < lo then u else lo) (if hi < v then v else hi) else if 0x007FFFFF ≤ lo ∧ hi < 0x7F800000 then hi else 0xFFFFFFFF termination_by n.toNat - i.toNat decreasing_by rw [usz i n h]; omega theorem usize_le (a : Array UInt32) : a.usize.toNat ≤ a.size := by simp only [Array.usize, Nat.toUSize_eq, USize.toNat_ofNat'] exact Nat.mod_le _ _ def pushZ (n k : USize) (a : FloatArray) : FloatArray := if h : k < n then pushZ n (k + 1) (a.push 0) else a termination_by n.toNat - k.toNat decreasing_by rw [usz k n h]; have := USize.lt_iff_toNat_lt.mp h; omega /-- Round both ends of an enclosure on the raw bits. All ones means undecided. -/ @[inline] def ziv (s e : Float) : UInt32 := let x : UInt64 := ((s + e).toBits + (0x0FFFFFFF : UInt64)) >>> (29 : UInt64) let y : UInt64 := ((s - e).toBits + (0x0FFFFFFF : UInt64)) >>> (29 : UInt64) let ex : UInt64 := (x >>> (23 : UInt64)) &&& (0x7FF : UInt64) if x == y && ex - (897 : UInt64) < (254 : UInt64) then (((x >>> (34 : UInt64)) <<< (31 : UInt64)) ||| ((x &&& (0x3FFFFFFFF : UInt64)) - ((896 : UInt64) <<< (23 : UInt64)))).toUInt32 else 0xFFFFFFFF /-- One output's exact value, used when the filter cannot decide. -/ @[noinline] def slowOut (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) : Word := act relu (dotFn (inW + 1) (fun t => if t < inW then X[i * inW + t]! else oneW) (fun t => if t < inW then W[j * inW + t]! else b[j]!)) /-- `t` is the running sum of |products|. -/ @[inline] def fin (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) (s nf t : Float) (bw : UInt32) : UInt32 := let bv := dv bw let r := ziv (s + bv) (nf * (t + bv.abs)) if r == 0xFFFFFFFF then slowOut inW relu X W b i j else act relu r structure F1 where (s t : Float) /-- One output in full with the tight bound. -/ def tightGo (X W : Array UInt32) (xo wo n t : USize) (s a : Float) : F1 := if h : t < n then let q := dv (rdW X (xo + t)) * dv (rdW W (wo + t)) tightGo X W xo wo n (t + 1) (s + q) (a + q.abs) else ⟨s, a⟩ termination_by n.toNat - t.toNat decreasing_by rw [usz t n h]; have := USize.lt_iff_toNat_lt.mp h; omega /-- Eight-row output: a coarse bound first, the tight one only if that cannot decide. -/ @[inline] def fin8 (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) (s nf a : Float) (hx bw : UInt32) (n : USize) : UInt32 := let bv := dv bw let r := ziv (s + bv) (nf * (dv hx * a + bv.abs)) if r == 0xFFFFFFFF then let q := tightGo X W (i * inW).toUSize (j * inW).toUSize n 0 0 0 fin inW relu X W b i j q.s nf q.t bw else act relu r structure R1 where (ix : Array UInt32) (buf : FloatArray) (c : USize) (hi : UInt32) @[inline] def mx (a w : UInt32) : UInt32 := let m := w &&& 0x7FFFFFFF if m ≤ a then a else m /-- Compress one row to its nonzero entries. -/ def decR (X : Array UInt32) (o n t : USize) (ix : Array UInt32) (buf : FloatArray) (c : USize) (hi : UInt32) : R1 := if h : t < n then let w := rdW X (o + t) if w &&& 0x7FFFFFFF == 0 then decR X o n (t + 1) ix buf c hi else decR X o n (t + 1) (wrW ix c t.toUInt32) (wrF buf c (dv w)) (c + 1) (mx hi w) else ⟨ix, buf, c, hi⟩ termination_by n.toNat - t.toNat decreasing_by all_goals (rw [usz t n h]; have := USize.lt_iff_toNat_lt.mp h; omega) theorem wi (w i m : USize) (W : Array UInt32) (hw : w.toNat + m.toNat ≤ W.size) (hs : W.size < 2 ^ 32) (hi : i.toNat < m.toNat) : (w + i).toNat < W.size := by rw [uadd _ _ (by omega)]; omega structure F4 where (a b c d : Float) /-- `n` nonzero entries of a row against four weight rows. -/ def go4 (x : FloatArray) (ix W : Array UInt32) (w0 w1 w2 w3 m n t : USize) (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (h1 : w1.toNat + m.toNat ≤ W.size) (h2 : w2.toNat + m.toNat ≤ W.size) (h3 : w3.toNat + m.toNat ≤ W.size) (a b c d : Float) : F4 := if h : t < n then have ht := USize.lt_iff_toNat_lt.mp h let v := x.uget t (by omega) let i := (ix.uget t (by omega)).toUSize if hm : i < m then have him := USize.lt_iff_toNat_lt.mp hm let q0 := v * dv (W.uget (w0 + i) (wi w0 i m W h0 hs him)) let q1 := v * dv (W.uget (w1 + i) (wi w1 i m W h1 hs him)) let q2 := v * dv (W.uget (w2 + i) (wi w2 i m W h2 hs him)) let q3 := v * dv (W.uget (w3 + i) (wi w3 i m W h3 hs him)) go4 x ix W w0 w1 w2 w3 m n (t + 1) hx hi hs h0 h1 h2 h3 (a + q0) (b + q1) (c + q2) (d + q3) else go4 x ix W w0 w1 w2 w3 m n (t + 1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a, b, c, d⟩ termination_by n.toNat - t.toNat decreasing_by all_goals (rw [usz t n h]; omega) def armT : Bool := System.Platform.target.startsWith "aarch64" /-- `go4B` for arm64, where the mixed products stop the compiler packing the lanes into vectors. -/ def go4B (x : FloatArray) (ix W : Array UInt32) (w0 w1 w2 w3 m n t : USize) (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (h1 : w1.toNat + m.toNat ≤ W.size) (h2 : w2.toNat + m.toNat ≤ W.size) (h3 : w3.toNat + m.toNat ≤ W.size) (a b c d : Float) : F4 := if h : t < n then have ht := USize.lt_iff_toNat_lt.mp h let v := x.uget t (by omega) let i := (ix.uget t (by omega)).toUSize if hm : i < m then have him := USize.lt_iff_toNat_lt.mp hm let q0 := v * dv (W.uget (w0 + i) (wi w0 i m W h0 hs him)) let q1 := dvB v (W.uget (w1 + i) (wi w1 i m W h1 hs him)) let q2 := v * dv (W.uget (w2 + i) (wi w2 i m W h2 hs him)) let q3 := dvB v (W.uget (w3 + i) (wi w3 i m W h3 hs him)) go4B x ix W w0 w1 w2 w3 m n (t + 1) hx hi hs h0 h1 h2 h3 (a + q0) (b + q1) (c + q2) (d + q3) else go4B x ix W w0 w1 w2 w3 m n (t + 1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a, b, c, d⟩ termination_by n.toNat - t.toNat decreasing_by all_goals (rw [usz t n h]; omega) def go1 (x : FloatArray) (ix W : Array UInt32) (w0 m n t : USize) (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (a : Float) : Float := if h : t < n then have ht := USize.lt_iff_toNat_lt.mp h let i := (ix.uget t (by omega)).toUSize if hm : i < m then have him := USize.lt_iff_toNat_lt.mp hm let q := x.uget t (by omega) * dv (W.uget (w0 + i) (wi w0 i m W h0 hs him)) go1 x ix W w0 m n (t + 1) hx hi hs h0 (a + q) else go1 x ix W w0 m n (t + 1) hx hi hs h0 a else a termination_by n.toNat - t.toNat decreasing_by all_goals (rw [usz t n h]; omega) def cols1 (inW outW : Nat) (relu : Bool) (X W b : Array UInt32) (i : Nat) (ix : Array UInt32) (x : FloatArray) (nf a : Float) (hx : UInt32) (m n : USize) (j : Nat) (base : USize) (out : Array UInt32) : Array UInt32 := if h4 : j + 4 ≤ outW then let ju := j.toUSize let w0 := ju * m let w1 := (ju + 1) * m let w2 := (ju + 2) * m let w3 := (ju + 3) * m if hg : n.toNat ≤ x.size ∧ n.toNat ≤ ix.size ∧ W.size < 2 ^ 32 ∧ w0.toNat + m.toNat ≤ W.size ∧ w1.toNat + m.toNat ≤ W.size ∧ w2.toNat + m.toNat ≤ W.size ∧ w3.toNat + m.toNat ≤ W.size then let r := if armT then go4B x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 else go4 x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 let o0 := fin8 inW relu X W b i j r.a nf a hx (rdW b ju) m let o1 := fin8 inW relu X W b i (j + 1) r.b nf a hx (rdW b (ju + 1)) m let o2 := fin8 inW relu X W b i (j + 2) r.c nf a hx (rdW b (ju + 2)) m let o3 := fin8 inW relu X W b i (j + 3) r.d nf a hx (rdW b (ju + 3)) m let out := wrW out (base + ju) o0 let out := wrW out (base + ju + 1) o1 let out := wrW out (base + ju + 2) o2 let out := wrW out (base + ju + 3) o3 cols1 inW outW relu X W b i ix x nf a hx m n (j + 4) base out else out else if h1 : j < outW then let ju := j.toUSize let w0 := ju * m if hg : n.toNat ≤ x.size ∧ n.toNat ≤ ix.size ∧ W.size < 2 ^ 32 ∧ w0.toNat + m.toNat ≤ W.size then let r := go1 x ix W w0 m n 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2 0 let o := fin8 inW relu X W b i j r nf a hx (rdW b ju) m cols1 inW outW relu X W b i ix x nf a hx m n (j + 1) base (wrW out (base + ju) o) else out else out termination_by outW - j def rows1 (rows inW outW : Nat) (relu : Bool) (X W b : Array UInt32) (nf a : Float) (m : USize) (i : Nat) (ix : Array UInt32) (buf : FloatArray) (out : Array UInt32) : Array UInt32 := if h : i < rows then let r := decR X (i * inW).toUSize m 0 ix buf 0 0 let out := cols1 inW outW relu X W b i r.ix r.buf nf a r.hi m r.c 0 (i * outW).toUSize out rows1 rows inW outW relu X W b nf a m (i + 1) r.ix r.buf out else out termination_by rows - i structure F8 where (s0 s1 s2 s3 s4 s5 s6 s7 t : Float) theorem xi (xs : FloatArray) (i t n : USize) (k : Nat) (hk : k < 8) (ht : t.toNat < n.toNat) (hi : i.toNat = 8 * t.toNat) (hx : 8 * n.toNat ≤ xs.size) (hs : xs.size < 2 ^ 32) : (i + USize.ofNat k).toNat < xs.size := by rw [uadd _ _ (by rw [uk k (by omega)]; omega), uk k (by omega)]; omega /-- Eight interleaved rows: each weight is decoded once and multiplied into eight sums. -/ def go8 (xs : FloatArray) (W : Array UInt32) (wo n t i : USize) (hx : 8 * n.toNat ≤ xs.size) (hsx : xs.size < 2 ^ 32) (hs : W.size < 2 ^ 32) (hw : wo.toNat + n.toNat ≤ W.size) (hi : i.toNat = 8 * t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a : Float) : F8 := if h : t < n then have ht := USize.lt_iff_toNat_lt.mp h let v := dv (W.uget (wo + t) (wi wo t n W hw hs ht)) go8 xs W wo n (t + 1) (i + USize.ofNat 8) hx hsx hs hw (by rw [usz t n h, uadd _ _ (by rw [uk 8 (by omega)]; omega), uk 8 (by omega)]; omega) (s0 + v * xs.uget (i + USize.ofNat 0) (xi xs i t n 0 (by omega) ht hi hx hsx)) (s1 + v * xs.uget (i + USize.ofNat 1) (xi xs i t n 1 (by omega) ht hi hx hsx)) (s2 + v * xs.uget (i + USize.ofNat 2) (xi xs i t n 2 (by omega) ht hi hx hsx)) (s3 + v * xs.uget (i + USize.ofNat 3) (xi xs i t n 3 (by omega) ht hi hx hsx)) (s4 + v * xs.uget (i + USize.ofNat 4) (xi xs i t n 4 (by omega) ht hi hx hsx)) (s5 + v * xs.uget (i + USize.ofNat 5) (xi xs i t n 5 (by omega) ht hi hx hsx)) (s6 + v * xs.uget (i + USize.ofNat 6) (xi xs i t n 6 (by omega) ht hi hx hsx)) (s7 + v * xs.uget (i + USize.ofNat 7) (xi xs i t n 7 (by omega) ht hi hx hsx)) (a + v.abs) else ⟨s0, s1, s2, s3, s4, s5, s6, s7, a⟩ termination_by n.toNat - t.toNat decreasing_by rw [usz t n h]; omega structure H8 where (xs : FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7 : UInt32) /-- Interleave eight rows by column. -/ def dec8 (X : Array UInt32) (n t : USize) (xs : FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7 : UInt32) : H8 := if h : t < n then let w0 := rdW X t let w1 := rdW X (n + t) let w2 := rdW X (2 * n + t) let w3 := rdW X (3 * n + t) let w4 := rdW X (4 * n + t) let w5 := rdW X (5 * n + t) let w6 := rdW X (6 * n + t) let w7 := rdW X (7 * n + t) dec8 X n (t + 1) ((((((((xs.push (dv w0)).push (dv w1)).push (dv w2)).push (dv w3)).push (dv w4)).push (dv w5)).push (dv w6)).push (dv w7)) (mx h0 w0) (mx h1 w1) (mx h2 w2) (mx h3 w3) (mx h4 w4) (mx h5 w5) (mx h6 w6) (mx h7 w7) else ⟨xs, h0, h1, h2, h3, h4, h5, h6, h7⟩ termination_by n.toNat - t.toNat decreasing_by rw [usz t n h]; have := USize.lt_iff_toNat_lt.mp h; omega def cols8 (inW outW : Nat) (relu : Bool) (X W b : Array UInt32) (d : H8) (nf : Float) (n : USize) (j : Nat) (out : Array UInt32) : Array UInt32 := if hj : j < outW then let ju := j.toUSize let wo := ju * n if hg : 8 * n.toNat ≤ d.xs.size ∧ d.xs.size < 2 ^ 32 ∧ W.size < 2 ^ 32 ∧ wo.toNat + n.toNat ≤ W.size then let r := go8 d.xs W wo n 0 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2 (by rw [show (0 : USize).toNat = 0 from rfl]) 0 0 0 0 0 0 0 0 0 let bw := rdW b ju let ow := outW.toUSize let out := wrW out ju (fin8 inW relu X W b 0 j r.s0 nf r.t d.h0 bw n) let out := wrW out (ow + ju) (fin8 inW relu X W b 1 j r.s1 nf r.t d.h1 bw n) let out := wrW out (2 * ow + ju) (fin8 inW relu X W b 2 j r.s2 nf r.t d.h2 bw n) let out := wrW out (3 * ow + ju) (fin8 inW relu X W b 3 j r.s3 nf r.t d.h3 bw n) let out := wrW out (4 * ow + ju) (fin8 inW relu X W b 4 j r.s4 nf r.t d.h4 bw n) let out := wrW out (5 * ow + ju) (fin8 inW relu X W b 5 j r.s5 nf r.t d.h5 bw n) let out := wrW out (6 * ow + ju) (fin8 inW relu X W b 6 j r.s6 nf r.t d.h6 bw n) let out := wrW out (7 * ow + ju) (fin8 inW relu X W b 7 j r.s7 nf r.t d.h7 bw n) cols8 inW outW relu X W b d nf n (j + 1) out else out else out termination_by outW - j def linF (rows inW outW : Nat) (relu : Bool) (X W b : Array UInt32) : Array UInt32 := let hw := scan W W.usize 0 (usize_le W) 0xFFFFFFFF 0 if rows * inW = X.size ∧ outW * inW = W.size ∧ outW = b.size ∧ 0 < inW ∧ 0 < outW ∧ inW < 2 ^ 20 ∧ X.size < 2 ^ 32 ∧ W.size < 2 ^ 32 ∧ b.size < 2 ^ 32 ∧ rows * outW < 2 ^ 32 ∧ scan X X.usize 0 (usize_le X) 0xFFFFFFFF 0 ≠ 0xFFFFFFFF ∧ hw ≠ 0xFFFFFFFF ∧ scan b b.usize 0 (usize_le b) 0xFFFFFFFF 0 ≠ 0xFFFFFFFF then let n := inW.toUSize let nf : Float := (inW.toUInt64 + 8).toFloat * Float.ofBits 0x3CC0000000000000 let out : Array UInt32 := Array.replicate (rows * outW) 0 if rows == 8 then cols8 inW outW relu X W b (dec8 X n 0 (FloatArray.emptyWithCapacity (8 * inW)) 0 0 0 0 0 0 0 0) nf n 0 out else rows1 rows inW outW relu X W b nf ((inW.toUInt64 + 1).toFloat * dv hw) n 0 (Array.replicate inW 0) (pushZ n 0 (FloatArray.emptyWithCapacity inW)) out else linearWith dotFn rows inW outW relu X W b /-! # Part A: the fast enclosure holds the exact dot product `encl_tight` and `A_encl_coarse` say the exact value lies strictly between the two ends of the filter. Each hardware operation is exact real arithmetic followed by one nearest-even rounding (`A_add`, `A_sub`, `A_mul`), so each loses at most `2^-53` relatively plus a subnormal floor (`A_err`). A good word decodes exactly (`A_dv`). `A_fsum_spec` sums the running error of a fold, `A_real` shows the margin `nfOf n * t` beats it, and `hbig` rules out the all-zero case. `encl_coarse` as stated in `Iface.lean` is false for `hh = 0x80000000`, so `A_encl_coarse` adds the hypothesis `hh ≤ 0x7FFFFFFF`, which `mx` and `dec8` keep. `encl_coarse1` is the same bound for a kept-position list, with the constant `(n + 1) * dv hv` in place of the running sum. `encl_tightZ` and `encl_coarseZ` also allow positions left out because the weight is zero. -/ section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary /-- Real value of a finite hardware double. -/ noncomputable def R (f : Float) : ℝ := Model.toReal (toModel (ofFloat f)) /-- Real value of a binary32 word. -/ noncomputable def wv (w : UInt32) : ℝ := Model.toReal (ofW w) /-- The exact value being rounded: the row dotted with the weights, plus the bias. -/ noncomputable def exactS (n : Nat) (xw ww : Nat → UInt32) (bw : UInt32) : ℝ := (Finset.range n).sum (fun t => wv (xw t) * wv (ww t)) + wv bw def nfOf (n : Nat) : Float := (n.toUInt64 + 8).toFloat * Float.ofBits 0x3CC0000000000000 /-! ## Bridges from hardware doubles to real numbers -/ theorem A_hf : FloatFormat.binary64.isIEEE = true := by decide theorem A_fin_model (a : Model FloatFormat.binary64) : Model.isFinite a = (Model.toModel a).isFinite := by have hd := Model.toDyadic?_ieee_eq_model _ A_hf a cases hm : Model.toModel a with | notANumber => rw [hm] at hd cases h : Model.isFinite a with | false => rfl | true => obtain ⟨d, hd'⟩ := Model.exists_toDyadic?_of_isFinite h rw [hd] at hd'; cases hd' | infinity s => rw [hm] at hd cases h : Model.isFinite a with | false => rfl | true => obtain ⟨d, hd'⟩ := Model.exists_toDyadic?_of_isFinite h rw [hd] at hd'; cases hd' | zero s => rw [hm] at hd exact Model.isFinite_eq_true_of_toDyadic?_some hd | finite s m e h => rw [hm] at hd exact Model.isFinite_eq_true_of_toDyadic?_some hd /-- The FloatLib word of a hardware double. -/ abbrev A_M (x : Float) : Model FloatFormat.binary64 := Model.ofFloatModel x.toModel theorem A_R (x : Float) : R x = Model.toReal (A_M x) := rfl theorem A_fin_iff (x : Float) : Model.isFinite (A_M x) = x.isFinite := by rw [A_fin_model, Model.toModel_ofFloatModel] rfl theorem A_bpow (e : ℤ) : Formats.Flocq.bpow FloatLib.Numerics.binaryRadix e = (2 : ℝ) ^ e := by simp [Formats.Flocq.bpow, FloatLib.Numerics.binaryRadix, FloatLib.Numerics.Radix.toReal] theorem A_maxF : (2 : ℝ) ^ (1000 : ℤ) ≤ Model.toReal (Model.posMaxFinite FloatFormat.binary64) := by rw [Model.toReal_posMaxFinite] have h1 : Model.pow2 FloatFormat.binary64.fracWidth + FloatFormat.binary64.maxFiniteFracField = 2 ^ 53 - 1 := by decide +kernel have h2 : FloatFormat.binary64.maxNormalExponent - Int.ofNat FloatFormat.binary64.fracWidth = 971 := by decide +kernel rw [h1, h2] have h3 : Model.bpow 971 = (2 : ℝ) ^ (971 : ℤ) := by simp only [Model.bpow, A_bpow] rw [h3, show (1000 : ℤ) = 29 + 971 from rfl, zpow_add₀ (by norm_num)] apply mul_le_mul_of_nonneg_right _ (by positivity) norm_num theorem A_add (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (hb : |R x| + |R y| ≤ 2 ^ (1000 : ℤ)) : (x + y).isFinite = true ∧ R (x + y) = Model.roundAt FloatFormat.binary64 (R x + R y) := by have hm : A_M (x + y) = Model.add (A_M x) (A_M y) := toModel_ofFloat_add_of_isFinite x y hx hy have hfx : Model.isFinite (A_M x) = true := by rw [A_fin_iff, hx] have hfy : Model.isFinite (A_M y) = true := by rw [A_fin_iff, hy] constructor · rw [← A_fin_iff, hm] exact Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) · rw [A_R, hm] exact Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) theorem A_sub (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (hb : |R x| + |R y| ≤ 2 ^ (1000 : ℤ)) : (x - y).isFinite = true ∧ R (x - y) = Model.roundAt FloatFormat.binary64 (R x - R y) := by have hm : A_M (x - y) = Model.sub (A_M x) (A_M y) := toModel_ofFloat_sub_of_isFinite x y hx hy have hfx : Model.isFinite (A_M x) = true := by rw [A_fin_iff, hx] have hfy : Model.isFinite (A_M y) = true := by rw [A_fin_iff, hy] constructor · rw [← A_fin_iff, hm] exact Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) · rw [A_R, hm] exact Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) open Float.Model.UnpackedFloat in theorem A_canon (b : BitVec 64) (s m e h) (hu : Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 b = .finite s m e h) : (2 ^ 52 ≤ m ∧ -1074 ≤ e) ∨ e = -1074 := by unfold Float.Model.UnpackedFloat.unpack at hu simp only at hu have hb : (Float.Model.Format.binary64.exponentBias : ℤ) = 1023 := by decide rw [hb] at hu split_ifs at hu with h1 h2 h3 h4 · injection hu with _ _ he right have : (unpackExponent (spec := Float.Model.Format.binary64) b).toNat = 0 := by simp [h3] rw [this] at he omega · injection hu with _ hm he left have : (unpackExponent (spec := Float.Model.Format.binary64) b).toNat ≠ 0 := by intro h0; exact h3 (BitVec.eq_of_toNat_eq (by simpa using h0)) constructor · rw [← hm, BitVec.toNat_append] refine le_trans ?_ Nat.left_le_or simp [Nat.shiftLeft_eq] · omega open Float.Model.UnpackedFloat in theorem A_round_eq (spec : Float.Model.Format) (s : Sign) (m : ℕ) (e : ℤ) (h : e ≤ spec.targetExponent (Float.Model.totalExponent m e)) : Float.Model.UnpackedFloat.round spec s m e = roundWithAccuracy spec s m e .exact := by unfold Float.Model.UnpackedFloat.round decreaseExponent have h0 : (e - spec.targetExponent (Float.Model.totalExponent m e)).toNat = 0 := by omega simp [h0] open Float.Model.UnpackedFloat in theorem A_hle (s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂) (b₁ b₂ : BitVec 64) (hu₁ : unpack Float.Model.Format.binary64 b₁ = .finite s₁ m₁ e₁ h₁) (hu₂ : unpack Float.Model.Format.binary64 b₂ = .finite s₂ m₂ e₂ h₂) : e₁ + e₂ ≤ Float.Model.Format.binary64.targetExponent (Float.Model.totalExponent (m₁ * m₂) (e₁ + e₂)) := by have c₁ := A_canon _ _ _ _ _ hu₁ have c₂ := A_canon _ _ _ _ _ hu₂ have hm0 : m₁ * m₂ ≠ 0 := Nat.mul_ne_zero h₁.ne' h₂.ne' have hlog : 2 ^ 52 ≤ m₁ * m₂ → 52 ≤ (m₁ * m₂).log2 := fun h => (Nat.le_log2 hm0).2 h simp only [Float.Model.Format.targetExponent, Float.Model.totalExponent, Float.Model.Format.mantissaBits, Float.Model.Format.minExponent] rcases c₁ with ⟨c₁, _⟩ | c₁ · have := hlog (le_trans c₁ (Nat.le_mul_of_pos_right _ h₂)) apply le_max_of_le_left norm_num omega · rcases c₂ with ⟨c₂, _⟩ | c₂ · have := hlog (le_trans c₂ (Nat.le_mul_of_pos_left _ h₁)) apply le_max_of_le_left norm_num omega · apply le_max_of_le_right norm_num omega open Float.Model.UnpackedFloat in theorem A_mul_model (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) : A_M (x * y) = Model.mul (A_M x) (A_M y) := by have hX : Model.toModel (A_M x) = x.toModel.unpack := Model.toModel_ofFloatModel _ have hY : Model.toModel (A_M y) = y.toModel.unpack := Model.toModel_ofFloatModel _ change Model.ofFloatModel (Float.Model.mul x.toModel y.toModel) = _ rw [Float.Model.mul, Model.ofFloatModel_pack, Model.Proof.mul_eq_spec, Model.Spec.mul, Model.toDyadic?_ieee_eq_model _ A_hf (A_M x), Model.toDyadic?_ieee_eq_model _ A_hf (A_M y), hX, hY] change x.toModel.unpack.isFinite = true at hx change y.toModel.unpack.isFinite = true at hy cases hxm : x.toModel.unpack <;> cases hym : y.toModel.unpack <;> simp only [hxm, hym, Float.Model.UnpackedFloat.isFinite, Bool.false_eq_true] at hx hy case zero.zero s₁ s₂ => cases s₁ <;> cases s₂ <;> simp [Model.unpackedToDyadic?, Float.Model.UnpackedFloat.mul] all_goals first | (rw [Model.negZero_eq_ofModel_zero]; rfl) | (rw [Model.posZero_eq_ofModel_zero]; rfl) case zero.finite s₁ s₂ m₂ e₂ h₂ => cases s₁ <;> cases s₂ <;> simp [Model.unpackedToDyadic?, Float.Model.UnpackedFloat.mul] all_goals first | (rw [Model.negZero_eq_ofModel_zero]; rfl) | (rw [Model.posZero_eq_ofModel_zero]; rfl) case finite.zero s₁ m₁ e₁ h₁ s₂ => cases s₁ <;> cases s₂ <;> simp [Model.unpackedToDyadic?, Float.Model.UnpackedFloat.mul] all_goals first | (rw [Model.negZero_eq_ofModel_zero]; rfl) | (rw [Model.posZero_eq_ofModel_zero]; rfl) case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => simp only [Model.unpackedToDyadic?, Float.Model.UnpackedFloat.mul] have hle := A_hle s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂ _ _ hxm hym have h0 : (m₁ == 0 || m₂ == 0) = false := by simp [h₁.ne', h₂.ne'] rw [← A_round_eq _ _ _ _ hle, h0] have hs : Model.modelSign (Model.modelSignBit s₁ ^^ Model.modelSignBit s₂) = s₁ * s₂ := by cases s₁ <;> cases s₂ <;> rfl simp only [Bool.false_eq_true, ite_false, Model.roundDyadic, A_hf, ite_true, Model.ieeeRoundDyadic, hs] rfl theorem A_mul (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (hb : |R x| * |R y| ≤ 2 ^ (1000 : ℤ)) : (x * y).isFinite = true ∧ R (x * y) = Model.roundAt FloatFormat.binary64 (R x * R y) := by have hm : A_M (x * y) = Model.mul (A_M x) (A_M y) := A_mul_model x y hx hy have hfx : Model.isFinite (A_M x) = true := by rw [A_fin_iff, hx] have hfy : Model.isFinite (A_M y) = true := by rw [A_fin_iff, hy] constructor · rw [← A_fin_iff, hm] exact Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) · rw [A_R, hm] exact Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ A_hf hfx hfy (hb.trans A_maxF) /-! ## Rounding error -/ /-- The floor of the subnormal grid, scaled so `A_z / 2^53 = 2^-1074`. -/ noncomputable def A_z : ℝ := 2 ^ (-1021 : ℤ) /-- A size far above every intermediate of the kernels. -/ noncomputable def A_B : ℝ := 2 ^ (300 : ℤ) theorem A_z_pos : 0 < A_z := by unfold A_z; positivity theorem A_z_le : A_z ≤ 1 / 2 ^ 200 * (1 / 2 ^ 200) := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) = 2 ^ (-400 : ℤ) := by rw [show (-400 : ℤ) = -200 + -200 from rfl, zpow_add₀ (by norm_num)] norm_num rw [this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) theorem A_B_pos : 0 < A_B := by unfold A_B; positivity theorem A_B_lo : (2 : ℝ) ^ 256 * 2 ^ 42 ≤ A_B := by have : ((2 : ℝ) ^ 256 * 2 ^ 42) = 2 ^ (298 : ℤ) := by rw [show (298 : ℤ) = 256 + 42 from rfl, zpow_add₀ (by norm_num)] norm_num rw [this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) theorem A_B_hi : A_B * 2 ≤ 2 ^ (1000 : ℤ) := by have : A_B * 2 = 2 ^ (301 : ℤ) := by unfold A_B rw [show (301 : ℤ) = 300 + 1 from rfl, zpow_add₀ (by norm_num)] norm_num rw [this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) theorem A_B_sq : A_B * A_B ≤ 2 ^ (1000 : ℤ) := by have : A_B * A_B = 2 ^ (600 : ℤ) := by unfold A_B rw [show (600 : ℤ) = 300 + 300 from rfl, zpow_add₀ (by norm_num)] rw [this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) /-- Half-ULP bound for binary64, with the subnormal term folded in. -/ theorem A_err (x : ℝ) : |Model.roundAt FloatFormat.binary64 x - x| ≤ 1 / 2 ^ 53 * (|x| + A_z) := by have ez : (1 / 2 ^ 53 * (|x| + A_z) : ℝ) = 2 ^ (-53 : ℤ) * |x| + 2 ^ (-1074 : ℤ) := by have e1 : (1 / 2 ^ 53 : ℝ) = 2 ^ (-53 : ℤ) := by norm_num rw [e1, show (-1074 : ℤ) = -53 + -1021 from rfl, zpow_add₀ (by norm_num) (-53), mul_add] rfl rw [ez] have hu : (0:ℝ) ≤ (2 : ℝ) ^ (-53 : ℤ) * |x| := by positivity have ht : (0:ℝ) < (2 : ℝ) ^ (-1074 : ℤ) := by positivity by_cases hx : x = 0 · subst hx rw [Model.roundAt_zero] simp only [sub_self, abs_zero] linarith refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf FloatFormat.binary64) x / 2 ≤ _ rw [Formats.Flocq.ulp.of_ne_zero _ _ x hx] simp only [Formats.Flocq.cexp, Model.fexpOf, Formats.Flocq.fltExp] have hl := Formats.Flocq.bpow_magnitude_sub_one_le FloatLib.Numerics.binaryRadix x hx rw [A_bpow] at hl rw [A_bpow, show ((FloatFormat.binary64.fracWidth : ℤ) + 1) = 53 from rfl, show FloatFormat.binary64.minSubnormalExponent = -1074 from rfl] generalize Formats.Flocq.magnitude FloatLib.Numerics.binaryRadix x = M at hl ⊢ rcases le_total (-1074) (M - 53) with h | h · rw [max_eq_left h] have e : (2 : ℝ) ^ (M - 53) / 2 = 2 ^ (-53 : ℤ) * 2 ^ (M - 1) := by rw [← zpow_add₀ (by norm_num), show (-53 : ℤ) + (M - 1) = M - 53 - 1 by ring, zpow_sub₀ (by norm_num) _ 1, zpow_one] rw [e] have := mul_le_mul_of_nonneg_left hl (show (0:ℝ) ≤ 2 ^ (-53 : ℤ) by positivity) linarith · rw [max_eq_right h] generalize (2 : ℝ) ^ (-1074 : ℤ) = t at ht ⊢ generalize (2 : ℝ) ^ (-53 : ℤ) * |x| = v at hu ⊢ linarith /-- One addition: finite, with relative error `2^-53`. -/ theorem A_addE (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x + y).isFinite = true ∧ |R (x + y) - (R x + R y)| ≤ 1 / 2 ^ 53 * (|R x + R y| + A_z) := by obtain ⟨hf, he⟩ := A_add x y hx hy (by have := A_B_hi; linarith) exact ⟨hf, he ▸ A_err _⟩ theorem A_subE (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x - y).isFinite = true ∧ |R (x - y) - (R x - R y)| ≤ 1 / 2 ^ 53 * (|R x - R y| + A_z) := by obtain ⟨hf, he⟩ := A_sub x y hx hy (by have := A_B_hi; linarith) exact ⟨hf, he ▸ A_err _⟩ theorem A_mulE (x y : Float) (hx : x.isFinite = true) (hy : y.isFinite = true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x * y).isFinite = true ∧ |R (x * y) - R x * R y| ≤ 1 / 2 ^ 53 * (|R x * R y| + A_z) := by have hb : |R x| * |R y| ≤ 2 ^ (1000 : ℤ) := (mul_le_mul h1 h2 (abs_nonneg _) A_B_pos.le).trans A_B_sq obtain ⟨hf, he⟩ := A_mul x y hx hy hb exact ⟨hf, he ▸ A_err _⟩ theorem A_round_exact (m k : ℤ) (hm : m.natAbs < 2 ^ 53) (hk : -1074 ≤ k) : Model.roundAt FloatFormat.binary64 ((m : ℝ) * 2 ^ k) = (m : ℝ) * 2 ^ k := by apply Formats.Flocq.round_preserves_generic apply (Formats.Flocq.generic_format_FLT_iff (FloatFormat.binary64.minSubnormalExponent) (FloatFormat.binary64.fracWidth + 1) (by decide) _).2 refine ⟨by decide, ⟨m, k⟩, ?_, ?_, ?_⟩ · simp [Formats.Flocq.toReal, A_bpow] · exact hm · exact hk theorem A_R_unpack (x : Float) : R x = Model.unpackedToReal x.toModel.unpack := by rw [A_R, Model.toReal_eq_unpackedToReal_toModel A_hf, Model.toModel_ofFloatModel] /-- Unpacking the packed positive copy of a finite value keeps it. -/ theorem A_abs_aux (f : Float) (s m e h) (hu : f.toModel.unpack = .finite s m e h) : (Model.ofModel FloatFormat.binary64 (.finite .positive m e h)).toModel = .finite .positive m e h := by have hm : Model.toModel (A_M f) = .finite s m e h := by rw [Model.toModel_ofFloatModel, hu] cases s with | positive => rw [← hm] exact Model.toModel_canonicalizeModel _ | negative => have hf : (Model.toModel (A_M f)).isFinite = true := by rw [hm]; rfl have hn := Model.toModel_neg_of_isFinite A_hf (A_M f) hf rw [hm] at hn have h2 : Float.Model.UnpackedFloat.neg (.finite .negative m e h) = .finite .positive m e h := rfl rw [h2] at hn rw [← hn] exact Model.toModel_canonicalizeModel _ theorem A_abs (f : Float) (hf : f.isFinite = true) : f.abs.isFinite = true ∧ R f.abs = |R f| := by have hA : A_M f.abs = Model.ofModel FloatFormat.binary64 f.toModel.unpack.abs := Model.ofFloatModel_pack _ change f.toModel.unpack.isFinite = true at hf cases hu : f.toModel.unpack with | notANumber => simp [hu, Float.Model.UnpackedFloat.isFinite] at hf | infinity s => simp [hu, Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw [hu] at hA constructor · rw [← A_fin_iff, hA] exact Model.isFinite_ofModel_zero _ A_hf _ · rw [A_R f.abs, hA, A_R_unpack f, hu] simp [Float.Model.UnpackedFloat.abs, Model.toReal_ofModel_zero _ A_hf] | finite s m e h => rw [hu] at hA have hk := A_abs_aux f s m e h hu change Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h)) = _ at hk constructor · rw [← A_fin_iff, hA, A_fin_model] change (Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))).isFinite = _ rw [hk]; rfl · rw [A_R f.abs, hA, A_R_unpack f, hu] change Model.toReal (Model.ofModel FloatFormat.binary64 (.finite .positive m e h)) = _ rw [Model.toReal_eq_unpackedToReal_toModel A_hf, hk] simp only [Model.unpackedToReal_finite, A_bpow] have h0 : (0:ℝ) ≤ (m : ℝ) * 2 ^ e := by positivity cases s <;> simp [Model.modelSignBit, abs_of_nonneg h0] /-! ## Exact conversions -/ /-- A hardware double built from a natural below `2^53` has that value. -/ theorem A_ofNat (f : Float) (k : ℕ) (hk : k < 2 ^ 53) (hf : f.toModel = Float.Model.pack (Float.Model.UnpackedFloat.ofNat Float.Model.Format.binary64 k)) : f.isFinite = true ∧ R f = k := by have hA : A_M f = Model.roundDyadic FloatFormat.binary64 (FloatLib.Numerics.Dyadic.ofScaledInt k 0) := by change Model.ofFloatModel f.toModel = _ rw [hf, Model.ofFloatModel_pack] exact Model.ofModel_ofInt_eq_roundDyadic _ A_hf (k : ℤ) have hd : (FloatLib.Numerics.Dyadic.ofScaledInt k 0).toReal = k := by rw [← FloatLib.Numerics.Dyadic.cast_toRat, FloatLib.Numerics.Dyadic.ofScaledInt_toRat] simp have hkR : (k : ℝ) ≤ 2 ^ (1000 : ℤ) := by have h1 : (k : ℝ) ≤ 2 ^ 53 := by exact_mod_cast hk.le have h2 : (2 : ℝ) ^ 53 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num calc (k : ℝ) ≤ 2 ^ 53 := h1 _ ≤ 2 ^ 256 * 2 ^ 42 := h2 _ ≤ A_B := A_B_lo _ ≤ A_B * 2 := by linarith [A_B_pos] _ ≤ _ := A_B_hi have hfin : Model.isFinite (A_M f) = true := by rw [hA] apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ A_hf rw [hd, abs_of_nonneg (Nat.cast_nonneg _)] exact hkR.trans A_maxF refine ⟨by rw [← A_fin_iff]; exact hfin, ?_⟩ rw [A_R, hA, Model.toReal_roundDyadic_eq_roundAt _ A_hf _ (hA ▸ hfin), hd] have := A_round_exact k 0 (by simpa using hk) (by norm_num) simpa using this theorem A_toF32 (m : UInt32) (h : m.toNat < 2 ^ 53) : m.toFloat.isFinite = true ∧ R m.toFloat = m.toNat := A_ofNat _ _ h rfl theorem A_toF64 (m : UInt64) (h : m.toNat < 2 ^ 53) : m.toFloat.isFinite = true ∧ R m.toFloat = m.toNat := A_ofNat _ _ h rfl theorem A_zero_bits : (0 : Float).toModel.unpack = .zero .positive := by rfl theorem A_zero : (0 : Float).isFinite = true ∧ R 0 = 0 := by refine ⟨?_, ?_⟩ · change (0 : Float).toModel.unpack.isFinite = true rw [A_zero_bits]; rfl · rw [A_R_unpack, A_zero_bits]; simp /-- The bits of `pwEntry`. -/ def A_bits (i : ℕ) : UInt64 := let e := (i % 256).toUInt64 let s : UInt64 := if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e + 873) <<< (52 : UInt64)) theorem A_pwEntry (i : ℕ) : pwEntry i = Float.ofBits (A_bits i) := by unfold pwEntry A_bits dsimp only split <;> rfl def A_chk (i : ℕ) : Bool := match Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec with | .finite s m e _ => (Model.modelSignBit s == decide (256 ≤ i)) && m == 2 ^ 52 && e == ((i % 256 : ℕ) : ℤ) - 202 && (i % 256 != 0) | .zero _ => i % 256 == 0 | _ => false theorem A_chk_all : ∀ i, i < 512 → A_chk i = true := by decide +kernel theorem A_pw (i : ℕ) (hi : i < 512) : (pwEntry i).isFinite = true ∧ R (pwEntry i) = if i % 256 = 0 then 0 else (if 256 ≤ i then -1 else 1) * 2 ^ (((i % 256 : ℕ) : ℤ) - 150) := by have hc := A_chk_all i hi rw [A_pwEntry] have hU : (Float.ofBits (A_bits i)).toModel.unpack = Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec := Model.unpack_pack_unpack _ _ unfold A_chk at hc rw [A_R_unpack, hU] change (Float.ofBits (A_bits i)).toModel.unpack.isFinite = true ∧ _ rw [hU] cases hu : Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec with | notANumber => rw [hu] at hc; simp at hc | infinity s => rw [hu] at hc; simp at hc | zero s => rw [hu] at hc simp only [beq_iff_eq] at hc refine ⟨rfl, ?_⟩ simp [hc] | finite s m e h => rw [hu] at hc simp only [Bool.and_eq_true, beq_iff_eq, bne_iff_ne] at hc obtain ⟨⟨⟨h1, h2⟩, h3⟩, h4⟩ := hc refine ⟨rfl, ?_⟩ have e : ((2 ^ 52 : ℕ) : ℝ) * 2 ^ (((i % 256 : ℕ) : ℤ) - 202) = 2 ^ (((i % 256 : ℕ) : ℤ) - 150) := by rw [Nat.cast_pow, Nat.cast_ofNat, ← zpow_natCast, ← zpow_add₀ (by norm_num)] congr 1 push_cast ring rw [Model.unpackedToReal_finite, A_bpow, h3, h1, h2, mul_assoc, e] simp only [decide_eq_true_eq, h4, ↓reduceIte] theorem A_pw_get (i : USize) (h : i.toNat < pw.size) : pw.uget i h = pwEntry i.toNat := by simp [FloatArray.uget, pw_data] /-! ## Decoding a word -/ theorem A_and31 (w : UInt32) : (w &&& 0x7FFFFFFF).toNat = w.toNat % 2 ^ 31 := by rw [UInt32.toNat_and] exact Nat.and_two_pow_sub_one_eq_mod w.toNat 31 theorem A_and23 (w : UInt32) : (w &&& 0x7FFFFF).toNat = w.toNat % 2 ^ 23 := by rw [UInt32.toNat_and] exact Nat.and_two_pow_sub_one_eq_mod w.toNat 23 theorem A_shr (w : UInt32) : (w >>> (23 : UInt32)).toNat = w.toNat / 2 ^ 23 := by rw [UInt32.toNat_shiftRight, Nat.shiftRight_eq_div_pow] rfl theorem A_exp (w : UInt32) : (expOf w).toNat = w.toNat / 2 ^ 23 % 2 ^ 8 := by unfold expOf rw [UInt32.toNat_and, A_shr] exact Nat.and_two_pow_sub_one_eq_mod _ 8 theorem A_sign (w : UInt32) : signOf w = decide (2 ^ 31 ≤ w.toNat) := by have hw := w.toNat_lt unfold signOf have h : ((w &&& 0x80000000).toNat) = (w.toNat / 2 ^ 31 % 2) * 2 ^ 31 := by rw [UInt32.toNat_and] show w.toNat &&& 2 ^ 31 = _ rw [Nat.and_two_pow, Nat.testBit_eq_decide_div_mod_eq] have h2 : w.toNat / 2 ^ 31 % 2 = 0 ∨ w.toNat / 2 ^ 31 % 2 = 1 := by omega rcases h2 with h2 | h2 <;> rw [h2] <;> rfl rw [Bool.eq_iff_iff] simp only [bne_iff_ne, ne_eq, decide_eq_true_eq] rw [← UInt32.toNat_inj, h] simp only [UInt32.toNat_zero] omega theorem A_mant (w : UInt32) : ((w &&& 0x7FFFFF) ||| 0x800000).toNat = 2 ^ 23 + w.toNat % 2 ^ 23 := by rw [UInt32.toNat_or, A_and23, Nat.or_comm] have h : w.toNat % 2 ^ 23 < 2 ^ 23 := Nat.mod_lt _ (by positivity) have := Nat.two_pow_add_eq_or_of_lt h 1 simpa using this.symm theorem A_good (w : UInt32) (h : good w = true) : w.toNat % 2 ^ 31 = 0 ∨ (2 ^ 23 ≤ w.toNat % 2 ^ 31 ∧ w.toNat % 2 ^ 31 < 0x7F800000) := by have hv := A_and31 w have hlt := (w &&& 0x7FFFFFFF).toNat_lt unfold good at h simp only [Bool.or_eq_true, beq_iff_eq, decide_eq_true_eq] at h rcases h with h | h · left rw [← hv, h]; rfl · right rw [UInt32.le_iff_toNat_le, UInt32.toNat_sub] at h rw [← hv] generalize (w &&& 0x7FFFFFFF).toNat = v at * simp at h omega /-- The magnitude of a good word from its low 31 bits. -/ noncomputable def A_mag (v : ℕ) : ℝ := if v = 0 then 0 else ((2 ^ 23 + v % 2 ^ 23 : ℕ) : ℝ) * 2 ^ (((v / 2 ^ 23 : ℕ) : ℤ) - 150) theorem A_mag_nonneg (v : ℕ) : 0 ≤ A_mag v := by unfold A_mag; split_ifs <;> positivity theorem A_wv_eq (w : UInt32) (hg : good w = true) : wv w = (if 2 ^ 31 ≤ w.toNat then -1 else 1) * A_mag (w.toNat % 2 ^ 31) := by have hN := w.toNat_lt have hgd := A_good w hg have hE := A_exp w have hF : (fracOf w).toNat = w.toNat % 2 ^ 23 := A_and23 w have h255 : expOf w ≠ 255 := by intro h rw [h] at hE change 255 = _ at hE omega have hv : wv w = (decodeD w).toReal := by unfold wv ofW rw [Model.toReal_eq, toDyadic_ofUInt32 w h255] rw [hv] unfold decodeD A_mag by_cases h0 : w.toNat % 2 ^ 31 = 0 · have e0 : expOf w = 0 := by apply UInt32.toNat_inj.mp rw [hE]; show _ = 0; omega have f0 : fracOf w = 0 := by apply UInt32.toNat_inj.mp rw [hF]; show _ = 0; omega simp only [e0, f0, h0, ↓reduceIte] simp [FloatLib.Numerics.Dyadic.toReal, FloatLib.Numerics.Dyadic.signedSignificand] · have e0 : expOf w ≠ 0 := by intro h; rw [h] at hE change 0 = _ at hE omega simp only [e0, h0, ↓reduceIte] have f1 : w.toNat % 2 ^ 31 % 2 ^ 23 = (fracOf w).toNat := by rw [hF]; omega have f2 : w.toNat % 2 ^ 31 / 2 ^ 23 = (expOf w).toNat := by rw [hE]; omega rw [f1, f2] simp only [FloatLib.Numerics.Dyadic.toReal, FloatLib.Numerics.Dyadic.signedSignificand, A_sign] simp split_ifs <;> ring theorem A_mag_le (v : ℕ) (h : v < 0x7F800000) : A_mag v ≤ 2 ^ 128 := by unfold A_mag split_ifs with h0 · positivity · have h1 : ((2 ^ 23 + v % 2 ^ 23 : ℕ) : ℝ) ≤ 2 ^ 24 := by have : 2 ^ 23 + v % 2 ^ 23 ≤ 2 ^ 24 := by omega exact_mod_cast this have h2 : (2 : ℝ) ^ (((v / 2 ^ 23 : ℕ) : ℤ) - 150) ≤ 2 ^ (104 : ℤ) := zpow_le_zpow_right₀ (by norm_num) (by omega) have h3 : (2 : ℝ) ^ (104 : ℤ) = 2 ^ 104 := by norm_num rw [h3] at h2 calc _ ≤ (2 : ℝ) ^ 24 * 2 ^ 104 := mul_le_mul h1 h2 (by positivity) (by positivity) _ = 2 ^ 128 := by norm_num theorem A_mag_mono (a b : ℕ) (hab : a ≤ b) (ha : a = 0 ∨ 2 ^ 23 ≤ a) : A_mag a ≤ A_mag b := by rcases ha with rfl | ha · simpa [A_mag] using A_mag_nonneg b unfold A_mag simp only [show ¬ a = 0 by omega, show ¬ b = 0 by omega, ↓reduceIte] have hdiv : a / 2 ^ 23 ≤ b / 2 ^ 23 := Nat.div_le_div_right hab rcases hdiv.eq_or_lt with he | hl · rw [he] apply mul_le_mul_of_nonneg_right _ (by positivity) have : 2 ^ 23 + a % 2 ^ 23 ≤ 2 ^ 23 + b % 2 ^ 23 := by omega exact_mod_cast this · have h1 : ((2 ^ 23 + a % 2 ^ 23 : ℕ) : ℝ) ≤ 2 ^ 23 * 2 := by have : 2 ^ 23 + a % 2 ^ 23 ≤ 2 ^ 23 * 2 := by omega exact_mod_cast this have h2 : (2 : ℝ) ^ (((a / 2 ^ 23 : ℕ) : ℤ) - 150) * 2 ≤ 2 ^ (((b / 2 ^ 23 : ℕ) : ℤ) - 150) := by rw [← zpow_add_one₀ (by norm_num)] exact zpow_le_zpow_right₀ (by norm_num) (by omega) have h3 : (2 : ℝ) ^ 23 ≤ ((2 ^ 23 + b % 2 ^ 23 : ℕ) : ℝ) := by have : 2 ^ 23 ≤ 2 ^ 23 + b % 2 ^ 23 := by omega exact_mod_cast this calc _ ≤ ((2 : ℝ) ^ 23 * 2) * 2 ^ (((a / 2 ^ 23 : ℕ) : ℤ) - 150) := mul_le_mul_of_nonneg_right h1 (by positivity) _ = 2 ^ 23 * (2 ^ (((a / 2 ^ 23 : ℕ) : ℤ) - 150) * 2) := by ring _ ≤ _ := mul_le_mul h3 h2 (by positivity) (by positivity) /-- A good word decodes exactly: the hardware product of mantissa and table entry is exact. -/ theorem A_dv (w : UInt32) (hg : good w = true) : (dv w).isFinite = true ∧ R (dv w) = wv w := by have hN := w.toNat_lt have hgd := A_good w hg have hM := A_mant w have hm := A_toF32 ((w &&& 0x7FFFFF) ||| 0x800000) (by rw [hM]; omega) have hI : (w >>> (23 : UInt32)).toUSize.toNat = w.toNat / 2 ^ 23 := by rw [UInt32.toNat_toUSize, A_shr] have hp := A_pw (w.toNat / 2 ^ 23) (by omega) unfold dv rw [A_pw_get, hI] have hR1 : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| ≤ 2 ^ 24 := by rw [hm.2, hM, abs_of_nonneg (Nat.cast_nonneg _)] have : 2 ^ 23 + w.toNat % 2 ^ 23 ≤ 2 ^ 24 := by omega exact_mod_cast this have hR2 : |R (pwEntry (w.toNat / 2 ^ 23))| ≤ 2 ^ 106 := by rw [hp.2] split_ifs · simp all_goals simp only [abs_mul, abs_neg, abs_one, one_mul] rw [abs_of_pos (by positivity)] have : (2 : ℝ) ^ (((w.toNat / 2 ^ 23 % 256 : ℕ) : ℤ) - 150) ≤ 2 ^ (106 : ℤ) := zpow_le_zpow_right₀ (by norm_num) (by omega) simpa using this have hb : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| * |R (pwEntry (w.toNat / 2 ^ 23))| ≤ 2 ^ (1000 : ℤ) := by have h1 := A_B_lo have e1 : (2 : ℝ) ^ 24 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num have e2 : (2 : ℝ) ^ 106 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num exact (mul_le_mul (hR1.trans (e1.trans h1)) (hR2.trans (e2.trans h1)) (abs_nonneg _) A_B_pos.le).trans A_B_sq obtain ⟨hf, he⟩ := A_mul _ _ hm.1 hp.1 hb refine ⟨hf, ?_⟩ rw [he, hm.2, hp.2, A_wv_eq w hg, hM] rcases hgd with h0 | ⟨h1, h2⟩ · have e0 : w.toNat / 2 ^ 23 % 256 = 0 := by omega rw [h0] simp only [e0, ↓reduceIte, mul_zero, Model.roundAt_zero] simp [A_mag] · have e0 : ¬ w.toNat / 2 ^ 23 % 256 = 0 := by omega have e1 : w.toNat % 2 ^ 31 / 2 ^ 23 = w.toNat / 2 ^ 23 % 256 := by omega have e2 : w.toNat % 2 ^ 31 % 2 ^ 23 = w.toNat % 2 ^ 23 := by omega have e3 : (256 ≤ w.toNat / 2 ^ 23) ↔ 2 ^ 31 ≤ w.toNat := by omega unfold A_mag rw [e1, e2] simp only [e0, e3, show ¬ w.toNat % 2 ^ 31 = 0 by omega, ↓reduceIte] have hk : -1074 ≤ ((w.toNat / 2 ^ 23 % 256 : ℕ) : ℤ) - 150 := by omega have hMl : 2 ^ 23 + w.toNat % 2 ^ 23 < 2 ^ 53 := by omega generalize ((w.toNat / 2 ^ 23 % 256 : ℕ) : ℤ) - 150 = k at hk ⊢ generalize 2 ^ 23 + w.toNat % 2 ^ 23 = M at hMl ⊢ by_cases hs : 2 ^ 31 ≤ w.toNat · have := A_round_exact (-(M : ℤ)) k (by simpa using hMl) hk simp only [hs, ↓reduceIte] push_cast at this rw [show (M : ℝ) * (-1 * 2 ^ k) = -(M : ℝ) * 2 ^ k by ring, show -1 * ((M : ℝ) * 2 ^ k) = -(M : ℝ) * 2 ^ k by ring] exact this · have := A_round_exact (M : ℤ) k (by simpa using hMl) hk simp only [hs, ↓reduceIte, one_mul] push_cast at this exact this /-! ## Running sums -/ /-- The hardware sum of a list, left to right from zero. -/ def A_fsum (l : List Float) : Float := l.foldl (· + ·) 0 /-- Sum of the magnitudes of the terms. -/ noncomputable def A_sa (l : List Float) : ℝ := (l.map fun q => |R q|).sum /-- Sum of the terms. -/ noncomputable def A_sr (l : List Float) : ℝ := (l.map R).sum theorem A_sa_nonneg (l : List Float) : 0 ≤ A_sa l := by induction l with | nil => simp [A_sa] | cons q l ih => simp only [A_sa, List.map_cons, List.sum_cons] at ih ⊢ positivity theorem A_sa_snoc (l : List Float) (q : Float) : A_sa (l ++ [q]) = A_sa l + |R q| := by simp [A_sa] theorem A_sr_snoc (l : List Float) (q : Float) : A_sr (l ++ [q]) = A_sr l + R q := by simp [A_sr] theorem A_fsum_snoc (l : List Float) (q : Float) : A_fsum (l ++ [q]) = A_fsum l + q := by simp [A_fsum] theorem A_sr_abs (l : List Float) : |A_sr l| ≤ A_sa l := by induction l with | nil => simp [A_sa, A_sr] | cons q l ih => simp only [A_sa, A_sr, List.map_cons, List.sum_cons] at ih ⊢ exact (abs_add_le _ _).trans (by linarith) /-- Running error of a hardware sum: at most two half-ulps of the total magnitude per term. -/ theorem A_fsum_spec (l : List Float) (hf : ∀ q ∈ l, q.isFinite = true) (hlen : l.length ≤ 2 ^ 21) (hb : A_sa l ≤ A_B / 4) : (A_fsum l).isFinite = true ∧ |R (A_fsum l) - A_sr l| ≤ 2 * l.length * (1 / 2 ^ 53 * (A_sa l + A_z)) := by induction l using List.reverseRecOn with | nil => simp only [A_fsum, List.foldl_nil, List.length_nil] refine ⟨A_zero.1, ?_⟩ simp [A_zero.2, A_sr] | append_singleton l q ih => rw [A_sa_snoc] at hb have hP := A_sa_nonneg l have hq0 := abs_nonneg (R q) obtain ⟨h1, h2⟩ := ih (fun x hx => hf x (by simp [hx])) (by simp at hlen; omega) (by linarith) have hk : (l.length : ℝ) + 1 ≤ 2 ^ 21 := by simp at hlen exact_mod_cast hlen have hk0 : (0 : ℝ) ≤ l.length := Nat.cast_nonneg _ have hS := A_sr_abs l have hz := A_z_pos have hz1 := A_z_le have hB := A_B_pos have hBl := A_B_lo rw [A_fsum_snoc, A_sa_snoc, A_sr_snoc, List.length_append, List.length_singleton] have hB1 : (8 : ℝ) ≤ A_B := by have : (8 : ℝ) ≤ 2 ^ 256 * 2 ^ 42 := by norm_num linarith have hz' : A_z ≤ 1 := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) ≤ 1 := by norm_num linarith push_cast set r := R (A_fsum l) set a := R q set P := A_sa l set S := A_sr l set k : ℝ := (l.length : ℝ) set W := (1 / 2 ^ 53 : ℝ) * (P + |a| + A_z) with hWdef have hW : (1 / 2 ^ 53 : ℝ) * (P + A_z) ≤ W := by rw [hWdef] apply mul_le_mul_of_nonneg_left _ (by norm_num) linarith have hW0 : 0 ≤ W := by rw [hWdef]; positivity have hD : |r - S| ≤ 2 * k * W := h2.trans (mul_le_mul_of_nonneg_left hW (by positivity)) have hk2 : 2 * k * (1 / 2 ^ 53) ≤ 1 := by norm_num at hk ⊢ linarith have hUD : (1 / 2 ^ 53 : ℝ) * |r - S| ≤ W := calc (1 / 2 ^ 53 : ℝ) * |r - S| ≤ 1 / 2 ^ 53 * (2 * k * W) := mul_le_mul_of_nonneg_left hD (by norm_num) _ = (2 * k * (1 / 2 ^ 53)) * W := by ring _ ≤ 1 * W := mul_le_mul_of_nonneg_right hk2 hW0 _ = W := one_mul _ have hDP : |r - S| ≤ P + 1 := calc |r - S| ≤ 2 * k * (1 / 2 ^ 53 * (P + A_z)) := h2 _ = (2 * k * (1 / 2 ^ 53)) * (P + A_z) := by ring _ ≤ 1 * (P + A_z) := mul_le_mul_of_nonneg_right hk2 (by positivity) _ ≤ P + 1 := by linarith have hr : |r| ≤ A_B := by have e : r = (r - S) + S := by ring have := abs_add_le (r - S) S rw [← e] at this linarith have ha : |a| ≤ A_B := by linarith obtain ⟨hf3, he⟩ := A_addE (A_fsum l) q h1 (hf q (by simp)) hr ha refine ⟨hf3, ?_⟩ have t1 : |R (A_fsum l + q) - (S + a)| ≤ |R (A_fsum l + q) - (r + a)| + |r - S| := by have e : R (A_fsum l + q) - (S + a) = (R (A_fsum l + q) - (r + a)) + (r - S) := by ring rw [e] exact abs_add_le _ _ have t2 : |r + a| ≤ |r - S| + P + |a| := by have e : r + a = (r - S) + S + a := by ring rw [e] have h3 := abs_add_le ((r - S) + S) a have h4 := abs_add_le (r - S) S linarith have t3 : (1 / 2 ^ 53 : ℝ) * (|r + a| + A_z) ≤ 1 / 2 ^ 53 * |r - S| + W := by rw [hWdef, ← mul_add] apply mul_le_mul_of_nonneg_left _ (by norm_num) linarith calc |R (A_fsum l + q) - (S + a)| ≤ W + W + 2 * k * W := by linarith _ = 2 * (k + 1) * W := by ring /-! ## The enclosure -/ theorem A_nf (n : ℕ) (hn : n < 2 ^ 20) : (nfOf n).isFinite = true ∧ R (nfOf n) = ((n : ℝ) + 8) / 2 ^ 51 := by have ht : (n.toUInt64 + 8).toNat = n + 8 := by have h8 : (8 : UInt64).toNat = 8 := rfl simp [UInt64.toNat_add, h8] omega have h1 := A_toF64 (n.toUInt64 + 8) (by rw [ht]; omega) have hu : (Float.ofBits 0x3CC0000000000000).toModel.unpack = .finite .positive (2 ^ 52) (-103) (by decide) := by rfl have h2 : (Float.ofBits 0x3CC0000000000000).isFinite = true := by change (Float.ofBits 0x3CC0000000000000).toModel.unpack.isFinite = true rw [hu]; rfl have h3 : R (Float.ofBits 0x3CC0000000000000) = 1 / 2 ^ 51 := by rw [A_R_unpack, hu, Model.unpackedToReal_finite, A_bpow] simp norm_num have h4 : (n : ℝ) + 8 ≤ 2 ^ 21 := by have : n + 8 ≤ 2 ^ 21 := by omega exact_mod_cast this obtain ⟨hf, he⟩ := A_mul _ _ h1.1 h2 (by rw [h1.2, h3, ht] have h5 : (1 : ℝ) ≤ 2 ^ 256 * 2 ^ 42 := by norm_num have h6 := A_B_lo have h7 : (2 : ℝ) ^ 21 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num refine le_trans (mul_le_mul ?_ ?_ (abs_nonneg _) A_B_pos.le) A_B_sq · rw [Nat.cast_add, abs_of_nonneg (by positivity)] push_cast linarith · rw [abs_of_nonneg (by positivity)] have : (1 : ℝ) / 2 ^ 51 ≤ 1 := by norm_num linarith) unfold nfOf refine ⟨hf, ?_⟩ rw [he, h1.2, h3, ht] have := A_round_exact ((n : ℤ) + 8) (-51) (by omega) (by norm_num) push_cast at this ⊢ have e : (2 : ℝ) ^ (-51 : ℤ) = 1 / 2 ^ 51 := by norm_num rw [e] at this rw [this] ring /-- The real-number core: the margin `e` beats every error term. -/ theorem A_real (c Y S T E z : ℝ) (hc1 : 8 ≤ c) (hc2 : c ≤ 2 ^ 20 + 8) (hz1 : z ≤ 1 / 2 ^ 200 * (1 / 2 ^ 200)) (hY : 0 ≤ Y) (hS : |S| ≤ 2 * Y) (hT : |T - Y| ≤ Y / 2 ^ 30 + 1 / 2 ^ 250) (hE : |E - c / 2 ^ 51 * T| ≤ 1 / 2 ^ 53 * (|c / 2 ^ 51 * T| + z)) (hbig : 1 / 2 ^ 127 ≤ |S + E| + 1 / 2 ^ 53 * (|S + E| + z)) : 4 * (c - 7) * (1 / 2 ^ 53 * Y) + 1 / 2 ^ 53 * (|S| + |E| + z) < E := by have hc0 : 0 < c := by linarith obtain ⟨hT1, hT2⟩ := abs_le.mp hT have hT' : |T| ≤ 2 * Y + 1 / 2 ^ 250 := by apply abs_le.mpr constructor <;> linarith have hcG : c / 2 ^ 51 ≤ 1 / 2 ^ 30 := by have : (2 : ℝ) ^ 20 + 8 ≤ 2 ^ 21 := by norm_num calc c / 2 ^ 51 ≤ 2 ^ 21 / 2 ^ 51 := by gcongr; linarith _ = 1 / 2 ^ 30 := by norm_num set G := c / 2 ^ 51 * T with hGdef have hG : |G| ≤ (2 * Y + 1 / 2 ^ 250) / 2 ^ 30 := by rw [hGdef, abs_mul, abs_of_pos (by positivity)] calc _ ≤ (1 / 2 ^ 30 : ℝ) * (2 * Y + 1 / 2 ^ 250) := mul_le_mul hcG hT' (abs_nonneg _) (by positivity) _ = _ := by ring have hE1 : |E| ≤ |E - G| + |G| := by have e : E = (E - G) + G := by ring have := abs_add_le (E - G) G rwa [← e] at this have hSE := abs_add_le S E have hG0 := abs_nonneg G have hE0 := abs_nonneg E have hYl : 1 / 2 ^ 129 ≤ Y := by linarith have hT3 : Y - Y / 2 ^ 30 - 1 / 2 ^ 250 ≤ T := by linarith have hT4 : 0 ≤ Y - Y / 2 ^ 30 - 1 / 2 ^ 250 := by linarith have hT5 : 0 ≤ T := by linarith have hG1 : 0 ≤ G := by rw [hGdef]; positivity rw [abs_of_nonneg hG1] at hE hE1 hG have hE2 := (abs_le.mp hE).1 have hG2 : c * (Y - Y / 2 ^ 30 - 1 / 2 ^ 250) / 2 ^ 51 ≤ G := by rw [hGdef, show c / 2 ^ 51 * T = c * T / 2 ^ 51 by ring] gcongr have hw1 : c * Y ≤ 2 ^ 21 * Y := by have : c ≤ 2 ^ 21 := by have : (2 : ℝ) ^ 20 + 8 ≤ 2 ^ 21 := by norm_num linarith exact mul_le_mul_of_nonneg_right this hY have hG3 : c * (Y - Y / 2 ^ 30 - 1 / 2 ^ 250) = c * Y - c * Y / 2 ^ 30 - c * (1 / 2 ^ 250) := by ring rw [hG3] at hG2 have hc3 : c * (1 / 2 ^ 250) ≤ 2 ^ 21 * (1 / 2 ^ 250) := by have : c ≤ 2 ^ 21 := by have : (2 : ℝ) ^ 20 + 8 ≤ 2 ^ 21 := by norm_num linarith exact mul_le_mul_of_nonneg_right this (by positivity) have hgoal : 4 * (c - 7) * (1 / 2 ^ 53 * Y) = c * Y / 2 ^ 51 - 28 * Y / 2 ^ 53 := by ring rw [hgoal] linarith theorem A_final (n : ℕ) (hn : n < 2 ^ 20) (s t : Float) (hs : s.isFinite = true) (ht : t.isFinite = true) (X Y : ℝ) (hY0 : 0 ≤ Y) (hYB : Y ≤ A_B / 8) (h1 : |R s - X| ≤ 4 * ((n : ℝ) + 1) * (1 / 2 ^ 53 * Y)) (h2 : |R t - Y| ≤ Y / 2 ^ 30 + 1 / 2 ^ 250) (h3 : |R s| ≤ 2 * Y) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + nfOf n * t)|) : R (s - nfOf n * t) < X ∧ X < R (s + nfOf n * t) := by have hB := A_B_pos have hB8 : (8 : ℝ) ≤ A_B := by have h : (8 : ℝ) ≤ 2 ^ 256 * 2 ^ 42 := by norm_num linarith [A_B_lo] have hnR : (n : ℝ) + 1 ≤ 2 ^ 20 := by exact_mod_cast hn have hn0 : (0 : ℝ) ≤ n := Nat.cast_nonneg n obtain ⟨hnf, hnv⟩ := A_nf n hn have hT : |R t| ≤ 3 / 8 * A_B := by have e : R t = (R t - Y) + Y := by ring have := abs_add_le (R t - Y) Y rw [← e, abs_of_nonneg hY0] at this have h : (1 : ℝ) / 2 ^ 250 ≤ 1 := by norm_num have h' : Y / 2 ^ 30 ≤ Y := by apply div_le_self hY0; norm_num linarith have hT3 : |R t| ≤ A_B := by linarith have hN : |R (nfOf n)| ≤ A_B := by rw [hnv, abs_of_nonneg (by positivity)] have : ((n : ℝ) + 8) / 2 ^ 51 ≤ 1 := by rw [div_le_one (by positivity)] norm_num at hnR ⊢ linarith linarith obtain ⟨hef, heE⟩ := A_mulE _ _ hnf ht hN hT3 have hNT : |R (nfOf n) * R t| ≤ |R t| := by rw [abs_mul, hnv, abs_of_nonneg (show 0 ≤ ((n : ℝ) + 8) / 2 ^ 51 by positivity)] have : ((n : ℝ) + 8) / 2 ^ 51 ≤ 1 := by rw [div_le_one (by positivity)] norm_num at hnR ⊢ linarith nlinarith [abs_nonneg (R t)] have hz := A_z_pos have hz1 := A_z_le have hz2 : A_z ≤ 1 := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) ≤ 1 := by norm_num linarith have hE : |R (nfOf n * t)| ≤ A_B := by have e : R (nfOf n * t) = (R (nfOf n * t) - R (nfOf n) * R t) + R (nfOf n) * R t := by ring have := abs_add_le (R (nfOf n * t) - R (nfOf n) * R t) (R (nfOf n) * R t) rw [← e] at this linarith have hSB : |R s| ≤ A_B := by linarith obtain ⟨hpf, hpE⟩ := A_addE s _ hs hef hSB hE obtain ⟨hmf, hmE⟩ := A_subE s _ hs hef hSB hE have hSb1 : |R s + R (nfOf n * t)| ≤ |R s| + |R (nfOf n * t)| := abs_add_le _ _ have hSb2 : |R s - R (nfOf n * t)| ≤ |R s| + |R (nfOf n * t)| := abs_sub _ _ rw [hnv] at heE have hP : |R (s + nfOf n * t)| ≤ |R s + R (nfOf n * t)| + |R (s + nfOf n * t) - (R s + R (nfOf n * t))| := by have e : R (s + nfOf n * t) = (R s + R (nfOf n * t)) + (R (s + nfOf n * t) - (R s + R (nfOf n * t))) := by ring have := abs_add_le (R s + R (nfOf n * t)) (R (s + nfOf n * t) - (R s + R (nfOf n * t))) rwa [← e] at this have hbig' : (1 : ℝ) / 2 ^ 127 ≤ |R s + R (nfOf n * t)| + 1 / 2 ^ 53 * (|R s + R (nfOf n * t)| + A_z) := by have e : (2 : ℝ) ^ (-127 : ℤ) = 1 / 2 ^ 127 := by norm_num rw [e] at hbig linarith have key := A_real ((n : ℝ) + 8) Y (R s) (R t) (R (nfOf n * t)) A_z (by linarith) (by norm_num at hnR ⊢; linarith) hz1 hY0 h3 h2 heE hbig' have e7 : 4 * ((n : ℝ) + 8 - 7) = 4 * ((n : ℝ) + 1) := by ring rw [e7] at key obtain ⟨h1a, h1b⟩ := abs_le.mp h1 obtain ⟨hpa, hpb⟩ := abs_le.mp hpE obtain ⟨hma, hmb⟩ := abs_le.mp hmE constructor <;> linarith /-! ## The folds -/ theorem A_fold (g h : ℕ → Float) (l : List ℕ) (a b : Float) : l.foldl (fun (acc : F1) t => F1.mk (acc.s + g t) (acc.t + h t)) ⟨a, b⟩ = ⟨(l.map g).foldl (· + ·) a, (l.map h).foldl (· + ·) b⟩ := by induction l generalizing a b with | nil => rfl | cons x l ih => simp only [List.foldl_cons, List.map_cons, ih] theorem A_sa_abs (l : List Float) (hf : ∀ q ∈ l, q.isFinite = true) : A_sa (l.map Float.abs) = A_sa l ∧ A_sr (l.map Float.abs) = A_sa l := by induction l with | nil => simp [A_sa, A_sr] | cons q l ih => obtain ⟨h1, h2⟩ := ih (fun x hx => hf x (by simp [hx])) have h3 := (A_abs q (hf q (by simp))).2 simp only [A_sa, A_sr, List.map_cons, List.sum_cons, List.map_map] at h1 h2 ⊢ simp only [Function.comp_def, h3, abs_abs] simp only [Function.comp_def] at h1 h2 rw [h1, h2] simp /-- Per-term bookkeeping over a list of positions. -/ theorem A_terms (ts : List ℕ) (q : ℕ → Float) (p : ℕ → ℝ) (hq : ∀ t ∈ ts, |R (q t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z)) : |A_sr (ts.map q) - (ts.map p).sum| ≤ 1 / 2 ^ 53 * ((ts.map fun t => |p t|).sum + ts.length * A_z) ∧ A_sa (ts.map q) ≤ (ts.map fun t => |p t|).sum + 1 / 2 ^ 53 * ((ts.map fun t => |p t|).sum + ts.length * A_z) ∧ (ts.map fun t => |p t|).sum ≤ 2 * A_sa (ts.map q) + ts.length * A_z := by induction ts with | nil => simp [A_sa, A_sr] | cons x ts ih => obtain ⟨h1, h2, h3⟩ := ih (fun t ht => hq t (by simp [ht])) have h4 := hq x (by simp) simp only [A_sa, A_sr, List.map_cons, List.sum_cons, List.map_map, List.length_cons] at h1 h2 h3 ⊢ simp only [Function.comp_def] at h1 h2 h3 ⊢ have hz := A_z_pos obtain ⟨h5, h6⟩ := abs_le.mp h4 obtain ⟨h7, h8⟩ := abs_le.mp h1 have hpq : |p x| ≤ |R (q x)| + 1 / 2 ^ 53 * (|p x| + A_z) := by have e : p x = R (q x) - (R (q x) - p x) := by ring have := abs_sub (R (q x)) (R (q x) - p x) rw [← e] at this linarith have hqp : |R (q x)| ≤ |p x| + 1 / 2 ^ 53 * (|p x| + A_z) := by have e : R (q x) = p x + (R (q x) - p x) := by ring have := abs_add_le (p x) (R (q x) - p x) rw [← e] at this linarith have hp0 := abs_nonneg (p x) refine ⟨abs_le.mpr ⟨?_, ?_⟩, ?_, ?_⟩ <;> push_cast <;> nlinarith theorem A_sum_le (ts : List ℕ) (f : ℕ → ℝ) (M : ℝ) (h : ∀ t ∈ ts, f t ≤ M) : (ts.map f).sum ≤ ts.length * M := by induction ts with | nil => simp | cons x ts ih => have h1 := ih (fun t ht => h t (by simp [ht])) have h2 := h x (by simp) simp only [List.map_cons, List.sum_cons, List.length_cons] push_cast linarith theorem A_wv_abs (w : UInt32) (hg : good w = true) : |wv w| = A_mag (w.toNat % 2 ^ 31) := by rw [A_wv_eq w hg, abs_mul, abs_of_nonneg (A_mag_nonneg _)] split_ifs <;> simp theorem A_wv_le (w : UInt32) (hg : good w = true) : |wv w| ≤ 2 ^ 128 := by rw [A_wv_abs w hg] apply A_mag_le rcases A_good w hg with h | h <;> omega theorem A_wv_zero (w : UInt32) (hg : good w = true) (h : w &&& 0x7FFFFFFF = 0) : wv w = 0 := by rw [A_wv_eq w hg, ← A_and31 w, h] simp [A_mag] theorem A_B_128 : (2 : ℝ) ^ 128 ≤ A_B := by have h : (2 : ℝ) ^ 128 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num linarith [A_B_lo] /-- One product of two good words. -/ theorem A_term (a b : UInt32) (ha : good a = true) (hb : good b = true) : (dv a * dv b).isFinite = true ∧ |R (dv a * dv b) - wv a * wv b| ≤ 1 / 2 ^ 53 * (|wv a * wv b| + A_z) ∧ |wv a * wv b| ≤ 2 ^ 256 := by obtain ⟨fa, ra⟩ := A_dv a ha obtain ⟨fb, rb⟩ := A_dv b hb have la := A_wv_le a ha have lb := A_wv_le b hb have hab : |wv a * wv b| ≤ 2 ^ 256 := by rw [abs_mul] calc |wv a| * |wv b| ≤ 2 ^ 128 * 2 ^ 128 := mul_le_mul la lb (abs_nonneg _) (by positivity) _ = 2 ^ 256 := by norm_num obtain ⟨h1, h2⟩ := A_mulE _ _ fa fb (by rw [ra]; exact la.trans A_B_128) (by rw [rb]; exact lb.trans A_B_128) rw [ra, rb] at h2 exact ⟨h1, h2, hab⟩ theorem A_exact_gen (n : ℕ) (xw ww : ℕ → UInt32) (bw : UInt32) (l : List ℕ) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (h0 : ∀ t, t < n → t ∉ l → wv (xw t) * wv (ww t) = 0) : exactS n xw ww bw = (l.map fun t => wv (xw t) * wv (ww t)).sum + wv bw := by unfold exactS congr 1 rw [← List.sum_toFinset _ hl] symm apply Finset.sum_subset · intro t ht simp only [List.mem_toFinset] at ht simpa using hl2 t ht · intro t ht hnl exact h0 t (by simpa using ht) (by simpa using hnl) theorem A_exact_tight (n : ℕ) (xw ww : ℕ → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (l : List ℕ) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0) : exactS n xw ww bw = (l.map fun t => wv (xw t) * wv (ww t)).sum + wv bw := A_exact_gen n xw ww bw l hl hl2 fun t h1 h2 => by simp [A_wv_zero (xw t) (hx t h1) (hz t h1 h2)] /-- As `A_exact_tight`, for positions dropped because either factor is zero. -/ theorem A_exact_Z (n : ℕ) (xw ww : ℕ → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (l : List ℕ) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0 ∨ ww t &&& 0x7FFFFFFF = 0) : exactS n xw ww bw = (l.map fun t => wv (xw t) * wv (ww t)).sum + wv bw := A_exact_gen n xw ww bw l hl hl2 fun t h1 h2 => by rcases hz t h1 h2 with h | h · simp [A_wv_zero (xw t) (hx t h1) h] · simp [A_wv_zero (ww t) (hw t h1) h] theorem A_len (n : ℕ) (l : List ℕ) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) : l.length ≤ n := by have h : l.toFinset ⊆ Finset.range n := by intro t ht simp only [List.mem_toFinset] at ht simpa using hl2 t ht have := Finset.card_le_card h rwa [List.toFinset_card_of_nodup hl, Finset.card_range] at this /-- The tight bound for abstract terms. -/ theorem A_tight_core (n : ℕ) (hn : n < 2 ^ 20) (ts : List ℕ) (hlen : ts.length ≤ n) (g : ℕ → Float) (p : ℕ → ℝ) (b : Float) (bv : ℝ) (hg : ∀ t ∈ ts, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) ∧ |p t| ≤ 2 ^ 256) (hbf : b.isFinite = true) (hbv : R b = bv) (hbl : |bv| ≤ 2 ^ 128) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n * A_fsum ((ts.map g ++ [b]).map Float.abs))|) : R (A_fsum (ts.map g ++ [b]) - nfOf n * A_fsum ((ts.map g ++ [b]).map Float.abs)) < (ts.map p).sum + bv ∧ (ts.map p).sum + bv < R (A_fsum (ts.map g ++ [b]) + nfOf n * A_fsum ((ts.map g ++ [b]).map Float.abs)) := by have hfq : ∀ q ∈ ts.map g ++ [b], q.isFinite = true := by intro q hq simp only [List.mem_append, List.mem_map, List.mem_singleton] at hq rcases hq with ⟨t, ht, rfl⟩ | rfl · exact (hg t ht).1 · exact hbf obtain ⟨hA1, hA2, hA3⟩ := A_terms ts g p (fun t ht => (hg t ht).2.1) have hAle := A_sum_le ts (fun t => |p t|) _ (fun t ht => (hg t ht).2.2) have hsa := A_sa_snoc (ts.map g) b have hsr := A_sr_snoc (ts.map g) b have hsag := A_sa_nonneg (ts.map g) have hsa0 := A_sa_nonneg (ts.map g ++ [b]) rw [hbv] at hsa hsr have hk : (ts.length : ℝ) ≤ n := by exact_mod_cast hlen have hk0 : (0 : ℝ) ≤ ts.length := Nat.cast_nonneg _ have hnR : (n : ℝ) + 1 ≤ 2 ^ 20 := by exact_mod_cast hn have hz := A_z_pos have hz1 := A_z_le have hz2 : A_z ≤ 1 := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) ≤ 1 := by norm_num linarith have hA0 : 0 ≤ (ts.map fun t => |p t|).sum := List.sum_nonneg (by simp only [List.mem_map]; rintro _ ⟨t, _, rfl⟩; exact abs_nonneg _) set A := (ts.map fun t => |p t|).sum set k : ℝ := (ts.length : ℝ) have hkz : k * A_z ≤ 2 ^ 20 := by nlinarith have hAl : A ≤ 2 ^ 20 * 2 ^ 256 := by refine hAle.trans (mul_le_mul_of_nonneg_right ?_ (by positivity)) linarith have hBl := A_B_lo have hnum : (2 : ℝ) ^ 20 * 2 ^ 256 * 3 + 2 ^ 128 + 2 ^ 20 + 2 ≤ 2 ^ 256 * 2 ^ 42 / 8 := by norm_num have hsaB : A_sa (ts.map g ++ [b]) + A_z ≤ A_B / 8 := by have hU : (1 / 2 ^ 53 : ℝ) * (A + k * A_z) ≤ A + k * A_z := by have : 0 ≤ A + k * A_z := by positivity nlinarith linarith have hB8 : (1 : ℝ) ≤ A_B / 8 := by have : (1 : ℝ) ≤ 2 ^ 256 * 2 ^ 42 / 8 := by norm_num linarith have hlen1 : (ts.map g ++ [b]).length ≤ 2 ^ 21 := by simp; omega obtain ⟨hf1, he1⟩ := A_fsum_spec _ hfq hlen1 (by linarith) obtain ⟨habs1, habs2⟩ := A_sa_abs _ hfq obtain ⟨hf2, he2⟩ := A_fsum_spec ((ts.map g ++ [b]).map Float.abs) (by intro q hq obtain ⟨x, hx, rfl⟩ := List.mem_map.mp hq exact (A_abs x (hfq x hx)).1) (by simpa using hlen1) (by rw [habs1]; linarith) rw [habs1, habs2] at he2 have hl1 : (((ts.map g ++ [b]).length : ℕ) : ℝ) = k + 1 := by simp [k] have hl2 : (((ts.map g ++ [b]).map Float.abs).length : ℝ) = k + 1 := by simp [k] rw [hl1] at he1 rw [hl2] at he2 generalize hsaD : A_sa (ts.map g ++ [b]) = sa at * set Y := sa + A_z with hYdef have hY0 : 0 ≤ Y := by positivity have hUY : 0 ≤ (1 / 2 ^ 53 : ℝ) * Y := by positivity have hKU : 2 * (k + 1) * (1 / 2 ^ 53 * Y) ≤ Y / 2 ^ 30 := by have h : k + 1 ≤ 2 ^ 20 := by linarith have := mul_le_mul_of_nonneg_right h hUY norm_num at this ⊢ linarith have hS1 : |A_sr (ts.map g ++ [b])| ≤ sa := by rw [← hsaD]; exact A_sr_abs _ obtain ⟨he1a, he1b⟩ := abs_le.mp he1 obtain ⟨he2a, he2b⟩ := abs_le.mp he2 refine A_final n hn _ _ hf1 hf2 _ Y hY0 (by linarith) ?_ ?_ ?_ hbig · have hKY : sa + k * A_z ≤ (k + 1) * Y := by rw [hYdef] nlinarith [mul_nonneg hk0 hsa0] have hU2 : (1 / 2 ^ 53 : ℝ) * (A + k * A_z) ≤ 2 * (k + 1) * (1 / 2 ^ 53 * Y) := by have h1 : A + k * A_z ≤ 2 * (sa + k * A_z) := by linarith [abs_nonneg bv] have h2 : A + k * A_z ≤ 2 * ((k + 1) * Y) := by linarith calc (1 / 2 ^ 53 : ℝ) * (A + k * A_z) ≤ 1 / 2 ^ 53 * (2 * ((k + 1) * Y)) := mul_le_mul_of_nonneg_left h2 (by norm_num) _ = _ := by ring have hn4 : 4 * (k + 1) * (1 / 2 ^ 53 * Y) ≤ 4 * ((n : ℝ) + 1) * (1 / 2 ^ 53 * Y) := mul_le_mul_of_nonneg_right (by linarith) hUY obtain ⟨hA1a, hA1b⟩ := abs_le.mp hA1 apply abs_le.mpr constructor <;> linarith · apply abs_le.mpr constructor · have : (0 : ℝ) ≤ 1 / 2 ^ 250 := by norm_num have : A_z ≤ 1 / 2 ^ 250 := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) ≤ 1 / 2 ^ 250 := by norm_num linarith linarith · linarith · have e : R (A_fsum (ts.map g ++ [b])) = (R (A_fsum (ts.map g ++ [b])) - A_sr (ts.map g ++ [b])) + A_sr (ts.map g ++ [b]) := by ring have h1 := abs_add_le (R (A_fsum (ts.map g ++ [b])) - A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b])) rw [← e] at h1 have h2 : Y / 2 ^ 30 ≤ Y := by apply div_le_self hY0; norm_num linarith /-- The tight bound, for positions left out because the row entry or the weight is zero. -/ theorem encl_tightZ (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0 ∨ ww t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : let r := l.foldl (fun (acc : F1) t => let q := dv (xw t) * dv (ww t) F1.mk (acc.s + q) (acc.t + q.abs)) ⟨0, 0⟩ s = r.s + dv bw ∧ e = nfOf n * (r.t + (dv bw).abs)) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)|) : R (s - e) < exactS n xw ww bw ∧ exactS n xw ww bw < R (s + e) := by set g : ℕ → Float := fun t => dv (xw t) * dv (ww t) with hg set p : ℕ → ℝ := fun t => wv (xw t) * wv (ww t) with hp have hfold : l.foldl (fun (acc : F1) t => let q := dv (xw t) * dv (ww t) F1.mk (acc.s + q) (acc.t + q.abs)) ⟨0, 0⟩ = ⟨(l.map g).foldl (· + ·) 0, (l.map fun t => (g t).abs).foldl (· + ·) 0⟩ := A_fold g (fun t => (g t).abs) l 0 0 obtain ⟨hs1, hs2⟩ := hs rw [hfold] at hs1 hs2 have hlen := A_len n l hl hl2 have hgt : ∀ t ∈ l, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) ∧ |p t| ≤ 2 ^ 256 := fun t ht => A_term _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) obtain ⟨hbf, hbv⟩ := A_dv bw hb have hS : s = A_fsum (l.map g ++ [dv bw]) := by rw [hs1, A_fsum_snoc]; rfl have hE : e = nfOf n * A_fsum ((l.map g ++ [dv bw]).map Float.abs) := by rw [hs2] congr 1 rw [List.map_append, List.map_map, List.map_singleton, A_fsum_snoc] rfl rw [hS, hE] at hbig ⊢ rw [A_exact_Z n xw ww bw hx hw l hl hl2 hz] exact A_tight_core n hn l hlen g p (dv bw) (wv bw) hgt hbf hbv (A_wv_le bw hb) hbig theorem encl_tight (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : let r := l.foldl (fun (acc : F1) t => let q := dv (xw t) * dv (ww t) F1.mk (acc.s + q) (acc.t + q.abs)) ⟨0, 0⟩ s = r.s + dv bw ∧ e = nfOf n * (r.t + (dv bw).abs)) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)|) : R (s - e) < exactS n xw ww bw ∧ exactS n xw ww bw < R (s + e) := encl_tightZ n hn xw ww bw hx hw hb l hl hl2 (fun t h1 h2 => Or.inl (hz t h1 h2)) s e hs hbig /-! ## The coarse bound -/ /-- The computed coarse bound stays close to `H * J + b`. -/ theorem A_real2 (H J Rj Rg Rt b z k : ℝ) (hH0 : 0 ≤ H) (hH : H ≤ 2 ^ 128) (hJ : 0 ≤ J) (hb : 0 ≤ b) (hz0 : 0 ≤ z) (hz1 : z ≤ 1 / 2 ^ 200 * (1 / 2 ^ 200)) (hk : k ≤ 2 ^ 20) (hj : |Rj - J| ≤ 2 * k * (1 / 2 ^ 53 * (J + z))) (hg : |Rg - H * Rj| ≤ 1 / 2 ^ 53 * (|H * Rj| + z)) (ht : |Rt - (Rg + b)| ≤ 1 / 2 ^ 53 * (|Rg + b| + z)) : |Rt - (H * J + b + z)| ≤ (H * J + b + z) / 2 ^ 30 + 1 / 2 ^ 250 := by have e1 : |Rj - J| ≤ (J + z) / 2 ^ 32 := by refine hj.trans ?_ have h : 0 ≤ (1 / 2 ^ 53 : ℝ) * (J + z) := mul_nonneg (by norm_num) (add_nonneg hJ hz0) have := mul_le_mul_of_nonneg_right hk h norm_num at this ⊢ linarith have e2 : |H * Rj - H * J| ≤ (H * J + H * z) / 2 ^ 32 := by rw [← mul_sub, abs_mul, abs_of_nonneg hH0] calc H * |Rj - J| ≤ H * ((J + z) / 2 ^ 32) := mul_le_mul_of_nonneg_left e1 hH0 _ = _ := by ring have e3 : H * z ≤ 1 / 2 ^ 250 := by calc H * z ≤ 2 ^ 128 * (1 / 2 ^ 200 * (1 / 2 ^ 200)) := mul_le_mul hH hz1 hz0 (by norm_num) _ ≤ 1 / 2 ^ 250 := by norm_num have hW : 0 ≤ H * J := mul_nonneg hH0 hJ have hZ0 : 0 ≤ H * z := mul_nonneg hH0 hz0 generalize H * J = W at * generalize H * z = Z at * generalize H * Rj = G at * obtain ⟨e2a, e2b⟩ := abs_le.mp e2 obtain ⟨hga, hgb⟩ := abs_le.mp hg obtain ⟨hta, htb⟩ := abs_le.mp ht have hG : |G| ≤ W + (W + Z) / 2 ^ 32 := by apply abs_le.mpr constructor <;> linarith have hRg : |Rg| ≤ 2 * W + 1 / 2 ^ 200 := by apply abs_le.mpr constructor <;> linarith have hR : |Rg + b| ≤ 2 * W + b + 1 / 2 ^ 200 := by have := abs_add_le Rg b rw [abs_of_nonneg hb] at this linarith apply abs_le.mpr constructor <;> linarith theorem A_sum_mul (ts : List ℕ) (f g : ℕ → ℝ) (H : ℝ) (h : ∀ t ∈ ts, f t ≤ H * g t) : (ts.map f).sum ≤ H * (ts.map g).sum := by induction ts with | nil => simp | cons x ts ih => have h1 := ih (fun t ht => h t (by simp [ht])) have h2 := h x (by simp) simp only [List.map_cons, List.sum_cons, mul_add] linarith /-- The coarse bound for abstract terms: `a` is any finite float within `2 K` half-ulps of `J`, and `H * J` bounds the sum of the term magnitudes. -/ theorem A_coarse_gen (n : ℕ) (hn : n < 2 ^ 20) (ts : List ℕ) (hlen : ts.length ≤ n) (g : ℕ → Float) (p : ℕ → ℝ) (b f a : Float) (bv H J K : ℝ) (hg : ∀ t ∈ ts, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z)) (hAJ : (ts.map fun t => |p t|).sum ≤ H * J) (hbf : b.isFinite = true) (hbv : R b = bv) (hbl : |bv| ≤ 2 ^ 128) (hf : f.isFinite = true) (hH : R f = H) (hH0 : 0 ≤ H) (hHl : H ≤ 2 ^ 128) (hjf : a.isFinite = true) (hJ0 : 0 ≤ J) (hJ2 : J ≤ 2 ^ 149) (hK : K ≤ 2 ^ 20) (hje : |R a - J| ≤ 2 * K * (1 / 2 ^ 53 * (J + A_z))) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n * (f * a + b.abs))|) : R (A_fsum (ts.map g ++ [b]) - nfOf n * (f * a + b.abs)) < (ts.map p).sum + bv ∧ (ts.map p).sum + bv < R (A_fsum (ts.map g ++ [b]) + nfOf n * (f * a + b.abs)) := by have hfq : ∀ q ∈ ts.map g ++ [b], q.isFinite = true := by intro q hq simp only [List.mem_append, List.mem_map, List.mem_singleton] at hq rcases hq with ⟨t, ht, rfl⟩ | rfl · exact (hg t ht).1 · exact hbf obtain ⟨hA1, hA2, hA3⟩ := A_terms ts g p (fun t ht => (hg t ht).2) have hsa := A_sa_snoc (ts.map g) b have hsr := A_sr_snoc (ts.map g) b have hsa0 := A_sa_nonneg (ts.map g ++ [b]) rw [hbv] at hsa hsr have hk : (ts.length : ℝ) ≤ n := by exact_mod_cast hlen have hk0 : (0 : ℝ) ≤ ts.length := Nat.cast_nonneg _ have hnR : (n : ℝ) + 1 ≤ 2 ^ 20 := by exact_mod_cast hn have hz := A_z_pos have hz1 := A_z_le have hz2 : A_z ≤ 1 := by have : (1 / 2 ^ 200 * (1 / 2 ^ 200) : ℝ) ≤ 1 := by norm_num linarith have hA0 : 0 ≤ (ts.map fun t => |p t|).sum := List.sum_nonneg (by simp only [List.mem_map]; rintro _ ⟨t, _, rfl⟩; exact abs_nonneg _) set A := (ts.map fun t => |p t|).sum set k : ℝ := (ts.length : ℝ) have hBl := A_B_lo have hB8 : (1 : ℝ) ≤ A_B / 8 := by have : (1 : ℝ) ≤ 2 ^ 256 * 2 ^ 42 / 8 := by norm_num linarith have hkR : k ≤ 2 ^ 20 := by linarith have hk2 : 2 * K * (1 / 2 ^ 53) ≤ 1 := by norm_num at hK ⊢ linarith have hje2 : |R a - J| ≤ J + 1 := calc |R a - J| ≤ 2 * K * (1 / 2 ^ 53 * (J + A_z)) := hje _ = (2 * K * (1 / 2 ^ 53)) * (J + A_z) := by ring _ ≤ 1 * (J + A_z) := mul_le_mul_of_nonneg_right hk2 (by linarith) _ ≤ J + 1 := by linarith have hRj : |R a| ≤ 2 ^ 151 := by have e : R a = (R a - J) + J := by ring have h1 := abs_add_le (R a - J) J rw [← e, abs_of_nonneg hJ0] at h1 have : (2 : ℝ) * 2 ^ 149 + 1 ≤ 2 ^ 151 := by norm_num linarith have h151 : (2 : ℝ) ^ 151 ≤ A_B := by have : (2 : ℝ) ^ 151 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num linarith obtain ⟨hgf, hge⟩ := A_mulE f a hf hjf (by rw [hH, abs_of_nonneg hH0]; linarith [A_B_128]) (hRj.trans h151) rw [hH] at hge have hHj : |H * R a| ≤ 2 ^ 128 * 2 ^ 151 := by rw [abs_mul, abs_of_nonneg hH0] exact mul_le_mul hHl hRj (abs_nonneg _) (by norm_num) have hgb : |R (f * a)| ≤ A_B := by have e : R (f * a) = (R (f * a) - H * R a) + H * R a := by ring have h1 := abs_add_le (R (f * a) - H * R a) (H * R a) rw [← e] at h1 have : (2 : ℝ) ^ 128 * 2 ^ 151 * 2 + 1 ≤ 2 ^ 256 * 2 ^ 42 := by norm_num have h2 : (1 / 2 ^ 53 : ℝ) * (|H * R a| + A_z) ≤ |H * R a| + 1 := by have := abs_nonneg (H * R a) linarith linarith obtain ⟨haf, hav⟩ := A_abs b hbf rw [hbv] at hav obtain ⟨htf, hte⟩ := A_addE (f * a) b.abs hgf haf hgb (by rw [hav, abs_abs]; linarith [A_B_128]) rw [hav] at hte have key2 := A_real2 H J (R a) (R (f * a)) (R (f * a + b.abs)) |bv| A_z K hH0 hHl hJ0 (abs_nonneg _) hz.le hz1 hK hje hge hte have hHJ : H * J ≤ 2 ^ 128 * 2 ^ 149 := mul_le_mul hHl hJ2 hJ0 (by norm_num) have hHJ0 : 0 ≤ H * J := mul_nonneg hH0 hJ0 have hbv0 := abs_nonneg bv have hkz : k * A_z ≤ 2 ^ 20 * A_z := mul_le_mul_of_nonneg_right hkR hz.le have hYB : H * J + |bv| + A_z ≤ A_B / 8 := by have : (2 : ℝ) ^ 128 * 2 ^ 149 + 2 ^ 128 + 1 ≤ 2 ^ 256 * 2 ^ 42 / 8 := by norm_num linarith have hsaY : A_sa (ts.map g ++ [b]) + A_z ≤ 3 / 2 * (H * J + |bv| + A_z) := by linarith have hlen1 : (ts.map g ++ [b]).length ≤ 2 ^ 21 := by simp; omega obtain ⟨hf1, he1⟩ := A_fsum_spec _ hfq hlen1 (by linarith) have hl1 : (((ts.map g ++ [b]).length : ℕ) : ℝ) = k + 1 := by simp [k] rw [hl1] at he1 have hS1 := A_sr_abs (ts.map g ++ [b]) set Y := H * J + |bv| + A_z with hYdef have hY0 : 0 ≤ Y := by linarith have hUY : 0 ≤ (1 / 2 ^ 53 : ℝ) * Y := mul_nonneg (by norm_num) hY0 have hD1 : 2 * (k + 1) * (1 / 2 ^ 53 * (A_sa (ts.map g ++ [b]) + A_z)) ≤ 3 * (k + 1) * (1 / 2 ^ 53 * Y) := calc 2 * (k + 1) * (1 / 2 ^ 53 * (A_sa (ts.map g ++ [b]) + A_z)) ≤ 2 * (k + 1) * (1 / 2 ^ 53 * (3 / 2 * Y)) := by apply mul_le_mul_of_nonneg_left _ (by linarith) exact mul_le_mul_of_nonneg_left hsaY (by norm_num) _ = _ := by ring have hkY : A + k * A_z ≤ (k + 1) * Y := by have h1 : A + A_z ≤ Y := by linarith have h2 := mul_le_mul_of_nonneg_left h1 (show 0 ≤ k + 1 by linarith) have h3 := mul_nonneg hk0 hA0 linarith have hD2 : (1 / 2 ^ 53 : ℝ) * (A + k * A_z) ≤ (k + 1) * (1 / 2 ^ 53 * Y) := calc (1 / 2 ^ 53 : ℝ) * (A + k * A_z) ≤ 1 / 2 ^ 53 * ((k + 1) * Y) := mul_le_mul_of_nonneg_left hkY (by norm_num) _ = _ := by ring have hn4 : 4 * (k + 1) * (1 / 2 ^ 53 * Y) ≤ 4 * ((n : ℝ) + 1) * (1 / 2 ^ 53 * Y) := mul_le_mul_of_nonneg_right (by linarith) hUY have hK3 : 3 * (k + 1) * (1 / 2 ^ 53 * Y) ≤ Y / 2 := by have h : k + 1 ≤ 2 ^ 20 := by linarith have := mul_le_mul_of_nonneg_right h hUY norm_num at this ⊢ linarith obtain ⟨he1a, he1b⟩ := abs_le.mp he1 obtain ⟨hA1a, hA1b⟩ := abs_le.mp hA1 refine A_final n hn _ _ hf1 htf _ Y hY0 hYB ?_ key2 ?_ hbig · apply abs_le.mpr constructor <;> linarith · have e : R (A_fsum (ts.map g ++ [b])) = (R (A_fsum (ts.map g ++ [b])) - A_sr (ts.map g ++ [b])) + A_sr (ts.map g ++ [b]) := by ring have h1 := abs_add_le (R (A_fsum (ts.map g ++ [b])) - A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b])) rw [← e] at h1 linarith /-- The coarse bound with the running sum of weight magnitudes. -/ theorem A_coarse_core (n : ℕ) (hn : n < 2 ^ 20) (ts : List ℕ) (hlen : ts.length ≤ n) (g v : ℕ → Float) (p a : ℕ → ℝ) (b f : Float) (bv H : ℝ) (hg : ∀ t ∈ ts, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) ∧ |p t| ≤ H * |a t|) (hv : ∀ t ∈ ts, (v t).isFinite = true ∧ R (v t) = a t ∧ |a t| ≤ 2 ^ 128) (hbf : b.isFinite = true) (hbv : R b = bv) (hbl : |bv| ≤ 2 ^ 128) (hf : f.isFinite = true) (hH : R f = H) (hH0 : 0 ≤ H) (hHl : H ≤ 2 ^ 128) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n * (f * A_fsum (ts.map fun t => (v t).abs) + b.abs))|) : R (A_fsum (ts.map g ++ [b]) - nfOf n * (f * A_fsum (ts.map fun t => (v t).abs) + b.abs)) < (ts.map p).sum + bv ∧ (ts.map p).sum + bv < R (A_fsum (ts.map g ++ [b]) + nfOf n * (f * A_fsum (ts.map fun t => (v t).abs) + b.abs)) := by have hkR : (ts.length : ℝ) ≤ 2 ^ 20 := by have : ts.length ≤ 2 ^ 20 := by omega exact_mod_cast this have hJ0 : 0 ≤ (ts.map fun t => |a t|).sum := List.sum_nonneg (by simp only [List.mem_map]; rintro _ ⟨t, _, rfl⟩; exact abs_nonneg _) have hJl := A_sum_le ts (fun t => |a t|) _ (fun t ht => (hv t ht).2.2) have hAJ := A_sum_mul ts (fun t => |p t|) (fun t => |a t|) H (fun t ht => (hg t ht).2.2) have hvl : (ts.map fun t => (v t).abs) = (ts.map v).map Float.abs := by simp [List.map_map, Function.comp_def] have hvf : ∀ q ∈ ts.map v, q.isFinite = true := by intro q hq obtain ⟨t, ht, rfl⟩ := List.mem_map.mp hq exact (hv t ht).1 obtain ⟨hv1, hv2⟩ := A_sa_abs (ts.map v) hvf have hJ1 : A_sa (ts.map v) = (ts.map fun t => |a t|).sum := by simp only [A_sa, List.map_map] congr 1 apply List.map_congr_left intro t ht simp [(hv t ht).2.1] rw [hvl] at hbig ⊢ rw [hJ1] at hv1 hv2 set J := (ts.map fun t => |a t|).sum have hJ2 : J ≤ 2 ^ 20 * 2 ^ 128 := hJl.trans (mul_le_mul_of_nonneg_right hkR (by norm_num)) have hBl := A_B_lo have hlenv : ((ts.map v).map Float.abs).length ≤ 2 ^ 21 := by simp; omega obtain ⟨hjf, hje⟩ := A_fsum_spec ((ts.map v).map Float.abs) (by intro q hq obtain ⟨x, hx, rfl⟩ := List.mem_map.mp hq exact (A_abs x (hvf x hx)).1) hlenv (by rw [hv1] have : (2 : ℝ) ^ 20 * 2 ^ 128 ≤ 2 ^ 256 * 2 ^ 42 / 4 := by norm_num linarith) rw [hv1, hv2] at hje have hl3 : (((ts.map v).map Float.abs).length : ℝ) = (ts.length : ℝ) := by simp rw [hl3] at hje exact A_coarse_gen n hn ts hlen g p b f _ bv H J _ (fun t ht => ⟨(hg t ht).1, (hg t ht).2.1⟩) hAJ hbf hbv hbl hf hH hH0 hHl hjf hJ0 (hJ2.trans (by norm_num)) hkR hje hbig /-- A sign-free good word `hh` bounds the magnitude of every word it bounds as an unsigned number. -/ theorem A_bound (hh : UInt32) (hg : good hh = true) (hp : hh ≤ 0x7FFFFFFF) : 0 ≤ wv hh ∧ wv hh ≤ 2 ^ 128 ∧ ∀ w, good w = true → w &&& 0x7FFFFFFF ≤ hh → |wv w| ≤ wv hh := by have hhN : hh.toNat < 2 ^ 31 := by have := UInt32.le_iff_toNat_le.mp hp change hh.toNat ≤ 2147483647 at this omega have hHeq : wv hh = A_mag hh.toNat := by rw [A_wv_eq hh hg, Nat.mod_eq_of_lt hhN] simp only [show ¬ 2 ^ 31 ≤ hh.toNat by omega, ↓reduceIte, one_mul] refine ⟨by rw [hHeq]; exact A_mag_nonneg _, le_trans (le_abs_self _) (A_wv_le hh hg), ?_⟩ intro w hg1 hm have h1 := UInt32.le_iff_toNat_le.mp hm rw [A_and31] at h1 rw [A_wv_abs _ hg1, hHeq] apply A_mag_mono _ _ h1 rcases A_good _ hg1 with h | h · exact Or.inl h · exact Or.inr h.1 /-- `encl_coarse` with the hypothesis it needs: `hh` carries no sign bit. As stated in `Iface.lean` it fails for `hh = 0x80000000`, which is good and bounds every word as an unsigned number while `dv hh` is `-0`. -/ theorem A_encl_coarse (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw hh : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (hg : good hh = true) (hm : ∀ t, t < n → xw t &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (s e : Float) (hs : let r := (List.range n).foldl (fun (acc : F1) t => let v := dv (ww t) F1.mk (acc.s + v * dv (xw t)) (acc.t + v.abs)) ⟨0, 0⟩ s = r.s + dv bw ∧ e = nfOf n * (dv hh * r.t + (dv bw).abs)) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)|) : R (s - e) < exactS n xw ww bw ∧ exactS n xw ww bw < R (s + e) := by set g : ℕ → Float := fun t => dv (ww t) * dv (xw t) with hgd set p : ℕ → ℝ := fun t => wv (ww t) * wv (xw t) with hpd set v : ℕ → Float := fun t => dv (ww t) with hvd have hfold : (List.range n).foldl (fun (acc : F1) t => let v := dv (ww t) F1.mk (acc.s + v * dv (xw t)) (acc.t + v.abs)) ⟨0, 0⟩ = ⟨((List.range n).map g).foldl (· + ·) 0, ((List.range n).map fun t => (v t).abs).foldl (· + ·) 0⟩ := A_fold g (fun t => (v t).abs) (List.range n) 0 0 obtain ⟨hs1, hs2⟩ := hs rw [hfold] at hs1 hs2 obtain ⟨hH0, hHl, hb1⟩ := A_bound hh hg hp have hle : ∀ t, t < n → |wv (xw t)| ≤ wv hh := fun t ht => hb1 _ (hx t ht) (hm t ht) have hgt : ∀ t ∈ List.range n, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) ∧ |p t| ≤ wv hh * |wv (ww t)| := by intro t ht have ht' := List.mem_range.mp ht obtain ⟨h1, h2, -⟩ := A_term _ _ (hw t ht') (hx t ht') refine ⟨h1, h2, ?_⟩ simp only [hpd, abs_mul] rw [mul_comm] exact mul_le_mul_of_nonneg_right (hle t ht') (abs_nonneg _) have hvt : ∀ t ∈ List.range n, (v t).isFinite = true ∧ R (v t) = wv (ww t) ∧ |wv (ww t)| ≤ 2 ^ 128 := by intro t ht have ht' := List.mem_range.mp ht obtain ⟨h1, h2⟩ := A_dv _ (hw t ht') exact ⟨h1, h2, A_wv_le _ (hw t ht')⟩ obtain ⟨hbf, hbv⟩ := A_dv bw hb obtain ⟨hhf, hhv⟩ := A_dv hh hg have hS : s = A_fsum ((List.range n).map g ++ [dv bw]) := by rw [hs1, A_fsum_snoc]; rfl have hE : e = nfOf n * (dv hh * A_fsum ((List.range n).map fun t => (v t).abs) + (dv bw).abs) := by rw [hs2]; rfl rw [hS, hE] at hbig ⊢ have hX : exactS n xw ww bw = ((List.range n).map p).sum + wv bw := by rw [A_exact_tight n xw ww bw hx (List.range n) List.nodup_range (fun t ht => List.mem_range.mp ht) (fun t ht h => absurd (List.mem_range.mpr ht) h)] simp only [hpd, mul_comm] rw [hX] exact A_coarse_core n hn (List.range n) (by simp) g v p (fun t => wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (A_wv_le bw hb) hhf hhv hH0 hHl hbig /-- The coarse bound for a kept-position list, with the constant `(n + 1) * dv hv` in place of a running sum of weight magnitudes. -/ theorem encl_coarse1 (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw hh hv : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (hg : good hh = true) (hgv : good hv = true) (hm : ∀ t, t < n → xw t &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (hmv : ∀ t, t < n → ww t &&& 0x7FFFFFFF ≤ hv) (hpv : hv ≤ 0x7FFFFFFF) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : s = l.foldl (fun (acc : Float) t => acc + dv (xw t) * dv (ww t)) 0 + dv bw ∧ e = nfOf n * (dv hh * ((n.toUInt64 + 1).toFloat * dv hv) + (dv bw).abs)) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)|) : R (s - e) < exactS n xw ww bw ∧ exactS n xw ww bw < R (s + e) := by set g : ℕ → Float := fun t => dv (xw t) * dv (ww t) with hgd set p : ℕ → ℝ := fun t => wv (xw t) * wv (ww t) with hpd obtain ⟨hs1, hs2⟩ := hs obtain ⟨hH0, hHl, hb1⟩ := A_bound hh hg hp obtain ⟨hV0, hVl, hb2⟩ := A_bound hv hgv hpv obtain ⟨hbf, hbv⟩ := A_dv bw hb obtain ⟨hhf, hhv⟩ := A_dv hh hg obtain ⟨hvf, hvv⟩ := A_dv hv hgv have hu : (n.toUInt64 + 1).toNat = n + 1 := by have h1 : (1 : UInt64).toNat = 1 := rfl simp [UInt64.toNat_add, h1] omega obtain ⟨hcf, hcv⟩ := A_toF64 (n.toUInt64 + 1) (by rw [hu]; omega) rw [hu] at hcv push_cast at hcv have hnR : (n : ℝ) + 1 ≤ 2 ^ 20 := by exact_mod_cast hn have hn0 : (0 : ℝ) ≤ n := Nat.cast_nonneg n have hB28 := A_B_128 have h20 : (2 : ℝ) ^ 20 ≤ 2 ^ 128 := by norm_num obtain ⟨haf, hae⟩ := A_mulE _ _ hcf hvf (by rw [hcv, abs_of_nonneg (by linarith)]; linarith) (by rw [hvv, abs_of_nonneg hV0]; linarith) rw [hcv, hvv] at hae have hJ0 : 0 ≤ ((n : ℝ) + 1) * wv hv := mul_nonneg (by linarith) hV0 rw [abs_of_nonneg hJ0] at hae have hJ2 : ((n : ℝ) + 1) * wv hv ≤ 2 ^ 149 := calc ((n : ℝ) + 1) * wv hv ≤ 2 ^ 20 * 2 ^ 128 := mul_le_mul hnR hVl hV0 (by norm_num) _ ≤ 2 ^ 149 := by norm_num have hAJ : (l.map fun t => |p t|).sum ≤ wv hh * (((n : ℝ) + 1) * wv hv) := by have h1 := A_sum_le l (fun t => |p t|) (wv hh * wv hv) (fun t ht => by have ht' := hl2 t ht simp only [hpd, abs_mul] exact mul_le_mul (hb1 _ (hx t ht') (hm t ht')) (hb2 _ (hw t ht') (hmv t ht')) (abs_nonneg _) hH0) have h2 : (l.length : ℝ) ≤ n := by exact_mod_cast A_len n l hl hl2 have h3 := mul_le_mul_of_nonneg_right (show (l.length : ℝ) ≤ (n : ℝ) + 1 by linarith) (mul_nonneg hH0 hV0) calc _ ≤ (l.length : ℝ) * (wv hh * wv hv) := h1 _ ≤ ((n : ℝ) + 1) * (wv hh * wv hv) := h3 _ = _ := by ring have hgt : ∀ t ∈ l, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) := fun t ht => by obtain ⟨h1, h2, -⟩ := A_term _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) exact ⟨h1, h2⟩ have hS : s = A_fsum (l.map g ++ [dv bw]) := by rw [hs1, A_fsum_snoc] simp [A_fsum, List.foldl_map, hgd] rw [hS, hs2] at hbig ⊢ rw [A_exact_tight n xw ww bw hx l hl hl2 hz] exact A_coarse_gen n hn l (A_len n l hl hl2) g p (dv bw) (dv hh) _ (wv bw) (wv hh) (((n : ℝ) + 1) * wv hv) (1 / 2) hgt hAJ hbf hbv (A_wv_le bw hb) hhf hhv hH0 hHl haf hJ0 hJ2 (by norm_num) (by linarith) hbig /-- The coarse bound for a kept-position list: `hh` bounds the row and `a` is the float sum of the kept weight magnitudes. Positions left out have a zero row entry or a zero weight. -/ theorem encl_coarseZ (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw hh : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (hg : good hh = true) (hm : ∀ t, t < n → xw t &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0 ∨ ww t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : s = l.foldl (fun (acc : Float) t => acc + dv (xw t) * dv (ww t)) 0 + dv bw ∧ e = nfOf n * (dv hh * (l.foldl (fun (acc : Float) t => acc + (dv (ww t)).abs) 0) + (dv bw).abs)) (hbig : (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)|) : R (s - e) < exactS n xw ww bw ∧ exactS n xw ww bw < R (s + e) := by set g : ℕ → Float := fun t => dv (xw t) * dv (ww t) with hgd set p : ℕ → ℝ := fun t => wv (xw t) * wv (ww t) with hpd set v : ℕ → Float := fun t => dv (ww t) with hvd obtain ⟨hs1, hs2⟩ := hs obtain ⟨hH0, hHl, hb1⟩ := A_bound hh hg hp obtain ⟨hbf, hbv⟩ := A_dv bw hb obtain ⟨hhf, hhv⟩ := A_dv hh hg have hgt : ∀ t ∈ l, (g t).isFinite = true ∧ |R (g t) - p t| ≤ 1 / 2 ^ 53 * (|p t| + A_z) ∧ |p t| ≤ wv hh * |wv (ww t)| := by intro t ht have ht' := hl2 t ht obtain ⟨h1, h2, -⟩ := A_term _ _ (hx t ht') (hw t ht') refine ⟨h1, h2, ?_⟩ simp only [hpd, abs_mul] exact mul_le_mul_of_nonneg_right (hb1 _ (hx t ht') (hm t ht')) (abs_nonneg _) have hvt : ∀ t ∈ l, (v t).isFinite = true ∧ R (v t) = wv (ww t) ∧ |wv (ww t)| ≤ 2 ^ 128 := by intro t ht obtain ⟨h1, h2⟩ := A_dv _ (hw t (hl2 t ht)) exact ⟨h1, h2, A_wv_le _ (hw t (hl2 t ht))⟩ have hS : s = A_fsum (l.map g ++ [dv bw]) := by rw [hs1, A_fsum_snoc] simp [A_fsum, List.foldl_map, hgd] have hE : e = nfOf n * (dv hh * A_fsum (l.map fun t => (v t).abs) + (dv bw).abs) := by rw [hs2] simp [A_fsum, List.foldl_map, hvd] rw [hS, hE] at hbig ⊢ rw [A_exact_Z n xw ww bw hx hw l hl hl2 hz] exact A_coarse_core n hn l (A_len n l hl hl2) g v p (fun t => wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (A_wv_le bw hb) hhf hhv hH0 hHl hbig end /-! `A_dvB`: the arm64 lane shape `(v * 2^e) * mantissa` equals `v * (mantissa * 2^e)`. Both scalings are exact (`A_pw_ex`), so a nonzero result is one rounding of one value (`A_mul_eq`) and a zero result has the product of the signs (`A_mul_z`, `A_sg_mul`). Uses `A.lean`; goes right after it. -/ section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model.UnpackedFloat (Sign) /-- The real sign of a model sign. -/ noncomputable def A_r (s : Sign) : ℝ := if Model.modelSignBit s then -1 else 1 theorem A_r_mul (s t : Sign) : A_r (s * t) = A_r s * A_r t := by cases s <;> cases t <;> first | rfl | simp [A_r, Model.modelSignBit] theorem A_r_inj (S T : Sign) (X Y : ℝ) (hX : 0 < X) (hY : 0 < Y) (h : A_r S * X = A_r T * Y) : S = T ∧ X = Y := by cases S <;> cases T <;> simp [A_r, Model.modelSignBit] at h ⊢ <;> linarith theorem A_val (f : Float) (s m e hm) (u : f.toModel.unpack = .finite s m e hm) : 0 < (m : ℝ) * 2 ^ e ∧ R f = A_r s * ((m : ℝ) * 2 ^ e) := by have : 0 < (m : ℝ) := by exact_mod_cast hm refine ⟨by positivity, ?_⟩ rw [A_R_unpack, u, Model.unpackedToReal_finite, A_bpow, mul_assoc, A_r] /-- A nonzero value comes from a finite unpacked float. -/ theorem A_fin_ne (f : Float) (h : R f ≠ 0) : ∃ s m e hm, f.toModel.unpack = .finite s m e hm ∧ R f = A_r s * ((m : ℝ) * 2 ^ e) := by rw [A_R_unpack] at h cases hu : f.toModel.unpack with | finite s m e hm => exact ⟨s, m, e, hm, rfl, (A_val f s m e hm hu).2⟩ | zero s => rw [hu] at h; simp at h | notANumber => rw [hu] at h; simp [Model.unpackedToReal, Model.unpackedToDyadic?] at h | infinity s => rw [hu] at h; simp [Model.unpackedToReal, Model.unpackedToDyadic?] at h theorem A_mul_round (a b : Float) (s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂) (u₁ : a.toModel.unpack = .finite s₁ m₁ e₁ h₁) (u₂ : b.toModel.unpack = .finite s₂ m₂ e₂ h₂) : a * b = .ofModel (Float.Model.pack (Float.Model.UnpackedFloat.round Float.Model.Format.binary64 (s₁ * s₂) (m₁ * m₂) (e₁ + e₂))) := by rw [A_round_eq _ _ _ _ (A_hle _ _ _ _ _ _ h₁ h₂ _ _ u₁ u₂)] change Float.ofModel (Float.Model.mul a.toModel b.toModel) = _ rw [Float.Model.mul, u₁, u₂] rfl /-- Rounding depends only on the value. -/ theorem A_round_le (s : Sign) (m n : ℕ) (e f : ℤ) (hm : m ≠ 0) (hle : f ≤ e) (h : (m : ℝ) * 2 ^ e = n * 2 ^ f) : Float.Model.UnpackedFloat.round Float.Model.Format.binary64 s m e = Float.Model.UnpackedFloat.round Float.Model.Format.binary64 s n f := by obtain ⟨k, rfl⟩ : ∃ k : ℕ, e = f + k := ⟨(e - f).toNat, by omega⟩ have hn : n = m <<< k := by rw [Nat.shiftLeft_eq] have h2 : (2 : ℝ) ^ f ≠ 0 := by positivity rw [zpow_add₀ two_ne_zero, zpow_natCast, mul_comm ((2 : ℝ) ^ f), ← mul_assoc] at h exact_mod_cast (mul_right_cancel₀ h2 h).symm rw [hn, ← Model.round_shiftLeft _ _ m k (f + k) hm, add_sub_cancel_right] /-- A nonzero product depends only on its value. -/ theorem A_mul_eq (a b c d : Float) (h : R a * R b = R c * R d) (h0 : R a * R b ≠ 0) : a * b = c * d := by have h1 := h0 rw [h] at h1 obtain ⟨s₁, m₁, e₁, p₁, u₁, r₁⟩ := A_fin_ne a (left_ne_zero_of_mul h0) obtain ⟨s₂, m₂, e₂, p₂, u₂, r₂⟩ := A_fin_ne b (right_ne_zero_of_mul h0) obtain ⟨s₃, m₃, e₃, p₃, u₃, r₃⟩ := A_fin_ne c (left_ne_zero_of_mul h1) obtain ⟨s₄, m₄, e₄, p₄, u₄, r₄⟩ := A_fin_ne d (right_ne_zero_of_mul h1) rw [A_mul_round _ _ _ _ _ _ _ _ _ _ u₁ u₂, A_mul_round _ _ _ _ _ _ _ _ _ _ u₃ u₄] have hm : ∀ (m n : ℕ) (e f : ℤ), 0 < m → 0 < n → 0 < ((m * n : ℕ) : ℝ) * 2 ^ (e + f) := by intro m n e f h1 h2 have : 0 < m * n := Nat.mul_pos h1 h2 positivity have hx : ∀ (s t : Sign) (m n : ℕ) (e f : ℤ), A_r (s * t) * (((m * n : ℕ) : ℝ) * 2 ^ (e + f)) = A_r s * ((m : ℝ) * 2 ^ e) * (A_r t * ((n : ℝ) * 2 ^ f)) := by intro s t m n e f rw [A_r_mul, zpow_add₀ two_ne_zero] push_cast ring obtain ⟨hS, hX⟩ := A_r_inj _ _ _ _ (hm m₁ m₂ e₁ e₂ p₁ p₂) (hm m₃ m₄ e₃ e₄ p₃ p₄) (by rw [hx, hx, ← r₁, ← r₂, ← r₃, ← r₄, h]) rw [hS] congr 2 rcases le_total (e₁ + e₂) (e₃ + e₄) with hl | hl · exact (A_round_le _ _ _ _ _ (Nat.mul_pos p₃ p₄).ne' hl hX.symm).symm · exact A_round_le _ _ _ _ _ (Nat.mul_pos p₁ p₂).ne' hl hX /-- The sign of a finite float. -/ def A_sg (f : Float) : Sign := match f.toModel.unpack with | .zero s => s | .finite s .. => s | _ => .positive /-- The packed zero of a sign. -/ def A_Z (s : Sign) : Float := .ofModel (Float.Model.pack (.zero s)) theorem A_Z_u (s : Sign) : (A_Z s).toModel.unpack = .zero s := Model.unpack_pack_zero _ _ /-- A product with a zero factor is the zero with the product of the signs. -/ theorem A_mul_z (f g : Float) (hf : f.isFinite = true) (hg : g.isFinite = true) (h : R f * R g = 0) : f * g = A_Z (A_sg f * A_sg g) := by change f.toModel.unpack.isFinite = true at hf change g.toModel.unpack.isFinite = true at hg change Float.ofModel (Float.Model.mul f.toModel g.toModel) = _ rw [Float.Model.mul, A_Z, A_sg, A_sg] cases hu : f.toModel.unpack <;> cases hv : g.toModel.unpack <;> simp only [hu, hv, Float.Model.UnpackedFloat.isFinite, Bool.false_eq_true] at hf hg all_goals try rfl obtain ⟨p₁, r₁⟩ := A_val f _ _ _ _ hu obtain ⟨p₂, r₂⟩ := A_val g _ _ _ _ hv rw [r₁, r₂] at h have := fun s (x : ℝ) (hx : 0 < x) => show A_r s * x ≠ 0 by cases s <;> simp [A_r, Model.modelSignBit, hx.ne'] exact absurd h (mul_ne_zero (this _ _ p₁) (this _ _ p₂)) theorem A_sg_mul (f g : Float) (hf : f.isFinite = true) (hg : g.isFinite = true) (h : R (f * g) = R f * R g) : A_sg (f * g) = A_sg f * A_sg g := by by_cases h0 : R f * R g = 0 · rw [A_mul_z f g hf hg h0, A_sg, A_Z_u] obtain ⟨s₁, m₁, e₁, p₁, u₁, r₁⟩ := A_fin_ne f (left_ne_zero_of_mul h0) obtain ⟨s₂, m₂, e₂, p₂, u₂, r₂⟩ := A_fin_ne g (right_ne_zero_of_mul h0) obtain ⟨s₃, m₃, e₃, p₃, u₃, r₃⟩ := A_fin_ne (f * g) (h ▸ h0) simp only [A_sg, u₁, u₂, u₃] refine (A_r_inj _ _ _ _ (A_val _ _ _ _ _ u₃).1 (mul_pos (A_val _ _ _ _ _ u₁).1 (A_val _ _ _ _ _ u₂).1) ?_).1 rw [← r₃, h, A_r_mul, r₁, r₂] ring /-- Two exact products by `p` associate, whatever the signs of zero. -/ theorem A_assoc (v m p : Float) (hv : v.isFinite = true) (hm : m.isFinite = true) (hp : p.isFinite = true) (f1 : (v * p).isFinite = true) (f2 : (m * p).isFinite = true) (h1 : R (v * p) = R v * R p) (h2 : R (m * p) = R m * R p) : v * p * m = v * (m * p) := by by_cases h0 : R v * R p * R m = 0 · rw [A_mul_z _ _ f1 hm (by rw [h1]; exact h0), A_mul_z _ _ hv f2 (by rw [h2]; linear_combination h0), A_sg_mul _ _ hv hp h1, A_sg_mul _ _ hm hp h2] cases A_sg v <;> cases A_sg p <;> cases A_sg m <;> rfl · exact A_mul_eq _ _ _ _ (by rw [h1, h2]; ring) (by rw [h1]; exact h0) /-- Scaling by a table entry is exact on a short mantissa. -/ theorem A_pw_ex (f : Float) (i : ℕ) (hi : i < 512) (ff : f.isFinite = true) (c k : ℤ) (hc : c.natAbs < 2 ^ 53) (hk : -149 ≤ k) (hb : |R f| ≤ A_B) (hv : R f = c * 2 ^ k) : (f * pwEntry i).isFinite = true ∧ R (f * pwEntry i) = R f * R (pwEntry i) := by obtain ⟨fp, rp⟩ := A_pw i hi have hj : ((i % 256 : ℕ) : ℤ) - 150 ≤ 106 := by omega have hj2 : (2 : ℝ) ^ (((i % 256 : ℕ) : ℤ) - 150) ≤ A_B := by refine (zpow_le_zpow_right₀ (by norm_num) hj).trans (le_trans ?_ A_B_128) norm_num have hB : |R (pwEntry i)| ≤ A_B := by rw [rp] split_ifs · simpa using A_B_pos.le all_goals simp only [abs_mul, abs_neg, abs_one, one_mul] rwa [abs_of_pos (by positivity)] obtain ⟨hf, he⟩ := A_mul f _ ff fp ((mul_le_mul hb hB (abs_nonneg _) A_B_pos.le).trans A_B_sq) refine ⟨hf, ?_⟩ have key : ∀ σ : ℤ, σ.natAbs ≤ 1 → Model.roundAt FloatFormat.binary64 ((c : ℝ) * 2 ^ k * (σ * 2 ^ (((i % 256 : ℕ) : ℤ) - 150))) = (c : ℝ) * 2 ^ k * (σ * 2 ^ (((i % 256 : ℕ) : ℤ) - 150)) := by intro σ hσ have hn : (c * σ).natAbs < 2 ^ 53 := by rw [Int.natAbs_mul] have := Nat.mul_le_mul_left c.natAbs hσ omega have := A_round_exact (c * σ) (k + (((i % 256 : ℕ) : ℤ) - 150)) hn (by omega) rw [zpow_add₀ two_ne_zero] at this push_cast at this rwa [show (c : ℝ) * 2 ^ k * (σ * 2 ^ (((i % 256 : ℕ) : ℤ) - 150)) = c * σ * (2 ^ k * 2 ^ (((i % 256 : ℕ) : ℤ) - 150)) by ring] rw [he, rp, hv] split_ifs · simp · simpa using key (-1) (by decide) · simpa using key 1 (by decide) theorem A_wv_form (w : UInt32) (hg : good w = true) : ∃ c k : ℤ, c.natAbs < 2 ^ 53 ∧ -149 ≤ k ∧ wv w = c * 2 ^ k := by rw [A_wv_eq w hg] rcases A_good w hg with h | ⟨h1, h2⟩ · exact ⟨0, 0, by norm_num, by norm_num, by rw [h]; simp [A_mag]⟩ have h0 : ¬ w.toNat % 2 ^ 31 = 0 := by omega refine ⟨(if 2 ^ 31 ≤ w.toNat then -1 else 1) * ((2 ^ 23 + w.toNat % 2 ^ 31 % 2 ^ 23 : ℕ) : ℤ), ((w.toNat % 2 ^ 31 / 2 ^ 23 : ℕ) : ℤ) - 150, ?_, by omega, ?_⟩ · rw [Int.natAbs_mul, Int.natAbs_natCast] split_ifs <;> simp only [Int.natAbs_neg, Int.natAbs_one] <;> omega · simp only [A_mag, h0, ↓reduceIte] generalize 2 ^ 23 + w.toNat % 2 ^ 31 % 2 ^ 23 = M split_ifs <;> push_cast <;> ring /-- The arm64 lane shape equals the product of the two decoded words. -/ theorem A_dvB (x w : UInt32) (hx : good x = true) : dvB (dv x) w = dv x * dv w := by obtain ⟨fv, rv⟩ := A_dv x hx obtain ⟨c, k, hc, hk, hf⟩ := A_wv_form x hx have hN := w.toNat_lt have hM := A_mant w obtain ⟨fm, rm⟩ := A_toF32 ((w &&& 0x7FFFFF) ||| 0x800000) (by rw [hM]; omega) have hI : (w >>> (23 : UInt32)).toUSize.toNat = w.toNat / 2 ^ 23 := by rw [UInt32.toNat_toUSize, A_shr] have hi : w.toNat / 2 ^ 23 < 512 := by omega have hd : dv w = ((w &&& 0x7FFFFF) ||| 0x800000).toFloat * pw.uget (w >>> (23 : UInt32)).toUSize (pw_idx w) := rfl unfold dvB rw [hd, A_pw_get, hI] rw [hM] at rm obtain ⟨f1, r1⟩ := A_pw_ex (dv x) _ hi fv c k hc hk (by rw [rv]; exact (A_wv_le x hx).trans A_B_128) (rv.trans hf) obtain ⟨f2, r2⟩ := A_pw_ex ((w &&& 0x7FFFFF) ||| 0x800000).toFloat _ hi fm ((2 ^ 23 + w.toNat % 2 ^ 23 : ℕ) : ℤ) 0 (by omega) (by norm_num) (by rw [rm, abs_of_nonneg (Nat.cast_nonneg _)] have h1 : ((2 ^ 23 + w.toNat % 2 ^ 23 : ℕ) : ℝ) ≤ ((2 ^ 24 : ℕ) : ℝ) := Nat.cast_le.mpr (by omega) have h2 : ((2 ^ 24 : ℕ) : ℝ) ≤ 2 ^ 128 := by norm_num linarith [A_B_128]) (by rw [rm, zpow_zero, mul_one, Int.cast_natCast]) exact A_assoc _ _ _ fv fm (A_pw _ hi).1 f1 f2 r1 r2 end /-! # Part B: `ziv` decides correctly `ziv_big`: if `ziv` decides, both ends are finite and the upper one is at least `2^-127` in magnitude. `ziv_dot`: if it decides and the exact value lies strictly between the two ends, the word is the spec's dot product. The proof works on magnitude codes. A double's magnitude code `M` (exponent field and fraction) and a binary32 code `(ex, fr)` meet at `(ex · 2^23 + fr) · 2^29`, and `ziv` deciding says both ends lie within `2^28` of that point (`B_near`). Codes are uniform across a binade boundary, so the narrower lower half-gap at a power of two needs no special case on the double side. `B_near_val` turns the codes into real bounds, `B_word` turns real bounds into the rounded word, and `B_dot` gives the FloatLib side. -/ section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary /-! ## Rounding a value that lies strictly inside a binary32 rounding interval -/ section open Float.Model Float.Model.UnpackedFloat theorem B_shift (m K : Nat) : (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K + 1)) = ⟨m / 2 ^ (K + 1), (m / 2 ^ K) % 2 != 0, decide (m % 2 ^ K ≠ 0)⟩ := by show Nat.repeat ExtendedMantissa.shiftRightOne (K + 1) _ = _ induction K with | zero => simp [Nat.repeat, ExtendedMantissa.shiftRightOne, ExtendedMantissa.ofMantissaAndAccuracy, Nat.mod_one] | succ K ih => show ExtendedMantissa.shiftRightOne (Nat.repeat _ (K + 1) _) = _ rw [ih] simp only [ExtendedMantissa.shiftRightOne, ExtendedMantissa.mk.injEq] refine ⟨?_, ?_, ?_⟩ · rw [Nat.div_div_eq_div_mul, ← Nat.pow_succ] · trivial · rw [Nat.mod_pow_succ] have h : 0 < 2 ^ K := by positivity by_cases h1 : m % 2 ^ K = 0 <;> by_cases h2 : m / 2 ^ K % 2 = 0 <;> simp [h1, h2] omega theorem B_divmod (H a b : Nat) (hb : b < H) : (H * a + b) / H = a ∧ (H * a + b) % H = b := by have hH : 0 < H := by omega constructor · rw [Nat.mul_add_div hH, Nat.div_eq_of_lt hb, Nat.add_zero] · rw [Nat.mul_add_mod, Nat.mod_eq_of_lt hb] /-- Strictly inside the half-integer interval around `r`, the mantissa rounds to `r`. -/ theorem B_rounded (m K r : Nat) (h1 : 2 * r * 2 ^ K < m + 2 ^ K) (h2 : m < (2 * r + 1) * 2 ^ K) : (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K + 1)).roundedMantissa = r := by rw [B_shift, Nat.pow_succ] have hH : 0 < 2 ^ K := by positivity generalize 2 ^ K = H at * have hdm := Nat.div_add_mod m (H * 2) have hlt : m % (H * 2) < H * 2 := Nat.mod_lt _ (by omega) generalize m / (H * 2) = q at * generalize m % (H * 2) = t at * subst hdm have hq : q = r ∨ q + 1 = r := by rcases Nat.lt_trichotomy q r with h | h | h · right by_contra hne have : q + 2 ≤ r := by omega have h3 : (q + 2) * H ≤ r * H := Nat.mul_le_mul_right H this nlinarith · exact Or.inl h · exfalso have h3 : (r + 1) * H ≤ q * H := Nat.mul_le_mul_right H h nlinarith rcases hq with rfl | rfl · have ht : t < H := by nlinarith obtain ⟨d1, d2⟩ := B_divmod H (2 * q) t ht rw [show H * 2 * q + t = H * (2 * q) + t by ring, d1, d2] by_cases h0 : t = 0 <;> simp [ExtendedMantissa.roundedMantissa, ExtendedMantissa.accuracy, Accuracy.roundToNearestEven, h0] · have ht : H < t := by nlinarith obtain ⟨t', rfl⟩ : ∃ t', t = H + t' := ⟨t - H, by omega⟩ have ht' : t' < H := by omega obtain ⟨d1, d2⟩ := B_divmod H (2 * q + 1) t' ht' rw [show H * 2 * q + (H + t') = H * (2 * q + 1) + t' by ring, d1, d2] have h0 : t' ≠ 0 := by omega have h2' : (2 * q + 1) % 2 = 1 := by omega simp [ExtendedMantissa.roundedMantissa, ExtendedMantissa.accuracy, Accuracy.roundToNearestEven, h0, h2'] theorem B_tgt (t : Int) : Format.binary32.targetExponent t = max (t - 24) (-149) := by simp [Format.targetExponent, Format.mantissaBits, Format.minExponent] theorem B_log2 (m L : Nat) (h1 : 2 ^ L ≤ m) (h2 : m < 2 ^ (L + 1)) : m.log2 = L := by have hne : m ≠ 0 := by have : 0 < 2 ^ L := by positivity omega apply le_antisymm · have := (Nat.log2_lt hne).2 h2; omega · exact (Nat.le_log2 hne).2 h1 /-- One rounding at target `T`, with the mantissa `r` that `B_rounded` finds. -/ theorem B_round_core (s : Sign) (m : Nat) (e T : Int) (K r : Nat) (hT : Format.binary32.targetExponent (totalExponent m e) = T) (hK : T = e + (K + 1 : Nat)) (h1 : 2 * r * 2 ^ K < m + 2 ^ K) (h2 : m < (2 * r + 1) * 2 ^ K) : UnpackedFloat.round Format.binary32 s m e = let p := shiftToTargetExponent Format.binary32 r T .exact if h : p.1.mantissa = 0 then .zero s else .finite s p.1.mantissa p.2 (Nat.pos_of_ne_zero h) := by unfold UnpackedFloat.round decreaseExponent roundWithAccuracy simp only [hT] have h0 : (e - T).toNat = 0 := by omega have hs : shiftToTargetExponent Format.binary32 m e .exact = (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K + 1), T) := by unfold shiftToTargetExponent shiftToExponent simp only [hT] have h3 : (T - e).toNat = K + 1 := by omega rw [h3, hK] simp only [h0, Nat.shiftLeft_zero, Int.natCast_zero, Int.sub_zero, hs, B_rounded m K r h1 h2] theorem B_final (r : Nat) (T : Int) (h1 : 2 ^ 23 ≤ r) (h2 : r < 2 ^ 24) (hT : -149 ≤ T) : shiftToTargetExponent Format.binary32 r T .exact = (ExtendedMantissa.ofMantissaAndAccuracy r .exact, T) := by have hl := B_log2 r 23 h1 h2 unfold shiftToTargetExponent shiftToExponent simp only [totalExponent, hl, B_tgt] have h3 : max (((23 : Nat) : Int) + 1 + T - 24) (-149) = T := by omega simp only [h3, sub_self, Int.toNat_zero, Int.natCast_zero, Int.add_zero] rfl theorem B_final' (T : Int) (hT : -150 ≤ T) : shiftToTargetExponent Format.binary32 (2 ^ 24) T .exact = (ExtendedMantissa.ofMantissaAndAccuracy (2 ^ 23) .exact, T + 1) := by have hl : (2 ^ 24 : Nat).log2 = 24 := B_log2 _ 24 (by norm_num) (by norm_num) unfold shiftToTargetExponent shiftToExponent simp only [totalExponent, hl, B_tgt] have h3 : max (((24 : Nat) : Int) + 1 + T - 24) (-149) - T = 1 := by omega rw [h3] rfl theorem B_fin_n (s : Sign) (P : Nat) (E : Int) (hP1 : 2 ^ 23 ≤ P) (hP2 : P < 2 ^ 24) (hE : -149 ≤ E) (hp : 0 < P) : (let p := shiftToTargetExponent Format.binary32 P E .exact if h : p.1.mantissa = 0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (Nat.pos_of_ne_zero h)) = .finite s P E hp := by have hne : P ≠ 0 := by omega simp only [B_final P E hP1 hP2 hE, ExtendedMantissa.ofMantissaAndAccuracy, hne, dite_false] theorem B_fin_c (s : Sign) (E : Int) (hE : -148 ≤ E) (hp : 0 < 2 ^ 23) : (let p := shiftToTargetExponent Format.binary32 (2 ^ 24) (E - 1) .exact if h : p.1.mantissa = 0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (Nat.pos_of_ne_zero h)) = .finite s (2 ^ 23) E hp := by have hne : (2 ^ 23 : Nat) ≠ 0 := by omega simp only [B_final' (E - 1) (by omega), ExtendedMantissa.ofMantissaAndAccuracy, hne, dite_false, sub_add_cancel] /-- The rounding interval of `P · 2^E`, as natural-number inequalities for `m` at `e = E - g - 2`. The lower half-gap is half as wide at a power of two. -/ theorem B_round_main (s : Sign) (m : Nat) (e : Int) (P : Nat) (E : Int) (g : Nat) (hp : 0 < P) (hP1 : 2 ^ 23 ≤ P) (hP2 : P < 2 ^ 24) (hE : -149 ≤ E) (hg : e + (g + 2 : Nat) = E) (hhi : 2 * m < (2 * P + 1) * 2 ^ (g + 2)) (hlo1 : P ≠ 2 ^ 23 → 2 * P * 2 ^ (g + 2) < 2 * m + 2 ^ (g + 2)) (hlo2 : P = 2 ^ 23 → 33554431 * 2 ^ (g + 2) < 4 * m) : UnpackedFloat.round Format.binary32 s m e = .finite s P E hp := by have e1 : 2 ^ (g + 2) = 4 * 2 ^ g := by rw [Nat.pow_add]; ring have e2 : 2 ^ (g + 1) = 2 * 2 ^ g := by rw [Nat.pow_succ]; ring have e3 : 2 ^ (g + 25) = 2 ^ 25 * 2 ^ g := by rw [Nat.pow_add]; ring have e4 : 2 ^ (g + 25 + 1) = 2 ^ 26 * 2 ^ g := by rw [Nat.pow_add, Nat.pow_add]; ring have e5 : 2 ^ (g + 24) = 2 ^ 24 * 2 ^ g := by rw [Nat.pow_add]; ring have hG : 0 < 2 ^ g := by positivity rw [e1] at hhi by_cases hc : 2 ^ (g + 25) ≤ m · -- `m` is in the binade of `P · 2^E`: target `E`, mantissa `P`. have hL : m.log2 = g + 25 := by apply B_log2 m _ hc rw [e4] have : (2 * P + 1) * (4 * 2 ^ g) ≤ (2 ^ 25 - 1) * (4 * 2 ^ g) := Nat.mul_le_mul_right _ (by omega) omega have hT : Format.binary32.targetExponent (totalExponent m e) = E := by simp only [totalExponent, hL, B_tgt]; omega rw [B_round_core s m e E (g + 1) P hT (by omega) ?_ ?_] · exact B_fin_n s P E hP1 hP2 hE hp · rw [e2] by_cases hP : P = 2 ^ 23 · subst hP; rw [e3] at hc; omega · have := hlo1 hP; rw [e1] at this; nlinarith · rw [e2]; nlinarith · -- Just below a power of two. have hP : P = 2 ^ 23 := by by_contra hne have h1 := hlo1 hne rw [e1] at h1 have h2 : 2 * (2 ^ 23 + 1) * (4 * 2 ^ g) ≤ 2 * P * (4 * 2 ^ g) := Nat.mul_le_mul_right _ (by omega) rw [e3] at hc omega have h1 := hlo2 hP subst hP rw [e1] at h1 rw [e3] at hc have hL : m.log2 = g + 24 := by apply B_log2 m _ (by rw [e5]; omega) rw [show g + 24 + 1 = g + 25 from rfl, e3]; omega by_cases hE' : -148 ≤ E · have hT : Format.binary32.targetExponent (totalExponent m e) = E - 1 := by simp only [totalExponent, hL, B_tgt]; omega rw [B_round_core s m e (E - 1) g (2 ^ 24) hT (by omega) (by omega) (by omega)] exact B_fin_c s E hE' hp · have hE2 : E = -149 := by omega have hT : Format.binary32.targetExponent (totalExponent m e) = E := by simp only [totalExponent, hL, B_tgt]; omega rw [B_round_core s m e E (g + 1) (2 ^ 23) hT (by omega) (by rw [e2]; omega) (by rw [e2]; omega)] exact B_fin_n s _ E (by norm_num) (by norm_num) hE hp theorem B_roundDyadic_eq (neg : Bool) (m : Nat) (e : Int) : Model.roundDyadic Fmt ⟨neg, m, e⟩ = Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e) := by simp [Model.roundDyadic, Model.ieeeRoundDyadic] rfl theorem B_sign_bit (s : Sign) : s.toBitVec.toNat = if s = .negative then 1 else 0 := by cases s <;> rfl theorem B_pack_normal (s : Sign) (M : Nat) (E : Int) (h : 0 < M) (hM1 : 2 ^ 23 ≤ M) (hM2 : M < 2 ^ 24) (hb1 : 1 ≤ E + 150) (hb2 : E + 150 ≤ 254) : (UnpackedFloat.pack Format.binary32 (.finite s M E h)).toNat = (if s = .negative then 2 ^ 31 else 0) + (E + 150).toNat * 2 ^ 23 + M % 2 ^ 23 := by have hl : M.log2 = 23 := B_log2 M 23 hM1 hM2 simp only [UnpackedFloat.pack, hl] have hbias : (E + ↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat = (E + 150).toNat := by simp only [Format.exponentBias]; norm_num; ring_nf rw [hbias] have hno : ¬ (2 ^ Format.binary32.exponentBits ≤ (E + 150).toNat + 1) := by show ¬ (2 ^ 8 ≤ (E + 150).toNat + 1); omega simp only [hno, ite_false] have hnorm : (23 + 1 = Format.binary32.mantissaBits) := rfl simp only [hnorm, ite_true, packComponents, BitVec.toNat_append, B_sign_bit, BitVec.toNat_ofNat] have h8 : (E + 150).toNat % 2 ^ 8 = (E + 150).toNat := Nat.mod_eq_of_lt (by omega) rw [h8, ← Nat.shiftLeft_add_eq_or_of_lt (by omega : (E + 150).toNat < 2 ^ 8), ← Nat.shiftLeft_add_eq_or_of_lt (Nat.mod_lt _ (by positivity) : M % 2 ^ 23 < 2 ^ 23)] split_ifs <;> simp [Nat.shiftLeft_eq] omega /-- `P · 2^E` is the word of every dyadic strictly inside its rounding interval. -/ theorem B_word (neg : Bool) (m : Nat) (e : Int) (P : Nat) (E : Int) (hm : m ≠ 0) (hP1 : 2 ^ 23 ≤ P) (hP2 : P < 2 ^ 24) (hE : -149 ≤ E) (hE2 : E + 150 ≤ 254) (hhi : 2 * ((m : ℝ) * 2 ^ e) < (2 * P + 1) * 2 ^ E) (hlo1 : P ≠ 2 ^ 23 → (2 * P - 1) * 2 ^ E < 2 * ((m : ℝ) * 2 ^ e)) (hlo2 : P = 2 ^ 23 → 33554431 * 2 ^ E < 4 * ((m : ℝ) * 2 ^ e)) : toWord (Model.roundDyadic Fmt ⟨neg, m, e⟩) = UInt32.ofNat ((if neg then 2 ^ 31 else 0) + (E + 150).toNat * 2 ^ 23 + P % 2 ^ 23) := by have hp : 0 < P := by omega obtain ⟨k, hk⟩ : ∃ k : Nat, e - E + 2 ≤ k := ⟨(e - E + 2).toNat, by omega⟩ obtain ⟨g, hg⟩ : ∃ g : Nat, (e - k) + (g + 2 : Nat) = E := ⟨(E - (e - k) - 2).toNat, by omega⟩ have hb : (0 : ℝ) < 2 ^ (e - k) := zpow_pos (by norm_num) _ have hV : (m : ℝ) * 2 ^ e = ((m * 2 ^ k : Nat) : ℝ) * 2 ^ (e - k) := by push_cast rw [mul_assoc, ← zpow_natCast, ← zpow_add₀ two_ne_zero] congr 2; ring have hEE : (2 : ℝ) ^ E = ((2 ^ (g + 2) : Nat) : ℝ) * 2 ^ (e - k) := by push_cast rw [← zpow_natCast, ← zpow_add₀ two_ne_zero, ← hg] congr 1; push_cast; ring rw [hV, hEE] at hhi have h1 : 2 * (m * 2 ^ k) < (2 * P + 1) * 2 ^ (g + 2) := by have : (2 : ℝ) * ((m * 2 ^ k : Nat) : ℝ) < (2 * P + 1) * ((2 ^ (g + 2) : Nat) : ℝ) := lt_of_mul_lt_mul_right (by linarith) hb.le exact_mod_cast this have h2 : P ≠ 2 ^ 23 → 2 * P * 2 ^ (g + 2) < 2 * (m * 2 ^ k) + 2 ^ (g + 2) := by intro hne have h3 := hlo1 hne rw [hV, hEE] at h3 have : (2 : ℝ) * P * ((2 ^ (g + 2) : Nat) : ℝ) < 2 * ((m * 2 ^ k : Nat) : ℝ) + ((2 ^ (g + 2) : Nat) : ℝ) := lt_of_mul_lt_mul_right (by linarith) hb.le exact_mod_cast this have h3 : P = 2 ^ 23 → 33554431 * 2 ^ (g + 2) < 4 * (m * 2 ^ k) := by intro hne have h3 := hlo2 hne rw [hV, hEE] at h3 have : (33554431 : ℝ) * ((2 ^ (g + 2) : Nat) : ℝ) < 4 * ((m * 2 ^ k : Nat) : ℝ) := lt_of_mul_lt_mul_right (by linarith) hb.le exact_mod_cast this rw [B_roundDyadic_eq, ← ExactRuntime.round_scale _ _ m e k hm, B_round_main _ (m * 2 ^ k) (e - k) P E g hp hP1 hP2 hE hg h1 h2 h3] simp only [toWord, Model.toNatBits, Model.ofModel, Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat = _ rw [B_pack_normal _ _ _ _ hP1 hP2 (by omega) (by omega)] cases neg <;> simp [Model.modelSign] end /-! ## The real value of a double, from its bits -/ abbrev B_D := ExecFloat.Binary.format (exponentBits := 11) (fractionBits := 52) theorem B_exp (f : Float) : Model.expField (toModel (ofFloat f)) = f.toBits.toNat / 2 ^ 52 % 2 ^ 11 := by show ((f.toBits.toBitVec >>> 52) &&& BitVec.ofNat 64 2047).toNat = _ rw [BitVec.toNat_and, BitVec.toNat_ushiftRight, BitVec.toNat_ofNat, Nat.shiftRight_eq_div_pow] exact Nat.and_two_pow_sub_one_eq_mod _ 11 theorem B_frac (f : Float) : Model.fracField (toModel (ofFloat f)) = f.toBits.toNat % 2 ^ 52 := by show (f.toBits.toBitVec &&& BitVec.ofNat 64 (2 ^ 52 - 1)).toNat = _ rw [BitVec.toNat_and, BitVec.toNat_ofNat] exact Nat.and_two_pow_sub_one_eq_mod _ 52 theorem B_sgn (f : Float) : Model.signBit (toModel (ofFloat f)) = decide (2 ^ 63 ≤ f.toBits.toNat) := by rw [Model.signBit_eq_msb] show f.toBits.toBitVec.msb = _ rw [BitVec.msb_eq_decide] rfl /-- A finite normal double, read from its fields. -/ theorem B_dy (f : Float) (s : Bool) (E F : Nat) (hE1 : 1 ≤ E) (hE : E < 2047) (hF : F < 2 ^ 52) (h : f.toBits.toNat = (if s then 2 ^ 63 else 0) + E * 2 ^ 52 + F) : Model.toDyadic? (toModel (ofFloat f)) = some ⟨s, 2 ^ 52 + F, (E : Int) - 1075⟩ := by have he := B_exp f have hf := B_frac f have hs := B_sgn f have he' : f.toBits.toNat / 2 ^ 52 % 2 ^ 11 = E := by split at h <;> omega have hf' : f.toBits.toNat % 2 ^ 52 = F := by split at h <;> omega have hs' : decide (2 ^ 63 ≤ f.toBits.toNat) = s := by cases s · simp only [Bool.false_eq_true, ite_false] at h; simp; omega · simp only [ite_true] at h; simp; omega rw [he'] at he rw [hf'] at hf rw [hs'] at hs have hx := Model.ofFields_signBit_expField_fracField (toModel (ofFloat f)) rw [he, hf, hs] at hx rw [← hx] have hfin : Model.isFinite (Model.ofFields B_D s E F) = true := by rw [Model.isFinite_ofFields B_D _ _ _ (by show E < 2 ^ 11; omega) hF] show (E != 2047) = true simp; omega rw [Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ (by show E < 2 ^ 11; omega) hF hfin] have hE0 : E ≠ 0 := by omega simp only [hE0, ite_false, show B_D.exponentBias = 1023 from rfl, show B_D.fracWidth = 52 from rfl] simp only [show Model.pow2 52 = 2 ^ 52 from rfl] congr 2 simp only [Int.ofNat_eq_natCast] push_cast ring theorem B_isFinite (f : Float) (d : FloatLib.Numerics.Dyadic) (h : Model.toDyadic? (toModel (ofFloat f)) = some d) : f.isFinite = true := by rw [Model.toDyadic?_ieee_eq_model _ (by decide)] at h show (Model.toModel (toModel (ofFloat f))).isFinite = true revert h generalize Model.toModel (toModel (ofFloat f)) = u cases u <;> simp [Model.unpackedToDyadic?, Float.Model.UnpackedFloat.isFinite] theorem B_R (f : Float) (s : Bool) (E F : Nat) (hE1 : 1 ≤ E) (hE : E < 2047) (hF : F < 2 ^ 52) (h : f.toBits.toNat = (if s then 2 ^ 63 else 0) + E * 2 ^ 52 + F) : f.isFinite = true ∧ R f = (if s then -1 else 1) * ((2 ^ 52 + F : Nat) : ℝ) * 2 ^ ((E : Int) - 1075) := by have hd := B_dy f s E F hE1 hE hF h refine ⟨B_isFinite f _ hd, ?_⟩ rw [R, Model.toReal_eq, hd] cases s <;> simp [FloatLib.Numerics.Dyadic.toReal, FloatLib.Numerics.Dyadic.signedSignificand] /-! ## What `ziv` decides -/ /-- `A` is a word whose magnitude code rounds, ties down, to the binary32 code `(ex, fr)`. -/ def B_near (A : Nat) (S : Bool) (ex fr : Nat) : Prop := ∃ M, A = (if S then 2 ^ 63 else 0) + M ∧ (ex * 2 ^ 23 + fr) * 2 ^ 29 < M + 2 ^ 28 ∧ M ≤ (ex * 2 ^ 23 + fr) * 2 ^ 29 + 2 ^ 28 theorem B_x (A x : Nat) (hA : A < 2 ^ 64) (hx : x = (A + 268435455) % 2 ^ 64 / 2 ^ 29) (h1 : 897 ≤ x / 2 ^ 23 % 2 ^ 11) (h2 : x / 2 ^ 23 % 2 ^ 11 < 1151) : B_near A (decide (2 ^ 34 ≤ x)) (x / 2 ^ 23 % 2 ^ 11) (x % 2 ^ 23) := by unfold B_near by_cases h : 2 ^ 34 ≤ x · simp only [h, decide_true, ite_true] exact ⟨A - 2 ^ 63, by omega, by omega, by omega⟩ · simp only [h, decide_false, Bool.false_eq_true, ite_false] exact ⟨A, by omega, by omega, by omega⟩ theorem B_x_toNat (A : UInt64) : ((A + (0x0FFFFFFF : UInt64)) >>> (29 : UInt64)).toNat = (A.toNat + 268435455) % 2 ^ 64 / 2 ^ 29 := by rw [UInt64.toNat_shiftRight, UInt64.toNat_add] norm_num [Nat.shiftRight_eq_div_pow] theorem B_ex_toNat (x : UInt64) : ((x >>> (23 : UInt64)) &&& (0x7FF : UInt64)).toNat = x.toNat / 2 ^ 23 % 2 ^ 11 := by rw [UInt64.toNat_and, UInt64.toNat_shiftRight] have := Nat.and_two_pow_sub_one_eq_mod (x.toNat >>> 23) 11 norm_num [Nat.shiftRight_eq_div_pow] at this ⊢ exact this theorem B_cond (v : UInt64) (h : v - (897 : UInt64) < (254 : UInt64)) : 897 ≤ v.toNat ∧ v.toNat < 1151 := by rw [UInt64.lt_iff_toNat_lt, UInt64.toNat_sub] at h have hv := v.toNat_lt norm_num at h hv omega theorem B_res (x : UInt64) (hx : x.toNat < 2 ^ 35) (h1 : 897 ≤ x.toNat / 2 ^ 23 % 2 ^ 11) (h2 : x.toNat / 2 ^ 23 % 2 ^ 11 < 1151) : (((x >>> (34 : UInt64)) <<< (31 : UInt64)) ||| ((x &&& (0x3FFFFFFFF : UInt64)) - ((896 : UInt64) <<< (23 : UInt64)))).toUInt32 = UInt32.ofNat ((if decide (2 ^ 34 ≤ x.toNat) then 2 ^ 31 else 0) + (x.toNat / 2 ^ 23 % 2 ^ 11 - 896) * 2 ^ 23 + x.toNat % 2 ^ 23) := by have ha : ((x >>> (34 : UInt64)) <<< (31 : UInt64)).toNat = (x.toNat / 2 ^ 34) * 2 ^ 31 := by rw [UInt64.toNat_shiftLeft, UInt64.toNat_shiftRight] norm_num [Nat.shiftRight_eq_div_pow, Nat.shiftLeft_eq] omega have hb : ((x &&& (0x3FFFFFFFF : UInt64)) - ((896 : UInt64) <<< (23 : UInt64))).toNat = x.toNat % 2 ^ 34 - 896 * 2 ^ 23 := by have h2 : (x &&& (0x3FFFFFFFF : UInt64)).toNat = x.toNat % 2 ^ 34 := by rw [UInt64.toNat_and] exact Nat.and_two_pow_sub_one_eq_mod _ 34 have h3 : ((896 : UInt64) <<< (23 : UInt64)) = 7516192768 := by decide rw [h3, UInt64.toNat_sub_of_le] · rw [h2]; rfl · rw [UInt64.le_iff_toNat_le, h2] norm_num omega have hc : (x.toNat / 2 ^ 34) * 2 ^ 31 ||| (x.toNat % 2 ^ 34 - 896 * 2 ^ 23) = (x.toNat / 2 ^ 34) * 2 ^ 31 + (x.toNat % 2 ^ 34 - 896 * 2 ^ 23) := by rw [Nat.mul_comm (x.toNat / 2 ^ 34)] exact (Nat.two_pow_add_eq_or_of_lt (by omega) _).symm apply UInt32.toNat_inj.mp rw [UInt64.toNat_toUInt32, UInt64.toNat_or, ha, hb, hc, UInt32.toNat_ofNat'] by_cases h : 2 ^ 34 ≤ x.toNat · simp only [h, decide_true, ite_true] omega · simp only [h, decide_false, Bool.false_eq_true, ite_false] omega theorem B_ziv_spec (s e : Float) (h : ziv s e ≠ 0xFFFFFFFF) : ∃ (S : Bool) (ex fr : Nat), 897 ≤ ex ∧ ex < 1151 ∧ fr < 2 ^ 23 ∧ ziv s e = UInt32.ofNat ((if S then 2 ^ 31 else 0) + (ex - 896) * 2 ^ 23 + fr) ∧ B_near (s + e).toBits.toNat S ex fr ∧ B_near (s - e).toBits.toNat S ex fr := by unfold ziv at h ⊢ dsimp only at h ⊢ generalize (s + e).toBits = A at h ⊢ generalize (s - e).toBits = B at h ⊢ have hAn := B_x_toNat A have hBn := B_x_toNat B generalize (A + (0x0FFFFFFF : UInt64)) >>> (29 : UInt64) = x at h hAn ⊢ generalize (B + (0x0FFFFFFF : UInt64)) >>> (29 : UInt64) = y at h hBn ⊢ have hex := B_ex_toNat x generalize (x >>> (23 : UInt64)) &&& (0x7FF : UInt64) = ex at h hex ⊢ split at h · rename_i hc have hc0 := hc obtain ⟨hxy, hc⟩ := Bool.and_eq_true_iff.mp hc have hxy : x = y := by simpa using hxy obtain ⟨h1, h2⟩ := B_cond ex (by simpa using hc) rw [hex] at h1 h2 subst hxy have hA := A.toNat_lt have hB := B.toNat_lt have hx35 : x.toNat < 2 ^ 35 := by omega simp only [hc0, ite_true] exact ⟨_, _, _, h1, h2, by omega, B_res x hx35 h1 h2, B_x A.toNat x.toNat hA hAn h1 h2, B_x B.toNat x.toNat hB hBn h1 h2⟩ · exact absurd rfl h /-! ## The two ends lie inside the rounding interval of the decided word -/ set_option maxRecDepth 8192 in theorem B_near_val (f : Float) (S : Bool) (ex fr : Nat) (h1 : 897 ≤ ex) (h2 : ex < 1151) (hfr : fr < 2 ^ 23) (hn : B_near f.toBits.toNat S ex fr) : f.isFinite = true ∧ ∃ v : ℝ, R f = (if S then -v else v) ∧ v ≤ (2 ^ 53 + fr * 2 ^ 30 + 2 ^ 29) * 2 ^ ((ex : Int) - 1076) ∧ (fr ≠ 0 → (2 ^ 53 + fr * 2 ^ 30 - 2 ^ 29) * 2 ^ ((ex : Int) - 1076) < v) ∧ (2 ^ 53 - 2 ^ 28) * 2 ^ ((ex : Int) - 1076) < v := by obtain ⟨M, hA, hl, hu⟩ := hn have hF : M % 2 ^ 52 < 2 ^ 52 := Nat.mod_lt _ (by norm_num) have hM : M = (M / 2 ^ 52) * 2 ^ 52 + M % 2 ^ 52 := by omega generalize M / 2 ^ 52 = E at hM generalize M % 2 ^ 52 = F at hM hF subst hM have hb : (0 : ℝ) < 2 ^ ((ex : Int) - 1076) := zpow_pos (by norm_num) _ have hE : E = ex ∨ (E + 1 = ex ∧ fr = 0) := by omega obtain ⟨hfin, hR⟩ := B_R f S E F (by omega) (by omega) hF (by rw [hA, Nat.add_assoc]) refine ⟨hfin, ((2 ^ 52 + F : Nat) : ℝ) * 2 ^ ((E : Int) - 1075), ?_, ?_⟩ · rw [hR]; cases S <;> simp ring rcases hE with rfl | ⟨rfl, rfl⟩ · have e : (2 : ℝ) ^ ((E : Int) - 1075) = 2 * 2 ^ ((E : Int) - 1076) := by rw [show (E : Int) - 1075 = 1 + ((E : Int) - 1076) by ring, zpow_add₀ two_ne_zero, zpow_one] rw [e] generalize (2 : ℝ) ^ ((E : Int) - 1076) = b at hb ⊢ have h3 : (F : ℝ) ≤ fr * 2 ^ 29 + 2 ^ 28 := by exact_mod_cast (by omega : F ≤ fr * 2 ^ 29 + 2 ^ 28) have h4' : fr * 2 ^ 29 < F + 2 ^ 28 := by have e1 : (E * 2 ^ 23 + fr) * 2 ^ 29 = E * 2 ^ 52 + fr * 2 ^ 29 := by ring rw [e1] at hl exact Nat.lt_of_add_lt_add_left (by rw [Nat.add_assoc] at hl; exact hl) have h4 : (fr : ℝ) * 2 ^ 29 < F + 2 ^ 28 := by exact_mod_cast h4' have h5 : (0 : ℝ) ≤ F := Nat.cast_nonneg _ have h6 : (0 : ℝ) ≤ fr := Nat.cast_nonneg _ have h7 := mul_le_mul_of_nonneg_right h3 hb.le have h8 := mul_lt_mul_of_pos_right h4 hb have h9 := mul_nonneg h5 hb.le have h10 := mul_nonneg h6 hb.le push_cast refine ⟨?_, ?_, ?_⟩ · linarith · intro _; linarith · linarith · have e : (2 : ℝ) ^ (((E + 1 : Nat) : Int) - 1076) = 2 ^ ((E : Int) - 1075) := by congr 1; push_cast; ring rw [e] at hb ⊢ generalize (2 : ℝ) ^ ((E : Int) - 1075) = b at hb ⊢ have h3 : (F : ℝ) ≤ 2 ^ 52 := by exact_mod_cast hF.le have h4 : (2 : ℝ) ^ 52 < F + 2 ^ 28 := by exact_mod_cast (by omega : 2 ^ 52 < F + 2 ^ 28) have h7 := mul_le_mul_of_nonneg_right h3 hb.le have h8 := mul_lt_mul_of_pos_right h4 hb push_cast refine ⟨?_, ?_, ?_⟩ · linarith · intro h0; exact absurd rfl h0 · linarith theorem ziv_big (s e : Float) (h : ziv s e ≠ 0xFFFFFFFF) : (s + e).isFinite = true ∧ (s - e).isFinite = true ∧ (2 : ℝ) ^ (-127 : ℤ) ≤ |R (s + e)| := by obtain ⟨S, ex, fr, h1, h2, h3, -, ha, hb⟩ := B_ziv_spec s e h obtain ⟨f1, v, hv, -, -, h5⟩ := B_near_val _ S ex fr h1 h2 h3 ha obtain ⟨f2, -⟩ := B_near_val _ S ex fr h1 h2 h3 hb refine ⟨f1, f2, ?_⟩ have hb0 : (2 : ℝ) ^ (-179 : ℤ) ≤ 2 ^ ((ex : Int) - 1076) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hpos : (0 : ℝ) < 2 ^ (-179 : ℤ) := zpow_pos (by norm_num) _ have e1 : (2 : ℝ) ^ (-127 : ℤ) = 2 ^ 52 * 2 ^ (-179 : ℤ) := by rw [← zpow_natCast, ← zpow_add₀ two_ne_zero]; norm_num have hv1 : (2 : ℝ) ^ (-127 : ℤ) ≤ v := by rw [e1] nlinarith have hv0 : 0 < v := lt_of_lt_of_le (zpow_pos (by norm_num) _) hv1 have : |R (s + e)| = v := by rw [hv]; cases S <;> simp [abs_of_pos hv0] rwa [this] /-! ## FloatLib's dot product of finite words -/ theorem B_wv (w : UInt32) (h : expOf w ≠ 255) : wv w = (rationalWord w : ℝ) := by rw [wv, Model.toReal_eq, ofW, toDyadic_ofUInt32 w h] simp only [rationalWord, FloatLib.Numerics.Dyadic.cast_toRat] theorem B_good (w : UInt32) (h : good w = true) : expOf w ≠ 255 := by intro he have he' : w.toNat / 2 ^ 23 % 2 ^ 8 = 255 := by have := congrArg UInt32.toNat he rw [expOf, UInt32.toNat_and, UInt32.toNat_shiftRight, show (255 : UInt32).toNat = 2 ^ 8 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] at this simpa [Nat.shiftRight_eq_div_pow] using this have hv : (w &&& 0x7FFFFFFF).toNat = w.toNat % 2 ^ 31 := by rw [UInt32.toNat_and, show (0x7FFFFFFF : UInt32).toNat = 2 ^ 31 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] simp only [good, Bool.or_eq_true, beq_iff_eq, decide_eq_true_eq] at h rcases h with h | h · have := congrArg UInt32.toNat h rw [hv] at this have h0 : (0 : UInt32).toNat = 0 := rfl omega · rw [UInt32.le_iff_toNat_le, UInt32.toNat_sub, hv] at h norm_num at h omega theorem B_scaled (a b : UInt32) : ((sv a : ℚ) * sv b * (2 : ℚ) ^ (idxOf a + idxOf b)) * 2 ^ (-298 : Int) = rationalWord a * rationalWord b := by simp only [rationalWord, decodeD_toRat] rw [show (-298 : Int) = -149 + -149 by norm_num, zpow_add₀ two_ne_zero] rw [zpow_sub₀ two_ne_zero, zpow_sub₀ two_ne_zero, zpow_natCast, zpow_natCast, pow_add] norm_num [zpow_neg, zpow_natCast] ring theorem B_dsum (a b : Array UInt32) (start n : Nat) : (dsum a b start n : ℚ) * 2 ^ (-298 : Int) = ∑ j ∈ Finset.range n, rationalWord a[start + j]! * rationalWord b[start + j]! := by induction n generalizing start with | zero => simp [dsum] | succ n ih => rw [dsum, Int.cast_add, Int.cast_mul, Int.cast_mul, Int.cast_pow, Int.cast_ofNat, add_mul, ih] conv_rhs => rw [Finset.sum_range_succ'] simp only [Nat.add_zero] rw [B_scaled] have ht : (∑ j ∈ Finset.range n, rationalWord a[start + 1 + j]! * rationalWord b[start + 1 + j]!) = ∑ j ∈ Finset.range n, rationalWord a[start + (j + 1)]! * rationalWord b[start + (j + 1)]! := by apply Finset.sum_congr rfl intro j _ simp [Nat.add_comm, Nat.add_left_comm] rw [ht] ring theorem B_dotSpec (a b : Array UInt32) (hs : a.size = b.size) (hf : ∀ j, j < a.size → expOf a[j]! ≠ 255 ∧ expOf b[j]! ≠ 255) (hn : (dsum a b 0 a.size : ℚ) * 2 ^ (-298 : Int) ≠ 0) : ∃ d : FloatLib.Numerics.Dyadic, d.toRat = (dsum a b 0 a.size : ℚ) * 2 ^ (-298 : Int) ∧ dotSpec a b = toWord (Model.roundDyadic Fmt d) := by let L := a.map (Model.ofUInt32 (fmt := Fmt)) let K := b.map (Model.ofUInt32 (fmt := Fmt)) have hLK : L.size = K.size := by simp [L, K, hs] have hLf : ∀ j (hj : j < L.size), Model.isFinite L[j] = true := by intro j hj have hj' : j < a.size := by simpa [L] using hj have he := (hf j hj').1 rw [getElem!_pos a j hj'] at he simp [L, isFinite_ofUInt32, he] have hKf : ∀ j (hj : j < K.size), Model.isFinite K[j] = true := by intro j hj have hj' : j < b.size := by simpa [K] using hj have he := (hf j (by omega)).2 rw [getElem!_pos b j hj'] at he simp [K, isFinite_ofUInt32, he] have hex := Model.Reduction.Internal.dotState_exact_toRat (destination := Fmt) L K hLK hLf hKf have hmath : Model.Reduction.Semantics.finiteDotSlice L K 0 L.size (by simp) (by simp [hLK]) = (dsum a b 0 a.size : ℚ) * 2 ^ (-298 : Int) := by simpa [L, K] using finiteDotSlice_eq a b 0 a.size (by simp) (by simp [hs]) (by intro j _ hj; exact hf j (by omega)) have hn' : Model.Reduction.Semantics.finiteDot L.toList K.toList ≠ .ok 0 := by rw [Model.Reduction.Semantics.finiteDot_toList_eq_slice L K hLK, hmath] simpa using hn refine ⟨(Model.Reduction.Internal.dotState L K hLK : Model.Reduction.Internal.State Fmt).exact, hex.trans hmath, ?_⟩ unfold dotSpec Model.dot rw [Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L K .nearestEven hLK hLf hKf hn'] rfl theorem B_dot (n : Nat) (fa fb : Nat → UInt32) (hf : ∀ j, j < n → expOf (fa j) ≠ 255 ∧ expOf (fb j) ≠ 255) (hn : (∑ j ∈ Finset.range n, rationalWord (fa j) * rationalWord (fb j)) ≠ 0) : ∃ d : FloatLib.Numerics.Dyadic, d.toRat = (∑ j ∈ Finset.range n, rationalWord (fa j) * rationalWord (fb j)) ∧ dotOf n fa fb = toWord (Model.roundDyadic Fmt d) := by have hsum : (dsum (Array.ofFn (n := n) fun i => fa i) (Array.ofFn (n := n) fun i => fb i) 0 n : ℚ) * 2 ^ (-298 : Int) = ∑ j ∈ Finset.range n, rationalWord (fa j) * rationalWord (fb j) := by rw [B_dsum] apply Finset.sum_congr rfl intro j hj have hj' := Finset.mem_range.mp hj rw [Nat.zero_add, getElem!_ofFn n fa j hj', getElem!_ofFn n fb j hj'] have hs : (Array.ofFn (n := n) fun i => fa i).size = n := by simp obtain ⟨d, hd, hw⟩ := B_dotSpec (Array.ofFn (n := n) fun i => fa i) (Array.ofFn (n := n) fun i => fb i) (by simp) (by intro j hj have hj' : j < n := by simpa using hj rw [getElem!_ofFn n fa j hj', getElem!_ofFn n fb j hj'] exact hf j hj') (by rw [hs, hsum]; exact hn) rw [hs, hsum] at hd exact ⟨d, hd, hw⟩ /-! ## The result -/ theorem B_one : rationalWord oneW = 1 := by have he : expOf oneW = 127 := by decide have hf : fracOf oneW = 0 := by decide have hs : signOf oneW = false := by decide norm_num [rationalWord, decodeD, he, hf, hs, FloatLib.Numerics.Dyadic.toRat, FloatLib.Numerics.Dyadic.signedSignificand] theorem B_sum (n : Nat) (xw ww : Nat → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) : ((∑ j ∈ Finset.range (n + 1), rationalWord (if j < n then xw j else oneW) * rationalWord (if j < n then ww j else bw) : ℚ) : ℝ) = exactS n xw ww bw := by rw [Finset.sum_range_succ] simp only [lt_self_iff_false, ite_false, B_one, one_mul] rw [Rat.cast_add, Rat.cast_sum, exactS, B_wv bw (B_good bw hb)] congr 1 apply Finset.sum_congr rfl intro t ht have ht' := Finset.mem_range.mp ht simp only [ht', ite_true] rw [B_wv _ (B_good _ (hx t ht')), B_wv _ (B_good _ (hw t ht')), Rat.cast_mul] theorem ziv_dot (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (s e : Float) (hlo : R (s - e) < exactS n xw ww bw) (hhi : exactS n xw ww bw < R (s + e)) (h : ziv s e ≠ 0xFFFFFFFF) : ziv s e = dotOf (n + 1) (fun t => if t < n then xw t else oneW) (fun t => if t < n then ww t else bw) := by obtain ⟨S, ex, fr, h1, h2, h3, hz, ha, hb'⟩ := B_ziv_spec s e h obtain ⟨-, va, hva, hia, loa1, loa⟩ := B_near_val _ S ex fr h1 h2 h3 ha obtain ⟨-, vb, hvb, hib, lob1, lob⟩ := B_near_val _ S ex fr h1 h2 h3 hb' have hsum := B_sum n xw ww bw hx hw hb generalize exactS n xw ww bw = V at hlo hhi hsum have hb0 : (0 : ℝ) < 2 ^ ((ex : Int) - 1076) := zpow_pos (by norm_num) _ have hE : (2 : ℝ) ^ ((ex : Int) - 1046) = 2 ^ 30 * 2 ^ ((ex : Int) - 1076) := by rw [← zpow_natCast, ← zpow_add₀ two_ne_zero] congr 1; push_cast; ring rw [hva] at hhi rw [hvb] at hlo -- The magnitude of the exact value lies strictly inside the rounding interval. obtain ⟨W, hVW, hW1, hW2, hW3⟩ : ∃ W : ℝ, V = (if S then -W else W) ∧ W < (2 ^ 53 + fr * 2 ^ 30 + 2 ^ 29) * 2 ^ ((ex : Int) - 1076) ∧ (fr ≠ 0 → (2 ^ 53 + fr * 2 ^ 30 - 2 ^ 29) * 2 ^ ((ex : Int) - 1076) < W) ∧ (2 ^ 53 - 2 ^ 28) * 2 ^ ((ex : Int) - 1076) < W := by cases S · simp only [Bool.false_eq_true, ite_false] at hhi hlo ⊢ exact ⟨V, rfl, lt_of_lt_of_le hhi hia, fun h => (lob1 h).trans hlo, lob.trans hlo⟩ · simp only [ite_true] at hhi hlo ⊢ exact ⟨-V, by simp, by linarith, fun h => by linarith [loa1 h], by linarith⟩ have hW0 : 0 < W := by refine lt_of_le_of_lt ?_ hW3 exact mul_nonneg (by norm_num) hb0.le have hne : (∑ j ∈ Finset.range (n + 1), rationalWord (if j < n then xw j else oneW) * rationalWord (if j < n then ww j else bw) : ℚ) ≠ 0 := by intro h0 rw [h0, Rat.cast_zero] at hsum rw [← hsum] at hVW cases S · simp only [Bool.false_eq_true, ite_false] at hVW; linarith · simp only [ite_true] at hVW; linarith obtain ⟨⟨neg, m, q⟩, hd, hw'⟩ := B_dot (n + 1) (fun t => if t < n then xw t else oneW) (fun t => if t < n then ww t else bw) (by intro j _ have hone : expOf oneW ≠ 255 := by decide by_cases hj : j < n · simp only [hj, ite_true] exact ⟨B_good _ (hx j hj), B_good _ (hw j hj)⟩ · simp only [hj, ite_false] exact ⟨hone, B_good _ hb⟩) hne have hdV : (FloatLib.Numerics.Dyadic.mk neg m q).toReal = V := by rw [← FloatLib.Numerics.Dyadic.cast_toRat, hd, hsum] have hq : (0 : ℝ) < 2 ^ q := zpow_pos (by norm_num) _ have hm0 : (0 : ℝ) ≤ m := Nat.cast_nonneg _ have hmq := mul_nonneg hm0 hq.le have hMW : (m : ℝ) * 2 ^ q = W ∧ neg = S := by simp only [FloatLib.Numerics.Dyadic.toReal, FloatLib.Numerics.Dyadic.signedSignificand] at hdV cases neg <;> cases S <;> simp only [Bool.false_eq_true, ite_false, ite_true, Int.ofNat_eq_natCast, Int.cast_neg, Int.cast_natCast] at hdV hVW · exact ⟨by linarith, rfl⟩ · exfalso; linarith · exfalso; linarith · exact ⟨by linarith, rfl⟩ obtain ⟨hMW, rfl⟩ := hMW have hm : m ≠ 0 := by rintro rfl simp at hMW linarith have hword := B_word neg m q (2 ^ 23 + fr) ((ex : Int) - 1046) hm (by omega) (by omega) (by omega) (by omega) (by rw [hMW, hE]; push_cast; linarith) (by intro hfr have hfr' : fr ≠ 0 := by intro h0; exact hfr (by omega) have := hW2 hfr' rw [hMW, hE]; push_cast; linarith) (by intro _ rw [hMW, hE]; linarith) have e1 : ((ex : Int) - 1046 + 150).toNat = ex - 896 := by omega have e2 : (2 ^ 23 + fr) % 2 ^ 23 = fr := by omega rw [hz, hw', hword, e1, e2] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary /-- What the loops use: Part A plus Part B. -/ theorem fast_tight (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : let r := l.foldl (fun (acc : F1) t => let q := dv (xw t) * dv (ww t) F1.mk (acc.s + q) (acc.t + q.abs)) ⟨0, 0⟩ s = r.s + dv bw ∧ e = nfOf n * (r.t + (dv bw).abs)) (h : ziv s e ≠ 0xFFFFFFFF) : ziv s e = dotOf (n + 1) (fun t => if t < n then xw t else oneW) (fun t => if t < n then ww t else bw) := by obtain ⟨-, -, hbig⟩ := ziv_big s e h obtain ⟨hlo, hhi⟩ := encl_tight n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact ziv_dot n hn xw ww bw hx hw hb s e hlo hhi h theorem fast_coarse (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw hh : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (hg : good hh = true) (hm : ∀ t, t < n → xw t &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (s e : Float) (hs : let r := (List.range n).foldl (fun (acc : F1) t => let v := dv (ww t) F1.mk (acc.s + v * dv (xw t)) (acc.t + v.abs)) ⟨0, 0⟩ s = r.s + dv bw ∧ e = nfOf n * (dv hh * r.t + (dv bw).abs)) (h : ziv s e ≠ 0xFFFFFFFF) : ziv s e = dotOf (n + 1) (fun t => if t < n then xw t else oneW) (fun t => if t < n then ww t else bw) := by obtain ⟨-, -, hbig⟩ := ziv_big s e h obtain ⟨hlo, hhi⟩ := A_encl_coarse n hn xw ww bw hh hx hw hb hg hm hp s e hs hbig exact ziv_dot n hn xw ww bw hx hw hb s e hlo hhi h theorem fast_coarse1 (n : Nat) (hn : n < 2 ^ 20) (xw ww : Nat → UInt32) (bw hh hv : UInt32) (hx : ∀ t, t < n → good (xw t) = true) (hw : ∀ t, t < n → good (ww t) = true) (hb : good bw = true) (hg : good hh = true) (hgv : good hv = true) (hm : ∀ t, t < n → xw t &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (hmv : ∀ t, t < n → ww t &&& 0x7FFFFFFF ≤ hv) (hpv : hv ≤ 0x7FFFFFFF) (l : List Nat) (hl : l.Nodup) (hl2 : ∀ t ∈ l, t < n) (hz : ∀ t, t < n → t ∉ l → xw t &&& 0x7FFFFFFF = 0) (s e : Float) (hs : s = l.foldl (fun (acc : Float) t => acc + dv (xw t) * dv (ww t)) 0 + dv bw ∧ e = nfOf n * (dv hh * ((n.toUInt64 + 1).toFloat * dv hv) + (dv bw).abs)) (h : ziv s e ≠ 0xFFFFFFFF) : ziv s e = dotOf (n + 1) (fun t => if t < n then xw t else oneW) (fun t => if t < n then ww t else bw) := by obtain ⟨-, -, hbig⟩ := ziv_big s e h obtain ⟨hlo, hhi⟩ := encl_coarse1 n hn xw ww bw hh hv hx hw hb hg hgv hm hp hmv hpv l hl hl2 hz s e hs hbig exact ziv_dot n hn xw ww bw hx hw hb s e hlo hhi h end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary /-! ## Indices -/ theorem C_tn (k : Nat) (h : k < 2 ^ 32) : k.toUSize.toNat = k := by rw [Nat.toUSize_eq]; exact uk k h theorem C_umul (a b : USize) (h : a.toNat * b.toNat < 2 ^ 32) : (a * b).toNat = a.toNat * b.toNat := by have h1 : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl rw [USize.toNat_mul]; exact Nat.mod_eq_of_lt (by omega) theorem C_usize {α : Type} (a : Array α) (h : a.size < 2 ^ 32) : a.usize.toNat = a.size := C_tn _ h theorem C_lit (k : Nat) (h : k < 2 ^ 32) : (USize.ofNat k).toNat = k := uk k h theorem C_rdW (a : Array UInt32) (i : USize) (hs : a.size < 2 ^ 32) : rdW a i = a[i.toNat]! := by unfold rdW split · rename_i h have h2 := lt_of_usz_w a i h rw [getElem!_pos a i.toNat h2]; rfl · rename_i h have h' : ¬ i.toNat < a.size := by intro h2; apply h; rw [USize.lt_iff_toNat_lt, C_usize a hs]; exact h2 rw [getElem!_neg a i.toNat h']; rfl theorem C_wrW_size (a : Array UInt32) (i : USize) (v : UInt32) : (wrW a i v).size = a.size := by unfold wrW; split <;> simp theorem C_wrW_get (a : Array UInt32) (i : USize) (v : UInt32) (q : Nat) (hs : a.size < 2 ^ 32) (hi : i.toNat < a.size) : (wrW a i v)[q]! = if q = i.toNat then v else a[q]! := by have h : i < a.usize := by rw [USize.lt_iff_toNat_lt, C_usize a hs]; exact hi unfold wrW; rw [dite_eq_left h] by_cases hq : q < a.size · have h3 : q < (a.uset i v (lt_of_usz_w a i h)).size := by simpa using hq rw [getElem!_pos _ q h3, getElem!_pos a q hq] simp only [Array.uset, Array.getElem_set] by_cases e : q = i.toNat <;> simp [e, eq_comm] · have h3 : ¬ q < (a.uset i v (lt_of_usz_w a i h)).size := by simpa using hq rw [getElem!_neg _ q h3, getElem!_neg a q hq, ite_eq_right (by omega)] /-- `out` has size `N` and agrees with `sp` where `S` holds. -/ def C_Inv (N : Nat) (sp : Nat → Word) (S : Nat → Prop) (out : Array Word) : Prop := out.size = N ∧ ∀ q, q < N → S q → out[q]! = sp q theorem C_Inv_wr {N : Nat} {sp : Nat → Word} {S : Nat → Prop} {out : Array Word} (hN : N < 2 ^ 32) (h : C_Inv N sp S out) {u : USize} {p : Nat} {v : Word} (hu : u.toNat = p) (hp : p < N) (hv : v = sp p) : C_Inv N sp (fun q => q = p ∨ S q) (wrW out u v) := by obtain ⟨h1, h2⟩ := h refine ⟨by rw [C_wrW_size, h1], fun q hq hS => ?_⟩ rw [C_wrW_get _ _ _ _ (by omega) (by omega), hu] by_cases e : q = p · rw [ite_eq_left e, e, hv] · rw [ite_eq_right e]; exact h2 q hq (hS.resolve_left e) theorem C_Inv_mono {N : Nat} {sp : Nat → Word} {S S' : Nat → Prop} {out : Array Word} (h : C_Inv N sp S out) (hS : ∀ q, q < N → S' q → S q) : C_Inv N sp S' out := ⟨h.1, fun q hq h' => h.2 q hq (hS q hq h')⟩ theorem C_ofFn {N : Nat} {sp : Nat → Word} {S : Nat → Prop} {out : Array Word} (h : C_Inv N sp S out) (hS : ∀ q, q < N → S q) : out = Array.ofFn (n := N) fun q => sp q.val := by obtain ⟨h1, h2⟩ := h apply Array.ext · simp [h1] · intro q h3 h4 have h5 : q < N := by omega have := h2 q h5 (hS q h5) rw [getElem!_pos out q h3] at this simp [this] /-! ## Good words -/ theorem C_good_of (v : UInt32) (h1 : (0x7FFFFF : UInt32) ≤ v - 1) (h2 : v < 0x7F800000) : (v == 0 || v - (0x00800000 : UInt32) ≤ (0x7EFFFFFF : UInt32)) = true := by have a1 : (0x7FFFFF : UInt32).toNat = 0x7FFFFF := rfl have a2 : (0x7F800000 : UInt32).toNat = 0x7F800000 := rfl have a3 : (0x00800000 : UInt32).toNat = 0x00800000 := rfl have a4 : (0x7EFFFFFF : UInt32).toNat = 0x7EFFFFFF := rfl have a5 : (1 : UInt32).toNat = 1 := rfl by_cases h0 : v = 0 · simp [h0] · have h3 : (1 : UInt32) ≤ v := by rw [UInt32.le_iff_toNat_le] have : v.toNat ≠ 0 := fun h => h0 (UInt32.toNat_inj.mp h) omega rw [UInt32.le_iff_toNat_le, UInt32.toNat_sub_of_le _ _ h3] at h1 rw [UInt32.lt_iff_toNat_lt] at h2 have h4 : (0x00800000 : UInt32) ≤ v := by rw [UInt32.le_iff_toNat_le]; omega simp only [Bool.or_eq_true, decide_eq_true_eq] right rw [UInt32.le_iff_toNat_le, UInt32.toNat_sub_of_le _ _ h4] omega theorem C_min (c u lo : UInt32) (h : c ≤ if u < lo then u else lo) : c ≤ u ∧ c ≤ lo := by simp only [UInt32.le_iff_toNat_le, UInt32.lt_iff_toNat_lt] at * split at h <;> omega theorem C_max (c v hi : UInt32) (h : (if hi < v then v else hi) < c) : v < c ∧ hi < c := by simp only [UInt32.lt_iff_toNat_lt] at * split at h <;> omega theorem C_good_mask (w : UInt32) (h : good w = true) : good (w &&& 0x7FFFFFFF) = true := by have e : (w &&& 0x7FFFFFFF) &&& 0x7FFFFFFF = w &&& 0x7FFFFFFF := by rw [UInt32.and_assoc, UInt32.and_self] unfold good at h ⊢ simpa only [e] using h theorem C_le_mask {h : UInt32} (h1 : h < 0x7F800000) : h ≤ 0x7FFFFFFF := by have a1 : (0x7F800000 : UInt32).toNat = 0x7F800000 := rfl have a2 : (0x7FFFFFFF : UInt32).toNat = 0x7FFFFFFF := rfl rw [UInt32.lt_iff_toNat_lt] at h1 rw [UInt32.le_iff_toNat_le] omega /-- What `scan` says about an array: `h` is good, sign-free, and bounds every magnitude, and every word is good. -/ def C_Hv (A : Array Word) (h : Word) : Prop := good h = true ∧ h ≤ 0x7FFFFFFF ∧ (∀ k, k < A.size → good A[k]! = true) ∧ ∀ k, k < A.size → A[k]! &&& 0x7FFFFFFF ≤ h theorem C_max_le (r v hi : UInt32) (h : (if hi < v then v else hi) ≤ r) : v ≤ r ∧ hi ≤ r := by simp only [UInt32.le_iff_toNat_le, UInt32.lt_iff_toNat_lt] at * split at h <;> omega theorem C_scan (A : Array UInt32) (n : USize) (hn : n.toNat ≤ A.size) (d : Nat) : ∀ (i : USize) (lo hi : UInt32), i.toNat + d = n.toNat → scan A n i hn lo hi ≠ 0xFFFFFFFF → (0x7FFFFF : UInt32) ≤ lo ∧ hi < 0x7F800000 ∧ hi ≤ scan A n i hn lo hi ∧ scan A n i hn lo hi < 0x7F800000 ∧ (good hi = true → good (scan A n i hn lo hi) = true) ∧ ∀ k, i.toNat ≤ k → k < n.toNat → good A[k]! = true ∧ A[k]! &&& 0x7FFFFFFF ≤ scan A n i hn lo hi := by induction d with | zero => intro i lo hi he h have e : scan A n i hn lo hi = if 0x007FFFFF ≤ lo ∧ hi < 0x7F800000 then hi else 0xFFFFFFFF := by rw [scan, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)] rw [e] at h ⊢ by_cases c : 0x007FFFFF ≤ lo ∧ hi < 0x7F800000 · rw [ite_eq_left c] exact ⟨c.1, c.2, UInt32.le_refl _, c.2, id, fun k h1 h2 => by omega⟩ · rw [ite_eq_right c] at h; exact absurd rfl h | succ d ih => intro i lo hi he h have hi' : i < n := by rw [USize.lt_iff_toNat_lt]; omega have hb : i.toNat < A.size := by omega have e : scan A n i hn lo hi = scan A n (i + 1) hn (if (A.uget i hb &&& 0x7FFFFFFF) - 1 < lo then (A.uget i hb &&& 0x7FFFFFFF) - 1 else lo) (if hi < A.uget i hb &&& 0x7FFFFFFF then A.uget i hb &&& 0x7FFFFFFF else hi) := by rw [scan, dite_eq_left hi'] rw [e] at h ⊢ obtain ⟨h1, h2, h3, h4, h5, h6⟩ := ih (i + 1) _ _ (by rw [usz i n hi']; omega) h obtain ⟨h7, h8⟩ := C_min _ _ _ h1 obtain ⟨h9, h10⟩ := C_max _ _ _ h2 obtain ⟨h11, h12⟩ := C_max_le _ _ _ h3 have hg : good (A.uget i hb) = true := C_good_of _ h7 h9 refine ⟨h8, h10, h12, h4, fun g0 => h5 ?_, fun k hk1 hk2 => ?_⟩ · split · exact C_good_mask _ hg · exact g0 · by_cases e2 : k = i.toNat · subst e2 rw [getElem!_pos A i.toNat hb] exact ⟨hg, h11⟩ · exact h6 k (by rw [usz i n hi']; omega) hk2 theorem C_scan_all (A : Array UInt32) (hs : A.size < 2 ^ 32) (h : scan A A.usize 0 (usize_le A) 0xFFFFFFFF 0 ≠ 0xFFFFFFFF) : C_Hv A (scan A A.usize 0 (usize_le A) 0xFFFFFFFF 0) := by have e := C_usize A hs have h0 : (0 : USize).toNat = 0 := rfl obtain ⟨-, -, -, h4, h5, h6⟩ := C_scan A A.usize (usize_le A) A.size 0 _ _ (by rw [h0, e]; omega) h have h7 : ∀ k, k < A.size → good A[k]! = true ∧ A[k]! &&& 0x7FFFFFFF ≤ scan A A.usize 0 (usize_le A) 0xFFFFFFFF 0 := fun k hk => h6 k (by omega) (by omega) exact ⟨h5 rfl, C_le_mask h4, fun k hk => (h7 k hk).1, fun k hk => (h7 k hk).2⟩ theorem C_mask_le (w : UInt32) : w &&& 0x7FFFFFFF ≤ 0x7FFFFFFF := by rw [UInt32.le_iff_toNat_le, UInt32.toNat_and] exact Nat.and_le_right /-- `h` is good and bounds the magnitudes of the first `t` words. -/ def C_hok (w : Nat → Word) (t : Nat) (h : Word) : Prop := good h = true ∧ (∀ k, k < t → w k &&& 0x7FFFFFFF ≤ h) ∧ h ≤ 0x7FFFFFFF theorem C_hok_step {w : Nat → Word} {t : Nat} {h : Word} (hg : good (w t) = true) (H : C_hok w t h) : C_hok w (t + 1) (mx h (w t)) := by obtain ⟨h1, h2, hp⟩ := H have hm := C_good_mask _ hg unfold mx dsimp only by_cases e : w t &&& 0x7FFFFFFF ≤ h · rw [ite_eq_left e] refine ⟨h1, fun k hk => ?_, hp⟩ by_cases e2 : k = t · rw [e2]; exact e · exact h2 k (by omega) · rw [ite_eq_right e] refine ⟨hm, fun k hk => ?_, C_mask_le _⟩ by_cases e2 : k = t · rw [e2]; exact UInt32.le_refl _ · have h3 := h2 k (by omega) simp only [UInt32.le_iff_toNat_le] at * omega theorem C_hok_skip {w : Nat → Word} {t : Nat} {h : Word} (hz : w t &&& 0x7FFFFFFF = 0) (H : C_hok w t h) : C_hok w (t + 1) h := by obtain ⟨h1, h2, hp⟩ := H refine ⟨h1, fun k hk => ?_, hp⟩ by_cases e2 : k = t · rw [e2, hz, UInt32.le_iff_toNat_le] exact Nat.zero_le _ · exact h2 k (by omega) /-! ## One output -/ /-- The spec's word for row `i`, column `j`. -/ def C_out (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) : Word := act relu (dotOf (inW + 1) (fun t => if t < inW then X[i * inW + t]! else oneW) (fun t => if t < inW then W[j * inW + t]! else b[j]!)) /-- The guard of `linF`. -/ structure C_G (rows inW outW : Nat) (X W b : Array Word) : Prop where hX : rows * inW = X.size hW : outW * inW = W.size hb : outW = b.size pi : 0 < inW po : 0 < outW ni : inW < 2 ^ 20 sX : X.size < 2 ^ 32 sW : W.size < 2 ^ 32 sb : b.size < 2 ^ 32 sO : rows * outW < 2 ^ 32 gX : ∀ q, q < X.size → good X[q]! = true gW : ∀ q, q < W.size → good W[q]! = true gb : ∀ q, q < b.size → good b[q]! = true theorem C_ib {rows inW i t : Nat} (hi : i < rows) (ht : t < inW) : i * inW + t < rows * inW := by have h : (i + 1) * inW ≤ rows * inW := Nat.mul_le_mul_right _ hi rw [Nat.add_mul, Nat.one_mul] at h; omega theorem C_ib' {rows inW i t : Nat} (hi : i < rows) (ht : t ≤ inW) : i * inW + t ≤ rows * inW := by have h : (i + 1) * inW ≤ rows * inW := Nat.mul_le_mul_right _ hi rw [Nat.add_mul, Nat.one_mul] at h; omega section variable {rows inW outW : Nat} {X W b : Array Word} theorem C_G.gx (G : C_G rows inW outW X W b) {i : Nat} (hi : i < rows) : ∀ t, t < inW → good X[i * inW + t]! = true := fun _ ht => G.gX _ (by rw [← G.hX]; exact C_ib hi ht) theorem C_G.gw (G : C_G rows inW outW X W b) {j : Nat} (hj : j < outW) : ∀ t, t < inW → good W[j * inW + t]! = true := fun _ ht => G.gW _ (by rw [← G.hW]; exact C_ib hj ht) theorem C_fin (G : C_G rows inW outW X W b) (relu : Bool) {i j : Nat} (hi : i < rows) (hj : j < outW) (L : List Nat) (hl : L.Nodup) (hl2 : ∀ t ∈ L, t < inW) (hz : ∀ t, t < inW → t ∉ L → X[i * inW + t]! &&& 0x7FFFFFFF = 0) {s e : Float} (hs : let r := L.foldl (fun (acc : F1) t => let q := dv X[i * inW + t]! * dv W[j * inW + t]! F1.mk (acc.s + q) (acc.t + q.abs)) ⟨0, 0⟩ s = r.s ∧ e = r.t) : fin inW relu X W b i j s (nfOf inW) e b[j]! = C_out inW relu X W b i j := by obtain ⟨h1, h2⟩ := hs unfold fin dsimp only by_cases h : ziv (s + dv b[j]!) (nfOf inW * (e + (dv b[j]!).abs)) = 0xFFFFFFFF · rw [ite_eq_left (by simpa using h)] unfold slowOut C_out rw [dotFn_eq_dotOf] · rw [ite_eq_right (by simpa using h)] unfold C_out congr 1 exact fast_tight inW G.ni (fun t => X[i * inW + t]!) (fun t => W[j * inW + t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw [← G.hb]; exact hj)) L hl hl2 hz _ _ ⟨by rw [h1], by rw [h2]⟩ h theorem C_tightGo {X W : Array UInt32} {xo wo n : USize} (hX : X.size < 2 ^ 32) (hW : W.size < 2 ^ 32) (h1 : xo.toNat + n.toNat ≤ X.size) (h2 : wo.toNat + n.toNat ≤ W.size) (d : Nat) : ∀ (t : USize) (s a : Float), t.toNat + d = n.toNat → tightGo X W xo wo n t s a = (List.range' t.toNat d).foldl (fun (acc : F1) k => let q := dv X[xo.toNat + k]! * dv W[wo.toNat + k]! F1.mk (acc.s + q) (acc.t + q.abs)) ⟨s, a⟩ := by induction d with | zero => intro t s a he rw [tightGo, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)] rfl | succ d ih => intro t s a he have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega rw [tightGo, dite_eq_left ht, List.range'_succ, List.foldl_cons] dsimp only rw [C_rdW _ _ hX, C_rdW _ _ hW, uadd _ _ (by omega), uadd _ _ (by omega)] rw [ih (t + 1) _ _ (by rw [usz t n ht]; omega), usz t n ht] /-- `fin8` is the spec's word as soon as the coarse filter is right when it decides. -/ theorem C_fin8g (G : C_G rows inW outW X W b) (relu : Bool) {i j : Nat} (hi : i < rows) (hj : j < outW) {s a : Float} {hh : Word} (hc : ziv (s + dv b[j]!) (nfOf inW * (dv hh * a + (dv b[j]!).abs)) ≠ 0xFFFFFFFF → ziv (s + dv b[j]!) (nfOf inW * (dv hh * a + (dv b[j]!).abs)) = dotOf (inW + 1) (fun t => if t < inW then X[i * inW + t]! else oneW) (fun t => if t < inW then W[j * inW + t]! else b[j]!)) : fin8 inW relu X W b i j s (nfOf inW) a hh b[j]! inW.toUSize = C_out inW relu X W b i j := by have hin : inW < 2 ^ 32 := lt_trans G.ni (by norm_num) have hX1 : i * inW + inW ≤ X.size := by rw [← G.hX]; exact C_ib' hi (le_refl _) have hW1 : j * inW + inW ≤ W.size := by rw [← G.hW]; exact C_ib' hj (le_refl _) have sX := G.sX have sW := G.sW have e1 : (i * inW).toUSize.toNat = i * inW := C_tn _ (by omega) have e2 : (j * inW).toUSize.toNat = j * inW := C_tn _ (by omega) have e3 : inW.toUSize.toNat = inW := C_tn _ hin unfold fin8 dsimp only by_cases h : ziv (s + dv b[j]!) (nfOf inW * (dv hh * a + (dv b[j]!).abs)) = 0xFFFFFFFF · rw [ite_eq_left (by simpa using h)] have e := C_tightGo (xo := (i * inW).toUSize) (wo := (j * inW).toUSize) (n := inW.toUSize) G.sX G.sW (by rw [e1, e3]; exact hX1) (by rw [e2, e3]; exact hW1) inW 0 0 0 (by rw [e3, show (0 : USize).toNat = 0 from rfl, Nat.zero_add]) rw [show (0 : USize).toNat = 0 from rfl, e1, e2, ← List.range_eq_range'] at e rw [C_fin G relu hi hj (List.range inW) List.nodup_range (fun t ht => List.mem_range.mp ht) (fun t ht h3 => absurd (List.mem_range.mpr ht) h3) (s := (tightGo X W (i * inW).toUSize (j * inW).toUSize inW.toUSize 0 0 0).s) (e := (tightGo X W (i * inW).toUSize (j * inW).toUSize inW.toUSize 0 0 0).t) ⟨by rw [e], by rw [e]⟩] · rw [ite_eq_right (by simpa using h)] unfold C_out congr 1 exact hc h theorem C_fin8 (G : C_G rows inW outW X W b) (relu : Bool) {i j : Nat} (hi : i < rows) (hj : j < outW) {s a : Float} {hh : Word} (hg : good hh = true) (hm : ∀ t, t < inW → X[i * inW + t]! &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (hs : let r := (List.range inW).foldl (fun (acc : F1) t => let v := dv W[j * inW + t]! F1.mk (acc.s + v * dv X[i * inW + t]!) (acc.t + v.abs)) ⟨0, 0⟩ s = r.s ∧ a = r.t) : fin8 inW relu X W b i j s (nfOf inW) a hh b[j]! inW.toUSize = C_out inW relu X W b i j := by obtain ⟨h1, h2⟩ := hs exact C_fin8g G relu hi hj fun h => fast_coarse inW G.ni (fun t => X[i * inW + t]!) (fun t => W[j * inW + t]!) b[j]! hh (G.gx hi) (G.gw hj) (G.gb j (by rw [← G.hb]; exact hj)) hg hm hp _ _ ⟨by rw [h1], by rw [h2]⟩ h /-- The single-row caller: `l` lists the kept positions, `hh` bounds the row, `hv` the weights. -/ theorem C_fin81 (G : C_G rows inW outW X W b) (relu : Bool) {i j : Nat} (hi : i < rows) (hj : j < outW) {s : Float} {hh hv : Word} (hg : good hh = true) (hm : ∀ t, t < inW → X[i * inW + t]! &&& 0x7FFFFFFF ≤ hh) (hp : hh ≤ 0x7FFFFFFF) (Hv : C_Hv W hv) (L : List Nat) (hl : L.Nodup) (hl2 : ∀ t ∈ L, t < inW) (hz : ∀ t, t < inW → t ∉ L → X[i * inW + t]! &&& 0x7FFFFFFF = 0) (hs : s = L.foldl (fun (acc : Float) t => acc + dv X[i * inW + t]! * dv W[j * inW + t]!) 0) : fin8 inW relu X W b i j s (nfOf inW) ((inW.toUInt64 + 1).toFloat * dv hv) hh b[j]! inW.toUSize = C_out inW relu X W b i j := by obtain ⟨g1, g2, -, g4⟩ := Hv have hW : ∀ t, t < inW → j * inW + t < W.size := fun _ ht => by rw [← G.hW]; exact C_ib hj ht exact C_fin8g G relu hi hj fun h => fast_coarse1 inW G.ni (fun t => X[i * inW + t]!) (fun t => W[j * inW + t]!) b[j]! hh hv (G.gx hi) (G.gw hj) (G.gb j (by rw [← G.hb]; exact hj)) hg g1 hm hp (fun t ht => g4 _ (hW t ht)) g2 L hl hl2 hz _ _ ⟨by rw [hs], rfl⟩ h end /-! ## Single rows -/ theorem C_fu (x : FloatArray) (i : USize) (h : i.toNat < x.size) : x.uget i h = x.data[i.toNat]! := by obtain ⟨ds⟩ := x have h' : i.toNat < ds.size := h rw [getElem!_pos ds i.toNat h']; rfl theorem C_au (a : Array UInt32) (i : USize) (h : i.toNat < a.size) : a.uget i h = a[i.toNat]! := by rw [getElem!_pos a i.toNat h]; rfl theorem C_wrF_size (a : FloatArray) (i : USize) (v : Float) : (wrF a i v).size = a.size := by unfold wrF; split · obtain ⟨ds⟩ := a; exact Array.size_uset _ · rfl theorem C_wrF_get (a : FloatArray) (i : USize) (v : Float) (q : Nat) (hs : a.size < 2 ^ 32) (hi : i.toNat < a.size) : (wrF a i v).data[q]! = if q = i.toNat then v else a.data[q]! := by have h : i < a.usize := by rw [USize.lt_iff_toNat_lt, FloatArray.usize]; rwa [C_tn _ hs] unfold wrF; rw [dite_eq_left h] obtain ⟨ds⟩ := a have hi' : i.toNat < ds.size := hi show (ds.set i.toNat v hi')[q]! = if q = i.toNat then v else ds[q]! by_cases hq : q < ds.size · have h3 : q < (ds.set i.toNat v hi').size := by simpa using hq rw [getElem!_pos _ q h3, getElem!_pos ds q hq] simp only [Array.getElem_set] by_cases e : q = i.toNat <;> simp [e, eq_comm] · have h3 : ¬ q < (ds.set i.toNat v hi').size := by simpa using hq rw [getElem!_neg _ q h3, getElem!_neg ds q hq, ite_eq_right (by omega)] /-- The positions kept by `decR` among the first `t`. -/ def C_keep (xw : Nat → Word) (t : Nat) : List Nat := (List.range t).filter (fun p => xw p &&& 0x7FFFFFFF != 0) theorem C_keep_len (xw : Nat → Word) (t : Nat) : (C_keep xw t).length ≤ t := by have h := List.length_filter_le (fun p => xw p &&& 0x7FFFFFFF != 0) (List.range t) simpa [C_keep] using h theorem C_keep_skip (xw : Nat → Word) (t : Nat) (h : xw t &&& 0x7FFFFFFF = 0) : C_keep xw (t + 1) = C_keep xw t := by simp [C_keep, List.range_succ, List.filter_append, h] theorem C_keep_add (xw : Nat → Word) (t : Nat) (h : ¬ xw t &&& 0x7FFFFFFF = 0) : C_keep xw (t + 1) = C_keep xw t ++ [t] := by simp [C_keep, List.range_succ, List.filter_append, h] /-- `decR`'s state after position `t`. -/ def C_Dec (xw : Nat → Word) (sz t : Nat) (ix : Array UInt32) (buf : FloatArray) (c : USize) (hh : Word) : Prop := c.toNat = (C_keep xw t).length ∧ ix.size = sz ∧ buf.size = sz ∧ (∀ k p : Nat, (C_keep xw t)[k]? = some p → (ix[k]! : UInt32).toNat = p ∧ buf.data[k]! = dv (xw p)) ∧ C_hok xw t hh theorem C_decR {X : Array UInt32} {o n : USize} (xw : Nat → Word) (hn : n.toNat < 2 ^ 32) (hxw : ∀ t : USize, t < n → rdW X (o + t) = xw t.toNat) (hg : ∀ t, t < n.toNat → good (xw t) = true) (d : Nat) : ∀ (t : USize) (ix : Array UInt32) (buf : FloatArray) (c : USize) (hh : Word), t.toNat + d = n.toNat → C_Dec xw n.toNat t.toNat ix buf c hh → C_Dec xw n.toNat n.toNat (decR X o n t ix buf c hh).ix (decR X o n t ix buf c hh).buf (decR X o n t ix buf c hh).c (decR X o n t ix buf c hh).hi := by induction d with | zero => intro t ix buf c hh he H rw [decR, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)] have e : t.toNat = n.toNat := by omega rw [e] at H; exact H | succ d ih => intro t ix buf c hh he H have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have hu := usz t n ht rw [decR, dite_eq_left ht] dsimp only rw [hxw t ht] obtain ⟨h1, h2, h3, h4, h5⟩ := H by_cases hz : xw t.toNat &&& 0x7FFFFFFF = 0 · rw [ite_eq_left (by simpa using hz)] apply ih (t + 1) _ _ _ _ (by omega) rw [hu, C_Dec, C_keep_skip xw _ hz] exact ⟨h1, h2, h3, h4, C_hok_skip hz h5⟩ · rw [ite_eq_right (by simpa using hz)] apply ih (t + 1) _ _ _ _ (by omega) have hk := C_keep_len xw t.toNat have hc : c.toNat < t.toNat + 1 := by omega rw [hu, C_Dec, C_keep_add xw _ hz] have hcn : c.toNat < n.toNat := by omega refine ⟨?_, by rw [C_wrW_size]; exact h2, by rw [C_wrF_size]; exact h3, fun k p hk2 => ?_, C_hok_step (hg _ (by omega)) h5⟩ · rw [uadd _ _ (by rw [USize.toNat_one]; omega), USize.toNat_one, h1]; simp · rw [C_wrW_get _ _ _ _ (by omega) (by omega), C_wrF_get _ _ _ _ (by omega) (by omega)] rw [List.getElem?_append, ← h1] at hk2 by_cases e : k = c.toNat · subst e simp at hk2 subst hk2 simp omega · rw [ite_eq_right e, ite_eq_right e] by_cases e2 : k < c.toNat · rw [ite_eq_left e2] at hk2; exact h4 k p hk2 · rw [ite_eq_right e2] at hk2 obtain ⟨m, hm⟩ : ∃ m, k - c.toNat = m + 1 := ⟨k - c.toNat - 1, by omega⟩ rw [hm] at hk2 simp at hk2 theorem C_fpush_size (a : FloatArray) (v : Float) : (a.push v).size = a.size + 1 := by obtain ⟨ds⟩ := a exact Array.size_push _ theorem C_pushZ (n : USize) (d : Nat) : ∀ (k : USize) (a : FloatArray), k.toNat + d = n.toNat → (pushZ n k a).size = a.size + d := by induction d with | zero => intro k a he rw [pushZ, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)]; rfl | succ d ih => intro k a he have ht : k < n := by rw [USize.lt_iff_toNat_lt]; omega rw [pushZ, dite_eq_left ht, ih (k + 1) _ (by rw [usz k n ht]; omega), C_fpush_size] omega theorem C_go1 {x : FloatArray} {ix W : Array UInt32} {w0 m n : USize} (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (xf : Nat → Float) (ww : Nat → Word) (hww : ∀ p, p < m.toNat → W[w0.toNat + p]! = ww p) (L : List Nat) : ∀ (t : USize) (a : Float), t.toNat + L.length = n.toNat → (∀ k p : Nat, L[k]? = some p → p < m.toNat ∧ (ix[t.toNat + k]! : UInt32).toNat = p ∧ x.data[t.toNat + k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a = L.foldl (fun (acc : Float) p => acc + xf p * dv (ww p)) a := by induction L with | nil => intro t a he _ rw [go1, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; simp at he; omega)] rfl | cons p L ih => intro t a he H simp only [List.length_cons] at he have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have htn : t.toNat < n.toNat := by omega obtain ⟨h1, h2, h3⟩ := H 0 p rfl rw [Nat.add_zero] at h2 h3 have hit : t.toNat < ix.size := by omega have e0 : (ix.uget t hit).toUSize.toNat = p := by rw [UInt32.toNat_toUSize, C_au]; exact h2 have hm : (ix.uget t hit).toUSize < m := by rw [USize.lt_iff_toNat_lt, e0]; exact h1 have e1 : x.uget t (by omega) = xf p := by rw [C_fu, h3] have e2 : ∀ pf, W.uget (w0 + (ix.uget t hit).toUSize) pf = ww p := by intro pf rw [C_au, uadd _ _ (by rw [e0]; omega), e0, hww p h1] rw [go1, dite_eq_left ht] dsimp only rw [dite_eq_left hm] rw [e1, e2, ih (t + 1) _ (by rw [usz t n ht]; omega), List.foldl_cons] intro k q hq have := H (k + 1) q (by simpa using hq) rw [usz t n ht, show t.toNat + 1 + k = t.toNat + (k + 1) by omega] exact this theorem C_go4 {x : FloatArray} {ix W : Array UInt32} {w0 w1 w2 w3 m n : USize} (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (h1 : w1.toNat + m.toNat ≤ W.size) (h2 : w2.toNat + m.toNat ≤ W.size) (h3 : w3.toNat + m.toNat ≤ W.size) (k : Nat) : ∀ (t : USize) (a b c d : Float), t.toNat + k = n.toNat → go4 x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a, go1 x ix W w1 m n t hx hi hs h1 b, go1 x ix W w2 m n t hx hi hs h2 c, go1 x ix W w3 m n t hx hi hs h3 d⟩ := by induction k with | zero => intro t a b c d he have hn : ¬ t < n := by rw [USize.lt_iff_toNat_lt]; omega rw [go4, go1.eq_1 x ix W w0, go1.eq_1 x ix W w1, go1.eq_1 x ix W w2, go1.eq_1 x ix W w3] simp only [dite_eq_right hn] | succ k ih => intro t a b c d he have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have htn : t.toNat < n.toNat := by omega rw [go4, go1.eq_1 x ix W w0, go1.eq_1 x ix W w1, go1.eq_1 x ix W w2, go1.eq_1 x ix W w3] simp only [dite_eq_left ht] by_cases hm : (ix.uget t (by omega)).toUSize < m · simp only [dite_eq_left hm] exact ih (t + 1) _ _ _ _ (by rw [usz t n ht]; omega) · simp only [dite_eq_right hm] exact ih (t + 1) _ _ _ _ (by rw [usz t n ht]; omega) /-- `go4B` is `go4`: every row entry is `dv` of a good word. -/ theorem C_go4B {x : FloatArray} {ix W : Array UInt32} {w0 w1 w2 w3 m n : USize} (hx : n.toNat ≤ x.size) (hi : n.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : w0.toNat + m.toNat ≤ W.size) (h1 : w1.toNat + m.toNat ≤ W.size) (h2 : w2.toNat + m.toNat ≤ W.size) (h3 : w3.toNat + m.toNat ≤ W.size) (hv : ∀ j, j < n.toNat → ∃ w, good w = true ∧ x.data[j]! = dv w) (k : Nat) : ∀ (t : USize) (a b c d : Float), t.toNat + k = n.toNat → go4B x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = go4 x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d := by induction k with | zero => intro t a b c d he have hn : ¬ t < n := by rw [USize.lt_iff_toNat_lt]; omega rw [go4B, go4] simp only [dite_eq_right hn] | succ k ih => intro t a b c d he have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have htn : t.toNat < n.toNat := by omega rw [go4B, go4] simp only [dite_eq_left ht] obtain ⟨w, g, e⟩ := hv _ htn have B : ∀ (u : UInt32) p0, dvB (x.uget t p0) u = x.uget t p0 * dv u := by intro u p0 rw [C_fu, e] exact A_dvB w u g by_cases hm : (ix.uget t (by omega)).toUSize < m · simp only [dite_eq_left hm, B] exact ih (t + 1) _ _ _ _ (by rw [usz t n ht]; omega) · simp only [dite_eq_right hm] exact ih (t + 1) _ _ _ _ (by rw [usz t n ht]; omega) /-- The spec's word at flat position `q`. -/ def C_sp (inW outW : Nat) (relu : Bool) (X W b : Array Word) (q : Nat) : Word := C_out inW relu X W b (q / outW) (q % outW) theorem C_sp_eq (inW outW : Nat) (relu : Bool) (X W b : Array Word) {i j : Nat} (hj : j < outW) : C_sp inW outW relu X W b (i * outW + j) = C_out inW relu X W b i j := by unfold C_sp have h0 : 0 < outW := by omega rw [Nat.mul_comm i outW, Nat.mul_add_div h0, Nat.mul_add_mod, Nat.div_eq_of_lt hj, Nat.mod_eq_of_lt hj, Nat.add_zero] theorem C_Inv_nxt {N : Nat} {sp : Nat → Word} {p : Nat} {out : Array Word} (hN : N < 2 ^ 32) (h : C_Inv N sp (fun q => q < p) out) {u : USize} {v : Word} (hu : u.toNat = p) (hp : p < N) (hv : v = sp p) : C_Inv N sp (fun q => q < p + 1) (wrW out u v) := C_Inv_mono (C_Inv_wr hN h hu hp hv) (fun q _ (hq : q < p + 1) => (by omega : q = p ∨ q < p)) /-- What `decR` leaves for row `i`. -/ structure C_Row (inW : Nat) (X : Array Word) (i : Nat) (ix : Array UInt32) (x : FloatArray) (c : USize) (L : List Nat) (hh : Word) : Prop where len : c.toNat = L.length cle : L.length ≤ inW six : ix.size = inW sx : x.size = inW nd : L.Nodup lt : ∀ t ∈ L, t < inW hz : ∀ t, t < inW → t ∉ L → X[i * inW + t]! &&& 0x7FFFFFFF = 0 rd : ∀ k p : Nat, L[k]? = some p → (ix[k]! : UInt32).toNat = p ∧ x.data[k]! = dv X[i * inW + p]! ok : C_hok (fun t => X[i * inW + t]!) inW hh section variable {rows inW outW : Nat} {X W b : Array Word} theorem C_G.nm (G : C_G rows inW outW X W b) : inW.toUSize.toNat = inW := C_tn _ (lt_trans G.ni (by norm_num)) theorem C_G.wj (G : C_G rows inW outW X W b) {j : Nat} {u : USize} (hu : u.toNat = j) (hj : j < outW) : (u * inW.toUSize).toNat = j * inW ∧ (u * inW.toUSize).toNat + inW.toUSize.toNat ≤ W.size := by have h1 : j * inW + inW ≤ outW * inW := C_ib' hj (le_refl _) have h2 := G.hW have h3 := G.sW have e : (u * inW.toUSize).toNat = j * inW := by rw [C_umul _ _ (by rw [hu, G.nm]; omega), hu, G.nm] exact ⟨e, by rw [e, G.nm]; omega⟩ theorem C_col (G : C_G rows inW outW X W b) (relu : Bool) {i j : Nat} (hi : i < rows) (hj : j < outW) {hv : Word} (Hv : C_Hv W hv) {ix : Array UInt32} {x : FloatArray} {c : USize} {L : List Nat} {hh : Word} (Rw : C_Row inW X i ix x c L hh) {u : USize} (hu : u.toNat = j) (hx : c.toNat ≤ x.size) (hi' : c.toNat ≤ ix.size) (hs : W.size < 2 ^ 32) (h0 : (u * inW.toUSize).toNat + inW.toUSize.toNat ≤ W.size) : fin8 inW relu X W b i j (go1 x ix W (u * inW.toUSize) inW.toUSize c 0 hx hi' hs h0 0) (nfOf inW) ((inW.toUInt64 + 1).toFloat * dv hv) hh (rdW b u) inW.toUSize = C_out inW relu X W b i j := by have e := C_go1 hx hi' hs h0 (fun p => dv X[i * inW + p]!) (fun p => W[j * inW + p]!) (fun p _ => by rw [(G.wj hu hj).1]) L 0 0 (by rw [show (0 : USize).toNat = 0 from rfl, Nat.zero_add]; exact Rw.len.symm) (fun k p hk => by have h1 := Rw.rd k p hk rw [show (0 : USize).toNat = 0 from rfl, Nat.zero_add, G.nm] exact ⟨Rw.lt p (List.mem_of_getElem? hk), h1⟩) rw [C_rdW _ _ G.sb, hu] exact C_fin81 G relu hi hj Rw.ok.1 Rw.ok.2.1 Rw.ok.2.2 Hv L Rw.nd Rw.lt Rw.hz e theorem C_cols1 (G : C_G rows inW outW X W b) (relu : Bool) {i : Nat} (hi : i < rows) {hv : Word} (Hv : C_Hv W hv) {ix : Array UInt32} {x : FloatArray} {c : USize} {L : List Nat} {hh : Word} (Rw : C_Row inW X i ix x c L hh) {base : USize} (hb : base.toNat = i * outW) (d : Nat) : ∀ (j : Nat) (out : Array Word), j + d = outW → C_Inv (rows * outW) (C_sp inW outW relu X W b) (fun q => q < i * outW + j) out → C_Inv (rows * outW) (C_sp inW outW relu X W b) (fun q => q < i * outW + outW) (cols1 inW outW relu X W b i ix x (nfOf inW) ((inW.toUInt64 + 1).toFloat * dv hv) hh inW.toUSize c j base out) := by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN := G.sO have sW := G.sW have hbs := G.sb have hob := G.hb have hle := Rw.cle have h1c : c.toNat ≤ x.size := by rw [Rw.sx, Rw.len]; exact hle have h2c : c.toNat ≤ ix.size := by rw [Rw.six, Rw.len]; exact hle have l2 : (2 : USize).toNat = 2 := uk 2 (by norm_num) have l3 : (3 : USize).toNat = 3 := uk 3 (by norm_num) have hsp := fun {i' j' : Nat} (h : j' < outW) => (C_sp_eq inW outW relu X W b (i := i') (j := j') h).symm by_cases h4 : j + 4 ≤ outW · have hP := C_ib hi (show j + 3 < outW by omega) have e0 : j.toUSize.toNat = j := C_tn _ (by omega) have e1 : (j.toUSize + 1).toNat = j + 1 := by rw [uadd _ _ (by rw [e0, USize.toNat_one]; omega), e0, USize.toNat_one] have e2 : (j.toUSize + 2).toNat = j + 2 := by rw [uadd _ _ (by rw [e0, l2]; omega), e0, l2] have e3 : (j.toUSize + 3).toNat = j + 3 := by rw [uadd _ _ (by rw [e0, l3]; omega), e0, l3] have g0 := (G.wj e0 (by omega)).2 have g1 := (G.wj e1 (by omega)).2 have g2 := (G.wj e2 (by omega)).2 have g3 := (G.wj e3 (by omega)).2 have a0 : (base + j.toUSize).toNat = i * outW + j := by rw [uadd _ _ (by rw [hb, e0]; omega), hb, e0] have a1 : (base + j.toUSize + 1).toNat = i * outW + j + 1 := by rw [uadd _ _ (by rw [a0, USize.toNat_one]; omega), a0, USize.toNat_one] have a2 : (base + j.toUSize + 2).toNat = i * outW + j + 1 + 1 := by rw [uadd _ _ (by rw [a0, l2]; omega), a0, l2] have a3 : (base + j.toUSize + 3).toNat = i * outW + j + 1 + 1 + 1 := by rw [uadd _ _ (by rw [a0, l3]; omega), a0, l3] have o0 := C_col G relu hi (show j < outW by omega) Hv Rw e0 h1c h2c sW g0 have o1 := C_col G relu hi (show j + 1 < outW by omega) Hv Rw e1 h1c h2c sW g1 have o2 := C_col G relu hi (show j + 2 < outW by omega) Hv Rw e2 h1c h2c sW g2 have o3 := C_col G relu hi (show j + 3 < outW by omega) Hv Rw e3 h1c h2c sW g3 rw [cols1, dite_eq_left h4] dsimp only rw [dite_eq_left ⟨h1c, h2c, sW, g0, g1, g2, g3⟩] rw [C_go4B _ _ _ _ _ _ _ (fun j hj => by rw [Rw.len] at hj have hp := List.getElem?_eq_getElem hj exact ⟨_, G.gx hi _ (Rw.lt _ (List.mem_of_getElem? hp)), (Rw.rd j _ hp).2⟩) c.toNat 0 _ _ _ _ (by rw [show (0 : USize).toNat = 0 from rfl, Nat.zero_add]), ite_self] rw [C_go4 _ _ _ _ _ _ _ c.toNat 0 _ _ _ _ (by rw [show (0 : USize).toNat = 0 from rfl, Nat.zero_add])] dsimp only rw [o0, o1, o2, o3] refine ih (d - 4) (by omega) (j + 4) _ (by omega) ?_ have s1 : C_out inW relu X W b i (j + 1) = C_sp inW outW relu X W b (i * outW + j + 1) := by rw [Nat.add_assoc]; exact hsp (by omega) have s2 : C_out inW relu X W b i (j + 2) = C_sp inW outW relu X W b (i * outW + j + 1 + 1) := by rw [show i * outW + j + 1 + 1 = i * outW + (j + 2) by omega]; exact hsp (by omega) have s3 : C_out inW relu X W b i (j + 3) = C_sp inW outW relu X W b (i * outW + j + 1 + 1 + 1) := by rw [show i * outW + j + 1 + 1 + 1 = i * outW + (j + 3) by omega]; exact hsp (by omega) rw [show i * outW + (j + 4) = i * outW + j + 1 + 1 + 1 + 1 by omega] exact C_Inv_nxt hN (C_Inv_nxt hN (C_Inv_nxt hN (C_Inv_nxt hN H a0 (by omega) (hsp (by omega))) a1 (by omega) s1) a2 (by omega) s2) a3 (by omega) s3 · by_cases h1 : j < outW · have hP := C_ib hi h1 have e0 : j.toUSize.toNat = j := C_tn _ (by omega) have g0 := (G.wj e0 h1).2 have a0 : (base + j.toUSize).toNat = i * outW + j := by rw [uadd _ _ (by rw [hb, e0]; omega), hb, e0] have o0 := C_col G relu hi h1 Hv Rw e0 h1c h2c sW g0 rw [cols1, dite_eq_right h4, dite_eq_left h1] dsimp only rw [dite_eq_left ⟨h1c, h2c, sW, g0⟩] rw [o0] exact ih (d - 1) (by omega) (j + 1) _ (by omega) (C_Inv_nxt hN H a0 hP (hsp h1)) · rw [cols1, dite_eq_right h4, dite_eq_right h1] have e : j = outW := by omega rw [e] at H; exact H theorem C_rows1 (G : C_G rows inW outW X W b) (relu : Bool) {hv : Word} (Hv : C_Hv W hv) (d : Nat) : ∀ (i : Nat) (ix : Array UInt32) (buf : FloatArray) (out : Array Word), i + d = rows → ix.size = inW → buf.size = inW → C_Inv (rows * outW) (C_sp inW outW relu X W b) (fun q => q < i * outW) out → C_Inv (rows * outW) (C_sp inW outW relu X W b) (fun q => q < rows * outW) (rows1 rows inW outW relu X W b (nfOf inW) ((inW.toUInt64 + 1).toFloat * dv hv) inW.toUSize i ix buf out) := by induction d with | zero => intro i ix buf out he _ _ H rw [rows1, dite_eq_right (by omega)] have e : i = rows := by omega rw [e] at H; exact H | succ d ih => intro i ix buf out he h1 h2 H have hi : i < rows := by omega have hin : inW < 2 ^ 32 := lt_trans G.ni (by norm_num) have hN := G.sO have sX := G.sX have hX := G.hX have hP : i * inW + inW ≤ rows * inW := C_ib' hi (le_refl _) have ho : (i * inW).toUSize.toNat = i * inW := C_tn _ (by omega) have hbs : (i * outW).toUSize.toNat = i * outW := by have h3 : i * outW + outW ≤ rows * outW := C_ib' hi (le_refl _) exact C_tn _ (by omega) have D := C_decR (X := X) (o := (i * inW).toUSize) (n := inW.toUSize) (fun p => X[i * inW + p]!) (by rw [G.nm]; exact hin) (fun t ht => by have h4 := USize.lt_iff_toNat_lt.mp ht rw [G.nm] at h4 rw [C_rdW _ _ sX, uadd _ _ (by rw [ho]; omega), ho]) (fun t ht => by rw [G.nm] at ht; exact G.gx hi t ht) inW 0 ix buf 0 0 (by rw [show (0 : USize).toNat = 0 from rfl, G.nm, Nat.zero_add]) ⟨by simp [C_keep], by rw [G.nm]; exact h1, by rw [G.nm]; exact h2, fun k p hk => by simp [C_keep] at hk, rfl, fun k hk => absurd hk (Nat.not_lt_zero _), (by decide)⟩ rw [G.nm] at D obtain ⟨d1, d2, d3, d4, d5⟩ := D have Rw : C_Row inW X i (decR X (i * inW).toUSize inW.toUSize 0 ix buf 0 0).ix (decR X (i * inW).toUSize inW.toUSize 0 ix buf 0 0).buf (decR X (i * inW).toUSize inW.toUSize 0 ix buf 0 0).c (C_keep (fun p => X[i * inW + p]!) inW) (decR X (i * inW).toUSize inW.toUSize 0 ix buf 0 0).hi := ⟨d1, C_keep_len _ _, d2, d3, (List.nodup_range).filter _, fun t ht => List.mem_range.mp (List.mem_filter.mp ht).1, fun t ht hn => by by_contra hc exact hn (List.mem_filter.mpr ⟨List.mem_range.mpr ht, by simpa using hc⟩), d4, d5⟩ rw [rows1, dite_eq_left hi] dsimp only refine ih (i + 1) _ _ _ (by omega) d2 d3 ?_ have h5 := C_cols1 G relu hi Hv Rw hbs outW 0 out (by omega) (by rw [Nat.add_zero]; exact H) rw [show (i + 1) * outW = i * outW + outW by rw [Nat.add_mul, Nat.one_mul]] exact h5 end /-! ## Eight rows -/ theorem C_fpg (xs : FloatArray) (v : Float) (q : Nat) : (xs.push v).data[q]! = if q = xs.size then v else xs.data[q]! := by obtain ⟨ds⟩ := xs show (ds.push v)[q]! = if q = ds.size then v else ds[q]! by_cases hq : q < ds.size · have h3 : q < (ds.push v).size := by simp; omega rw [getElem!_pos _ q h3, getElem!_pos ds q hq, ite_eq_right (by omega), Array.getElem_push_lt] · by_cases e : q = ds.size · subst e have h3 : ds.size < (ds.push v).size := by simp rw [getElem!_pos (ds.push v) ds.size h3, ite_eq_left rfl, Array.getElem_push_eq] · have h3 : ¬ q < (ds.push v).size := by simp; omega rw [getElem!_neg _ q h3, getElem!_neg ds q hq, ite_eq_right e] theorem C_p8_new (xs : FloatArray) (a : Nat → Float) (r : Nat) (hr : r < 8) : (((((((((xs.push (a 0)).push (a 1)).push (a 2)).push (a 3)).push (a 4)).push (a 5)).push (a 6)).push (a 7))).data[xs.size + r]! = a r := by simp only [C_fpg, C_fpush_size] interval_cases r <;> repeat (first | rw [ite_eq_left (by omega)] | rw [ite_eq_right (by omega)]) theorem C_p8_old (xs : FloatArray) (a0 a1 a2 a3 a4 a5 a6 a7 : Float) (q : Nat) (hq : q < xs.size) : (((((((((xs.push a0).push a1).push a2).push a3).push a4).push a5).push a6).push a7)).data[q]! = xs.data[q]! := by simp only [C_fpg, C_fpush_size] rw [ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_right (by omega)] /-- The eight maxima of `dec8`, by row. -/ def C_hf (d : H8) : Nat → UInt32 | 0 => d.h0 | 1 => d.h1 | 2 => d.h2 | 3 => d.h3 | 4 => d.h4 | 5 => d.h5 | 6 => d.h6 | _ => d.h7 /-- The eight sums of `go8`, by row. -/ def C_fs (d : F8) : Nat → Float | 0 => d.s0 | 1 => d.s1 | 2 => d.s2 | 3 => d.s3 | 4 => d.s4 | 5 => d.s5 | 6 => d.s6 | _ => d.s7 /-- `dec8`'s state after `t` columns of rows of width `m`. -/ def C_D8 (X : Array Word) (m t : Nat) (d : H8) : Prop := d.xs.size = 8 * t ∧ (∀ k r : Nat, k < t → r < 8 → d.xs.data[8 * k + r]! = dv X[r * m + k]!) ∧ ∀ r : Nat, r < 8 → C_hok (fun k => X[r * m + k]!) t (C_hf d r) theorem C_u8 {n t c : USize} {k : Nat} (hc : c.toNat = k) (h : k * n.toNat + t.toNat < 2 ^ 32) : (c * n + t).toNat = k * n.toNat + t.toNat := by have h0 : k * n.toNat < 2 ^ 32 := by omega rw [uadd _ _ (by rw [C_umul _ _ (by rw [hc]; exact h0), hc]; exact h), C_umul _ _ (by rw [hc]; exact h0), hc] theorem C_r8 {X : Array Word} {n t c : USize} {k : Nat} (hs : X.size < 2 ^ 32) (hc : c.toNat = k) (h : k * n.toNat + t.toNat < 2 ^ 32) : rdW X (c * n + t) = X[k * n.toNat + t.toNat]! := by rw [C_rdW _ _ hs, C_u8 hc h] theorem C_dec8 {X : Array Word} {n : USize} (hs : X.size < 2 ^ 32) (h8 : 8 * n.toNat ≤ X.size) (hg : ∀ q, q < X.size → good X[q]! = true) (d : Nat) : ∀ (t : USize) (xs : FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7 : UInt32), t.toNat + d = n.toNat → C_D8 X n.toNat t.toNat ⟨xs, h0, h1, h2, h3, h4, h5, h6, h7⟩ → C_D8 X n.toNat n.toNat (dec8 X n t xs h0 h1 h2 h3 h4 h5 h6 h7) := by induction d with | zero => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H rw [dec8, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)] have e : t.toNat = n.toNat := by omega rw [e] at H; exact H | succ d ih => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have hu := usz t n ht have l2 : (2 : USize).toNat = 2 := uk 2 (by norm_num) have l3 : (3 : USize).toNat = 3 := uk 3 (by norm_num) have l4 : (4 : USize).toNat = 4 := uk 4 (by norm_num) have l5 : (5 : USize).toNat = 5 := uk 5 (by norm_num) have l6 : (6 : USize).toNat = 6 := uk 6 (by norm_num) have l7 : (7 : USize).toNat = 7 := uk 7 (by norm_num) have r0 : rdW X t = X[0 * n.toNat + t.toNat]! := by rw [C_rdW _ _ hs, Nat.zero_mul, Nat.zero_add] have r1 : rdW X (n + t) = X[1 * n.toNat + t.toNat]! := by rw [C_rdW _ _ hs, uadd _ _ (by omega), Nat.one_mul] rw [dec8, dite_eq_left ht] dsimp only rw [r0, r1, C_r8 hs l2 (by omega), C_r8 hs l3 (by omega), C_r8 hs l4 (by omega), C_r8 hs l5 (by omega), C_r8 hs l6 (by omega), C_r8 hs l7 (by omega)] obtain ⟨H1, H2, H3⟩ := H have hgood : ∀ r, r < 8 → good X[r * n.toNat + t.toNat]! = true := by intro r hr apply hg interval_cases r <;> omega dsimp only at H1 H2 refine ih (t + 1) _ _ _ _ _ _ _ _ _ (by omega) ⟨?_, ?_, ?_⟩ · simp only [C_fpush_size, H1, hu]; omega · intro k r hk hr rw [hu] at hk by_cases e : k = t.toNat · subst e have h9 := C_p8_new xs (fun r => dv X[r * n.toNat + t.toNat]!) r hr rw [H1] at h9 exact h9 · have h10 : 8 * k + r < xs.size := by omega rw [C_p8_old _ _ _ _ _ _ _ _ _ _ h10] exact H2 k r (by omega) hr · intro r hr have h9 := H3 r hr have h10 := hgood r hr rw [hu] interval_cases r <;> exact C_hok_step h10 h9 /-- The fold `fast_coarse` asks for. -/ def C_cf (xf : Nat → Float) (ww : Nat → Word) (l : List Nat) (acc : F1) : F1 := l.foldl (fun (acc : F1) t => let v := dv (ww t) F1.mk (acc.s + v * xf t) (acc.t + v.abs)) acc theorem C_xs {xs : FloatArray} {i t n : USize} {xf : Nat → Nat → Float} (hxf : ∀ r k : Nat, r < 8 → k < n.toNat → xs.data[8 * k + r]! = xf r k) (hi : i.toNat = 8 * t.toNat) (ht : t.toNat < n.toNat) (hsx : xs.size < 2 ^ 32) (hx : 8 * n.toNat ≤ xs.size) (r : Nat) (hr : r < 8) (pf : (i + USize.ofNat r).toNat < xs.size) : xs.uget (i + USize.ofNat r) pf = xf r t.toNat := by rw [C_fu, uadd _ _ (by rw [uk r (by omega)]; omega), uk r (by omega), hi, hxf r _ hr ht] theorem C_go8 {xs : FloatArray} {W : Array UInt32} {wo n : USize} (hx : 8 * n.toNat ≤ xs.size) (hsx : xs.size < 2 ^ 32) (hs : W.size < 2 ^ 32) (hw : wo.toNat + n.toNat ≤ W.size) (xf : Nat → Nat → Float) (ww : Nat → Word) (hxf : ∀ r k : Nat, r < 8 → k < n.toNat → xs.data[8 * k + r]! = xf r k) (hww : ∀ k, k < n.toNat → W[wo.toNat + k]! = ww k) (d : Nat) : ∀ (t i : USize) (hi : i.toNat = 8 * t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a : Float), t.toNat + d = n.toNat → ∀ r, r < 8 → F1.mk (C_fs (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a) r) (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a).t = C_cf (xf r) ww (List.range' t.toNat d) ⟨C_fs ⟨s0, s1, s2, s3, s4, s5, s6, s7, a⟩ r, a⟩ := by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a he r hr rw [go8, dite_eq_right (by rw [USize.lt_iff_toNat_lt]; omega)] rfl | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a he r hr have ht : t < n := by rw [USize.lt_iff_toNat_lt]; omega have htn : t.toNat < n.toNat := by omega have hu := usz t n ht have e : dv (W.uget (wo + t) (wi wo t n W hw hs htn)) = dv (ww t.toNat) := by rw [C_au, uadd _ _ (by omega), hww _ htn] rw [go8, dite_eq_left ht] dsimp only rw [e, C_xs hxf hi htn hsx hx 0 (by omega), C_xs hxf hi htn hsx hx 1 (by omega), C_xs hxf hi htn hsx hx 2 (by omega), C_xs hxf hi htn hsx hx 3 (by omega), C_xs hxf hi htn hsx hx 4 (by omega), C_xs hxf hi htn hsx hx 5 (by omega), C_xs hxf hi htn hsx hx 6 (by omega), C_xs hxf hi htn hsx hx 7 (by omega)] rw [ih (t + 1) _ _ _ _ _ _ _ _ _ _ _ (by omega) r hr, hu, List.range'_succ] unfold C_cf rw [List.foldl_cons] interval_cases r <;> rfl theorem C_cover {outW q j : Nat} (hj : j < outW) (hq : q < 8 * outW) (h : q % outW < j + 1) : q = 7 * outW + j ∨ q = 6 * outW + j ∨ q = 5 * outW + j ∨ q = 4 * outW + j ∨ q = 3 * outW + j ∨ q = 2 * outW + j ∨ q = 1 * outW + j ∨ q = 0 * outW + j ∨ q % outW < j := by by_cases e : q % outW < j · simp only [e, or_true] · have e2 : q % outW = j := by omega have e3 := Nat.div_add_mod q outW have e4 : q / outW < 8 := Nat.div_lt_of_lt_mul (by rwa [Nat.mul_comm]) rw [e2, Nat.mul_comm] at e3 generalize q / outW = a at e3 e4 interval_cases a <;> omega section variable {inW outW : Nat} {X W b : Array Word} theorem C_cols8 (G : C_G 8 inW outW X W b) (relu : Bool) {d : H8} (D : C_D8 X inW inW d) (k : Nat) : ∀ (j : Nat) (out : Array Word), j + k = outW → C_Inv (8 * outW) (C_sp inW outW relu X W b) (fun q => q % outW < j) out → C_Inv (8 * outW) (C_sp inW outW relu X W b) (fun q => q % outW < outW) (cols8 inW outW relu X W b d (nfOf inW) inW.toUSize j out) := by induction k with | zero => intro j out he H rw [cols8, dite_eq_right (by omega)] have e : j = outW := by omega rw [e] at H; exact H | succ k ih => intro j out he H have hj : j < outW := by omega have hN := G.sO have hbs := G.sb have hob := G.hb have hnm := G.nm have hin := G.ni have sxs : 8 * inW ≤ 8 * 2 ^ 20 := by omega have e0 : j.toUSize.toNat = j := C_tn _ (by omega) have eo : outW.toUSize.toNat = outW := C_tn _ (by omega) have g0 := G.wj e0 hj have hx : 8 * inW.toUSize.toNat ≤ d.xs.size := by rw [hnm, D.1] have hsx : d.xs.size < 2 ^ 32 := by rw [D.1]; omega have sW := G.sW have hg := g0.2 have h8 : 8 * outW < 2 ^ 32 := hN have l2 : (2 : USize).toNat = 2 := uk 2 (by norm_num) have l3 : (3 : USize).toNat = 3 := uk 3 (by norm_num) have l4 : (4 : USize).toNat = 4 := uk 4 (by norm_num) have l5 : (5 : USize).toNat = 5 := uk 5 (by norm_num) have l6 : (6 : USize).toNat = 6 := uk 6 (by norm_num) have l7 : (7 : USize).toNat = 7 := uk 7 (by norm_num) have hi0 : (0 : USize).toNat = 8 * (0 : USize).toNat := by rw [show (0 : USize).toNat = 0 from rfl] have key := C_go8 hx hsx sW hg (fun r k => dv X[r * inW + k]!) (fun k => W[j * inW + k]!) (fun r k hr hk => by rw [hnm] at hk; exact D.2.1 k r hk hr) (fun k _ => by rw [g0.1]) inW 0 0 hi0 0 0 0 0 0 0 0 0 0 (by rw [hnm, show (0 : USize).toNat = 0 from rfl, Nat.zero_add]) rw [show (0 : USize).toNat = 0 from rfl, ← List.range_eq_range'] at key rw [cols8, dite_eq_left hj] dsimp only rw [dite_eq_left ⟨hx, hsx, sW, hg⟩] generalize go8 d.xs W (j.toUSize * inW.toUSize) inW.toUSize 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 0 = R at key ⊢ have O : ∀ r, r < 8 → fin8 inW relu X W b r j (C_fs R r) (nfOf inW) R.t (C_hf d r) b[j]! inW.toUSize = C_sp inW outW relu X W b (r * outW + j) := by intro r hr rw [C_sp_eq _ _ _ _ _ _ hj] have k1 := key r hr have e1 : C_fs ⟨0, 0, 0, 0, 0, 0, 0, 0, 0⟩ r = 0 := by interval_cases r <;> rfl rw [e1] at k1 have H1 := D.2.2 r hr exact C_fin8 G relu hr hj H1.1 H1.2.1 H1.2.2 ⟨(congrArg F1.s k1 :), (congrArg F1.t k1 :)⟩ have o0 : fin8 inW relu X W b 0 j R.s0 (nfOf inW) R.t d.h0 b[j]! inW.toUSize = _ := O 0 (by omega) have o1 : fin8 inW relu X W b 1 j R.s1 (nfOf inW) R.t d.h1 b[j]! inW.toUSize = _ := O 1 (by omega) have o2 : fin8 inW relu X W b 2 j R.s2 (nfOf inW) R.t d.h2 b[j]! inW.toUSize = _ := O 2 (by omega) have o3 : fin8 inW relu X W b 3 j R.s3 (nfOf inW) R.t d.h3 b[j]! inW.toUSize = _ := O 3 (by omega) have o4 : fin8 inW relu X W b 4 j R.s4 (nfOf inW) R.t d.h4 b[j]! inW.toUSize = _ := O 4 (by omega) have o5 : fin8 inW relu X W b 5 j R.s5 (nfOf inW) R.t d.h5 b[j]! inW.toUSize = _ := O 5 (by omega) have o6 : fin8 inW relu X W b 6 j R.s6 (nfOf inW) R.t d.h6 b[j]! inW.toUSize = _ := O 6 (by omega) have o7 : fin8 inW relu X W b 7 j R.s7 (nfOf inW) R.t d.h7 b[j]! inW.toUSize = _ := O 7 (by omega) rw [C_rdW _ _ hbs, e0, o0, o1, o2, o3, o4, o5, o6, o7] have u0 : j.toUSize.toNat = 0 * outW + j := by rw [e0]; omega have u1 : (outW.toUSize + j.toUSize).toNat = 1 * outW + j := by rw [uadd _ _ (by rw [eo, e0]; omega), eo, e0]; omega have a2 := C_u8 (n := outW.toUSize) (t := j.toUSize) l2 (by rw [eo, e0]; omega) have a3 := C_u8 (n := outW.toUSize) (t := j.toUSize) l3 (by rw [eo, e0]; omega) have a4 := C_u8 (n := outW.toUSize) (t := j.toUSize) l4 (by rw [eo, e0]; omega) have a5 := C_u8 (n := outW.toUSize) (t := j.toUSize) l5 (by rw [eo, e0]; omega) have a6 := C_u8 (n := outW.toUSize) (t := j.toUSize) l6 (by rw [eo, e0]; omega) have a7 := C_u8 (n := outW.toUSize) (t := j.toUSize) l7 (by rw [eo, e0]; omega) rw [eo, e0] at a2 a3 a4 a5 a6 a7 have I0 := C_Inv_wr h8 H u0 (by omega) rfl have I1 := C_Inv_wr h8 I0 u1 (by omega) rfl have I2 := C_Inv_wr h8 I1 a2 (by omega) rfl have I3 := C_Inv_wr h8 I2 a3 (by omega) rfl have I4 := C_Inv_wr h8 I3 a4 (by omega) rfl have I5 := C_Inv_wr h8 I4 a5 (by omega) rfl have I6 := C_Inv_wr h8 I5 a6 (by omega) rfl have I7 := C_Inv_wr h8 I6 a7 (by omega) rfl exact ih (j + 1) _ (by omega) (C_Inv_mono I7 (fun q hq hq' => C_cover hj hq hq')) end theorem C_linF_eq : linF = linearSpec := by funext rows inW outW relu X W b unfold linF dsimp only split · rename_i h obtain ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13⟩ := h have Hv := C_scan_all W h8 h12 have G : C_G rows inW outW X W b := ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, (C_scan_all X h7 h11).2.2.1, Hv.2.2.1, (C_scan_all b h9 h13).2.2.1⟩ have e : linearSpec rows inW outW relu X W b = Array.ofFn (n := rows * outW) fun q => C_sp inW outW relu X W b q.val := rfl have hnf : (inW.toUInt64 + 8).toFloat * Float.ofBits 0x3CC0000000000000 = nfOf inW := rfl rw [e, hnf] split · rename_i hr have hr8 : rows = 8 := by simpa using hr subst hr8 have H0 : C_Inv (8 * outW) (C_sp inW outW relu X W b) (fun q => q % outW < 0) (Array.replicate (8 * outW) 0) := ⟨by simp, fun q _ h => absurd h (Nat.not_lt_zero _)⟩ have D0 : C_D8 X inW.toUSize.toNat (0 : USize).toNat ⟨FloatArray.emptyWithCapacity (8 * inW), 0, 0, 0, 0, 0, 0, 0, 0⟩ := by have e0 : (0 : USize).toNat = 0 := rfl rw [e0] refine ⟨rfl, fun k r hk => absurd hk (Nat.not_lt_zero _), fun r hr => ?_⟩ have g0 : good 0 = true := rfl interval_cases r <;> exact ⟨g0, fun k hk => absurd hk (Nat.not_lt_zero _), (show (0 : UInt32) ≤ 0x7FFFFFFF from by decide)⟩ have D := C_dec8 (n := inW.toUSize) G.sX (by rw [G.nm]; exact G.hX.le) G.gX inW 0 _ 0 0 0 0 0 0 0 0 (by rw [G.nm, show (0 : USize).toNat = 0 from rfl, Nat.zero_add]) D0 rw [G.nm] at D exact C_ofFn (C_cols8 G relu D outW 0 _ (by omega) H0) (fun q _ => Nat.mod_lt _ G.po) · have H0 : C_Inv (rows * outW) (C_sp inW outW relu X W b) (fun q => q < 0 * outW) (Array.replicate (rows * outW) 0) := ⟨by simp, fun q _ h => by rw [Nat.zero_mul] at h; exact absurd h (Nat.not_lt_zero _)⟩ have P0 : (pushZ inW.toUSize 0 (FloatArray.emptyWithCapacity inW)).size = inW := by rw [C_pushZ inW.toUSize inW 0 _ (by rw [G.nm, show (0 : USize).toNat = 0 from rfl, Nat.zero_add])] exact Nat.zero_add _ exact C_ofFn (C_rows1 G relu Hv rows 0 _ _ _ (by omega) (by simp) P0 H0) (fun q hq => hq) · rw [dotFn_eq_dotOf]; rfl end theorem linF_eq : linF = linearSpec := C_linF_eq def kernels : Kernels where core := specCore dense := ⟨linF, gemmSpec⟩ conv := specConv elem := specElem norm := specNorm theorem correct : Obligation kernels := by simp only [Obligation, kernels, specKernels, specDense, linF_eq] end Submission