import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats structure Acc where pos : Array Nat neg : Array Nat special : Bool negZero : Bool def Acc.empty : Acc := ⟨Array.replicate 18 0, Array.replicate 18 0, false, true⟩ def Acc.total (acc : Acc) : Int := (limbVal acc.pos.toList : Int) - limbVal acc.neg.toList @[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⟩ @[inline] def Acc.markSpecial (acc : Acc) : Acc := match acc with | ⟨pos, ng, _, nz⟩ => ⟨pos, ng, true, nz⟩ 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 section open FloatLib.Numerics 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) 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] 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 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 def topGo (r : Array Nat) : Nat → Option Nat | 0 => none | k + 1 => if r[k]! != 0 then some k else topGo r k 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] 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 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] 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 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 @[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)) @[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 @[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 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 .. 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] 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]) 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 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 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] theorem dotFn_eq_dotOf : dotFn = dotOf := funext fun n => funext fun fa => funext fun fb => dotFn_eq n fa fb 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 @[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 @[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] 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] def shiftTabs : ByteArray := ⟨(Array.range 65536).map fun i => if i % 256 = 0 then 41 else if i % 256 ≠ 255 ∧ i / 256 ≤ i % 256 ∧ i % 256 ≤ i / 256 + 7 then (i % 256 - i / 256).toUInt8 else 40⟩ theorem shiftTabs_size : shiftTabs.size = 65536 := by simp [shiftTabs, ByteArray.size] @[inline] def expU (w : UInt32) : USize := ((w >>> 23) &&& 0xFF).toUSize theorem expU_toNat (w : UInt32) : (expU w).toNat = ((w >>> 23) &&& 0xFF).toNat := by simp only [expU, UInt32.toNat_toUSize] theorem expU_lt (w : UInt32) : (expU w).toNat < 256 := by rw [expU_toNat, UInt32.toNat_and] exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) theorem usize_add_toNat (a b : USize) (h : a.toNat + b.toNat < 2 ^ 32) : (a + b).toNat = a.toNat + b.toNat := by rw [USize.toNat_add] have : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl exact Nat.mod_eq_of_lt (by omega) @[inline] def magW (o : USize) (ho : o.toNat ≤ 65280) (w : UInt32) : UInt64 := let m := ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 m <<< (shiftTabs.uget (o + expU w) (by rw [shiftTabs_size, usize_add_toNat _ _ (by have := expU_lt w; omega)] have := expU_lt w; omega)).toUInt64 @[inline] def sgnW (w : UInt32) (mag : UInt64) : UInt64 := if signOf w then 0 - mag else mag @[inline] def sx (v : UInt32) : UInt64 := v.toInt32.toInt64.toUInt64 theorem usize_succ_toNat (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) def RowsOk (IX : Array UInt32) (W : Array UInt32) (n : Nat) (w0 w1 w2 w3 : USize) : Prop := ∀ s, s < n → ∀ wr, (wr = w0 ∨ wr = w1 ∨ wr = w2 ∨ wr = w3) → wr.toNat + (IX[s]!).toNat < W.size theorem idx_ok (IX W : Array UInt32) (t wr : USize) (ht : t.toNat < IX.size) (hb : wr.toNat + (IX[t.toNat]!).toNat < W.size) (hWs : W.size < 2 ^ 32) : (wr + (IX.uget t ht).toUSize).toNat < W.size := by have he : (IX.uget t ht).toUSize.toNat = (IX[t.toNat]!).toNat := by simp only [Array.uget, UInt32.toNat_toUSize, getElem!_pos IX t.toNat ht] rw [usize_add_toNat _ _ (by omega), he]; exact hb structure Acc8 where a0 : UInt64 b0 : UInt64 a1 : UInt64 b1 : UInt64 a2 : UInt64 b2 : UInt64 a3 : UInt64 b3 : UInt64 chk : UInt64 structure Acc12 where a0 : UInt64 b0 : UInt64 c0 : UInt64 a1 : UInt64 b1 : UInt64 c1 : UInt64 a2 : UInt64 b2 : UInt64 c2 : UInt64 a3 : UInt64 b3 : UInt64 c3 : UInt64 chk : UInt64 def dot4n (IX X0 X1 W : Array UInt32) (o : USize) (ho : o.toNat ≤ 65280) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (hW : RowsOk IX W n.toNat w0 w1 w2 w3) (hWs : W.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 chk : UInt64) : Acc8 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hti : t.toNat < IX.size := by omega let x0 := sx (X0.uget t (by omega)) let x1 := sx (X1.uget t (by omega)) let v0 := W.uget (w0 + (IX.uget t hti).toUSize) (idx_ok IX W t w0 hti (hW _ ht w0 (by simp)) hWs) let v1 := W.uget (w1 + (IX.uget t hti).toUSize) (idx_ok IX W t w1 hti (hW _ ht w1 (by simp)) hWs) let v2 := W.uget (w2 + (IX.uget t hti).toUSize) (idx_ok IX W t w2 hti (hW _ ht w2 (by simp)) hWs) let v3 := W.uget (w3 + (IX.uget t hti).toUSize) (idx_ok IX W t w3 hti (hW _ ht w3 (by simp)) hWs) let m0 := magW o ho v0 let m1 := magW o ho v1 let m2 := magW o ho v2 let m3 := magW o ho v3 let s0 := sx (sgnW v0 m0).toUInt32 let s1 := sx (sgnW v1 m1).toUInt32 let s2 := sx (sgnW v2 m2).toUInt32 let s3 := sx (sgnW v3 m3).toUInt32 dot4n IX X0 X1 W o ho w0 w1 w2 w3 n hI h0 h1 hW hWs (t + 1) (a0 + x0 * s0) (b0 + x1 * s0) (a1 + x0 * s1) (b1 + x1 * s1) (a2 + x0 * s2) (b2 + x1 * s2) (a3 + x0 * s3) (b3 + x1 * s3) (chk ||| m0 ||| m1 ||| m2 ||| m3) else ⟨a0, b0, a1, b1, a2, b2, a3, b3, chk⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega def dot4w (IX X0 X1 X2 W : Array UInt32) (o : USize) (ho : o.toNat ≤ 65280) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (h2 : n.toNat ≤ X2.size) (hW : RowsOk IX W n.toNat w0 w1 w2 w3) (hWs : W.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 chk : UInt64) : Acc12 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hti : t.toNat < IX.size := by omega let x0 := sx (X0.uget t (by omega)) let x1 := sx (X1.uget t (by omega)) let x2 := sx (X2.uget t (by omega)) let v0 := W.uget (w0 + (IX.uget t hti).toUSize) (idx_ok IX W t w0 hti (hW _ ht w0 (by simp)) hWs) let v1 := W.uget (w1 + (IX.uget t hti).toUSize) (idx_ok IX W t w1 hti (hW _ ht w1 (by simp)) hWs) let v2 := W.uget (w2 + (IX.uget t hti).toUSize) (idx_ok IX W t w2 hti (hW _ ht w2 (by simp)) hWs) let v3 := W.uget (w3 + (IX.uget t hti).toUSize) (idx_ok IX W t w3 hti (hW _ ht w3 (by simp)) hWs) let m0 := magW o ho v0 let m1 := magW o ho v1 let m2 := magW o ho v2 let m3 := magW o ho v3 let s0 := sx (sgnW v0 m0).toUInt32 let s1 := sx (sgnW v1 m1).toUInt32 let s2 := sx (sgnW v2 m2).toUInt32 let s3 := sx (sgnW v3 m3).toUInt32 dot4w IX X0 X1 X2 W o ho w0 w1 w2 w3 n hI h0 h1 h2 hW hWs (t + 1) (a0 + x0 * s0) (b0 + x1 * s0) (c0 + x2 * s0) (a1 + x0 * s1) (b1 + x1 * s1) (c1 + x2 * s1) (a2 + x0 * s2) (b2 + x1 * s2) (c2 + x2 * s2) (a3 + x0 * s3) (b3 + x1 * s3) (c3 + x2 * s3) (chk ||| m0 ||| m1 ||| m2 ||| m3) else ⟨a0, b0, c0, a1, b1, c1, a2, b2, c2, a3, b3, c3, chk⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega def dot4Pn (IX X0 X1 D : Array UInt32) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (hW : RowsOk IX D n.toNat w0 w1 w2 w3) (hWs : D.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 : UInt64) : Acc8 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hti : t.toNat < IX.size := by omega let x0 := sx (X0.uget t (by omega)) let x1 := sx (X1.uget t (by omega)) let s0 := sx (D.uget (w0 + (IX.uget t hti).toUSize) (idx_ok IX D t w0 hti (hW _ ht w0 (by simp)) hWs)) let s1 := sx (D.uget (w1 + (IX.uget t hti).toUSize) (idx_ok IX D t w1 hti (hW _ ht w1 (by simp)) hWs)) let s2 := sx (D.uget (w2 + (IX.uget t hti).toUSize) (idx_ok IX D t w2 hti (hW _ ht w2 (by simp)) hWs)) let s3 := sx (D.uget (w3 + (IX.uget t hti).toUSize) (idx_ok IX D t w3 hti (hW _ ht w3 (by simp)) hWs)) dot4Pn IX X0 X1 D w0 w1 w2 w3 n hI h0 h1 hW hWs (t + 1) (a0 + x0 * s0) (b0 + x1 * s0) (a1 + x0 * s1) (b1 + x1 * s1) (a2 + x0 * s2) (b2 + x1 * s2) (a3 + x0 * s3) (b3 + x1 * s3) else ⟨a0, b0, a1, b1, a2, b2, a3, b3, 0⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega def dot4Pw (IX X0 X1 X2 D : Array UInt32) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (h2 : n.toNat ≤ X2.size) (hW : RowsOk IX D n.toNat w0 w1 w2 w3) (hWs : D.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 : UInt64) : Acc12 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hti : t.toNat < IX.size := by omega let x0 := sx (X0.uget t (by omega)) let x1 := sx (X1.uget t (by omega)) let x2 := sx (X2.uget t (by omega)) let s0 := sx (D.uget (w0 + (IX.uget t hti).toUSize) (idx_ok IX D t w0 hti (hW _ ht w0 (by simp)) hWs)) let s1 := sx (D.uget (w1 + (IX.uget t hti).toUSize) (idx_ok IX D t w1 hti (hW _ ht w1 (by simp)) hWs)) let s2 := sx (D.uget (w2 + (IX.uget t hti).toUSize) (idx_ok IX D t w2 hti (hW _ ht w2 (by simp)) hWs)) let s3 := sx (D.uget (w3 + (IX.uget t hti).toUSize) (idx_ok IX D t w3 hti (hW _ ht w3 (by simp)) hWs)) dot4Pw IX X0 X1 X2 D w0 w1 w2 w3 n hI h0 h1 h2 hW hWs (t + 1) (a0 + x0 * s0) (b0 + x1 * s0) (c0 + x2 * s0) (a1 + x0 * s1) (b1 + x1 * s1) (c1 + x2 * s1) (a2 + x0 * s2) (b2 + x1 * s2) (c2 + x2 * s2) (a3 + x0 * s3) (b3 + x1 * s3) (c3 + x2 * s3) else ⟨a0, b0, c0, a1, b1, c1, a2, b2, c2, a3, b3, c3, 0⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega theorem usize_lt_succ (t n : USize) (h : t < n) : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h structure Acc6 where a0 : UInt64 b0 : UInt64 c0 : UInt64 a1 : UInt64 b1 : UInt64 c1 : UInt64 chk : UInt64 def narrow : Bool := System.Platform.target.startsWith "x86" def decX (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (k c : USize) (IX P0 P1 P2 : Array UInt32) (hck : c.toNat ≤ k.toNat) (hs : n.toNat ≤ IX.size ∧ n.toNat ≤ P0.size ∧ n.toNat ≤ P1.size ∧ n.toNat ≤ P2.size) (ok : Bool) : Array UInt32 × Array UInt32 × Array UInt32 × Array UInt32 × USize × Bool := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h have hk1 : (k + 1).toNat = k.toNat + 1 := usize_succ_toNat k n h let w := X.uget (o + k) (by rw [usize_add_toNat _ _ (by omega)]; omega) let e := (w >>> 23) &&& 0xFF if e == 0 then decX X o n lx lim hX hXs (k + 1) c IX P0 P1 P2 (by omega) hs (ok && (w &&& 0x7FFFFF) == 0) else have hc1 : (c + 1).toNat = c.toNat + 1 := usize_succ_toNat c n (USize.lt_iff_toNat_lt.mpr (by omega)) let d := e - lx let mag := ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< d.toUInt64 let p0 := mag &&& 0x1FFFFF let p1 := (mag >>> 21) &&& 0x1FFFFF let p2 := mag >>> 42 let neg := signOf w decX X o n lx lim hX hXs (k + 1) (c + 1) (IX.uset c k.toUInt32 (by omega)) (P0.uset c (if neg then 0 - p0 else p0).toUInt32 (by omega)) (P1.uset c (if neg then 0 - p1 else p1).toUInt32 (by omega)) (P2.uset c (if neg then 0 - p2 else p2).toUInt32 (by omega)) (by omega) (by simp only [Array.size_uset]; exact hs) (ok && e != 255 && lx ≤ e && d ≤ lim) else (IX, P0, P1, P2, c, ok) termination_by n.toNat - k.toNat decreasing_by all_goals (rw [hk1]; omega) def decWRow (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c : UInt64) : Array UInt32 × UInt64 := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h have hok : (o + k).toNat = o.toNat + k.toNat := usize_add_toNat _ _ (by omega) let v := W.uget (o + k) (by omega) let m := magW wo hwo v decWRow W wo hwo o n hW hWs (k + 1) (D.uset (o + k) (sgnW v m).toUInt32 (by omega)) (by simp only [Array.size_uset]; exact hD) (c ||| m) else (D, c) termination_by n.toNat - k.toNat decreasing_by rw [usize_succ_toNat k n h]; omega def orAll (W : Array UInt32) (n : USize) (hW : n.toNat ≤ W.size) (k : USize) (c : UInt32) : UInt32 := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h orAll W n hW (k + 1) (c ||| W.uget k (by omega)) else c termination_by n.toNat - k.toNat decreasing_by rw [usize_succ_toNat k n h]; omega def scanR (X : Array UInt32) (o n : USize) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (k : USize) (lo hi : UInt32) : UInt32 × UInt32 := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h let w := X.uget (o + k) (by rw [usize_add_toNat _ _ (by omega)]; omega) let e := (w >>> 23) &&& 0xFF let nz := (w &&& 0x7FFFFFFF) != 0 scanR X o n hX hXs (k + 1) (if nz && e < lo then e else lo) (if nz && e > hi then e else hi) else (lo, hi) termination_by n.toNat - k.toNat decreasing_by rw [usize_succ_toNat k n h]; omega section open Float.Model Float.Model.UnpackedFloat open FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats namespace Bridge theorem em_mantissa (em : ExtendedMantissa) (n : Nat) : (em >>> n).mantissa = em.mantissa / 2 ^ n := by show (Nat.repeat ExtendedMantissa.shiftRightOne n em).mantissa = _ induction n with | zero => simp [Nat.repeat] | succ n ih => simp only [Nat.repeat, ExtendedMantissa.shiftRightOne, ih, Nat.pow_succ, Nat.div_div_eq_div_mul] theorem log2_shl (m k : Nat) (hm : m ≠ 0) : (m <<< k).log2 = m.log2 + k := by induction k with | zero => simp | succ k ih => have h1 : m <<< (k + 1) = 2 * (m <<< k) := by simp [Nat.shiftLeft_eq, Nat.pow_succ]; ring have h2 : m <<< k ≠ 0 := by rw [Nat.shiftLeft_eq]; exact Nat.mul_ne_zero hm (by positivity) rw [h1, Nat.log2_two_mul h2, ih]; ring theorem tgt (t : Int) (h : -149 ≤ t - 24) : Format.binary32.targetExponent t = t - 24 := by simp only [Format.targetExponent, Format.mantissaBits, Format.minExponent] omega theorem rne_ge (m : Nat) (a : Accuracy) : m ≤ a.roundToNearestEven m := by cases a with | exact => simp [Accuracy.roundToNearestEven] | inexact o => cases o <;> simp [Accuracy.roundToNearestEven] theorem rm_ge (em : ExtendedMantissa) : em.mantissa ≤ em.roundedMantissa := by unfold ExtendedMantissa.roundedMantissa; exact rne_ge _ _ end Bridge namespace Bridge def sh0 (m : Nat) : Nat := (23 - (m.log2 : Int)).toNat def m1 (m : Nat) : Nat := m <<< sh0 m def sh1 (m : Nat) : Nat := ((m.log2 : Int) - 23).toNat def em1 (m : Nat) : ExtendedMantissa := ExtendedMantissa.ofMantissaAndAccuracy (m1 m) .exact >>> sh1 m def rm (m : Nat) : Nat := (em1 m).roundedMantissa def sh2 (m : Nat) : Nat := (((rm m).log2 : Int) - 23).toNat def fm (m : Nat) : Nat := (ExtendedMantissa.ofMantissaAndAccuracy (rm m) .exact >>> sh2 m).mantissa theorem em1_mantissa (m : Nat) (hm : m ≠ 0) : 2 ^ 23 ≤ (em1 m).mantissa ∧ (em1 m).mantissa < 2 ^ 24 := by have hl := Nat.log2_self_le hm have hu := Nat.lt_log2_self (n := m) unfold em1 rw [em_mantissa] simp only [ExtendedMantissa.ofMantissaAndAccuracy, m1, sh0, sh1] rcases Nat.lt_or_ge m.log2 23 with h | h · have e0 : (23 - (m.log2 : Int)).toNat = 23 - m.log2 := by omega have e1 : ((m.log2 : Int) - 23).toNat = 0 := by omega rw [e0, e1, Nat.shiftLeft_eq, pow_zero, Nat.div_one] constructor · calc 2 ^ 23 = 2 ^ m.log2 * 2 ^ (23 - m.log2) := by rw [← pow_add]; congr 1; omega _ ≤ m * 2 ^ (23 - m.log2) := Nat.mul_le_mul_right _ hl · calc m * 2 ^ (23 - m.log2) < 2 ^ (m.log2 + 1) * 2 ^ (23 - m.log2) := Nat.mul_lt_mul_of_pos_right hu (by positivity) _ = 2 ^ 24 := by rw [← pow_add]; congr 1; omega · have e0 : (23 - (m.log2 : Int)).toNat = 0 := by omega have e1 : ((m.log2 : Int) - 23).toNat = m.log2 - 23 := by omega rw [e0, e1, Nat.shiftLeft_zero] constructor · rw [Nat.le_div_iff_mul_le (by positivity), ← pow_add] calc 2 ^ (23 + (m.log2 - 23)) = 2 ^ m.log2 := by congr 1; omega _ ≤ m := hl · rw [Nat.div_lt_iff_lt_mul (by positivity), ← pow_add] calc m < 2 ^ (m.log2 + 1) := hu _ = 2 ^ (24 + (m.log2 - 23)) := by congr 1; omega theorem rne_le (m : Nat) (a : Accuracy) : a.roundToNearestEven m ≤ m + 1 := by cases a with | exact => simp [Accuracy.roundToNearestEven] | inexact o => cases o <;> simp [Accuracy.roundToNearestEven] <;> omega theorem rm_bounds (m : Nat) (hm : m ≠ 0) : 2 ^ 23 ≤ rm m ∧ rm m ≤ 2 ^ 24 := by obtain ⟨h1, h2⟩ := em1_mantissa m hm have a := rm_ge (em1 m) have b : (em1 m).roundedMantissa ≤ (em1 m).mantissa + 1 := by unfold ExtendedMantissa.roundedMantissa; exact rne_le _ _ unfold rm; omega theorem rm_log2 (m : Nat) (hm : m ≠ 0) : 23 ≤ (rm m).log2 ∧ (rm m).log2 ≤ 24 := by obtain ⟨h1, h2⟩ := rm_bounds m hm have hne : rm m ≠ 0 := by omega constructor · exact (Nat.le_log2 hne).2 h1 · have : rm m < 2 ^ 25 := by omega have := (Nat.log2_lt hne).2 this omega theorem fm_bounds (m : Nat) (hm : m ≠ 0) : 2 ^ 23 ≤ fm m ∧ fm m < 2 ^ 24 := by obtain ⟨l1, l2⟩ := rm_log2 m hm have hne : rm m ≠ 0 := by have := (rm_bounds m hm).1; omega have hl := Nat.log2_self_le hne have hu := Nat.lt_log2_self (n := rm m) unfold fm rw [em_mantissa] simp only [ExtendedMantissa.ofMantissaAndAccuracy, sh2] have e2 : (((rm m).log2 : Int) - 23).toNat = (rm m).log2 - 23 := by omega rw [e2] constructor · rw [Nat.le_div_iff_mul_le (by positivity), ← pow_add] calc 2 ^ (23 + ((rm m).log2 - 23)) = 2 ^ (rm m).log2 := by congr 1; omega _ ≤ rm m := hl · rw [Nat.div_lt_iff_lt_mul (by positivity), ← pow_add] calc rm m < 2 ^ ((rm m).log2 + 1) := hu _ = 2 ^ (24 + ((rm m).log2 - 23)) := by congr 1; omega end Bridge namespace Bridge theorem S1 (m : Nat) (e : Int) (hm : m ≠ 0) (hT : -149 ≤ (m.log2 : Int) + e - 23) : shiftToTargetExponent Format.binary32 (m1 m) (e - (sh0 m : Int)) .exact = (em1 m, e + m.log2 - 23) := by have hl : (m1 m).log2 = m.log2 + sh0 m := log2_shl m _ hm unfold shiftToTargetExponent shiftToExponent have htot : totalExponent (m1 m) (e - (sh0 m : Int)) = (m.log2 : Int) + 1 + e := by simp only [totalExponent, hl]; push_cast; ring rw [htot, tgt _ (by omega)] have hs : ((m.log2 : Int) + 1 + e - 24 - (e - (sh0 m : Int))).toNat = sh1 m := by simp only [sh0, sh1]; omega dsimp only rw [hs] simp only [em1, Prod.mk.injEq, true_and] simp only [sh0, sh1]; omega theorem S2 (m : Nat) (e : Int) (hm : m ≠ 0) (hT : -149 ≤ (m.log2 : Int) + e - 23) : shiftToTargetExponent Format.binary32 (rm m) (e + m.log2 - 23) .exact = (ExtendedMantissa.ofMantissaAndAccuracy (rm m) .exact >>> sh2 m, e + m.log2 - 23 + sh2 m) := by obtain ⟨l1, l2⟩ := rm_log2 m hm unfold shiftToTargetExponent shiftToExponent have htot : totalExponent (rm m) (e + m.log2 - 23) = ((rm m).log2 : Int) + 1 + (e + m.log2 - 23) := by simp only [totalExponent] rw [htot, tgt _ (by omega)] have hs : (((rm m).log2 : Int) + 1 + (e + m.log2 - 23) - 24 - (e + m.log2 - 23)).toNat = sh2 m := by simp only [sh2]; omega dsimp only rw [hs] theorem round_char (s : Sign) (m : Nat) (e : Int) (hm : m ≠ 0) (hT : -149 ≤ (m.log2 : Int) + e - 23) : UnpackedFloat.round Format.binary32 s m e = .finite s (fm m) (e + m.log2 - 23 + sh2 m) (by have := (fm_bounds m hm).1; omega) := by have h0 : Format.binary32.targetExponent (totalExponent m e) = (m.log2 : Int) + e - 23 := by rw [tgt _ (by simp only [totalExponent]; omega)]; simp only [totalExponent]; ring unfold UnpackedFloat.round decreaseExponent simp only [h0] have hs0 : (e - ((m.log2 : Int) + e - 23)).toNat = sh0 m := by simp only [sh0]; omega rw [hs0] have hm1 : m <<< sh0 m = m1 m := rfl have he1 : e - ((sh0 m : Nat) : Int) = e - (sh0 m : Int) := rfl rw [hm1] unfold roundWithAccuracy rw [S1 m e hm hT] simp only [] rw [show (em1 m).roundedMantissa = rm m from rfl, S2 m e hm hT] have hfm : fm m ≠ 0 := by have := (fm_bounds m hm).1; omega simp only [show (ExtendedMantissa.ofMantissaAndAccuracy (rm m) .exact >>> sh2 m).mantissa = fm m from rfl, hfm, dite_false] end Bridge theorem toFloat32_bits (m : UInt64) : (UInt64.toFloat32 m).toBits = UInt32.ofBitVec (UnpackedFloat.pack Format.binary32 (UnpackedFloat.ofNat Format.binary32 m.toNat)) := rfl theorem ofNat_pos (n : Nat) (hn : n ≠ 0) : UnpackedFloat.ofNat Format.binary32 n = UnpackedFloat.round Format.binary32 .positive n 0 := by simp only [UnpackedFloat.ofNat, UnpackedFloat.ofInt, UnpackedFloat.normalize] have : compare (n : Int) 0 = .gt := by rw [compare_gt_iff_gt]; omega rw [this] simp theorem 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, show Fmt.isIEEE = true by decide] rfl theorem sign_bit (s : Sign) : s.toBitVec.toNat = if s = .negative then 1 else 0 := by cases s <;> rfl theorem 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 := by have hne : M ≠ 0 := by omega apply le_antisymm · have := (Nat.log2_lt hne).2 hM2; omega · exact (Nat.le_log2 hne).2 hM1 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 simp only [Format.binary32]; omega simp only [hno, ite_false] have hnorm : (23 + 1 = Format.binary32.mantissaBits) := rfl simp only [hnorm, ite_true, packComponents, BitVec.toNat_append, sign_bit, BitVec.toNat_ofNat] have h8 : (E + 150).toNat % 2 ^ 8 = (E + 150).toNat := Nat.mod_eq_of_lt (by omega) simp only [Format.binary32] at * 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 namespace Bridge theorem sh2_le (m : Nat) (hm : m ≠ 0) : sh2 m ≤ 1 := by have := (rm_log2 m hm).2; simp only [sh2]; omega @[inline] def roundFast (neg : Bool) (m : UInt64) (ex : Int) : Option UInt32 := let bits := (UInt64.toFloat32 m).toBits let E : Int := (bits >>> 23).toNat + ex if 2 ≤ E ∧ E ≤ 254 then some (UInt32.ofNat ((if neg then 2 ^ 31 else 0) + E.toNat * 2 ^ 23 + bits.toNat % 2 ^ 23)) else none theorem roundFast_eq (neg : Bool) (m : UInt64) (ex : Int) (w : UInt32) (hm : m ≠ 0) (h : roundFast neg m ex = some w) : w = toWord (Model.roundDyadic Fmt ⟨neg, m.toNat, ex⟩) := by have hn : m.toNat ≠ 0 := by intro h0; apply hm; exact UInt64.toNat_inj.mp (by simpa using h0) have hn64 : m.toNat < 2 ^ 64 := m.toNat_lt have hlog : m.toNat.log2 < 64 := (Nat.log2_lt hn).2 hn64 have hs2 := sh2_le m.toNat hn have hfm := fm_bounds m.toNat hn have hb : (UInt64.toFloat32 m).toBits.toNat = ((m.toNat.log2 : Int) - 23 + sh2 m.toNat + 150).toNat * 2 ^ 23 + fm m.toNat % 2 ^ 23 := by rw [toFloat32_bits, ofNat_pos _ hn, round_char _ _ _ hn (by omega)] simp only [UInt32.toNat_ofBitVec] rw [pack_normal _ _ _ _ hfm.1 hfm.2 (by omega) (by omega)] simp have hE : ((UInt64.toFloat32 m).toBits >>> 23).toNat = ((m.toNat.log2 : Int) - 23 + sh2 m.toNat + 150).toNat := by rw [UInt32.toNat_shiftRight, hb] simp only [show (23 : UInt32).toNat % 32 = 23 from rfl, Nat.shiftRight_eq_div_pow] have : fm m.toNat % 2 ^ 23 < 2 ^ 23 := Nat.mod_lt _ (by positivity) omega have hF : (UInt64.toFloat32 m).toBits.toNat % 2 ^ 23 = fm m.toNat % 2 ^ 23 := by rw [hb]; omega unfold roundFast at h simp only [hE, hF] at h split at h · rename_i hc obtain ⟨hc1, hc2⟩ := hc injection h with h rw [← h, roundDyadic_eq, round_char _ _ _ hn (by omega)] simp only [toWord, Model.toNatBits, Model.ofModel, Model.ofModelBits] congr 1 show _ = (UnpackedFloat.pack Format.binary32 _).toNat rw [pack_normal _ _ _ _ hfm.1 hfm.2 (by omega) (by omega)] cases neg <;> simp [Model.modelSign] <;> omega · simp at h end Bridge end structure W128 where fst : UInt64 snd : UInt64 structure SM128 where fst : Bool snd : W128 structure J64 where fst : UInt64 snd : Nat @[inline] def jam64 (mh ml : UInt64) : J64 := if mh == 0 then ⟨ml, 0⟩ else if mh < 0x100000000 then ⟨(mh <<< 32) ||| (ml >>> 32) ||| (if ml &&& 0xFFFFFFFF != 0 then 1 else 0), 32⟩ else ⟨mh ||| (if ml != 0 then 1 else 0), 64⟩ @[inline] def hiOf (v : UInt64) : UInt64 := if v >>> 63 == 0 then 0 else 0xFFFFFFFFFFFFFFFF @[inline] def sh128 (v : UInt64) (d : Nat) : W128 := match d with | 0 => ⟨hiOf v, v⟩ | 1 => ⟨(v >>> 43) ||| (hiOf v <<< 21), v <<< 21⟩ | 2 => ⟨(v >>> 22) ||| (hiOf v <<< 42), v <<< 42⟩ | 3 => ⟨(v >>> 1) ||| (hiOf v <<< 63), v <<< 63⟩ | _ => ⟨v <<< 20, 0⟩ @[inline] def add128 (x y : W128) : W128 := let l := x.2 + y.2 ⟨x.1 + y.1 + (if l < x.2 then 1 else 0), l⟩ @[inline] def biasWord (bw : UInt32) (sb : Int) : UInt64 := let bm : UInt64 := if expOf bw == 0 then 0 else (fracOf bw ||| 0x800000).toUInt64 <<< (sb.toNat % 21).toUInt64 if signOf bw then 0 - bm else bm @[inline] def total3 (a b c bs : UInt64) (d : Nat) : W128 := add128 (add128 (add128 (sh128 a 0) (sh128 b 1)) (sh128 c 2)) (sh128 bs d) @[inline] def magOf (t : W128) : SM128 := let neg := t.1 >>> 63 == 1 ⟨neg, if neg then ~~~t.1 + (if t.2 == 0 then 1 else 0) else t.1, if neg then 0 - t.2 else t.2⟩ @[inline] def fin3 (relu : Bool) (bw : UInt32) (base : Nat) (a b c : UInt64) : Option Word := if expOf bw == 255 || (expOf bw == 0 && fracOf bw != 0) then none else if expOf bw != 0 && ((148 : Int) + (expOf bw).toNat - base < 0 || (148 : Int) + (expOf bw).toNat - base > 100) then none else let sb : Int := (148 : Int) + (expOf bw).toNat - base let q := magOf (total3 a b c (biasWord bw sb) (min (sb.toNat / 21) 4)) let mj := jam64 q.2.1 q.2.2 if mj.1 == 0 then none else match Bridge.roundFast q.1 mj.1 ((mj.2 : Int) + base - 298) with | some r => some (act relu r) | none => none theorem push_get!_lt {α : Type} [Inhabited α] (xs : Array α) (x : α) (i : Nat) (h : i < xs.size) : (xs.push x)[i]! = xs[i]! := by rw [getElem!_pos (xs.push x) i (by simp; omega), getElem!_pos xs i h, Array.getElem_push_lt h] theorem push_get!_eq {α : Type} [Inhabited α] (xs : Array α) (x : α) : (xs.push x)[xs.size]! = x := by rw [getElem!_pos (xs.push x) xs.size (by simp), Array.getElem_push_eq] theorem uset_get! (a : Array UInt32) (i : USize) (v : UInt32) (h : i.toNat < a.size) (s : Nat) : (a.uset i v h)[s]! = if s = i.toNat then v else a[s]! := by by_cases hs : s < a.size · rw [getElem!_pos (a.uset i v h) s (by simpa using hs), getElem!_pos a s hs] simp only [Array.uset, Array.getElem_set] by_cases hsi : s = i.toNat · subst hsi; simp · rw [if_neg (Ne.symm hsi), if_neg hsi] · rw [getElem!_neg (a.uset i v h) s (by simpa using hs), getElem!_neg a s hs] rw [if_neg (by omega)] theorem decX_ix (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (k c : USize) (IX P0 P1 P2 : Array UInt32) (hck : c.toNat ≤ k.toNat) (hs : n.toNat ≤ IX.size ∧ n.toNat ≤ P0.size ∧ n.toNat ≤ P1.size ∧ n.toNat ≤ P2.size) (ok : Bool) (hkn : k.toNat ≤ n.toNat) (hIX : ∀ s, s < c.toNat → (IX[s]!).toNat < n.toNat) : let r := decX X o n lx lim hX hXs k c IX P0 P1 P2 hck hs ok (∀ s, s < r.2.2.2.2.1.toNat → (r.1[s]!).toNat < n.toNat) ∧ r.2.2.2.2.1.toNat ≤ n.toNat ∧ n.toNat ≤ r.1.size ∧ n.toNat ≤ r.2.1.size ∧ n.toNat ≤ r.2.2.1.size ∧ n.toNat ≤ r.2.2.2.1.size := by induction k, c, IX, P0, P1, P2, hck, hs, ok using decX.induct X o n lx lim hX hXs with | case1 k c IX P0 P1 P2 hck hs ok h hk hk1 w e he ih => unfold decX; simp only [dite_cond_eq_true (eq_true h)] split · exact ih (by omega) hIX · exact absurd he ‹_› | case2 k c IX P0 P1 P2 hck hs ok h hk hk1 w e he hc1 d mag p0 p1 p2 neg ih => unfold decX; simp only [dite_cond_eq_true (eq_true h)] split · exact absurd ‹_› he · apply ih (by omega) intro s hs' rw [hc1] at hs' rw [uset_get!] split_ifs with hsc · simp only [USize.toNat_toUInt32] have : k.toNat < 2 ^ 32 := by omega rw [Nat.mod_eq_of_lt this]; exact hk · exact hIX s (by omega) | case3 k c IX P0 P1 P2 hck hs ok h => unfold decX; simp only [dite_cond_eq_false (eq_false h)] exact ⟨hIX, by omega, hs⟩ theorem decWRow_size (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c : UInt64) : (decWRow W wo hwo o n hW hWs k D hD c).1.size = D.size := by induction k, D, hD, c using decWRow.induct W wo hwo o n hW hWs with | case1 k D hD c h hk hok v m ih => unfold decWRow; simp only [dite_cond_eq_true (eq_true h)] rw [ih]; simp | case2 k D hD c h => unfold decWRow; simp only [dite_cond_eq_false (eq_false h)] theorem toUSize_toNat (m : Nat) (h : m < 2 ^ 32) : m.toUSize.toNat = m := by have : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl simp only [Nat.toUSize] exact Nat.mod_eq_of_lt (by omega) theorem row_ok (IX : Array UInt32) (Wn nz inW outW jr : Nat) (hjr : jr < outW) (hIX : ∀ s, s < nz → (IX[s]!).toNat < inW) (hWsz : outW * inW ≤ Wn) (hWs : Wn < 2 ^ 32) : ∀ s, s < nz → (jr * inW).toUSize.toNat + (IX[s]!).toNat < Wn := by intro s hs have h1 := hIX s hs have h2 : (jr + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hjr rw [toUSize_toNat _ (by nlinarith)] nlinarith theorem rows_ok (IX W : Array UInt32) (nz inW outW j : Nat) (hj : j < outW) (hIX : ∀ s, s < nz → (IX[s]!).toNat < inW) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) : RowsOk IX W nz (j * inW).toUSize ((if j + 1 < outW then j + 1 else j) * inW).toUSize ((if j + 2 < outW then j + 2 else j) * inW).toUSize ((if j + 3 < outW then j + 3 else j) * inW).toUSize := by intro s hs wr hwr rcases hwr with rfl | rfl | rfl | rfl · exact row_ok IX W.size nz inW outW j hj hIX hWsz hWs s hs · exact row_ok IX W.size nz inW outW _ (by split <;> omega) hIX hWsz hWs s hs · exact row_ok IX W.size nz inW outW _ (by split <;> omega) hIX hWsz hWs s hs · exact row_ok IX W.size nz inW outW _ (by split <;> omega) hIX hWsz hWs s hs def DenseOk (D : Array UInt32) (n : Nat) (w0 w1 w2 w3 : USize) : Prop := w0.toNat + n ≤ D.size ∧ w1.toNat + n ≤ D.size ∧ w2.toNat + n ≤ D.size ∧ w3.toNat + n ≤ D.size theorem dense_idx (D : Array UInt32) (w t : USize) (n : Nat) (ht : t.toNat < n) (hw : w.toNat + n ≤ D.size) (hDs : D.size < 2 ^ 32) : (w + t).toNat < D.size := by rw [usize_add_toNat _ _ (by omega)]; omega def dotD4 (U0 U1 D : Array UInt32) (w0 w1 w2 w3 n : USize) (h0 : n.toNat ≤ U0.size) (h1 : n.toNat ≤ U1.size) (hW : DenseOk D n.toNat w0 w1 w2 w3) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 : UInt64) : Acc8 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h let x0 := (U0.uget t (by omega)).toUInt64 let x1 := (U1.uget t (by omega)).toUInt64 let d0 := (D.uget (w0 + t) (dense_idx D w0 t _ ht hW.1 hDs)).toUInt64 let d1 := (D.uget (w1 + t) (dense_idx D w1 t _ ht hW.2.1 hDs)).toUInt64 let d2 := (D.uget (w2 + t) (dense_idx D w2 t _ ht hW.2.2.1 hDs)).toUInt64 let d3 := (D.uget (w3 + t) (dense_idx D w3 t _ ht hW.2.2.2 hDs)).toUInt64 dotD4 U0 U1 D w0 w1 w2 w3 n h0 h1 hW hDs (t + 1) (a0 + x0 * d0) (b0 + x1 * d0) (a1 + x0 * d1) (b1 + x1 * d1) (a2 + x0 * d2) (b2 + x1 * d2) (a3 + x0 * d3) (b3 + x1 * d3) else ⟨a0, b0, a1, b1, a2, b2, a3, b3, 0⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega def dotD2w (U0 U1 U2 D : Array UInt32) (w0 w1 n : USize) (h0 : n.toNat ≤ U0.size) (h1 : n.toNat ≤ U1.size) (h2 : n.toNat ≤ U2.size) (hW : DenseOk D n.toNat w0 w1 w0 w1) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 : UInt64) : Acc6 := if h : t < n then have ht : t.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h let x0 := (U0.uget t (by omega)).toUInt64 let x1 := (U1.uget t (by omega)).toUInt64 let x2 := (U2.uget t (by omega)).toUInt64 let d0 := (D.uget (w0 + t) (dense_idx D w0 t _ ht hW.1 hDs)).toUInt64 let d1 := (D.uget (w1 + t) (dense_idx D w1 t _ ht hW.2.1 hDs)).toUInt64 dotD2w U0 U1 U2 D w0 w1 n h0 h1 h2 hW hDs (t + 1) (a0 + x0 * d0) (b0 + x1 * d0) (c0 + x2 * d0) (a1 + x0 * d1) (b1 + x1 * d1) (c1 + x2 * d1) else ⟨a0, b0, c0, a1, b1, c1, 0⟩ termination_by n.toNat - t.toNat decreasing_by rw [usize_succ_toNat t n h]; omega @[inline] def offP (neg : Bool) (p : UInt64) : UInt64 := if neg then 0x200000 - p else 0x200000 + p def decXD (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (k : USize) (U0 U1 U2 : Array UInt32) (hs : n.toNat ≤ U0.size ∧ n.toNat ≤ U1.size ∧ n.toNat ≤ U2.size) (s0 s1 s2 : UInt64) (ok : Bool) : Array UInt32 × Array UInt32 × Array UInt32 × UInt64 × UInt64 × UInt64 × Bool := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h let w := X.uget (o + k) (by rw [usize_add_toNat _ _ (by omega)]; omega) let e := (w >>> 23) &&& 0xFF let d := e - lx let mag : UInt64 := if e == 0 then 0 else ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< d.toUInt64 let neg := signOf w let q0 := (offP neg (mag &&& 0x1FFFFF)).toUInt32 let q1 := (offP neg ((mag >>> 21) &&& 0x1FFFFF)).toUInt32 let q2 := (offP neg (mag >>> 42)).toUInt32 decXD X o n lx lim hX hXs (k + 1) (U0.uset k q0 (by omega)) (U1.uset k q1 (by omega)) (U2.uset k q2 (by omega)) (by simp only [Array.size_uset]; exact hs) (s0 + q0.toUInt64) (s1 + q1.toUInt64) (s2 + q2.toUInt64) (ok && (if e == 0 then (w &&& 0x7FFFFF) == 0 else e != 255 && lx ≤ e && d ≤ lim)) else (U0, U1, U2, s0, s1, s2, ok) termination_by n.toNat - k.toNat decreasing_by rw [usize_succ_toNat k n h]; omega def decWRowD (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c sd : UInt64) : Array UInt32 × UInt64 × UInt64 := if h : k < n then have hk : k.toNat < n.toNat := usize_lt_succ k n h have hok : (o + k).toNat = o.toNat + k.toNat := usize_add_toNat _ _ (by omega) let v := W.uget (o + k) (by omega) let m := magW wo hwo v let u := (sgnW v m + 0x80000000).toUInt32 decWRowD W wo hwo o n hW hWs (k + 1) (D.uset (o + k) u (by omega)) (by simp only [Array.size_uset]; exact hD) (c ||| m) (sd + u.toUInt64) else (D, c, sd) termination_by n.toNat - k.toNat decreasing_by rw [usize_succ_toNat k n h]; omega theorem dense_ok (D : Array UInt32) (inW outW j : Nat) (hj : j < outW) (hDsz : outW * inW ≤ D.size) (hDs : D.size < 2 ^ 32) : DenseOk D inW (j * inW).toUSize ((if j + 1 < outW then j + 1 else j) * inW).toUSize ((if j + 2 < outW then j + 2 else j) * inW).toUSize ((if j + 3 < outW then j + 3 else j) * inW).toUSize := by have hr : ∀ jr, jr < outW → (jr * inW).toUSize.toNat + inW ≤ D.size := by intro jr hjr have h2 : (jr + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hjr rw [toUSize_toNat _ (by nlinarith)]; nlinarith exact ⟨hr j hj, hr _ (by split <;> omega), hr _ (by split <;> omega), hr _ (by split <;> omega)⟩ theorem dense_ok01 (D : Array UInt32) (n : Nat) (w0 w1 w2 w3 : USize) (h : DenseOk D n w0 w1 w2 w3) : DenseOk D n w0 w1 w0 w1 := ⟨h.1, h.2.1, h.1, h.2.1⟩ theorem dense_ok23 (D : Array UInt32) (n : Nat) (w0 w1 w2 w3 : USize) (h : DenseOk D n w0 w1 w2 w3) : DenseOk D n w2 w3 w2 w3 := ⟨h.2.2.1, h.2.2.2, h.2.2.1, h.2.2.2⟩ theorem decXD_sizes (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (k : USize) (U0 U1 U2 : Array UInt32) (hs : n.toNat ≤ U0.size ∧ n.toNat ≤ U1.size ∧ n.toNat ≤ U2.size) (s0 s1 s2 : UInt64) (ok : Bool) : (decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok).1.size = U0.size ∧ (decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok).2.1.size = U1.size ∧ (decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok).2.2.1.size = U2.size := by induction k, U0, U1, U2, hs, s0, s1, s2, ok using decXD.induct X o n lx lim hX hXs with | case1 k U0 U1 U2 hs s0 s1 s2 ok h => rename_i ih unfold decXD; simp only [dite_cond_eq_true (eq_true h)] obtain ⟨a, b, c⟩ := ih; simp only [Array.size_uset] at a b c; exact ⟨a, b, c⟩ | case2 k U0 U1 U2 hs s0 s1 s2 ok h => unfold decXD; simp only [dite_cond_eq_false (eq_false h)]; simp theorem decWRowD_size (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c sd : UInt64) : (decWRowD W wo hwo o n hW hWs k D hD c sd).1.size = D.size := by induction k, D, hD, c, sd using decWRowD.induct W wo hwo o n hW hWs with | case1 k D hD c sd h => rename_i ih unfold decWRowD; simp only [dite_cond_eq_true (eq_true h)] rw [ih]; simp | case2 k D hD c sd h => unfold decWRowD; simp only [dite_cond_eq_false (eq_false h)] def slowDot : DotFn := dotFn def slowOut (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) : Word := act relu (slowDot (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]!)) def slowRow (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) (out : Array Word) : Array Word := if j < outW then slowRow inW outW relu X W b i (j + 1) (out.push (slowOut inW relu X W b i j)) else out termination_by outW - j @[inline] def pick (inW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (r : Option Word) : Word := match r with | some w => w | none => slowOut inW relu X W b i jj @[inline] def pushBlock (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i j base : Nat) (r : Acc12) (ok : Nat → Bool) (out : Array Word) : Array Word := let f := fun (jj : Nat) (pa pb pc : UInt64) => pick inW relu X W b i jj (if ok jj then fin3 relu b[jj]! base pa pb pc else none) let out := out.push (f j r.a0 r.b0 r.c0) let out := if j + 1 < outW then out.push (f (j + 1) r.a1 r.b1 r.c1) else out let out := if j + 2 < outW then out.push (f (j + 2) r.a2 r.b2 r.c2) else out if j + 3 < outW then out.push (f (j + 3) r.a3 r.b3 r.c3) else out def flyBlocks (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (IX X0 X1 X2 : Array UInt32) (nz : USize) (wide : Bool) (o : USize) (ho : o.toNat ≤ 65280) (base : Nat) (hI : nz.toNat ≤ IX.size) (h0 : nz.toNat ≤ X0.size) (h1 : nz.toNat ≤ X1.size) (h2 : nz.toNat ≤ X2.size) (hIX : ∀ s, s < nz.toNat → (IX[s]!).toNat < inW) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (out : Array Word) : Array Word := if hj : j < outW then let w0 := (j * inW).toUSize let w1 := ((if j + 1 < outW then j + 1 else j) * inW).toUSize let w2 := ((if j + 2 < outW then j + 2 else j) * inW).toUSize let w3 := ((if j + 3 < outW then j + 3 else j) * inW).toUSize have hr := rows_ok IX W nz.toNat inW outW j hj hIX hWsz hWs let r : Acc12 := if wide then dot4w IX X0 X1 X2 W o ho w0 w1 w2 w3 nz hI h0 h1 h2 hr hWs 0 0 0 0 0 0 0 0 0 0 0 0 0 0 else let q := dot4n IX X0 X1 W o ho w0 w1 w2 w3 nz hI h0 h1 hr hWs 0 0 0 0 0 0 0 0 0 0 ⟨q.a0, q.b0, 0, q.a1, q.b1, 0, q.a2, q.b2, 0, q.a3, q.b3, 0, q.chk⟩ let ok := r.chk < 0x80000000 flyBlocks inW outW relu X W b i IX X0 X1 X2 nz wide o ho base hI h0 h1 h2 hIX hWsz hWs (j + 4) (pushBlock inW outW relu X W b i j base r (fun _ => ok) out) else out termination_by outW - j def preBlocks (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (IX X0 X1 X2 D : Array UInt32) (oks : Array Bool) (nz : USize) (wide : Bool) (base : Nat) (hI : nz.toNat ≤ IX.size) (h0 : nz.toNat ≤ X0.size) (h1 : nz.toNat ≤ X1.size) (h2 : nz.toNat ≤ X2.size) (hIX : ∀ s, s < nz.toNat → (IX[s]!).toNat < inW) (hDsz : outW * inW ≤ D.size) (hDs : D.size < 2 ^ 32) (j : Nat) (out : Array Word) : Array Word := if hj : j < outW then let w0 := (j * inW).toUSize let w1 := ((if j + 1 < outW then j + 1 else j) * inW).toUSize let w2 := ((if j + 2 < outW then j + 2 else j) * inW).toUSize let w3 := ((if j + 3 < outW then j + 3 else j) * inW).toUSize have hr := rows_ok IX D nz.toNat inW outW j hj hIX hDsz hDs let r : Acc12 := if wide then dot4Pw IX X0 X1 X2 D w0 w1 w2 w3 nz hI h0 h1 h2 hr hDs 0 0 0 0 0 0 0 0 0 0 0 0 0 else let q := dot4Pn IX X0 X1 D w0 w1 w2 w3 nz hI h0 h1 hr hDs 0 0 0 0 0 0 0 0 0 ⟨q.a0, q.b0, 0, q.a1, q.b1, 0, q.a2, q.b2, 0, q.a3, q.b3, 0, 0⟩ preBlocks inW outW relu X W b i IX X0 X1 X2 D oks nz wide base hI h0 h1 h2 hIX hDsz hDs (j + 4) (pushBlock inW outW relu X W b i j base r (fun jj => oks[jj]!) out) else out termination_by outW - j theorem decX_facts (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (z : Array UInt32) (hz : n.toNat ≤ z.size) : let r := decX X o n lx lim hX hXs 0 0 z z z z (by simp) ⟨hz, hz, hz, hz⟩ true (∀ s, s < r.2.2.2.2.1.toNat → (r.1[s]!).toNat < n.toNat) ∧ r.2.2.2.2.1.toNat ≤ n.toNat ∧ n.toNat ≤ r.1.size ∧ n.toNat ≤ r.2.1.size ∧ n.toNat ≤ r.2.2.1.size ∧ n.toNat ≤ r.2.2.2.1.size := decX_ix X o n lx lim hX hXs 0 0 z z z z (by simp) ⟨hz, hz, hz, hz⟩ true (by simp) (by simp) @[inline] def flyRowWith (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (lx lim : UInt32) (wide : Bool) (hXsz : (i + 1) * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (out : Array Word) : Array Word := let xo := (i * inW).toUSize let n := inW.toUSize have hn : n.toNat = inW := toUSize_toNat _ (by omega) have hX : xo.toNat + n.toNat ≤ X.size := by rw [hn, toUSize_toNat _ (by nlinarith)]; nlinarith let z : Array UInt32 := Array.replicate inW 0 have hz : n.toNat ≤ z.size := by simp [z, hn] let r := decX X xo n lx lim hX hXs 0 0 z z z z (by simp) ⟨hz, hz, hz, hz⟩ true have hd : (∀ s, s < r.2.2.2.2.1.toNat → (r.1[s]!).toNat < n.toNat) ∧ r.2.2.2.2.1.toNat ≤ n.toNat ∧ n.toNat ≤ r.1.size ∧ n.toNat ≤ r.2.1.size ∧ n.toNat ≤ r.2.2.1.size ∧ n.toNat ≤ r.2.2.2.1.size := decX_facts X xo n lx lim hX hXs z hz if r.2.2.2.2.2 && 1 ≤ lx && (r.2.2.2.2.1.toNat == inW || (((orAll W (outW * inW).toUSize (by rw [toUSize_toNat _ (by omega)]; omega) 0 0) >>> 23) &&& 0xFF) != 0xFF) then flyBlocks inW outW relu X W b i r.1 r.2.1 r.2.2.1 r.2.2.2.1 r.2.2.2.2.1 wide o ho (lx.toNat + lw - 2) (by omega) (by omega) (by omega) (by omega) (fun s hs => by rw [← hn]; exact hd.1 s hs) hWsz hWs 0 out else slowRow inW outW relu X W b i 0 out def flyRow (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (hXsz : (i + 1) * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (out : Array Word) : Array Word := have hX : (i * inW).toUSize.toNat + inW.toUSize.toNat ≤ X.size := by rw [toUSize_toNat (i * inW) (by nlinarith), toUSize_toNat inW (by omega)]; nlinarith let lh := scanR X (i * inW).toUSize inW.toUSize hX hXs 0 255 0 let wide : Bool := lh.2 - lh.1 > 18 flyRowWith inW outW relu X W b i o ho lw lh.1 (if wide then 39 else 18) wide hXsz hWsz hXs hWs hin out def flyRows (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (hXsz : rows * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (i : Nat) (out : Array Word) : Array Word := if hi : i < rows then flyRows rows inW outW relu X W b o ho lw hXsz hWsz hXs hWs hin (i + 1) (flyRow (i := i) inW outW relu X W b o ho lw (le_trans (Nat.mul_le_mul_right _ hi) hXsz) hWsz hXs hWs hin out) else out termination_by rows - i def preDecode (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) : Array UInt32 × Array Bool := if hj : j < outW then have h1 : (j * inW).toUSize.toNat + inW.toUSize.toNat ≤ W.size := by have h2 : (j + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hj rw [toUSize_toNat _ (by nlinarith), toUSize_toNat _ (by nlinarith)]; nlinarith let r := decWRow W wo hwo (j * inW).toUSize inW.toUSize h1 hWs 0 D hD 0 preDecode W wo hwo inW outW hWsz hWs (j + 1) r.1 (by rw [decWRow_size]; exact hD) (oks.push (r.2 < 0x80000000)) else (D, oks) termination_by outW - j theorem preDecode_size (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) : (preDecode W wo hwo inW outW hWsz hWs j D hD oks).1.size = D.size := by induction j, D, hD, oks using preDecode.induct W wo hwo inW outW hWsz hWs with | case1 j D hD oks hj h1 r ih => unfold preDecode; simp only [dite_cond_eq_true (eq_true hj)] rw [ih]; exact decWRow_size _ _ _ _ _ _ _ _ _ _ _ | case2 j D hD oks hj => unfold preDecode; simp only [dite_cond_eq_false (eq_false hj)] def preRows (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (D : Array UInt32) (oks : Array Bool) (lw : Nat) (hXsz : rows * inW ≤ X.size) (hDsz : outW * inW ≤ D.size) (hXs : X.size < 2 ^ 32) (hDs : D.size < 2 ^ 32) (hin : inW < 2048) (IX P0 P1 P2 : Array UInt32) (hP : inW ≤ IX.size ∧ inW ≤ P0.size ∧ inW ≤ P1.size ∧ inW ≤ P2.size) (i : Nat) (out : Array Word) : Array Word := if hi : i < rows then let xo := (i * inW).toUSize let n := inW.toUSize have hn : n.toNat = inW := toUSize_toNat _ (by omega) have hX : xo.toNat + n.toNat ≤ X.size := by have h2 : (i + 1) * inW ≤ rows * inW := Nat.mul_le_mul_right _ hi rw [hn, toUSize_toNat _ (by nlinarith)]; nlinarith let lh := scanR X xo n hX hXs 0 255 0 let wide : Bool := lh.2 - lh.1 > 18 have hs : n.toNat ≤ IX.size ∧ n.toNat ≤ P0.size ∧ n.toNat ≤ P1.size ∧ n.toNat ≤ P2.size := by rw [hn]; exact hP let r := decX X xo n lh.1 (if wide then 39 else 18) hX hXs 0 0 IX P0 P1 P2 (by simp) hs true have hd : (∀ s, s < r.2.2.2.2.1.toNat → (r.1[s]!).toNat < n.toNat) ∧ r.2.2.2.2.1.toNat ≤ n.toNat ∧ n.toNat ≤ r.1.size ∧ n.toNat ≤ r.2.1.size ∧ n.toNat ≤ r.2.2.1.size ∧ n.toNat ≤ r.2.2.2.1.size := decX_ix X xo n lh.1 (if wide then 39 else 18) hX hXs 0 0 IX P0 P1 P2 (by simp) hs true (by simp) (by simp) have hr : inW ≤ r.1.size ∧ inW ≤ r.2.1.size ∧ inW ≤ r.2.2.1.size ∧ inW ≤ r.2.2.2.1.size := by rw [← hn]; exact hd.2.2 let out := if r.2.2.2.2.2 && 1 ≤ lh.1 then preBlocks inW outW relu X W b i r.1 r.2.1 r.2.2.1 r.2.2.2.1 D oks r.2.2.2.2.1 wide (lh.1.toNat + lw - 2) (by omega) (by omega) (by omega) (by omega) (fun s hs' => by rw [← hn]; exact hd.1 s hs') hDsz hDs 0 out else slowRow inW outW relu X W b i 0 out preRows rows inW outW relu X W b D oks lw hXsz hDsz hXs hDs hin r.1 r.2.1 r.2.2.1 r.2.2.2.1 hr (i + 1) out else out termination_by rows - i def preDecodeD (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) (sds : Array UInt64) : Array UInt32 × Array Bool × Array UInt64 := if hj : j < outW then have h1 : (j * inW).toUSize.toNat + inW.toUSize.toNat ≤ W.size := by have h2 : (j + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hj rw [toUSize_toNat _ (by nlinarith), toUSize_toNat _ (by nlinarith)]; nlinarith let r := decWRowD W wo hwo (j * inW).toUSize inW.toUSize h1 hWs 0 D hD 0 0 preDecodeD W wo hwo inW outW hWsz hWs (j + 1) r.1 (by rw [decWRowD_size]; exact hD) (oks.push (r.2.1 < 0x80000000)) (sds.push r.2.2) else (D, oks, sds) termination_by outW - j theorem preDecodeD_size (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) (sds : Array UInt64) : (preDecodeD W wo hwo inW outW hWsz hWs j D hD oks sds).1.size = D.size := by induction j, D, hD, oks, sds using preDecodeD.induct W wo hwo inW outW hWsz hWs with | case1 j D hD oks sds hj => rename_i ih unfold preDecodeD; simp only [dite_cond_eq_true (eq_true hj)] rw [ih]; exact decWRowD_size _ _ _ _ _ _ _ _ _ _ _ _ | case2 j D hD oks sds hj => unfold preDecodeD; simp only [dite_cond_eq_false (eq_false hj)] @[inline] def fixD (n acc su sd : UInt64) : UInt64 := acc - su * 2147483648 - sd * 2097152 + n * 4503599627370496 def denseBlocks (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (U0 U1 U2 D : Array UInt32) (oks : Array Bool) (sds : Array UInt64) (s0 s1 s2 : UInt64) (wide : Bool) (base : Nat) (h0 : inW ≤ U0.size) (h1 : inW ≤ U1.size) (h2 : inW ≤ U2.size) (hin : inW < 2048) (hDsz : outW * inW ≤ D.size) (hDs : D.size < 2 ^ 32) (j : Nat) (out : Array Word) : Array Word := if hj : j < outW then let n := inW.toUSize have hn : n.toNat = inW := toUSize_toNat _ (by omega) let w0 := (j * inW).toUSize let w1 := ((if j + 1 < outW then j + 1 else j) * inW).toUSize let w2 := ((if j + 2 < outW then j + 2 else j) * inW).toUSize let w3 := ((if j + 3 < outW then j + 3 else j) * inW).toUSize have hr : DenseOk D n.toNat w0 w1 w2 w3 := by rw [hn]; exact dense_ok D inW outW j hj hDsz hDs let nn := inW.toUInt64 let e0 := sds[j]! let e1 := sds[if j + 1 < outW then j + 1 else j]! let e2 := sds[if j + 2 < outW then j + 2 else j]! let e3 := sds[if j + 3 < outW then j + 3 else j]! let r : Acc12 := if wide then let p := dotD2w U0 U1 U2 D w0 w1 n (by omega) (by omega) (by omega) (dense_ok01 _ _ _ _ _ _ hr) hDs 0 0 0 0 0 0 0 let q := dotD2w U0 U1 U2 D w2 w3 n (by omega) (by omega) (by omega) (dense_ok23 _ _ _ _ _ _ hr) hDs 0 0 0 0 0 0 0 ⟨fixD nn p.a0 s0 e0, fixD nn p.b0 s1 e0, fixD nn p.c0 s2 e0, fixD nn p.a1 s0 e1, fixD nn p.b1 s1 e1, fixD nn p.c1 s2 e1, fixD nn q.a0 s0 e2, fixD nn q.b0 s1 e2, fixD nn q.c0 s2 e2, fixD nn q.a1 s0 e3, fixD nn q.b1 s1 e3, fixD nn q.c1 s2 e3, 0⟩ else let q := dotD4 U0 U1 D w0 w1 w2 w3 n (by omega) (by omega) hr hDs 0 0 0 0 0 0 0 0 0 ⟨fixD nn q.a0 s0 e0, fixD nn q.b0 s1 e0, 0, fixD nn q.a1 s0 e1, fixD nn q.b1 s1 e1, 0, fixD nn q.a2 s0 e2, fixD nn q.b2 s1 e2, 0, fixD nn q.a3 s0 e3, fixD nn q.b3 s1 e3, 0, 0⟩ denseBlocks inW outW relu X W b i U0 U1 U2 D oks sds s0 s1 s2 wide base h0 h1 h2 hin hDsz hDs (j + 4) (pushBlock inW outW relu X W b i j base r (fun jj => oks[jj]!) out) else out termination_by outW - j def denseRows (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (D : Array UInt32) (oks : Array Bool) (sds : Array UInt64) (lw : Nat) (hXsz : rows * inW ≤ X.size) (hDsz : outW * inW ≤ D.size) (hXs : X.size < 2 ^ 32) (hDs : D.size < 2 ^ 32) (hin : inW < 2048) (U0 U1 U2 : Array UInt32) (hU : inW ≤ U0.size ∧ inW ≤ U1.size ∧ inW ≤ U2.size) (i : Nat) (out : Array Word) : Array Word := if hi : i < rows then let xo := (i * inW).toUSize let n := inW.toUSize have hn : n.toNat = inW := toUSize_toNat _ (by omega) have hX : xo.toNat + n.toNat ≤ X.size := by have h2 : (i + 1) * inW ≤ rows * inW := Nat.mul_le_mul_right _ hi rw [hn, toUSize_toNat _ (by nlinarith)]; nlinarith let lh := scanR X xo n hX hXs 0 255 0 let wide : Bool := lh.2 - lh.1 > 18 let r := decXD X xo n lh.1 (if wide then 39 else 18) hX hXs 0 U0 U1 U2 (by rw [hn]; exact hU) 0 0 0 true have hr : inW ≤ r.1.size ∧ inW ≤ r.2.1.size ∧ inW ≤ r.2.2.1.size := by obtain ⟨e0, e1, e2⟩ := decXD_sizes X xo n lh.1 (if wide then 39 else 18) hX hXs 0 U0 U1 U2 (by rw [hn]; exact hU) 0 0 0 true exact ⟨by rw [e0]; exact hU.1, by rw [e1]; exact hU.2.1, by rw [e2]; exact hU.2.2⟩ let out := if r.2.2.2.2.2.2 && 1 ≤ lh.1 then denseBlocks inW outW relu X W b i r.1 r.2.1 r.2.2.1 D oks sds r.2.2.2.1 r.2.2.2.2.1 r.2.2.2.2.2.1 wide (lh.1.toNat + lw - 2) hr.1 hr.2.1 hr.2.2 hin hDsz hDs 0 out else slowRow inW outW relu X W b i 0 out denseRows rows inW outW relu X W b D oks sds lw hXsz hDsz hXs hDs hin r.1 r.2.1 r.2.2.1 hr (i + 1) out else out termination_by rows - i def topRows (W : Array UInt32) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j cnt : Nat) (hi : UInt32) : UInt32 := match cnt with | 0 => hi | c + 1 => if hj : j < outW then have h1 : (j * inW).toUSize.toNat + inW.toUSize.toNat ≤ W.size := by have h2 : (j + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hj rw [toUSize_toNat _ (by nlinarith), toUSize_toNat _ (by nlinarith)]; nlinarith let h := (scanR W (j * inW).toUSize inW.toUSize h1 hWs 0 255 0).2 topRows W inW outW hWsz hWs (j + 1) c (if h > hi then h else hi) else hi theorem win_ok (lw : UInt32) (h : lw ≤ 254) : (lw.toUSize * 256).toNat ≤ 65280 := by have h1 : lw.toNat ≤ 254 := h have h2 : (lw.toUSize).toNat = lw.toNat := UInt32.toNat_toUSize lw rw [USize.toNat_mul, h2] have : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl simp only [USize.toNat_ofNat] rw [Nat.mod_eq_of_lt (by omega : 256 < 2 ^ System.Platform.numBits), Nat.mod_eq_of_lt (by omega)] omega def linFast (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) : Array Word := if hg : rows * inW ≤ X.size ∧ outW * inW ≤ W.size ∧ 0 < inW ∧ inW < 2048 ∧ X.size < 2 ^ 32 ∧ W.size < 2 ^ 32 then let lw0 := (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) let lw : UInt32 := if lw0 == 0 || lw0 > 254 then 1 else lw0 have hlw : lw ≤ 254 := by simp only [lw]; split <;> simp_all let wo := lw.toUSize * 256 if rows ≥ 2 then if narrow then let pd := preDecodeD W wo (win_ok lw hlw) inW outW hg.2.1 hg.2.2.2.2.2 0 (Array.replicate W.size 0) (by simp) #[] #[] have hD : pd.1.size = W.size := by rw [preDecodeD_size]; simp let z : Array UInt32 := Array.replicate inW 0 denseRows rows inW outW relu X W b pd.1 pd.2.1 pd.2.2 lw.toNat hg.1 (by rw [hD]; exact hg.2.1) hg.2.2.2.2.1 (by rw [hD]; exact hg.2.2.2.2.2) hg.2.2.2.1 z z z (by simp [z]) 0 #[] else let pd := preDecode W wo (win_ok lw hlw) inW outW hg.2.1 hg.2.2.2.2.2 0 (Array.replicate W.size 0) (by simp) #[] have hD : pd.1.size = W.size := by rw [preDecode_size]; simp let z : Array UInt32 := Array.replicate inW 0 preRows rows inW outW relu X W b pd.1 pd.2 lw.toNat hg.1 (by rw [hD]; exact hg.2.1) hg.2.2.2.2.1 (by rw [hD]; exact hg.2.2.2.2.2) hg.2.2.2.1 z z z z (by simp [z]) 0 #[] else flyRows rows inW outW relu X W b wo (win_ok lw hlw) lw.toNat hg.1 hg.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2 hg.2.2.2.1 0 #[] else linearWith slowDot rows inW outW relu X W b theorem or_disj (x y k : Nat) (hx : x < 2 ^ k) (hy : y % 2 ^ k = 0) : x ||| y = x + y := by have : y = (y / 2 ^ k) <<< k := by rw [Nat.shiftLeft_eq]; exact (Nat.div_mul_cancel (Nat.dvd_of_mod_eq_zero hy)).symm rw [this, Nat.lor_comm, ← Nat.shiftLeft_add_eq_or_of_lt hx]; ring theorem shiftTabs_get (i : USize) (h : i.toNat < shiftTabs.size) : shiftTabs.uget i h = if i.toNat % 256 = 0 then 41 else if i.toNat % 256 ≠ 255 ∧ i.toNat / 256 ≤ i.toNat % 256 ∧ i.toNat % 256 ≤ i.toNat / 256 + 7 then (i.toNat % 256 - i.toNat / 256).toUInt8 else 40 := by simp [ByteArray.uget, shiftTabs] theorem expU_eq (w : UInt32) : (expU w).toNat = (expOf w).toNat := by rw [expU_toNat]; rfl theorem fracOf_lt (w : UInt32) : (w &&& 0x7FFFFF).toNat < 2 ^ 23 := by rw [UInt32.toNat_and]; exact Nat.lt_of_le_of_lt Nat.and_le_right (by decide) theorem magW_spec (lw : Nat) (hlw : lw ≤ 254) (o : USize) (hoe : o.toNat = lw * 256) (ho : o.toNat ≤ 65280) (w : UInt32) (hm : (magW o ho w).toNat < 2 ^ 31) : (magW o ho w).toNat = mantOf w * 2 ^ ((expOf w).toNat - lw) ∧ (mantOf w = 0 ∨ ((expOf w).toNat ≠ 0 ∧ (expOf w).toNat ≠ 255 ∧ lw ≤ (expOf w).toNat ∧ (expOf w).toNat ≤ lw + 7)) := by have he := expOf_toNat_lt w have hf := fracOf_lt w have heu := expU_eq w have hidx : (o + expU w).toNat = lw * 256 + (expOf w).toNat := by rw [usize_add_toNat _ _ (by have := expU_lt w; omega), hoe, heu] have hfr : (fracOf w).toNat = (w &&& 0x7FFFFF).toNat := rfl have hf' : w.toNat &&& 8388607 < 2 ^ 23 := by simpa [UInt32.toNat_and] using hf have hm23 : ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64.toNat = 2 ^ 23 + (fracOf w).toNat := by rw [UInt32.toNat_toUInt64, UInt32.toNat_or, hfr] rw [or_disj _ _ 23 hf (by norm_num)]; norm_num; omega unfold magW at hm ⊢ simp only [shiftTabs_get, hidx] at hm ⊢ have hmod : (lw * 256 + (expOf w).toNat) % 256 = (expOf w).toNat := by omega have hdiv : (lw * 256 + (expOf w).toNat) / 256 = lw := by omega simp only [hmod, hdiv] at hm ⊢ rw [UInt64.toNat_shiftLeft, hm23] at hm ⊢ have hml : mantOf w < 2 ^ 24 := mantOf_lt_24 w split_ifs at hm ⊢ with h0 hc · have h41 : ((41 : UInt8).toUInt64.toNat % 64) = 41 := by decide rw [h41, Nat.shiftLeft_eq] at hm ⊢ have hw : (2 ^ 23 + (fracOf w).toNat) * 2 ^ 41 % 2 ^ 64 = (fracOf w).toNat * 2 ^ 41 := by have : (fracOf w).toNat * 2 ^ 41 < 2 ^ 64 := by calc (fracOf w).toNat * 2 ^ 41 < 2 ^ 23 * 2 ^ 41 := Nat.mul_lt_mul_of_pos_right (by omega) (by positivity) _ = 2 ^ 64 := by norm_num rw [Nat.add_mul, show (2 : Nat) ^ 23 * 2 ^ 41 = 2 ^ 64 by norm_num, Nat.add_mod, Nat.mod_self, zero_add, Nat.mod_mod, Nat.mod_eq_of_lt this] rw [hw] at hm ⊢ have hf0 : (fracOf w).toNat = 0 := by by_contra hne have : 2 ^ 41 ≤ (fracOf w).toNat * 2 ^ 41 := Nat.le_mul_of_pos_left _ (Nat.pos_of_ne_zero hne) omega have he0 : expOf w = 0 := UInt32.toNat_inj.mp (by simpa using h0) have hm0 : mantOf w = 0 := by simp [mantOf, he0, hf0] rw [hf0, hm0]; simp · have hmant : mantOf w = 2 ^ 23 + (fracOf w).toNat := by have : expOf w ≠ 0 := fun h' => h0 (by simp [h']) simp [mantOf, this] have ht : (((expOf w).toNat - lw).toUInt8.toUInt64.toNat % 64) = (expOf w).toNat - lw := by simp only [UInt8.toNat_toUInt64, Nat.toUInt8, UInt8.toNat_ofNat']; omega rw [ht, Nat.shiftLeft_eq, ← hmant] at hm ⊢ refine ⟨Nat.mod_eq_of_lt ?_, Or.inr ⟨h0, hc.1, hc.2.1, hc.2.2⟩⟩ calc mantOf w * 2 ^ ((expOf w).toNat - lw) < 2 ^ 24 * 2 ^ 8 := Nat.mul_lt_mul_of_lt_of_le hml (Nat.pow_le_pow_right (by norm_num) (by omega)) (by positivity) _ ≤ 2 ^ 64 := by norm_num · exfalso have h40 : ((40 : UInt8).toUInt64.toNat % 64) = 40 := by decide rw [h40, Nat.shiftLeft_eq] at hm have hlt : (2 ^ 23 + (fracOf w).toNat) * 2 ^ 40 < 2 ^ 64 := by calc (2 ^ 23 + (fracOf w).toNat) * 2 ^ 40 < 2 ^ 24 * 2 ^ 40 := Nat.mul_lt_mul_of_pos_right (by omega) (by positivity) _ = 2 ^ 64 := by norm_num rw [Nat.mod_eq_of_lt hlt] at hm have : 2 ^ 63 ≤ (2 ^ 23 + (fracOf w).toNat) * 2 ^ 40 := by calc 2 ^ 63 = 2 ^ 23 * 2 ^ 40 := by norm_num _ ≤ _ := Nat.mul_le_mul_right _ (by omega) omega def wAt (W IX : Array UInt32) (wr : USize) (s : Nat) : UInt32 := W[wr.toNat + (IX[s]!).toNat]! theorem uget_eq (a : Array UInt32) (i : USize) (h : i.toNat < a.size) : a.uget i h = a[i.toNat]! := by simp only [Array.uget, getElem!_pos a i.toNat h] theorem uget_wAt (IX W : Array UInt32) (t wr : USize) (ht : t.toNat < IX.size) (hb : wr.toNat + (IX[t.toNat]!).toNat < W.size) (hWs : W.size < 2 ^ 32) (h) : W.uget (wr + (IX.uget t ht).toUSize) h = wAt W IX wr t.toNat := by have he : (IX.uget t ht).toUSize.toNat = (IX[t.toNat]!).toNat := by simp only [Array.uget, UInt32.toNat_toUSize, getElem!_pos IX t.toNat ht] rw [uget_eq, usize_add_toNat _ _ (by omega), he]; rfl theorem or_lt (x y : UInt64) : (x ||| y).toNat < 2 ^ 31 ↔ x.toNat < 2 ^ 31 ∧ y.toNat < 2 ^ 31 := by rw [UInt64.toNat_or] constructor · intro h; exact ⟨Nat.lt_of_le_of_lt Nat.left_le_or h, Nat.lt_of_le_of_lt Nat.right_le_or h⟩ · intro ⟨h1, h2⟩; exact Nat.or_lt_two_pow h1 h2 theorem Ico_succ (f : Nat → UInt64) (t n : Nat) (h : t < n) : ∑ s ∈ Finset.Ico t n, f s = f t + ∑ s ∈ Finset.Ico (t + 1) n, f s := Finset.sum_eq_sum_Ico_succ_bot h f theorem dot4n_spec (IX X0 X1 W : Array UInt32) (o : USize) (ho : o.toNat ≤ 65280) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (hW : RowsOk IX W n.toNat w0 w1 w2 w3) (hWs : W.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 chk : UInt64) : let r := dot4n IX X0 X1 W o ho w0 w1 w2 w3 n hI h0 h1 hW hWs t a0 b0 a1 b1 a2 b2 a3 b3 chk let g := fun (P : Array UInt32) (wr : USize) (s : Nat) => sx P[s]! * sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w0 s ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w0 s ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w1 s ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w1 s ∧ r.a2 = a2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w2 s ∧ r.b2 = b2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w2 s ∧ r.a3 = a3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w3 s ∧ r.b3 = b3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w3 s ∧ (r.chk.toNat < 2 ^ 31 → chk.toNat < 2 ^ 31 ∧ ∀ s, t.toNat ≤ s → s < n.toNat → (magW o ho (wAt W IX w0 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w1 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w2 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w3 s)).toNat < 2 ^ 31) := by induction t, a0, b0, a1, b1, a2, b2, a3, b3, chk using dot4n.induct IX X0 X1 W o ho w0 w1 w2 w3 n hI h0 h1 hW hWs with | case1 t a0 b0 a1 b1 a2 b2 a3 b3 chk h ht hti x0 x1 v0 v1 v2 v3 m0 m1 m2 m3 s0 s1 s2 s3 ih => unfold dot4n simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, ec⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = sx X0[t.toNat]! := by simp only [x0, uget_eq] have hx1 : x1 = sx X1[t.toNat]! := by simp only [x1, uget_eq] have hv0 : v0 = wAt W IX w0 t.toNat := by simp only [v0]; exact uget_wAt _ _ _ _ _ (hW _ ht w0 (by simp)) hWs _ have hv1 : v1 = wAt W IX w1 t.toNat := by simp only [v1]; exact uget_wAt _ _ _ _ _ (hW _ ht w1 (by simp)) hWs _ have hv2 : v2 = wAt W IX w2 t.toNat := by simp only [v2]; exact uget_wAt _ _ _ _ _ (hW _ ht w2 (by simp)) hWs _ have hv3 : v3 = wAt W IX w3 t.toNat := by simp only [v3]; exact uget_wAt _ _ _ _ _ (hW _ ht w3 (by simp)) hWs _ rw [hsucc] at e0 e1 e2 e3 e4 e5 e6 e7 ec refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s0, m0, hx0, hv0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s0, m0, hx1, hv0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s1, m1, hx0, hv1] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s1, m1, hx1, hv1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s2, m2, hx0, hv2] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s2, m2, hx1, hv2] · rw [e6, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s3, m3, hx0, hv3] · rw [e7, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s3, m3, hx1, hv3] · intro hc obtain ⟨hc1, hc2⟩ := ec hc simp only [or_lt] at hc1 obtain ⟨⟨⟨⟨hq, hq0⟩, hq1⟩, hq2⟩, hq3⟩ := hc1 refine ⟨hq, fun s hs1 hs2 => ?_⟩ rcases Nat.lt_or_ge s (t.toNat + 1) with hs | hs · have : s = t.toNat := by omega subst this rw [← hv0, ← hv1, ← hv2, ← hv3]; exact ⟨hq0, hq1, hq2, hq3⟩ · exact hc2 s hs hs2 | case2 t a0 b0 a1 b1 a2 b2 a3 b3 chk h => unfold dot4n simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, true_and] intro hc; exact ⟨hc, fun s h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩ theorem dot4w_spec (IX X0 X1 X2 W : Array UInt32) (o : USize) (ho : o.toNat ≤ 65280) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (h2 : n.toNat ≤ X2.size) (hW : RowsOk IX W n.toNat w0 w1 w2 w3) (hWs : W.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 chk : UInt64) : let r := dot4w IX X0 X1 X2 W o ho w0 w1 w2 w3 n hI h0 h1 h2 hW hWs t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 chk let g := fun (P : Array UInt32) (wr : USize) (s : Nat) => sx P[s]! * sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w0 s ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w0 s ∧ r.c0 = c0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X2 w0 s ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w1 s ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w1 s ∧ r.c1 = c1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X2 w1 s ∧ r.a2 = a2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w2 s ∧ r.b2 = b2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w2 s ∧ r.c2 = c2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X2 w2 s ∧ r.a3 = a3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X0 w3 s ∧ r.b3 = b3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X1 w3 s ∧ r.c3 = c3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, g X2 w3 s ∧ (r.chk.toNat < 2 ^ 31 → chk.toNat < 2 ^ 31 ∧ ∀ s, t.toNat ≤ s → s < n.toNat → (magW o ho (wAt W IX w0 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w1 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w2 s)).toNat < 2 ^ 31 ∧ (magW o ho (wAt W IX w3 s)).toNat < 2 ^ 31) := by induction t, a0, b0, c0, a1, b1, c1, a2, b2, c2, a3, b3, c3, chk using dot4w.induct IX X0 X1 X2 W o ho w0 w1 w2 w3 n hI h0 h1 h2 hW hWs with | case1 t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 chk h ht hti x0 x1 x2 v0 v1 v2 v3 m0 m1 m2 m3 s0 s1 s2 s3 ih => unfold dot4w simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, ec⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = sx X0[t.toNat]! := by simp only [x0, uget_eq] have hx1 : x1 = sx X1[t.toNat]! := by simp only [x1, uget_eq] have hx2 : x2 = sx X2[t.toNat]! := by simp only [x2, uget_eq] have hv0 : v0 = wAt W IX w0 t.toNat := by simp only [v0]; exact uget_wAt _ _ _ _ _ (hW _ ht w0 (by simp)) hWs _ have hv1 : v1 = wAt W IX w1 t.toNat := by simp only [v1]; exact uget_wAt _ _ _ _ _ (hW _ ht w1 (by simp)) hWs _ have hv2 : v2 = wAt W IX w2 t.toNat := by simp only [v2]; exact uget_wAt _ _ _ _ _ (hW _ ht w2 (by simp)) hWs _ have hv3 : v3 = wAt W IX w3 t.toNat := by simp only [v3]; exact uget_wAt _ _ _ _ _ (hW _ ht w3 (by simp)) hWs _ rw [hsucc] at e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 ec refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s0, m0, hx0, hv0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s0, m0, hx1, hv0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s0, m0, hx2, hv0] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s1, m1, hx0, hv1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s1, m1, hx1, hv1] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s1, m1, hx2, hv1] · rw [e6, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s2, m2, hx0, hv2] · rw [e7, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s2, m2, hx1, hv2] · rw [e8, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s2, m2, hx2, hv2] · rw [e9, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s3, m3, hx0, hv3] · rw [e10, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s3, m3, hx1, hv3] · rw [e11, Ico_succ _ _ _ ht, ← add_assoc]; simp only [s3, m3, hx2, hv3] · intro hc obtain ⟨hc1, hc2⟩ := ec hc simp only [or_lt] at hc1 obtain ⟨⟨⟨⟨hq, hq0⟩, hq1⟩, hq2⟩, hq3⟩ := hc1 refine ⟨hq, fun s hs1 hs2 => ?_⟩ rcases Nat.lt_or_ge s (t.toNat + 1) with hs | hs · have : s = t.toNat := by omega subst this rw [← hv0, ← hv1, ← hv2, ← hv3]; exact ⟨hq0, hq1, hq2, hq3⟩ · exact hc2 s hs hs2 | case2 t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 chk h => unfold dot4w simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, true_and] intro hc; exact ⟨hc, fun s h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩ theorem dot4Pn_spec (IX X0 X1 D : Array UInt32) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (hW : RowsOk IX D n.toNat w0 w1 w2 w3) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 : UInt64) : let r := dot4Pn IX X0 X1 D w0 w1 w2 w3 n hI h0 h1 hW hDs t a0 b0 a1 b1 a2 b2 a3 b3 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w0 s) ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w0 s) ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w1 s) ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w1 s) ∧ r.a2 = a2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w2 s) ∧ r.b2 = b2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w2 s) ∧ r.a3 = a3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w3 s) ∧ r.b3 = b3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w3 s) := by induction t, a0, b0, a1, b1, a2, b2, a3, b3 using dot4Pn.induct IX X0 X1 D w0 w1 w2 w3 n hI h0 h1 hW hDs with | case1 t a0 b0 a1 b1 a2 b2 a3 b3 h ht hti x0 x1 s0 s1 s2 s3 ih => unfold dot4Pn simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = sx X0[t.toNat]! := by simp only [x0, uget_eq] have hx1 : x1 = sx X1[t.toNat]! := by simp only [x1, uget_eq] have hs0 : s0 = sx (wAt D IX w0 t.toNat) := by simp only [s0]; rw [uget_wAt _ _ _ _ _ (hW _ ht w0 (by simp)) hDs _] have hs1 : s1 = sx (wAt D IX w1 t.toNat) := by simp only [s1]; rw [uget_wAt _ _ _ _ _ (hW _ ht w1 (by simp)) hDs _] have hs2 : s2 = sx (wAt D IX w2 t.toNat) := by simp only [s2]; rw [uget_wAt _ _ _ _ _ (hW _ ht w2 (by simp)) hDs _] have hs3 : s3 = sx (wAt D IX w3 t.toNat) := by simp only [s3]; rw [uget_wAt _ _ _ _ _ (hW _ ht w3 (by simp)) hDs _] rw [hsucc] at e0 e1 e2 e3 e4 e5 e6 e7 refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs1] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs2] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs2] · rw [e6, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs3] · rw [e7, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs3] | case2 t a0 b0 a1 b1 a2 b2 a3 b3 h => unfold dot4Pn simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, and_self] theorem dot4Pw_spec (IX X0 X1 X2 D : Array UInt32) (w0 w1 w2 w3 n : USize) (hI : n.toNat ≤ IX.size) (h0 : n.toNat ≤ X0.size) (h1 : n.toNat ≤ X1.size) (h2 : n.toNat ≤ X2.size) (hW : RowsOk IX D n.toNat w0 w1 w2 w3) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 : UInt64) : let r := dot4Pw IX X0 X1 X2 D w0 w1 w2 w3 n hI h0 h1 h2 hW hDs t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w0 s) ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w0 s) ∧ r.c0 = c0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X2[s]! * sx (wAt D IX w0 s) ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w1 s) ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w1 s) ∧ r.c1 = c1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X2[s]! * sx (wAt D IX w1 s) ∧ r.a2 = a2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w2 s) ∧ r.b2 = b2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w2 s) ∧ r.c2 = c2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X2[s]! * sx (wAt D IX w2 s) ∧ r.a3 = a3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X0[s]! * sx (wAt D IX w3 s) ∧ r.b3 = b3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X1[s]! * sx (wAt D IX w3 s) ∧ r.c3 = c3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, sx X2[s]! * sx (wAt D IX w3 s) := by induction t, a0, b0, c0, a1, b1, c1, a2, b2, c2, a3, b3, c3 using dot4Pw.induct IX X0 X1 X2 D w0 w1 w2 w3 n hI h0 h1 h2 hW hDs with | case1 t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 h ht hti x0 x1 x2 s0 s1 s2 s3 ih => unfold dot4Pw simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = sx X0[t.toNat]! := by simp only [x0, uget_eq] have hx1 : x1 = sx X1[t.toNat]! := by simp only [x1, uget_eq] have hx2 : x2 = sx X2[t.toNat]! := by simp only [x2, uget_eq] have hs0 : s0 = sx (wAt D IX w0 t.toNat) := by simp only [s0]; rw [uget_wAt _ _ _ _ _ (hW _ ht w0 (by simp)) hDs _] have hs1 : s1 = sx (wAt D IX w1 t.toNat) := by simp only [s1]; rw [uget_wAt _ _ _ _ _ (hW _ ht w1 (by simp)) hDs _] have hs2 : s2 = sx (wAt D IX w2 t.toNat) := by simp only [s2]; rw [uget_wAt _ _ _ _ _ (hW _ ht w2 (by simp)) hDs _] have hs3 : s3 = sx (wAt D IX w3 t.toNat) := by simp only [s3]; rw [uget_wAt _ _ _ _ _ (hW _ ht w3 (by simp)) hDs _] rw [hsucc] at e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hs0] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs1] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hs1] · rw [e6, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs2] · rw [e7, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs2] · rw [e8, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hs2] · rw [e9, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hs3] · rw [e10, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hs3] · rw [e11, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hs3] | case2 t a0 b0 c0 a1 b1 c1 a2 b2 c2 a3 b3 c3 h => unfold dot4Pw simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, and_self] def v128 (x : W128) : Nat := x.1.toNat * 2 ^ 64 + x.2.toNat theorem v128_lt (x : W128) : v128 x < 2 ^ 128 := by have := x.1.toNat_lt; have := x.2.toNat_lt; unfold v128; nlinarith theorem add128_val (x y : W128) : v128 (add128 x y) = (v128 x + v128 y) % 2 ^ 128 := by unfold add128 v128 have hx1 := x.1.toNat_lt; have hx2 := x.2.toNat_lt; have hy1 := y.1.toNat_lt; have hy2 := y.2.toNat_lt simp only [UInt64.toNat_add, UInt64.lt_iff_toNat_lt] split_ifs with hc <;> simp only [UInt64.toNat_one, UInt64.toNat_zero] <;> omega theorem hiOf_toNat (v : UInt64) : (hiOf v).toNat = if v.toNat < 2 ^ 63 then 0 else 2 ^ 64 - 1 := by unfold hiOf have hv := v.toNat_lt have key : (v >>> 63 == 0) = true ↔ v.toNat < 2 ^ 63 := by rw [beq_iff_eq, ← UInt64.toNat_inj] simp only [UInt64.toNat_shiftRight, UInt64.toNat_zero, Nat.shiftRight_eq_div_pow] norm_num <;> omega by_cases h : v.toNat < 2 ^ 63 · rw [if_pos (key.mpr h), if_pos h]; rfl · rw [if_neg (fun h' => h (key.mp h')), if_neg h]; rfl def sv64 (v : UInt64) : Int := v.toBitVec.toInt theorem sv64_eq (v : UInt64) : sv64 v = if v.toNat < 2 ^ 63 then (v.toNat : Int) else (v.toNat : Int) - 2 ^ 64 := by unfold sv64 rw [BitVec.toInt_eq_toNat_cond] simp only [UInt64.toNat_toBitVec] split_ifs with h1 h2 h2 <;> push_cast <;> omega theorem sh128_val (v : UInt64) (d : Nat) (hd : d ≤ 4) : (v128 (sh128 v d) : Int) = (sv64 v * 2 ^ (21 * d)) % 2 ^ 128 := by have hv := v.toNat_lt rw [sv64_eq] interval_cases d <;> simp only [sh128, v128, hiOf_toNat, UInt64.toNat_or, UInt64.toNat_shiftLeft, UInt64.toNat_shiftRight] <;> norm_num [Nat.shiftLeft_eq, Nat.shiftRight_eq_div_pow] all_goals (split_ifs <;> (try simp only [Nat.or_zero]) <;> (try (first | rw [or_disj _ _ 21 (by omega) (by norm_num)] | rw [or_disj _ _ 42 (by omega) (by norm_num)] | rw [or_disj _ _ 63 (by omega) (by norm_num)])) <;> push_cast <;> omega) section open FloatLib.Numerics FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats 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) theorem neg128 (h l : UInt64) (hne : v128 ⟨h, l⟩ ≠ 0) : v128 ⟨~~~h + (if l == 0 then 1 else 0), 0 - l⟩ = 2 ^ 128 - v128 ⟨h, l⟩ := by unfold v128 at * have hh := h.toNat_lt; have hl := l.toNat_lt have hs : UInt64.size = 2 ^ 64 := rfl simp only [UInt64.toNat_add, UInt64.toNat_not, UInt64.toNat_sub, UInt64.toNat_zero, hs] at * by_cases hl0 : l = 0 · subst hl0 simp only [beq_self_eq_true, ite_true, UInt64.toNat_one, UInt64.toNat_zero] at * omega · have hl0' : l.toNat ≠ 0 := fun h' => hl0 (UInt64.toNat_inj.mp (by simpa using h')) simp only [show (l == 0) = false from by simp [hl0], Bool.false_eq_true, ite_false, UInt64.toNat_zero] omega theorem or_disj' (x y k : Nat) (hx : x % 2 ^ k = 0) (hy : y < 2 ^ k) : x ||| y = x + y := by rw [Nat.lor_comm, or_disj y x k hy hx, Nat.add_comm] theorem bne_toNat (x : UInt64) : (x != 0) = (x.toNat != 0) := by by_cases h : x = 0 · subst h; rfl · have : x.toNat ≠ 0 := fun h' => h (UInt64.toNat_inj.mp (by simpa using h')) rw [show (x != 0) = true from bne_iff_ne.mpr h, show (x.toNat != 0) = true from bne_iff_ne.mpr this] theorem or_bit (q : Nat) (b : Bool) : q ||| (if b then 1 else 0) = if b then q ||| 1 else q := by cases b <;> simp theorem jam64_spec (mh ml : UInt64) (hne : v128 ⟨mh, ml⟩ ≠ 0) : (jam64 mh ml).1.toNat = shiftRightJam (v128 ⟨mh, ml⟩) (jam64 mh ml).2 ∧ ((jam64 mh ml).2 = 0 ∨ (jam64 mh ml).2 + 25 ≤ (v128 ⟨mh, ml⟩).log2) := by unfold jam64 v128 at * have hh := mh.toNat_lt; have hl := ml.toNat_lt by_cases h0 : mh = 0 · subst h0 simp only [beq_self_eq_true, ite_true, UInt64.toNat_zero, zero_mul, zero_add, true_or, and_true] simp [shiftRightJam, Nat.mod_one] · have h0' : mh.toNat ≠ 0 := fun h' => h0 (UInt64.toNat_inj.mp (by simpa using h')) have hne0 : mh.toNat * 2 ^ 64 + ml.toNat ≠ 0 := by positivity simp only [show (mh == 0) = false from by simp [h0], Bool.false_eq_true, ite_false] by_cases h32 : mh < 0x100000000 · have h32' : mh.toNat < 2 ^ 32 := by simpa [UInt64.lt_iff_toNat_lt] using h32 simp only [h32, ite_true] refine ⟨?_, Or.inr ?_⟩ · have e1 : (mh <<< 32).toNat = mh.toNat * 2 ^ 32 := by rw [UInt64.toNat_shiftLeft] simp only [show (32 : UInt64).toNat % 64 = 32 from rfl, Nat.shiftLeft_eq] exact Nat.mod_eq_of_lt (by omega) have e2 : (ml >>> 32).toNat = ml.toNat / 2 ^ 32 := by rw [UInt64.toNat_shiftRight] simp only [show (32 : UInt64).toNat % 64 = 32 from rfl, Nat.shiftRight_eq_div_pow] have e3 : (ml &&& 0xFFFFFFFF).toNat = ml.toNat % 2 ^ 32 := by rw [UInt64.toNat_and]; exact Nat.and_two_pow_sub_one_eq_mod _ 32 have e4 : ((if ml &&& 0xFFFFFFFF != 0 then (1 : UInt64) else 0)).toNat = if ml.toNat % 2 ^ 32 != 0 then 1 else 0 := by have : (ml &&& 0xFFFFFFFF != 0) = (ml.toNat % 2 ^ 32 != 0) := by rw [← e3, bne_toNat] rw [this]; split <;> rfl have hQ : mh.toNat * 2 ^ 32 ||| ml.toNat / 2 ^ 32 = mh.toNat * 2 ^ 32 + ml.toNat / 2 ^ 32 := or_disj' _ _ 32 (by simp) (by omega) rw [UInt64.toNat_or, UInt64.toNat_or, e1, e2, e4, hQ, or_bit] have q2 : (mh.toNat * 2 ^ 64 + ml.toNat) / 2 ^ 32 = mh.toNat * 2 ^ 32 + ml.toNat / 2 ^ 32 := by omega have q3 : (mh.toNat * 2 ^ 64 + ml.toNat) % 2 ^ 32 = ml.toNat % 2 ^ 32 := by omega simp only [shiftRightJam, q2, q3] by_cases hs : ml.toNat % 2 ^ 32 = 0 <;> simp [hs] · rw [Nat.le_log2 hne0] calc 2 ^ (32 + 25) ≤ 1 * 2 ^ 64 := by norm_num _ ≤ mh.toNat * 2 ^ 64 := Nat.mul_le_mul_right _ (by omega) _ ≤ _ := by omega · have h32' : 2 ^ 32 ≤ mh.toNat := by have : ¬ mh.toNat < 4294967296 := by simpa [UInt64.lt_iff_toNat_lt] using h32 omega simp only [h32, ite_false] refine ⟨?_, Or.inr ?_⟩ · have e4 : ((if ml != 0 then (1 : UInt64) else 0)).toNat = if ml.toNat != 0 then 1 else 0 := by rw [bne_toNat]; split <;> rfl rw [UInt64.toNat_or, e4, or_bit] have q2 : (mh.toNat * 2 ^ 64 + ml.toNat) / 2 ^ 64 = mh.toNat := by omega have q3 : (mh.toNat * 2 ^ 64 + ml.toNat) % 2 ^ 64 = ml.toNat := by omega simp only [shiftRightJam, q2, q3] by_cases hs : ml.toNat = 0 <;> simp [hs] · rw [Nat.le_log2 hne0] calc 2 ^ (64 + 25) ≤ 2 ^ 32 * 2 ^ 64 := by norm_num _ ≤ mh.toNat * 2 ^ 64 := Nat.mul_le_mul_right _ h32' _ ≤ _ := by omega theorem sv64_small (x : UInt64) (h : x.toNat < 2 ^ 63) : sv64 x = x.toNat := by rw [sv64_eq, if_pos h] theorem sv64_neg (x : UInt64) (h : x.toNat < 2 ^ 63) (h0 : x.toNat ≠ 0) : sv64 (0 - x) = -(x.toNat : Int) := by have hx := x.toNat_lt rw [sv64_eq] have e : (0 - x).toNat = 2 ^ 64 - x.toNat := by rw [UInt64.toNat_sub, UInt64.toNat_zero]; have : UInt64.size = 2 ^ 64 := rfl; omega rw [e, if_neg (by omega)]; push_cast; omega theorem bias_val (bw : UInt32) (sb : Nat) (hsb : sb ≤ 100) (he0 : expOf bw ≠ 0) : let bm : UInt64 := ((bw &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (sb % 21).toUInt64 let bs : UInt64 := if signOf bw then 0 - bm else bm sv64 bs * 2 ^ (21 * (sb / 21)) = sv bw * 2 ^ sb := by intro bm bs have hf := fracOf_lt bw have hmant : mantOf bw = 2 ^ 23 + (bw &&& 0x7FFFFF).toNat := by unfold mantOf; simp [he0]; rfl have hor : ((bw &&& 0x7FFFFF) ||| 0x800000).toUInt64.toNat = mantOf bw := by rw [UInt32.toNat_toUInt64, UInt32.toNat_or, hmant] rw [or_disj _ _ 23 hf (by norm_num)]; norm_num; omega have hr : sb % 21 < 21 := Nat.mod_lt _ (by norm_num) have hm24 := mantOf_lt_24 bw have hbm : bm.toNat = mantOf bw * 2 ^ (sb % 21) := by simp only [bm, UInt64.toNat_shiftLeft, hor] have : ((sb % 21).toUInt64.toNat % 64) = sb % 21 := by simp only [Nat.toUInt64, UInt64.toNat_ofNat']; omega rw [this, Nat.shiftLeft_eq] apply Nat.mod_eq_of_lt calc mantOf bw * 2 ^ (sb % 21) < 2 ^ 24 * 2 ^ 21 := Nat.mul_lt_mul_of_lt_of_le hm24 (Nat.pow_le_pow_right (by norm_num) (by omega)) (by positivity) _ ≤ 2 ^ 64 := by norm_num have hsmall : bm.toNat < 2 ^ 63 := by rw [hbm] calc mantOf bw * 2 ^ (sb % 21) < 2 ^ 24 * 2 ^ 21 := Nat.mul_lt_mul_of_lt_of_le hm24 (Nat.pow_le_pow_right (by norm_num) (by omega)) (by positivity) _ ≤ 2 ^ 63 := by norm_num have hne : bm.toNat ≠ 0 := by rw [hbm, hmant]; positivity have hpow : (2 : Int) ^ (sb % 21) * 2 ^ (21 * (sb / 21)) = 2 ^ sb := by rw [← pow_add]; congr 1; omega simp only [bs] unfold sv split_ifs with hsg · rw [sv64_neg _ hsmall hne, hbm]; push_cast; rw [← hpow]; ring · rw [sv64_small _ hsmall, hbm]; push_cast; rw [← hpow]; ring theorem total128 (a b c bs : UInt64) (d : Nat) (hd : d ≤ 4) : (v128 (add128 (add128 (add128 (sh128 a 0) (sh128 b 1)) (sh128 c 2)) (sh128 bs d)) : Int) = (sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv64 bs * 2 ^ (21 * d)) % 2 ^ 128 := by have ha := sh128_val a 0 (by norm_num) have hb := sh128_val b 1 (by norm_num) have hc := sh128_val c 2 (by norm_num) have hs := sh128_val bs d hd generalize sv64 bs * 2 ^ (21 * d) = Bb at hs ⊢ simp only [add128_val] push_cast rw [ha, hb, hc, hs] norm_num <;> omega theorem v128_hi (t : W128) : (t.1 >>> 63 == 1) = decide (2 ^ 127 ≤ v128 t) := by unfold v128 have h1 := t.1.toNat_lt; have h2 := t.2.toNat_lt rw [Bool.eq_iff_iff, beq_iff_eq, decide_eq_true_iff, ← UInt64.toNat_inj] simp only [UInt64.toNat_shiftRight, UInt64.toNat_one, Nat.shiftRight_eq_div_pow] norm_num omega theorem signmag (t : W128) (T : Int) (hT : (v128 t : Int) = T % 2 ^ 128) (hb1 : -2 ^ 127 < T) (hb2 : T < 2 ^ 127) : (t.1 >>> 63 == 1) = decide (T < 0) ∧ v128 ⟨if t.1 >>> 63 == 1 then ~~~t.1 + (if t.2 == 0 then 1 else 0) else t.1, if t.1 >>> 63 == 1 then 0 - t.2 else t.2⟩ = T.natAbs := by have hlt := v128_lt t rw [v128_hi] by_cases hneg : T < 0 · have hv : (v128 t : Int) = T + 2 ^ 128 := by rw [hT]; omega have hge : 2 ^ 127 ≤ v128 t := by omega simp only [hneg, decide_true, hge, ite_true, and_true, true_and] have hne : v128 ⟨t.1, t.2⟩ ≠ 0 := by show v128 t ≠ 0; omega rw [neg128 t.1 t.2 hne] show 2 ^ 128 - v128 t = T.natAbs omega · have hv : (v128 t : Int) = T := by rw [hT]; omega have hge : ¬ 2 ^ 127 ≤ v128 t := by omega simp only [hneg, decide_false, hge, ite_false, true_and] show v128 t = T.natAbs omega theorem jam64_zero : (jam64 0 0).1 = 0 := by decide theorem round_total (T : Int) (mh ml : UInt64) (hv : v128 ⟨mh, ml⟩ = T.natAbs) (base : Nat) (r : UInt32) (hm : (jam64 mh ml).1 ≠ 0) (hr : Bridge.roundFast (decide (T < 0)) (jam64 mh ml).1 (((jam64 mh ml).2 : Int) + base - 298) = some r) (nz : Bool) : T ≠ 0 ∧ r = finishWord T ((base : Int) - 298) nz := by have hT0 : T ≠ 0 := by intro h0 rw [h0] at hv have : mh = 0 ∧ ml = 0 := by unfold v128 at hv; simp at hv exact ⟨UInt64.toNat_inj.mp (by simpa using hv.1), UInt64.toNat_inj.mp (by simpa using hv.2)⟩ rw [this.1, this.2] at hm; exact hm jam64_zero have hne : v128 ⟨mh, ml⟩ ≠ 0 := by rw [hv]; omega obtain ⟨hj1, hj2⟩ := jam64_spec mh ml hne refine ⟨hT0, ?_⟩ rw [Bridge.roundFast_eq _ _ _ _ hm hr, hj1, hv] unfold finishWord rw [if_neg hT0] congr 1 simp only [ofIntDyadic] rcases hj2 with h0 | hj · rw [h0]; simp [shiftRightJam, Nat.mod_one] · rw [hv] at hj have := roundDyadic_jam' (decide (T < 0)) T.natAbs ((base : Int) - 298) (jam64 mh ml).2 hj rw [← this]; congr 2; ring theorem sv64_bounds (x : UInt64) : -2 ^ 63 ≤ sv64 x ∧ sv64 x < 2 ^ 63 := by have := x.toNat_lt; rw [sv64_eq]; split_ifs <;> omega theorem sv_abs (w : UInt32) : -(2 ^ 24 : Int) < sv w ∧ sv w < 2 ^ 24 := by have := mantOf_lt_24 w unfold sv; split_ifs <;> omega theorem sv_zero (w : UInt32) (he : expOf w = 0) (hf : fracOf w = 0) : sv w = 0 := by unfold sv mantOf; simp [he, hf] theorem fin3_spec (relu : Bool) (bw : UInt32) (base : Nat) (a b c : UInt64) (w : Word) (nz : Bool) (h : fin3 relu bw base a b c = some w) : expOf bw ≠ 255 ∧ (expOf bw = 0 → fracOf bw = 0) ∧ (expOf bw ≠ 0 → base ≤ 148 + (expOf bw).toNat) ∧ (sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv bw * 2 ^ (148 + (expOf bw).toNat - base)) ≠ 0 ∧ w = act relu (finishWord (sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv bw * 2 ^ (148 + (expOf bw).toNat - base)) ((base : Int) - 298) nz) := by unfold fin3 at h split at h · simp at h rename_i hc1 split at h · simp at h rename_i hc2 have h255 : expOf bw ≠ 255 := by intro h'; simp [h'] at hc1 have hz : expOf bw = 0 → fracOf bw = 0 := by intro h0; by_contra hf; simp [h0, hf] at hc1 have hsb : expOf bw ≠ 0 → 0 ≤ (148 : Int) + (expOf bw).toNat - base ∧ (148 : Int) + (expOf bw).toNat - base ≤ 100 := by intro h0; by_contra hb; apply hc2 simp only [Bool.and_eq_true, bne_iff_ne, ne_eq, h0, not_false_eq_true, true_and, Bool.or_eq_true, decide_eq_true_eq] omega suffices hsuff : (sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv bw * 2 ^ (148 + (expOf bw).toNat - base)) ≠ 0 ∧ w = act relu (finishWord (sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv bw * 2 ^ (148 + (expOf bw).toNat - base)) ((base : Int) - 298) nz) from ⟨h255, hz, fun h0 => by have := hsb h0; omega, hsuff⟩ dsimp only at h split at h · simp at h rename_i hm0 split at h · rename_i r hr injection h with h rw [← h] set sb : Int := (148 : Int) + (expOf bw).toNat - base with hsbdef set d := min (sb.toNat / 21) 4 with hd have hbias : sv64 (biasWord bw sb) * 2 ^ (21 * d) = sv bw * 2 ^ (148 + (expOf bw).toNat - base) := by by_cases h0 : expOf bw = 0 · have hbs0 : biasWord bw sb = 0 := by unfold biasWord; simp [h0] rw [hbs0, sv_zero bw h0 (hz h0)]; simp [sv64] · obtain ⟨s1, s2⟩ := hsb h0 have hsbn : sb.toNat = 148 + (expOf bw).toNat - base := by omega have hd' : d = sb.toNat / 21 := by omega have := bias_val bw sb.toNat (by omega) h0 simp only at this rw [hd', ← hsbn] have hbw : biasWord bw sb = (if signOf bw then 0 - (((bw &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (sb.toNat % 21).toUInt64) else ((bw &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (sb.toNat % 21).toUInt64) := by unfold biasWord; simp [h0]; rfl rw [hbw]; exact this have hT := total128 a b c (biasWord bw sb) d (by omega) rw [hbias] at hT set T := sv64 a + sv64 b * 2 ^ 21 + sv64 c * 2 ^ 42 + sv bw * 2 ^ (148 + (expOf bw).toNat - base) with hTdef have hBi : -(2 : Int) ^ 124 < sv bw * 2 ^ (148 + (expOf bw).toNat - base) ∧ sv bw * 2 ^ (148 + (expOf bw).toNat - base) < 2 ^ 124 := by by_cases h0 : expOf bw = 0 · rw [sv_zero bw h0 (hz h0)]; norm_num · have := hsb h0 have hp : (2 : Int) ^ (148 + (expOf bw).toNat - base) ≤ 2 ^ 100 := pow_le_pow_right₀ (by norm_num) (by omega) have hp0 : (0 : Int) < 2 ^ (148 + (expOf bw).toNat - base) := by positivity obtain ⟨v1, v2⟩ := sv_abs bw constructor <;> nlinarith have ha := sv64_bounds a; have hb := sv64_bounds b; have hc := sv64_bounds c have hT' : (v128 (total3 a b c (biasWord bw sb) d) : Int) = T % 2 ^ 128 := hT obtain ⟨sg, mg⟩ := signmag _ T hT' (by omega) (by omega) have hq1 : (magOf (total3 a b c (biasWord bw sb) d)).1 = decide (T < 0) := by unfold magOf; exact sg have hq2 : v128 ⟨(magOf (total3 a b c (biasWord bw sb) d)).2.1, (magOf (total3 a b c (biasWord bw sb) d)).2.2⟩ = T.natAbs := by unfold magOf; exact mg rw [hq1] at hr obtain ⟨hT0, hrt⟩ := round_total T _ _ hq2 base r (by simpa using hm0) hr nz exact ⟨hT0, by rw [hrt]⟩ · simp at h def v32 (v : UInt32) : Int := v.toBitVec.toInt theorem v32_eq (v : UInt32) : v32 v = if v.toNat < 2 ^ 31 then (v.toNat : Int) else (v.toNat : Int) - 2 ^ 32 := by unfold v32 rw [BitVec.toInt_eq_toNat_cond] simp only [UInt32.toNat_toBitVec] split_ifs <;> push_cast <;> omega theorem part_val (p : UInt64) (hp : p.toNat < 2 ^ 21) (neg : Bool) : v32 ((if neg then 0 - p else p).toUInt32) = if neg then -(p.toNat : Int) else p.toNat := by rw [v32_eq] have hs : UInt64.size = 2 ^ 64 := rfl cases neg · simp only [Bool.false_eq_true, ite_false, UInt64.toNat_toUInt32] rw [Nat.mod_eq_of_lt (by omega), if_pos (by omega)] · simp only [ite_true, UInt64.toNat_toUInt32, UInt64.toNat_sub, UInt64.toNat_zero, hs] by_cases h0 : p.toNat = 0 · simp [h0] · have : (2 ^ 64 - p.toNat + 0) % 2 ^ 64 % 2 ^ 32 = 2 ^ 32 - p.toNat := by omega rw [this, if_neg (by omega)]; push_cast; omega def wv (w : UInt32) (lx : Nat) : Int := sv w * 2 ^ ((expOf w).toNat - lx) def pv (P0 P1 P2 : Array UInt32) (t : Nat) : Int := v32 P0[t]! + v32 P1[t]! * 2 ^ 21 + v32 P2[t]! * 2 ^ 42 def PartsOk' (a b c : UInt32) : Prop := (v32 a).natAbs < 2 ^ 21 ∧ (v32 b).natAbs < 2 ^ 21 ∧ (v32 c).natAbs < 2 ^ 21 def PartsOk (P0 P1 P2 : Array UInt32) (t : Nat) : Prop := PartsOk' P0[t]! P1[t]! P2[t]! def XOk (X : Array UInt32) (o : Nat) (lx lim : Nat) (k : Nat) : Prop := (expOf X[o + k]!).toNat ≠ 255 ∧ ((expOf X[o + k]!).toNat = 0 → (fracOf X[o + k]!).toNat = 0) ∧ ((expOf X[o + k]!).toNat ≠ 0 → lx ≤ (expOf X[o + k]!).toNat ∧ (expOf X[o + k]!).toNat ≤ lx + lim) theorem parts_wv (w : UInt32) (lx lim : UInt32) (hlim : lim ≤ 39) (he0 : (expOf w).toNat ≠ 0) (hlx : lx ≤ (w >>> 23) &&& 0xFF) (hd : ((w >>> 23) &&& 0xFF) - lx ≤ lim) : let mag := ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (((w >>> 23) &&& 0xFF) - lx).toUInt64 let p0 := mag &&& 0x1FFFFF let p1 := (mag >>> 21) &&& 0x1FFFFF let p2 := mag >>> 42 p0.toNat < 2 ^ 21 ∧ p1.toNat < 2 ^ 21 ∧ p2.toNat < 2 ^ 21 ∧ (if signOf w then -(p0.toNat : Int) else p0.toNat) + (if signOf w then -(p1.toNat : Int) else p1.toNat) * 2 ^ 21 + (if signOf w then -(p2.toNat : Int) else p2.toNat) * 2 ^ 42 = wv w lx.toNat := by intro mag p0 p1 p2 have he : (w >>> 23) &&& 0xFF = expOf w := rfl rw [he] at hlx hd have hlim' : lim.toNat ≤ 39 := hlim have hlx' : lx.toNat ≤ (expOf w).toNat := hlx have hd' : ((expOf w) - lx).toNat = (expOf w).toNat - lx.toNat := by rw [UInt32.toNat_sub_of_le _ _ hlx] have hdl : (expOf w).toNat - lx.toNat ≤ 39 := by have : ((expOf w) - lx).toNat ≤ lim.toNat := hd omega have hfr : (fracOf w).toNat = (w &&& 0x7FFFFF).toNat := rfl have hf := fracOf_lt w have hmant : mantOf w = 2 ^ 23 + (fracOf w).toNat := by have : expOf w ≠ 0 := fun h' => he0 (by simp [h']) simp [mantOf, this] have hm23 : ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64.toNat = mantOf w := by rw [UInt32.toNat_toUInt64, UInt32.toNat_or, hmant, hfr] rw [or_disj _ _ 23 hf (by norm_num)]; norm_num; omega have hml := mantOf_lt_24 w set d := (expOf w).toNat - lx.toNat with hddef have hmag : mag.toNat = mantOf w * 2 ^ d := by simp only [mag, he, UInt64.toNat_shiftLeft, hm23] have : ((expOf w - lx).toUInt64.toNat % 64) = d := by simp only [UInt32.toNat_toUInt64, hd']; omega rw [this, Nat.shiftLeft_eq] apply Nat.mod_eq_of_lt calc mantOf w * 2 ^ d < 2 ^ 24 * 2 ^ 39 := Nat.mul_lt_mul_of_lt_of_le hml (Nat.pow_le_pow_right (by norm_num) (by omega)) (by positivity) _ ≤ 2 ^ 64 := by norm_num have hmag63 : mag.toNat < 2 ^ 63 := by rw [hmag] calc mantOf w * 2 ^ d < 2 ^ 24 * 2 ^ 39 := Nat.mul_lt_mul_of_lt_of_le hml (Nat.pow_le_pow_right (by norm_num) (by omega)) (by positivity) _ = 2 ^ 63 := by norm_num have h0 : p0.toNat = mag.toNat % 2 ^ 21 := by simp only [p0, UInt64.toNat_and]; exact Nat.and_two_pow_sub_one_eq_mod _ 21 have h1 : p1.toNat = mag.toNat / 2 ^ 21 % 2 ^ 21 := by simp only [p1, UInt64.toNat_and, UInt64.toNat_shiftRight] rw [show (21 : UInt64).toNat % 64 = 21 from rfl, Nat.shiftRight_eq_div_pow] exact Nat.and_two_pow_sub_one_eq_mod _ 21 have h2 : p2.toNat = mag.toNat / 2 ^ 42 := by simp only [p2, UInt64.toNat_shiftRight] rw [show (42 : UInt64).toNat % 64 = 42 from rfl, Nat.shiftRight_eq_div_pow] refine ⟨by omega, by omega, by omega, ?_⟩ have hsum : (p0.toNat : Int) + p1.toNat * 2 ^ 21 + p2.toNat * 2 ^ 42 = mag.toNat := by rw [h0, h1, h2]; omega unfold wv sv rw [← hddef] have hm : ((mantOf w : Nat) : Int) * 2 ^ d = (mag.toNat : Int) := by rw [hmag]; push_cast; ring split_ifs with hs · push_cast at hsum ⊢; linarith · push_cast at hsum ⊢; linarith theorem parts_wv' (w lx lim : UInt32) (hlim : lim ≤ 39) (he0 : (expOf w).toNat ≠ 0) (hlx : lx ≤ (w >>> 23) &&& 0xFF) (hd : ((w >>> 23) &&& 0xFF) - lx ≤ lim) (mag p0 p1 p2 : UInt64) (neg : Bool) (hmag : mag = ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (((w >>> 23) &&& 0xFF) - lx).toUInt64) (h0 : p0 = mag &&& 0x1FFFFF) (h1 : p1 = (mag >>> 21) &&& 0x1FFFFF) (h2 : p2 = mag >>> 42) (hn : neg = signOf w) : PartsOk' (if _h : neg = true then 0 - p0 else p0).toUInt32 (if _h : neg = true then 0 - p1 else p1).toUInt32 (if _h : neg = true then 0 - p2 else p2).toUInt32 ∧ (lim ≤ 18 → v32 (if _h : neg = true then 0 - p2 else p2).toUInt32 = 0) ∧ v32 (if _h : neg = true then 0 - p0 else p0).toUInt32 + v32 (if _h : neg = true then 0 - p1 else p1).toUInt32 * 2 ^ 21 + v32 (if _h : neg = true then 0 - p2 else p2).toUInt32 * 2 ^ 42 = wv w lx.toNat := by subst hmag h0 h1 h2 hn obtain ⟨q0, q1, q2, hval⟩ := parts_wv w lx lim hlim he0 hlx hd simp only [dite_eq_ite] rw [part_val _ q0, part_val _ q1, part_val _ q2] refine ⟨?_, ?_, hval⟩ rotate_left · intro h18 have he : (w >>> 23) &&& 0xFF = expOf w := rfl have hlx' : lx.toNat ≤ (expOf w).toNat := by rw [he] at hlx; exact hlx have hdn : ((expOf w) - lx).toNat = (expOf w).toNat - lx.toNat := UInt32.toNat_sub_of_le _ _ (by rw [he] at hlx; exact hlx) have hd18 : (expOf w).toNat - lx.toNat ≤ 18 := by have : ((expOf w) - lx).toNat ≤ lim.toNat := by rw [he] at hd; exact hd have : lim.toNat ≤ 18 := h18 omega have hml := mantOf_lt_24 w have hmant : mantOf w = 2 ^ 23 + (fracOf w).toNat := by have : expOf w ≠ 0 := fun h' => he0 (by simp [h']) simp [mantOf, this] have hfr : (fracOf w).toNat = (w &&& 0x7FFFFF).toNat := rfl have hf := fracOf_lt w have hm23 : ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64.toNat = mantOf w := by rw [UInt32.toNat_toUInt64, UInt32.toNat_or, hmant, hfr] rw [or_disj _ _ 23 hf (by norm_num)]; norm_num; omega have hp2 : ((((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (((w >>> 23) &&& 0xFF) - lx).toUInt64) >>> 42).toNat = 0 := by rw [UInt64.toNat_shiftRight, UInt64.toNat_shiftLeft, hm23, he] have : ((expOf w - lx).toUInt64.toNat % 64) = (expOf w).toNat - lx.toNat := by simp only [UInt32.toNat_toUInt64, hdn]; omega rw [this, Nat.shiftLeft_eq, show (42 : UInt64).toNat % 64 = 42 from rfl, Nat.shiftRight_eq_div_pow] have hlt : mantOf w * 2 ^ ((expOf w).toNat - lx.toNat) < 2 ^ 42 := by calc mantOf w * 2 ^ ((expOf w).toNat - lx.toNat) < 2 ^ 24 * 2 ^ 18 := Nat.mul_lt_mul_of_lt_of_le hml (Nat.pow_le_pow_right (by norm_num) hd18) (by positivity) _ = 2 ^ 42 := by norm_num rw [Nat.mod_eq_of_lt (by omega)] exact Nat.div_eq_of_lt hlt rw [hp2]; split <;> simp rotate_right unfold PartsOk' clear hval rw [part_val _ q0, part_val _ q1, part_val _ q2] refine ⟨?_, ?_, ?_⟩ <;> split <;> simp only [Int.natAbs_neg, Int.natAbs_natCast] <;> omega theorem decX_sem (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (hlim : lim ≤ 39) (k c : USize) (IX P0 P1 P2 : Array UInt32) (hck : c.toNat ≤ k.toNat) (hs : n.toNat ≤ IX.size ∧ n.toNat ≤ P0.size ∧ n.toNat ≤ P1.size ∧ n.toNat ≤ P2.size) (ok : Bool) (hkn : k.toNat ≤ n.toNat) : let r := decX X o n lx lim hX hXs k c IX P0 P1 P2 hck hs ok c.toNat ≤ r.2.2.2.2.1.toNat ∧ (r.2.2.2.2.2 = true → ok = true ∧ (∀ k', k.toNat ≤ k' → k' < n.toNat → XOk X o.toNat lx.toNat lim.toNat k') ∧ (∀ t, t < c.toNat → r.1[t]! = IX[t]! ∧ r.2.1[t]! = P0[t]! ∧ r.2.2.1[t]! = P1[t]! ∧ r.2.2.2.1[t]! = P2[t]!) ∧ (∀ t, c.toNat ≤ t → t < r.2.2.2.2.1.toNat → PartsOk r.2.1 r.2.2.1 r.2.2.2.1 t ∧ (lim ≤ 18 → v32 r.2.2.2.1[t]! = 0) ∧ pv r.2.1 r.2.2.1 r.2.2.2.1 t = wv X[o.toNat + (r.1[t]!).toNat]! lx.toNat) ∧ (∀ G : Nat → Int, ∑ t ∈ Finset.Ico c.toNat r.2.2.2.2.1.toNat, G (r.1[t]!).toNat = ∑ k' ∈ Finset.Ico k.toNat n.toNat, if (expOf X[o.toNat + k']!).toNat ≠ 0 then G k' else 0)) := by induction k, c, IX, P0, P1, P2, hck, hs, ok using decX.induct X o n lx lim hX hXs with | case1 k c IX P0 P1 P2 hck hs ok h hk hk1 w e he ih => have hw : w = X[o.toNat + k.toNat]! := by simp only [w]; rw [uget_eq, usize_add_toNat _ _ (by omega)] have he0 : expOf w = 0 := by have := he; simp only [beq_iff_eq] at this; exact this have heq : decX X o n lx lim hX hXs k c IX P0 P1 P2 hck hs ok = decX X o n lx lim hX hXs (k + 1) c IX P0 P1 P2 (by omega) hs (ok && (w &&& 0x7FFFFF) == 0) := by conv_lhs => unfold decX simp only [dite_cond_eq_true (eq_true h)] exact if_pos he rw [heq] obtain ⟨hmono, ih2⟩ := ih (by omega) refine ⟨hmono, fun hfin => ?_⟩ obtain ⟨hok', hxk, hold, hnew, hsum⟩ := ih2 hfin simp only [Bool.and_eq_true, beq_iff_eq] at hok' refine ⟨hok'.1, ?_, hold, hnew, ?_⟩ · intro k' hk1' hk2' rcases Nat.lt_or_ge k' (k.toNat + 1) with hlt | hge · have hk' : k' = k.toNat := by omega subst hk' unfold XOk rw [← hw] have hf0 : (fracOf w).toNat = 0 := by have : fracOf w = 0 := hok'.2 simp [this] refine ⟨by rw [he0]; decide, fun _ => hf0, fun h' => absurd (by simp [he0]) h'⟩ · exact hxk k' (by omega) hk2' · intro G rw [hsum G, hk1, Finset.sum_eq_sum_Ico_succ_bot hk] rw [← hw, he0]; simp | case2 k c IX P0 P1 P2 hck hs ok h hk hk1 w e he hc1 d mag p0 p1 p2 neg ih => have hw : w = X[o.toNat + k.toNat]! := by simp only [w]; rw [uget_eq, usize_add_toNat _ _ (by omega)] have he0 : (expOf w).toNat ≠ 0 := by intro h0; apply he; have : expOf w = 0 := UInt32.toNat_inj.mp (by simpa using h0) simp only [beq_iff_eq]; exact this have heq : decX X o n lx lim hX hXs k c IX P0 P1 P2 hck hs ok = decX X o n lx lim hX hXs (k + 1) (c + 1) (IX.uset c k.toUInt32 (by omega)) (P0.uset c (if _h : neg = true then 0 - p0 else p0).toUInt32 (by omega)) (P1.uset c (if _h : neg = true then 0 - p1 else p1).toUInt32 (by omega)) (P2.uset c (if _h : neg = true then 0 - p2 else p2).toUInt32 (by omega)) (by omega) (by simp only [Array.size_uset]; exact hs) (ok && e != 255 && lx ≤ e && d ≤ lim) := by conv_lhs => unfold decX simp only [dite_cond_eq_true (eq_true h)] exact if_neg he rw [heq] obtain ⟨hmono, ih2⟩ := ih (by omega) refine ⟨by omega, fun hfin => ?_⟩ obtain ⟨hok', hxk, hold, hnew, hsum⟩ := ih2 hfin simp only [Bool.and_eq_true, bne_iff_ne, ne_eq, decide_eq_true_eq] at hok' obtain ⟨⟨⟨hok, h255⟩, hlx⟩, hdl⟩ := hok' have hlx' : lx.toNat ≤ (expOf w).toNat := hlx have hdn : ((expOf w) - lx).toNat = (expOf w).toNat - lx.toNat := UInt32.toNat_sub_of_le _ _ hlx obtain ⟨hpo, h2z, hval⟩ := parts_wv' w lx lim hlim he0 hlx hdl mag p0 p1 p2 neg rfl rfl rfl rfl rfl have hc_lt : c.toNat < (c + 1).toNat := by omega refine ⟨hok, ?_, ?_, ?_, ?_⟩ · intro k' hk1' hk2' rcases Nat.lt_or_ge k' (k.toNat + 1) with hlt | hge · have hk' : k' = k.toNat := by omega subst hk' unfold XOk rw [← hw] have hdl' : ((expOf w) - lx).toNat ≤ lim.toNat := hdl refine ⟨fun h' => h255 (by have : expOf w = 255 := UInt32.toNat_inj.mp (by simpa using h'); exact this), fun h' => absurd h' he0, fun _ => ⟨hlx', by omega⟩⟩ · exact hxk k' (by omega) hk2' · intro t ht obtain ⟨o1, o2, o3, o4⟩ := hold t (by omega) refine ⟨by rw [o1, uset_get!, if_neg (by omega)], by rw [o2, uset_get!, if_neg (by omega)], by rw [o3, uset_get!, if_neg (by omega)], by rw [o4, uset_get!, if_neg (by omega)]⟩ · intro t ht1 ht2 rcases Nat.lt_or_ge t (c.toNat + 1) with hlt | hge · have ht : t = c.toNat := by omega subst ht obtain ⟨o1, o2, o3, o4⟩ := hold c.toNat hc_lt refine ⟨?_, ?_, ?_⟩ · unfold PartsOk; rw [o2, o3, o4, uset_get!, uset_get!, uset_get!, if_pos rfl, if_pos rfl, if_pos rfl] exact hpo · intro h18; rw [o4, uset_get!, if_pos rfl]; exact h2z h18 · unfold pv; rw [o1, o2, o3, o4] rw [uset_get!, uset_get!, uset_get!, uset_get!, if_pos rfl, if_pos rfl, if_pos rfl, if_pos rfl, hval] simp only [USize.toNat_toUInt32] have : k.toNat < 2 ^ 32 := by omega rw [Nat.mod_eq_of_lt this, ← hw] · exact hnew t (by omega) ht2 · intro G have hs2 := hsum G rw [hc1, hk1] at hs2 rw [Finset.sum_eq_sum_Ico_succ_bot (by omega), hs2, Finset.sum_eq_sum_Ico_succ_bot hk, ← hw, if_pos he0] congr 1 obtain ⟨o1, -, -, -⟩ := hold c.toNat hc_lt rw [o1, uset_get!, if_pos rfl] simp only [USize.toNat_toUInt32] have : k.toNat < 2 ^ 32 := by omega rw [Nat.mod_eq_of_lt this] | case3 k c IX P0 P1 P2 hck hs ok h => unfold decX; simp only [dite_cond_eq_false (eq_false h)] have hkn' : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') refine ⟨le_rfl, fun hfin => ⟨hfin, fun k' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), fun t _ => ⟨by simp, by simp, by simp, by simp⟩, fun t h1 h2 => absurd (lt_of_le_of_lt h1 h2) (lt_irrefl _), ?_⟩⟩ intro G rw [Finset.Ico_self, Finset.Ico_eq_empty (by omega)] simp theorem decWRow_sem (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c : UInt64) (hkn : k.toNat ≤ n.toNat) : let r := decWRow W wo hwo o n hW hWs k D hD c (∀ i, ¬(o.toNat + k.toNat ≤ i ∧ i < o.toNat + n.toNat) → r.1[i]! = D[i]!) ∧ (∀ i, o.toNat + k.toNat ≤ i → i < o.toNat + n.toNat → r.1[i]! = (sgnW W[i]! (magW wo hwo W[i]!)).toUInt32) ∧ (r.2.toNat < 2 ^ 31 → c.toNat < 2 ^ 31 ∧ ∀ i, o.toNat + k.toNat ≤ i → i < o.toNat + n.toNat → (magW wo hwo W[i]!).toNat < 2 ^ 31) := by induction k, D, hD, c using decWRow.induct W wo hwo o n hW hWs with | case1 k D hD c h hk hok v m ih => have hv : v = W[o.toNat + k.toNat]! := by simp only [v]; rw [uget_eq, hok] have heq : decWRow W wo hwo o n hW hWs k D hD c = decWRow W wo hwo o n hW hWs (k + 1) (D.uset (o + k) (sgnW v m).toUInt32 (by omega)) (by simp only [Array.size_uset]; exact hD) (c ||| m) := by conv_lhs => unfold decWRow simp only [dite_cond_eq_true (eq_true h)] rfl rw [heq] have hk1 := usize_succ_toNat k n h obtain ⟨i1, i2, i3⟩ := ih (by omega) rw [hk1] at i1 i2 i3 refine ⟨?_, ?_, ?_⟩ · intro i hi rw [i1 i (by omega), uset_get!, if_neg (by omega)] · intro i h1 h2 rcases Nat.lt_or_ge i (o.toNat + k.toNat + 1) with hlt | hge · have : i = o.toNat + k.toNat := by omega subst this rw [i1 _ (by omega), uset_get!, if_pos hok.symm] simp only [m]; rw [hv] · exact i2 i (by omega) h2 · intro hc obtain ⟨hc1, hc2⟩ := i3 hc rw [or_lt] at hc1 refine ⟨hc1.1, fun i h1 h2 => ?_⟩ rcases Nat.lt_or_ge i (o.toNat + k.toNat + 1) with hlt | hge · have : i = o.toNat + k.toNat := by omega subst this; rw [← hv]; exact hc1.2 · exact hc2 i (by omega) h2 | case2 k D hD c h => unfold decWRow; simp only [dite_cond_eq_false (eq_false h)] have : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') exact ⟨fun _ _ => by simp, fun i h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), fun hc => ⟨hc, fun i h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩⟩ theorem preDecode_sem (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) : let r := preDecode W wo hwo inW outW hWsz hWs j D hD oks (∀ i, i < j * inW → r.1[i]! = D[i]!) ∧ (∀ j', j ≤ j' → j' < outW → ∀ k, k < inW → r.1[j' * inW + k]! = (sgnW W[j' * inW + k]! (magW wo hwo W[j' * inW + k]!)).toUInt32) ∧ (∀ j', j' < oks.size → r.2[j']! = oks[j']!) ∧ (∀ j', j ≤ j' → j' < outW → r.2[oks.size + (j' - j)]! = true → ∀ k, k < inW → (magW wo hwo W[j' * inW + k]!).toNat < 2 ^ 31) := by induction j, D, hD, oks using preDecode.induct W wo hwo inW outW hWsz hWs with | case1 j D hD oks hj h1 r ih => have heq : preDecode W wo hwo inW outW hWsz hWs j D hD oks = preDecode W wo hwo inW outW hWsz hWs (j + 1) r.1 (by rw [decWRow_size]; exact hD) (oks.push (r.2 < 0x80000000)) := by conv_lhs => unfold preDecode simp only [dite_cond_eq_true (eq_true hj)] rfl rw [heq] obtain ⟨p1, p2, p3, p4⟩ := ih have hji : (j * inW).toUSize.toNat = j * inW := toUSize_toNat _ (by nlinarith) have hni : inW.toUSize.toNat = inW := toUSize_toNat _ (by nlinarith) obtain ⟨d1, d2, d3⟩ := decWRow_sem W wo hwo (j * inW).toUSize inW.toUSize h1 hWs 0 D hD 0 (by simp) simp only [USize.toNat_zero, Nat.add_zero, hji, hni] at d1 d2 d3 refine ⟨?_, ?_, ?_, ?_⟩ · intro i hi rw [p1 i (by nlinarith), d1 i (by omega)] · intro j' hj1 hj2 k hk rcases Nat.lt_or_ge j' (j + 1) with hlt | hge · have : j' = j := by omega subst this rw [p1 _ (by nlinarith), d2 _ (by omega) (by nlinarith)] · exact p2 j' hge hj2 k hk · intro j' hj' rw [p3 j' (by simp; omega), push_get!_lt _ _ _ hj'] · intro j' hj1 hj2 hok k hk rcases Nat.lt_or_ge j' (j + 1) with hlt | hge · have hjj : j' = j := by omega have h3 := p3 oks.size (by simp) rw [push_get!_eq] at h3 have hidx : oks.size + (j' - j) = oks.size := by omega rw [hidx, h3] at hok have hc' : r.2.toNat < 2 ^ 31 := by simpa [UInt64.lt_iff_toNat_lt] using hok rw [hjj] exact (d3 hc').2 _ (by omega) (by nlinarith) · have := p4 j' hge hj2 (by rw [show (oks.push (r.2 < 0x80000000)).size + (j' - (j + 1)) = oks.size + (j' - j) by simp; omega] exact hok) exact this k hk | case2 j D hD oks hj => unfold preDecode; simp only [dite_cond_eq_false (eq_false hj)] exact ⟨fun _ _ => by simp, fun j' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), fun _ _ => by simp, fun j' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩ theorem sx_bv (v : UInt32) : (sx v).toBitVec = BitVec.ofInt 64 (v32 v) := by simp [sx, v32, BitVec.signExtend] theorem v32_toUInt32 (x : UInt64) (h : -2 ^ 31 ≤ sv64 x ∧ sv64 x < 2 ^ 31) : v32 x.toUInt32 = sv64 x := by have hx := x.toNat_lt rw [v32_eq, sv64_eq] at * simp only [UInt64.toNat_toUInt32] split_ifs at h ⊢ <;> push_cast <;> omega theorem sx_toUInt32 (x : UInt64) (h : -2 ^ 31 ≤ sv64 x ∧ sv64 x < 2 ^ 31) : sx x.toUInt32 = x := by apply UInt64.toBitVec_inj.mp rw [sx_bv, v32_toUInt32 x h] unfold sv64 exact BitVec.ofInt_toInt theorem lor_255 (a : Nat) (h : a < 256) : a ||| 255 = 255 := by interval_cases a <;> rfl theorem expOf_or_255 (x y : UInt32) (h : expOf y = 255) : expOf (x ||| y) = 255 := by apply UInt32.toNat_inj.mp have hy : (expOf y).toNat = 255 := by rw [h]; rfl unfold expOf at hy ⊢ simp only [UInt32.toNat_and, UInt32.toNat_shiftRight, UInt32.toNat_or] at hy ⊢ norm_num at hy ⊢ rw [Nat.shiftRight_or_distrib, Nat.and_or_distrib_right, hy] exact lor_255 _ (Nat.lt_of_le_of_lt Nat.and_le_right (by norm_num)) theorem orAll_spec (W : Array UInt32) (n : USize) (hW : n.toNat ≤ W.size) (k : USize) (c : UInt32) (hkn : k.toNat ≤ n.toNat) : expOf (orAll W n hW k c) ≠ 255 → expOf c ≠ 255 ∧ ∀ i, k.toNat ≤ i → i < n.toNat → expOf W[i]! ≠ 255 := by induction k, c using orAll.induct W n hW with | case1 k c h hk ih => have heq : orAll W n hW k c = orAll W n hW (k + 1) (c ||| W.uget k (by omega)) := by conv_lhs => unfold orAll simp only [dite_cond_eq_true (eq_true h)] rw [heq] intro hne have hk1 := usize_succ_toNat k n h obtain ⟨h1, h2⟩ := ih (by omega) hne have hwk : W.uget k (by omega) = W[k.toNat]! := uget_eq _ _ _ refine ⟨fun hc => h1 ?_, fun i hi1 hi2 => ?_⟩ · have hcomm : (c ||| W.uget k (by omega)) = (W.uget k (by omega) ||| c) := by apply UInt32.toNat_inj.mp; rw [UInt32.toNat_or, UInt32.toNat_or, Nat.lor_comm] rw [hcomm]; exact expOf_or_255 _ _ hc rcases Nat.lt_or_ge i (k.toNat + 1) with hlt | hge · have : i = k.toNat := by omega subst this intro h255; apply h1; rw [hwk]; exact expOf_or_255 _ _ h255 · exact h2 i (by omega) hi2 | case2 k c h => unfold orAll; simp only [dite_cond_eq_false (eq_false h)] have : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') exact fun hc => ⟨hc, fun i h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩ theorem dsum_eq (a b : Array UInt32) (st c : Nat) : dsum a b st c = ∑ k ∈ Finset.range c, sv a[st + k]! * sv b[st + k]! * 2 ^ (idxOf a[st + k]! + idxOf b[st + k]!) := by induction c generalizing st with | zero => simp [dsum] | succ c ih => rw [dsum, ih (st + 1), Finset.sum_range_succ'] simp only [Nat.add_zero, Nat.add_assoc, Nat.add_comm 1] ring theorem dotSpec_eq_finish (A B : Array UInt32) (hs : A.size = B.size) (hfin : ∀ k, k < A.size → expOf A[k]! ≠ 255 ∧ expOf B[k]! ≠ 255) (hS0 : dsum A B 0 A.size ≠ 0) (nz : Bool) : dotSpec A B = finishWord (dsum A B 0 A.size) (-298) nz := by 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, hs] have hLsize : L.size = A.size := by simp [hL] 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 := (hfin k hk').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 := (hfin k (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]) = (dsum A B 0 A.size : ℚ) * 2 ^ (-298 : ℤ) := by have := finiteDotSlice_eq A B 0 L.size (by simp [hL]) (by simp [hL, hR, hs]) (by intro k k1 k2; exact hfin k (by omega)) exact this.trans (by rw [hLsize]) have hexact := Model.Reduction.Internal.dotState_exact_toRat (destination := Fmt) L R hLR hLfin hRfin unfold dotSpec Model.dot 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])).symm theorem term_wv (x : UInt32) (lx : Nat) (hlx : 1 ≤ lx) (h : (expOf x = 0 ∧ fracOf x = 0) ∨ (lx ≤ (expOf x).toNat)) : sv x * 2 ^ (idxOf x) = wv x lx * 2 ^ (lx - 1) := by rcases h with ⟨he, hf⟩ | hle · rw [sv_zero x he hf]; unfold wv; rw [sv_zero x he hf]; ring · have hne : expOf x ≠ 0 := by intro h0; rw [h0] at hle; simp at hle; omega have hidx : idxOf x = (expOf x).toNat - 1 := by simp [idxOf, hne] unfold wv; rw [hidx, mul_assoc, ← pow_add]; congr 2; omega 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 oneW_facts : expOf oneW = 127 ∧ sv oneW = 2 ^ 23 ∧ idxOf oneW = 126 := by decide theorem out_core (inW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (lx lw : Nat) (hlx : 1 ≤ lx) (hlw : 1 ≤ lw) (hx : ∀ k, k < inW → expOf X[i * inW + k]! ≠ 255 ∧ ((expOf X[i * inW + k]! = 0 ∧ fracOf X[i * inW + k]! = 0) ∨ lx ≤ (expOf X[i * inW + k]!).toNat)) (hw : ∀ k, k < inW → expOf W[jj * inW + k]! ≠ 255) (hwx : ∀ k, k < inW → expOf X[i * inW + k]! ≠ 0 → ((expOf W[jj * inW + k]! = 0 ∧ fracOf W[jj * inW + k]! = 0) ∨ lw ≤ (expOf W[jj * inW + k]!).toNat)) (a bb c : UInt64) (hlanes : sv64 a + sv64 bb * 2 ^ 21 + sv64 c * 2 ^ 42 = ∑ k ∈ Finset.range inW, wv X[i * inW + k]! lx * wv W[jj * inW + k]! lw) (w : Word) (h : fin3 relu b[jj]! (lx + lw - 2) a bb c = some w) : w = act relu (dotOf (inW + 1) (fun t => if t < inW then X[i * inW + t]! else oneW) (fun t => if t < inW then W[jj * inW + t]! else b[jj]!)) := by obtain ⟨h255, hzb, hbase, hT0, hw'⟩ := fin3_spec relu b[jj]! (lx + lw - 2) a bb c w false h rw [hw'] congr 1 set base := lx + lw - 2 with hbd set S := ∑ k ∈ Finset.range inW, wv X[i * inW + k]! lx * wv W[jj * inW + k]! lw with hSdef rw [hlanes] at hT0 ⊢ set fa := fun t => if t < inW then X[i * inW + t]! else oneW with hfa set fb := fun t => if t < inW then W[jj * inW + t]! else b[jj]! with hfb set A := Array.ofFn (n := inW + 1) fun t => fa t.val with hA set B := Array.ofFn (n := inW + 1) fun t => fb t.val with hB have hAs : A.size = inW + 1 := by simp [hA] have hBs : B.size = inW + 1 := by simp [hB] have hAk : ∀ k, k < inW + 1 → A[k]! = fa k := fun k hk => getElem!_ofFn' _ _ _ hk have hBk : ∀ k, k < inW + 1 → B[k]! = fb k := fun k hk => getElem!_ofFn' _ _ _ hk have hdsum : dsum A B 0 A.size = (S + sv b[jj]! * 2 ^ (148 + (expOf b[jj]!).toNat - base)) * 2 ^ base := by rw [dsum_eq, hAs, Finset.sum_range_succ] have hlast : sv A[0 + inW]! * sv B[0 + inW]! * 2 ^ (idxOf A[0 + inW]! + idxOf B[0 + inW]!) = sv b[jj]! * 2 ^ (148 + (expOf b[jj]!).toNat - base) * 2 ^ base := by rw [Nat.zero_add, hAk _ (by omega), hBk _ (by omega)] simp only [hfa, hfb, lt_irrefl, if_false] obtain ⟨_, hsv1, hidx1⟩ := oneW_facts rw [hsv1, hidx1] by_cases heb : expOf b[jj]! = 0 · rw [sv_zero _ heb (hzb heb)]; ring · have hidx : idxOf b[jj]! = (expOf b[jj]!).toNat - 1 := by simp [idxOf, heb] have hb0 : (expOf b[jj]!).toNat ≠ 0 := fun h' => heb (UInt32.toNat_inj.mp (by simpa using h')) have := hbase heb rw [hidx] have e1 : (2 : Int) ^ 23 * 2 ^ (126 + ((expOf b[jj]!).toNat - 1)) = 2 ^ (148 + (expOf b[jj]!).toNat) := by rw [← pow_add]; congr 1; omega have e2 : (2 : Int) ^ (148 + (expOf b[jj]!).toNat - base) * 2 ^ base = 2 ^ (148 + (expOf b[jj]!).toNat) := by rw [← pow_add]; congr 1; omega calc (2 : Int) ^ 23 * sv b[jj]! * 2 ^ (126 + ((expOf b[jj]!).toNat - 1)) = sv b[jj]! * ((2 : Int) ^ 23 * 2 ^ (126 + ((expOf b[jj]!).toNat - 1))) := by ring _ = sv b[jj]! * ((2 : Int) ^ (148 + (expOf b[jj]!).toNat - base) * 2 ^ base) := by rw [e1, e2] _ = _ := by ring have hbody : ∑ k ∈ Finset.range inW, sv A[0 + k]! * sv B[0 + k]! * 2 ^ (idxOf A[0 + k]! + idxOf B[0 + k]!) = S * 2 ^ base := by rw [hSdef, Finset.sum_mul] apply Finset.sum_congr rfl intro k hk have hk' := Finset.mem_range.mp hk rw [Nat.zero_add, hAk _ (by omega), hBk _ (by omega)] simp only [hfa, hfb, hk', if_true] obtain ⟨_, hxk⟩ := hx k hk' by_cases hxe : expOf X[i * inW + k]! = 0 · have hxf : fracOf X[i * inW + k]! = 0 := by rcases hxk with ⟨_, hf⟩ | hle · exact hf · rw [hxe] at hle; simp at hle; omega rw [sv_zero _ hxe hxf]; unfold wv; rw [sv_zero _ hxe hxf]; ring · have tx := term_wv X[i * inW + k]! lx hlx hxk have tw := term_wv W[jj * inW + k]! lw hlw (hwx k hk' hxe) rw [pow_add, show sv X[i * inW + k]! * sv W[jj * inW + k]! * (2 ^ idxOf X[i * inW + k]! * 2 ^ idxOf W[jj * inW + k]!) = (sv X[i * inW + k]! * 2 ^ idxOf X[i * inW + k]!) * (sv W[jj * inW + k]! * 2 ^ idxOf W[jj * inW + k]!) by ring, tx, tw, show base = (lx - 1) + (lw - 1) by omega, pow_add] ring rw [hbody, hlast]; ring have hfin : ∀ k, k < A.size → expOf A[k]! ≠ 255 ∧ expOf B[k]! ≠ 255 := by intro k hk rw [hAs] at hk rw [hAk k hk, hBk k hk] simp only [hfa, hfb] split_ifs with hk' · exact ⟨(hx k hk').1, hw k hk'⟩ · exact ⟨by rw [oneW_facts.1]; decide, h255⟩ have hne : dsum A B 0 A.size ≠ 0 := by rw [hdsum]; exact mul_ne_zero hT0 (pow_ne_zero _ (by norm_num)) unfold dotOf rw [← hA, ← hB, dotSpec_eq_finish A B (by rw [hAs, hBs]) hfin hne false, hdsum, ← finishWord_scale] congr 1 theorem bv_sum (N : Nat) (f : Nat → UInt64) : (∑ s ∈ Finset.range N, f s).toBitVec = ∑ s ∈ Finset.range N, (f s).toBitVec := by induction N with | zero => simp | succ N ih => rw [Finset.sum_range_succ, Finset.sum_range_succ, UInt64.toBitVec_add, ih] theorem ofInt_sum (N : Nat) (f : Nat → Int) : BitVec.ofInt 64 (∑ s ∈ Finset.range N, f s) = ∑ s ∈ Finset.range N, BitVec.ofInt 64 (f s) := by induction N with | zero => simp | succ N ih => rw [Finset.sum_range_succ, Finset.sum_range_succ, BitVec.ofInt_add, ih] theorem natAbs_sum_le (N : Nat) (f : Nat → Int) : (∑ s ∈ Finset.range N, f s).natAbs ≤ ∑ s ∈ Finset.range N, (f s).natAbs := by induction N with | zero => simp | succ N ih => rw [Finset.sum_range_succ, Finset.sum_range_succ] exact le_trans (Int.natAbs_add_le _ _) (by omega) theorem lane_int (N : Nat) (hN : N < 2048) (P : Array UInt32) (S : Nat → UInt64) (hP : ∀ s, s < N → (v32 P[s]!).natAbs < 2 ^ 21) (hS : ∀ s, s < N → (sv64 (S s)).natAbs < 2 ^ 31) : sv64 (0 + ∑ s ∈ Finset.Ico 0 N, sx P[s]! * S s) = ∑ s ∈ Finset.range N, v32 P[s]! * sv64 (S s) := by rw [zero_add, ← Finset.range_eq_Ico] have hterm : ∀ s, s < N → (sx P[s]! * S s).toBitVec = BitVec.ofInt 64 (v32 P[s]! * sv64 (S s)) := by intro s _ rw [UInt64.toBitVec_mul, sx_bv, BitVec.ofInt_mul] congr 1 unfold sv64; exact BitVec.ofInt_toInt.symm have hsum : (∑ s ∈ Finset.range N, sx P[s]! * S s).toBitVec = BitVec.ofInt 64 (∑ s ∈ Finset.range N, v32 P[s]! * sv64 (S s)) := by rw [bv_sum, ofInt_sum] exact Finset.sum_congr rfl fun s hs => hterm s (Finset.mem_range.mp hs) have hb : (∑ s ∈ Finset.range N, v32 P[s]! * sv64 (S s)).natAbs < 2 ^ 63 := by calc (∑ s ∈ Finset.range N, v32 P[s]! * sv64 (S s)).natAbs ≤ ∑ s ∈ Finset.range N, (v32 P[s]! * sv64 (S s)).natAbs := natAbs_sum_le _ _ _ ≤ ∑ _s ∈ Finset.range N, (2 ^ 52 : Nat) := by apply Finset.sum_le_sum intro s hs have h1 := hP s (Finset.mem_range.mp hs) have h2 := hS s (Finset.mem_range.mp hs) rw [Int.natAbs_mul] calc (v32 P[s]!).natAbs * (sv64 (S s)).natAbs ≤ 2 ^ 21 * 2 ^ 31 := Nat.mul_le_mul (by omega) (by omega) _ = 2 ^ 52 := by norm_num _ = N * 2 ^ 52 := by simp _ < 2 ^ 63 := by calc N * 2 ^ 52 < 2048 * 2 ^ 52 := Nat.mul_lt_mul_of_pos_right hN (by positivity) _ = 2 ^ 63 := by norm_num unfold sv64 rw [hsum, BitVec.toInt_ofInt] apply Int.bmod_eq_of_le <;> omega theorem sgnW_sv (w : UInt32) (m : UInt64) (hm : m.toNat < 2 ^ 31) : sv64 (sgnW w m) = if signOf w then -(m.toNat : Int) else m.toNat := by unfold sgnW split · by_cases h0 : m.toNat = 0 · have : m = 0 := UInt64.toNat_inj.mp (by simpa using h0) subst this; simp [sv64] · exact sv64_neg m (by omega) h0 · exact sv64_small m (by omega) theorem sgnW_wv_gen (lw : Nat) (w : UInt32) (M : UInt64) (hm : M.toNat < 2 ^ 31) (hmag : M.toNat = mantOf w * 2 ^ ((expOf w).toNat - lw)) : sv64 (sgnW w M) = wv w lw ∧ (sv64 (sgnW w M)).natAbs < 2 ^ 31 := by rw [sgnW_sv w _ hm] refine ⟨?_, ?_⟩ · unfold wv sv; rw [hmag]; split <;> push_cast <;> ring · split <;> simp only [Int.natAbs_neg, Int.natAbs_natCast] <;> omega theorem sgnW_wv (lw : Nat) (hlw : lw ≤ 254) (o : USize) (hoe : o.toNat = lw * 256) (ho : o.toNat ≤ 65280) (w : UInt32) (hm : (magW o ho w).toNat < 2 ^ 31) : sv64 (sgnW w (magW o ho w)) = wv w lw ∧ (sv64 (sgnW w (magW o ho w))).natAbs < 2 ^ 31 := sgnW_wv_gen lw w (magW o ho w) hm (magW_spec lw hlw o hoe ho w hm).1 theorem lanes_sum (n nz : Nat) (hnz : nz < 2048) (IX X0 X1 X2 : Array UInt32) (S : Nat → UInt64) (xw wrow : Nat → UInt32) (lx lw : Nat) (hparts : ∀ t, t < nz → PartsOk X0 X1 X2 t ∧ pv X0 X1 X2 t = wv (xw (IX[t]!).toNat) lx) (hS : ∀ t, t < nz → sv64 (S t) = wv (wrow (IX[t]!).toNat) lw ∧ (sv64 (S t)).natAbs < 2 ^ 31) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 n, if (expOf (xw k)).toNat ≠ 0 then G k else 0) (hzero : ∀ k, k < n → (expOf (xw k)).toNat = 0 → wv (xw k) lx = 0) : sv64 (0 + ∑ s ∈ Finset.Ico 0 nz, sx X0[s]! * S s) + sv64 (0 + ∑ s ∈ Finset.Ico 0 nz, sx X1[s]! * S s) * 2 ^ 21 + sv64 (0 + ∑ s ∈ Finset.Ico 0 nz, sx X2[s]! * S s) * 2 ^ 42 = ∑ k ∈ Finset.range n, wv (xw k) lx * wv (wrow k) lw := by have hb := fun t (ht : t < nz) => (hparts t ht).1 have hs := fun t (ht : t < nz) => (hS t ht).2 rw [lane_int nz hnz X0 S (fun t ht => (hb t ht).1) hs, lane_int nz hnz X1 S (fun t ht => (hb t ht).2.1) hs, lane_int nz hnz X2 S (fun t ht => (hb t ht).2.2) hs] have hpv : ∑ s ∈ Finset.range nz, v32 X0[s]! * sv64 (S s) + (∑ s ∈ Finset.range nz, v32 X1[s]! * sv64 (S s)) * 2 ^ 21 + (∑ s ∈ Finset.range nz, v32 X2[s]! * sv64 (S s)) * 2 ^ 42 = ∑ s ∈ Finset.range nz, pv X0 X1 X2 s * sv64 (S s) := by rw [Finset.sum_mul, Finset.sum_mul, ← Finset.sum_add_distrib, ← Finset.sum_add_distrib] apply Finset.sum_congr rfl; intro s _; unfold pv; ring rw [hpv] have hterm : ∑ s ∈ Finset.range nz, pv X0 X1 X2 s * sv64 (S s) = ∑ t ∈ Finset.Ico 0 nz, (fun k => wv (xw k) lx * wv (wrow k) lw) (IX[t]!).toNat := by rw [← Finset.range_eq_Ico] apply Finset.sum_congr rfl; intro t ht have ht' := Finset.mem_range.mp ht rw [(hparts t ht').2, (hS t ht').1] rw [hterm, hG (fun k => wv (xw k) lx * wv (wrow k) lw), ← Finset.range_eq_Ico] apply Finset.sum_congr rfl; intro k hk have hk' := Finset.mem_range.mp hk split_ifs with he · rfl · push_neg at he; rw [hzero k hk' he]; ring theorem lanes_sum2 (n nz : Nat) (hnz : nz < 2048) (IX X0 X1 X2 : Array UInt32) (S : Nat → UInt64) (xw wrow : Nat → UInt32) (lx lw : Nat) (hparts : ∀ t, t < nz → PartsOk X0 X1 X2 t ∧ pv X0 X1 X2 t = wv (xw (IX[t]!).toNat) lx) (h2z : ∀ t, t < nz → v32 X2[t]! = 0) (hS : ∀ t, t < nz → sv64 (S t) = wv (wrow (IX[t]!).toNat) lw ∧ (sv64 (S t)).natAbs < 2 ^ 31) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 n, if (expOf (xw k)).toNat ≠ 0 then G k else 0) (hzero : ∀ k, k < n → (expOf (xw k)).toNat = 0 → wv (xw k) lx = 0) : sv64 (0 + ∑ s ∈ Finset.Ico 0 nz, sx X0[s]! * S s) + sv64 (0 + ∑ s ∈ Finset.Ico 0 nz, sx X1[s]! * S s) * 2 ^ 21 + sv64 0 * 2 ^ 42 = ∑ k ∈ Finset.range n, wv (xw k) lx * wv (wrow k) lw := by rw [← lanes_sum n nz hnz IX X0 X1 X2 S xw wrow lx lw hparts hS hG hzero] have hb := fun t (ht : t < nz) => (hparts t ht).1 rw [lane_int nz hnz X2 S (fun t ht => (hb t ht).2.2) (fun t ht => (hS t ht).2)] have : ∑ s ∈ Finset.range nz, v32 X2[s]! * sv64 (S s) = 0 := by apply Finset.sum_eq_zero; intro s hs; rw [h2z s (Finset.mem_range.mp hs)]; ring rw [this]; simp [sv64] theorem slowDot_eq : slowDot = dotOf := dotFn_eq_dotOf def specOut (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]!)) theorem slowOut_eq (inW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) : slowOut inW relu X W b i j = specOut inW relu X W b i j := by unfold slowOut specOut; rw [slowDot_eq] def Pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (out : Array Word) (k : Nat) : Prop := out.size = k ∧ ∀ q, q < k → out[q]! = specOut inW relu X W b (q / outW) (q % outW) theorem pref_push (inW outW : Nat) (relu : Bool) (X W b : Array Word) (out : Array Word) (i j : Nat) (hj : j < outW) (h : Pref inW outW relu X W b out (i * outW + j)) (v : Word) (hv : v = specOut inW relu X W b i j) : Pref inW outW relu X W b (out.push v) (i * outW + j + 1) := by obtain ⟨hs, hq⟩ := h refine ⟨by simp [hs], fun q hq' => ?_⟩ rcases Nat.lt_or_ge q (i * outW + j) with hlt | hge · rw [push_get!_lt _ _ _ (by omega)]; exact hq q hlt · have : q = out.size := by omega subst this rw [push_get!_eq, hv, hs] congr 1 · rw [Nat.add_comm, Nat.add_mul_div_right _ _ (by omega), Nat.div_eq_of_lt hj, Nat.zero_add] · rw [Nat.add_comm, Nat.add_mul_mod_self_right, Nat.mod_eq_of_lt hj] theorem slowRow_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i j : Nat) (out : Array Word) (hj : j ≤ outW) (h : Pref inW outW relu X W b out (i * outW + j)) : Pref inW outW relu X W b (slowRow inW outW relu X W b i j out) ((i + 1) * outW) := by induction j, out using slowRow.induct inW outW relu X W b i with | case1 j out hj' ih => unfold slowRow; rw [if_pos hj'] apply ih (by omega) exact pref_push inW outW relu X W b out i j hj' h _ (slowOut_eq _ _ _ _ _ _ _) | case2 j out hj' => unfold slowRow; rw [if_neg hj'] have : j = outW := by omega subst this rw [show (i + 1) * j = i * j + j by ring]; exact h def LaneGood (inW : Nat) (relu : Bool) (X W b : Array Word) (i jj base : Nat) (ok : Bool) (a bb c : UInt64) : Prop := ok = true → ∀ w, fin3 relu b[jj]! base a bb c = some w → w = specOut inW relu X W b i jj theorem pick_eq (inW : Nat) (relu : Bool) (X W b : Array Word) (i jj base : Nat) (ok : Bool) (a bb c : UInt64) (h : LaneGood inW relu X W b i jj base ok a bb c) : pick inW relu X W b i jj (if ok then fin3 relu b[jj]! base a bb c else none) = specOut inW relu X W b i jj := by unfold pick split · rename_i w hw split at hw · exact h ‹_› w hw · simp at hw · exact slowOut_eq _ _ _ _ _ _ _ theorem pushBlock_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i j base : Nat) (r : Acc12) (ok : Nat → Bool) (out : Array Word) (hj : j < outW) (h : Pref inW outW relu X W b out (i * outW + j)) (g0 : LaneGood inW relu X W b i j base (ok j) r.a0 r.b0 r.c0) (g1 : j + 1 < outW → LaneGood inW relu X W b i (j + 1) base (ok (j + 1)) r.a1 r.b1 r.c1) (g2 : j + 2 < outW → LaneGood inW relu X W b i (j + 2) base (ok (j + 2)) r.a2 r.b2 r.c2) (g3 : j + 3 < outW → LaneGood inW relu X W b i (j + 3) base (ok (j + 3)) r.a3 r.b3 r.c3) : Pref inW outW relu X W b (pushBlock inW outW relu X W b i j base r ok out) (i * outW + min (j + 4) outW) := by unfold pushBlock dsimp only have p0 := pref_push inW outW relu X W b out i j hj h _ (pick_eq _ _ _ _ _ _ _ _ _ _ _ _ g0) by_cases h1 : j + 1 < outW · have p1 := pref_push inW outW relu X W b _ i (j + 1) h1 p0 _ (pick_eq _ _ _ _ _ _ _ _ _ _ _ _ (g1 h1)) rw [if_pos h1] by_cases h2 : j + 2 < outW · have p2 := pref_push inW outW relu X W b _ i (j + 2) h2 (by rw [← Nat.add_assoc]; exact p1) _ (pick_eq _ _ _ _ _ _ _ _ _ _ _ _ (g2 h2)) rw [if_pos h2] by_cases h3 : j + 3 < outW · have p3 := pref_push inW outW relu X W b _ i (j + 3) h3 (by rw [← Nat.add_assoc]; exact p2) _ (pick_eq _ _ _ _ _ _ _ _ _ _ _ _ (g3 h3)) rw [if_pos h3, show min (j + 4) outW = j + 4 by omega] rw [show i * outW + (j + 4) = i * outW + (j + 3) + 1 by ring]; exact p3 · rw [if_neg h3, show min (j + 4) outW = j + 3 by omega] rw [show i * outW + (j + 3) = i * outW + (j + 2) + 1 by ring]; exact p2 · rw [if_neg h2, if_neg (by omega), show min (j + 4) outW = j + 2 by omega] rw [show i * outW + (j + 2) = i * outW + (j + 1) + 1 by ring]; exact p1 · rw [if_neg h1, if_neg (by omega), if_neg (by omega), show min (j + 4) outW = j + 1 by omega] exact p0 theorem mem_of_G (nz n : Nat) (IX : Array UInt32) (p : Nat → Prop) [DecidablePred p] (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 n, if p k then G k else 0) (k : Nat) (hk : k < n) (hp : p k) : ∃ t, t < nz ∧ (IX[t]!).toNat = k := by by_contra hno push_neg at hno have h := hG (fun k' => if k' = k then 1 else 0) have hl : ∑ t ∈ Finset.Ico 0 nz, (fun k' => if k' = k then (1 : Int) else 0) (IX[t]!).toNat = 0 := by apply Finset.sum_eq_zero; intro t ht have := hno t (Finset.mem_Ico.mp ht).2 simp [this] have hr : ∑ k' ∈ Finset.Ico 0 n, (if p k' then (fun k'' => if k'' = k then (1 : Int) else 0) k' else 0) = 1 := by rw [Finset.sum_eq_single k] · simp [hp] · intro b _ hb; simp [hb] · intro hk'; exact absurd (Finset.mem_Ico.mpr ⟨Nat.zero_le _, hk⟩) hk' rw [hl, hr] at h; exact absurd h (by norm_num) theorem lane_good_core (inW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (IX X0 X1 X2 : Array UInt32) (nz : Nat) (hnz : nz < 2048) (lx lw lim : Nat) (hlx : 1 ≤ lx) (hlw : 1 ≤ lw) (S : Nat → UInt64) (hS : ∀ t, t < nz → sv64 (S t) = wv W[jj * inW + (IX[t]!).toNat]! lw ∧ (sv64 (S t)).natAbs < 2 ^ 31) (hparts : ∀ t, t < nz → PartsOk X0 X1 X2 t ∧ (lim ≤ 18 → v32 X2[t]! = 0) ∧ pv X0 X1 X2 t = wv X[i * inW + (IX[t]!).toNat]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 inW, if (expOf X[i * inW + k]!).toNat ≠ 0 then G k else 0) (hwfin : ∀ k, k < inW → expOf W[jj * inW + k]! ≠ 255) (hwwin : ∀ k, k < inW → expOf X[i * inW + k]! ≠ 0 → ((expOf W[jj * inW + k]! = 0 ∧ fracOf W[jj * inW + k]! = 0) ∨ lw ≤ (expOf W[jj * inW + k]!).toNat)) (a bb c : UInt64) (ok : Bool) (ha : a = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X0[s]! * S s) (hb : bb = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X1[s]! * S s) (hc : c = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X2[s]! * S s ∨ (lim ≤ 18 ∧ c = 0)) : LaneGood inW relu X W b i jj (lx + lw - 2) ok a bb c := by intro _ w hw have hzero : ∀ k, k < inW → (expOf X[i * inW + k]!).toNat = 0 → wv X[i * inW + k]! lx = 0 := by intro k hk he have hx := hxok k hk unfold XOk at hx have hf := hx.2.1 he have he' : expOf X[i * inW + k]! = 0 := UInt32.toNat_inj.mp (by simpa using he) have hf' : fracOf X[i * inW + k]! = 0 := UInt32.toNat_inj.mp (by simpa using hf) unfold wv; rw [sv_zero _ he' hf']; ring have hlanes : sv64 a + sv64 bb * 2 ^ 21 + sv64 c * 2 ^ 42 = ∑ k ∈ Finset.range inW, wv X[i * inW + k]! lx * wv W[jj * inW + k]! lw := by rcases hc with hc | ⟨h18, hc⟩ · rw [ha, hb, hc] exact lanes_sum inW nz hnz IX X0 X1 X2 S (fun k => X[i * inW + k]!) (fun k => W[jj * inW + k]!) lx lw (fun t ht => ⟨(hparts t ht).1, (hparts t ht).2.2⟩) hS hG hzero · rw [ha, hb, hc] exact lanes_sum2 inW nz hnz IX X0 X1 X2 S (fun k => X[i * inW + k]!) (fun k => W[jj * inW + k]!) lx lw (fun t ht => ⟨(hparts t ht).1, (hparts t ht).2.2⟩) (fun t ht => (hparts t ht).2.1 h18) hS hG hzero unfold specOut rw [out_core inW relu X W b i jj lx lw hlx hlw ?_ hwfin hwwin a bb c hlanes w hw] intro k hk have hx := hxok k hk unfold XOk at hx refine ⟨fun h' => hx.1 (by rw [h']; rfl), ?_⟩ by_cases he : (expOf X[i * inW + k]!).toNat = 0 · left exact ⟨UInt32.toNat_inj.mp (by simpa using he), UInt32.toNat_inj.mp (by simpa using hx.2.1 he)⟩ · right; exact (hx.2.2 he).1 theorem all_nz_of_full (nz n : Nat) (IX : Array UInt32) (p : Nat → Prop) [DecidablePred p] (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 n, if p k then G k else 0) (hfull : nz = n) (k : Nat) (hk : k < n) : p k := by by_contra hp have h := hG (fun _ => 1) simp only [Finset.sum_const, Nat.card_Ico, Nat.sub_zero, nsmul_eq_mul, mul_one] at h have hle : ∑ k' ∈ Finset.Ico 0 n, (if p k' then (1 : Int) else 0) ≤ ((n - 1 : Nat) : Int) := by have hsplit : ∑ k' ∈ Finset.Ico 0 n, (if p k' then (1 : Int) else 0) = ∑ k' ∈ (Finset.Ico 0 n).erase k, (if p k' then (1 : Int) else 0) := by rw [← Finset.add_sum_erase _ _ (Finset.mem_Ico.mpr ⟨Nat.zero_le _, hk⟩), if_neg hp, zero_add] rw [hsplit] calc ∑ k' ∈ (Finset.Ico 0 n).erase k, (if p k' then (1 : Int) else 0) ≤ ∑ _k' ∈ (Finset.Ico 0 n).erase k, (1 : Int) := Finset.sum_le_sum fun _ _ => by split <;> norm_num _ = ((n - 1 : Nat) : Int) := by rw [Finset.sum_const, Finset.card_erase_of_mem (Finset.mem_Ico.mpr ⟨Nat.zero_le _, hk⟩), Nat.card_Ico] simp have : (nz : Int) ≤ ((n - 1 : Nat) : Int) := by rw [← h] at hle; exact_mod_cast hle omega theorem lane_good_fly (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (hjj : jj < outW) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (IX X0 X1 X2 : Array UInt32) (nz : Nat) (hnz : nz < 2048) (hnzle : nz ≤ inW) (lx lw lim : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (o : USize) (hoe : o.toNat = lw * 256) (ho : o.toNat ≤ 65280) (hparts : ∀ t, t < nz → PartsOk X0 X1 X2 t ∧ (lim ≤ 18 → v32 X2[t]! = 0) ∧ pv X0 X1 X2 t = wv X[i * inW + (IX[t]!).toNat]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 inW, if (expOf X[i * inW + k]!).toNat ≠ 0 then G k else 0) (hguard : nz = inW ∨ ∀ k, k < inW → expOf W[jj * inW + k]! ≠ 255) (wr : USize) (hwrd : wr = (jj * inW).toUSize) (a bb c chk : UInt64) (ha : a = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X0[s]! * sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32) (hb : bb = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X1[s]! * sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32) (hc : c = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X2[s]! * sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32 ∨ (lim ≤ 18 ∧ c = 0)) (hchk : chk.toNat < 2 ^ 31 → ∀ s, s < nz → (magW o ho (wAt W IX wr s)).toNat < 2 ^ 31) : LaneGood inW relu X W b i jj (lx + lw - 2) (decide (chk < 0x80000000)) a bb c := by intro hok have hc31 : chk.toNat < 2 ^ 31 := by simpa [UInt64.lt_iff_toNat_lt] using hok have hm := hchk hc31 have hwr : ∀ s, wAt W IX wr s = W[jj * inW + (IX[s]!).toNat]! := by intro s; unfold wAt; rw [hwrd, toUSize_toNat _ (by nlinarith)] have hmk : ∀ k, k < inW → (expOf X[i * inW + k]!).toNat ≠ 0 → (magW o ho W[jj * inW + k]!).toNat < 2 ^ 31 := by intro k hk hne obtain ⟨t, ht, hIt⟩ := mem_of_G nz inW IX _ hG k hk hne have := hm t ht; rw [hwr, hIt] at this; exact this have hwspec := fun k hk hne => magW_spec lw hlw o hoe ho W[jj * inW + k]! (hmk k hk hne) apply lane_good_core inW relu X W b i jj IX X0 X1 X2 nz hnz lx lw lim hlx hlw1 (fun s => sx (sgnW (wAt W IX wr s) (magW o ho (wAt W IX wr s))).toUInt32) _ hparts hxok hG _ _ a bb c true ha hb hc rfl · intro t ht have hb31 := (sgnW_wv lw hlw o hoe ho _ (hm t ht)).2 show sv64 (sx (sgnW (wAt W IX wr t) (magW o ho (wAt W IX wr t))).toUInt32) = _ ∧ _ rw [sx_toUInt32 _ (by omega)] rw [hwr]; exact sgnW_wv lw hlw o hoe ho _ (by rw [← hwr]; exact hm t ht) · intro k hk rcases hguard with hfull | hg · have hne := all_nz_of_full nz inW IX _ hG hfull k hk obtain ⟨_, hm2⟩ := hwspec k hk hne rcases hm2 with hm0 | ⟨_, h255, _⟩ · intro h'; unfold mantOf at hm0; rw [h'] at hm0; simp at hm0 · exact fun h' => h255 (by rw [h']; rfl) · exact hg k hk · intro k hk hne have hne' : (expOf X[i * inW + k]!).toNat ≠ 0 := fun h' => hne (UInt32.toNat_inj.mp (by simpa using h')) obtain ⟨_, hm2⟩ := hwspec k hk hne' rcases hm2 with hm0 | ⟨_, _, hle, _⟩ · left unfold mantOf at hm0 by_cases he : expOf W[jj * inW + k]! = 0 · rw [if_pos he] at hm0 exact ⟨he, UInt32.toNat_inj.mp (by simpa using hm0)⟩ · rw [if_neg he] at hm0; omega · right; exact hle theorem flyBlocks_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (IX X0 X1 X2 : Array UInt32) (nz : USize) (wide : Bool) (o : USize) (ho : o.toNat ≤ 65280) (base : Nat) (hI : nz.toNat ≤ IX.size) (h0 : nz.toNat ≤ X0.size) (h1 : nz.toNat ≤ X1.size) (h2 : nz.toNat ≤ X2.size) (hIX : ∀ s, s < nz.toNat → (IX[s]!).toNat < inW) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (hnz : nz.toNat < 2048) (hnzle : nz.toNat ≤ inW) (lx lw : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hoe : o.toNat = lw * 256) (hbase : base = lx + lw - 2) (lim : Nat) (hwl : wide = false → lim ≤ 18) (hparts : ∀ t, t < nz.toNat → PartsOk X0 X1 X2 t ∧ (lim ≤ 18 → v32 X2[t]! = 0) ∧ pv X0 X1 X2 t = wv X[i * inW + (IX[t]!).toNat]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz.toNat, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 inW, if (expOf X[i * inW + k]!).toNat ≠ 0 then G k else 0) (hguard : nz.toNat = inW ∨ ∀ jj, jj < outW → ∀ k, k < inW → expOf W[jj * inW + k]! ≠ 255) (j : Nat) (out : Array Word) (h : Pref inW outW relu X W b out (i * outW + min j outW)) : Pref inW outW relu X W b (flyBlocks inW outW relu X W b i IX X0 X1 X2 nz wide o ho base hI h0 h1 h2 hIX hWsz hWs j out) ((i + 1) * outW) := by induction j, out using flyBlocks.induct inW outW relu X W b i IX X0 X1 X2 nz wide o ho base hI h0 h1 h2 hIX hWsz hWs with | case1 j out hj w0 w1 w2 w3 hr r ok ih => have heq : flyBlocks inW outW relu X W b i IX X0 X1 X2 nz wide o ho base hI h0 h1 h2 hIX hWsz hWs j out = flyBlocks inW outW relu X W b i IX X0 X1 X2 nz wide o ho base hI h0 h1 h2 hIX hWsz hWs (j + 4) (pushBlock inW outW relu X W b i j base r (fun _ => ok) out) := by conv_lhs => unfold flyBlocks simp only [dite_cond_eq_true (eq_true hj)] rfl rw [heq] apply ih rw [show min j outW = j by omega] at h have hg : ∀ jj, jj < outW → (nz.toNat = inW ∨ ∀ k, k < inW → expOf W[jj * inW + k]! ≠ 255) := by intro jj hjj; rcases hguard with hf | hg · exact Or.inl hf · exact Or.inr (hg jj hjj) have L := fun (jj : Nat) (hjj : jj < outW) (wr : USize) (hwr : wr = (jj * inW).toUSize) (a bb c chk : UInt64) => lane_good_fly inW outW relu X W b i jj hjj hWsz hWs IX X0 X1 X2 nz.toNat hnz hnzle lx lw lim hlx hlw1 hlw o hoe ho hparts hxok hG (hg jj hjj) wr hwr a bb c chk rw [hbase] at * have hw0 : w0 = (j * inW).toUSize := rfl have hw1 : j + 1 < outW → w1 = ((j + 1) * inW).toUSize := fun h' => by show ((if j + 1 < outW then j + 1 else j) * inW).toUSize = _; rw [if_pos h'] have hw2 : j + 2 < outW → w2 = ((j + 2) * inW).toUSize := fun h' => by show ((if j + 2 < outW then j + 2 else j) * inW).toUSize = _; rw [if_pos h'] have hw3 : j + 3 < outW → w3 = ((j + 3) * inW).toUSize := fun h' => by show ((if j + 3 < outW then j + 3 else j) * inW).toUSize = _; rw [if_pos h'] have z : ∀ s : Nat, (0 : USize).toNat ≤ s := fun s => Nat.zero_le _ simp only [ok, r] by_cases hw : wide = true <;> simp only [hw, ite_true, ite_false, dite_true, dite_false, Bool.false_eq_true] · obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, ec⟩ := dot4w_spec IX X0 X1 X2 W o ho w0 w1 w2 w3 nz hI h0 h1 h2 hr hWs 0 0 0 0 0 0 0 0 0 0 0 0 0 0 simp only [USize.toNat_zero] at e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ _ e0 e1 (Or.inl e2) (fun hc s hs => ((ec hc).2 s (z s) hs).1) · intro h1' exact L (j + 1) h1' w1 (hw1 h1') _ _ _ _ e3 e4 (Or.inl e5) (fun hc s hs => ((ec hc).2 s (z s) hs).2.1) · intro h2' exact L (j + 2) h2' w2 (hw2 h2') _ _ _ _ e6 e7 (Or.inl e8) (fun hc s hs => ((ec hc).2 s (z s) hs).2.2.1) · intro h3' exact L (j + 3) h3' w3 (hw3 h3') _ _ _ _ e9 e10 (Or.inl e11) (fun hc s hs => ((ec hc).2 s (z s) hs).2.2.2) · obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, ec⟩ := dot4n_spec IX X0 X1 W o ho w0 w1 w2 w3 nz hI h0 h1 hr hWs 0 0 0 0 0 0 0 0 0 0 simp only [USize.toNat_zero] at e0 e1 e2 e3 e4 e5 e6 e7 have h18 : lim ≤ 18 := hwl (by simpa using hw) apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ _ e0 e1 (Or.inr ⟨h18, rfl⟩) (fun hc s hs => ((ec hc).2 s (z s) hs).1) · intro h1' exact L (j + 1) h1' w1 (hw1 h1') _ _ _ _ e2 e3 (Or.inr ⟨h18, rfl⟩) (fun hc s hs => ((ec hc).2 s (z s) hs).2.1) · intro h2' exact L (j + 2) h2' w2 (hw2 h2') _ _ _ _ e4 e5 (Or.inr ⟨h18, rfl⟩) (fun hc s hs => ((ec hc).2 s (z s) hs).2.2.1) · intro h3' exact L (j + 3) h3' w3 (hw3 h3') _ _ _ _ e6 e7 (Or.inr ⟨h18, rfl⟩) (fun hc s hs => ((ec hc).2 s (z s) hs).2.2.2) | case2 j out hj => unfold flyBlocks; simp only [dite_cond_eq_false (eq_false hj)] rw [show min j outW = outW by omega] at h rw [show (i + 1) * outW = i * outW + outW by ring]; exact h theorem lane_good_pre (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (hjj : jj < outW) (D : Array UInt32) (oks : Array Bool) (wo : USize) (hwo : wo.toNat ≤ 65280) (IX X0 X1 X2 : Array UInt32) (nz : Nat) (hnz : nz < 2048) (hIX : ∀ s, s < nz → (IX[s]!).toNat < inW) (lx lw lim : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hparts : ∀ t, t < nz → PartsOk X0 X1 X2 t ∧ (lim ≤ 18 → v32 X2[t]! = 0) ∧ pv X0 X1 X2 t = wv X[i * inW + (IX[t]!).toNat]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 inW, if (expOf X[i * inW + k]!).toNat ≠ 0 then G k else 0) (hD : ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)).toUInt32) (hok : oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (wr : USize) (hwrd : wr = (jj * inW).toUSize) (hWj : (jj + 1) * inW < 2 ^ 32) (a bb c : UInt64) (ha : a = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X0[s]! * sx (wAt D IX wr s)) (hb : bb = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X1[s]! * sx (wAt D IX wr s)) (hc : c = 0 + ∑ s ∈ Finset.Ico 0 nz, sx X2[s]! * sx (wAt D IX wr s) ∨ (lim ≤ 18 ∧ c = 0)) : LaneGood inW relu X W b i jj (lx + lw - 2) (oks[jj]!) a bb c := by intro hk have hm := hok hk have hspec := fun k hk' => magW_spec lw hlw wo hwoe hwo W[jj * inW + k]! (hm k hk') have hwv := fun k hk' => sgnW_wv lw hlw wo hwoe hwo W[jj * inW + k]! (hm k hk') have hS : ∀ t, t < nz → sx (wAt D IX wr t) = sgnW W[jj * inW + (IX[t]!).toNat]! (magW wo hwo W[jj * inW + (IX[t]!).toNat]!) := by intro t ht have hk' := hIX t ht unfold wAt; rw [hwrd, toUSize_toNat _ (by nlinarith), hD _ hk'] apply sx_toUInt32 obtain ⟨_, h2⟩ := hwv _ hk' omega apply lane_good_core inW relu X W b i jj IX X0 X1 X2 nz hnz lx lw lim hlx hlw1 (fun s => sx (wAt D IX wr s)) _ hparts hxok hG _ _ a bb c true ha hb hc rfl · intro t ht rw [hS t ht]; exact hwv _ (hIX t ht) · intro k hk' obtain ⟨_, hm2⟩ := hspec k hk' rcases hm2 with hm0 | ⟨_, h255, _⟩ · intro h'; unfold mantOf at hm0; rw [h'] at hm0; simp at hm0 · exact fun h' => h255 (by rw [h']; rfl) · intro k hk' _ obtain ⟨_, hm2⟩ := hspec k hk' rcases hm2 with hm0 | ⟨_, _, hle, _⟩ · left unfold mantOf at hm0 by_cases he : expOf W[jj * inW + k]! = 0 · rw [if_pos he] at hm0 exact ⟨he, UInt32.toNat_inj.mp (by simpa using hm0)⟩ · rw [if_neg he] at hm0; omega · right; exact hle theorem preBlocks_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (IX X0 X1 X2 D : Array UInt32) (oks : Array Bool) (nz : USize) (wide : Bool) (base : Nat) (hI : nz.toNat ≤ IX.size) (h0 : nz.toNat ≤ X0.size) (h1 : nz.toNat ≤ X1.size) (h2 : nz.toNat ≤ X2.size) (hIX : ∀ s, s < nz.toNat → (IX[s]!).toNat < inW) (hDsz : outW * inW ≤ D.size) (hDs : D.size < 2 ^ 32) (hnz : nz.toNat < 2048) (wo : USize) (hwo : wo.toNat ≤ 65280) (lx lw : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hbase : base = lx + lw - 2) (lim : Nat) (hwl : wide = false → lim ≤ 18) (hparts : ∀ t, t < nz.toNat → PartsOk X0 X1 X2 t ∧ (lim ≤ 18 → v32 X2[t]! = 0) ∧ pv X0 X1 X2 t = wv X[i * inW + (IX[t]!).toNat]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hG : ∀ G : Nat → Int, ∑ t ∈ Finset.Ico 0 nz.toNat, G (IX[t]!).toNat = ∑ k ∈ Finset.Ico 0 inW, if (expOf X[i * inW + k]!).toNat ≠ 0 then G k else 0) (hD : ∀ jj, jj < outW → ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)).toUInt32) (hok : ∀ jj, jj < outW → oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (j : Nat) (out : Array Word) (h : Pref inW outW relu X W b out (i * outW + min j outW)) : Pref inW outW relu X W b (preBlocks inW outW relu X W b i IX X0 X1 X2 D oks nz wide base hI h0 h1 h2 hIX hDsz hDs j out) ((i + 1) * outW) := by induction j, out using preBlocks.induct inW outW relu X W b i IX X0 X1 X2 D oks nz wide base hI h0 h1 h2 hIX hDsz hDs with | case1 j out hj w0 w1 w2 w3 hr r ih => have heq : preBlocks inW outW relu X W b i IX X0 X1 X2 D oks nz wide base hI h0 h1 h2 hIX hDsz hDs j out = preBlocks inW outW relu X W b i IX X0 X1 X2 D oks nz wide base hI h0 h1 h2 hIX hDsz hDs (j + 4) (pushBlock inW outW relu X W b i j base r (fun jj => oks[jj]!) out) := by conv_lhs => unfold preBlocks simp only [dite_cond_eq_true (eq_true hj)] rfl rw [heq] apply ih rw [show min j outW = j by omega] at h have L := fun (jj : Nat) (hjj : jj < outW) (wr : USize) (hwr : wr = (jj * inW).toUSize) (a bb c : UInt64) => lane_good_pre inW outW relu X W b i jj hjj D oks wo hwo IX X0 X1 X2 nz.toNat hnz hIX lx lw lim hlx hlw1 hlw hwoe hparts hxok hG (hD jj hjj) (hok jj hjj) wr hwr (by have : (jj + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hjj; omega) a bb c rw [hbase] at * have hw0 : w0 = (j * inW).toUSize := rfl have hw1 : j + 1 < outW → w1 = ((j + 1) * inW).toUSize := fun h' => by show ((if j + 1 < outW then j + 1 else j) * inW).toUSize = _; rw [if_pos h'] have hw2 : j + 2 < outW → w2 = ((j + 2) * inW).toUSize := fun h' => by show ((if j + 2 < outW then j + 2 else j) * inW).toUSize = _; rw [if_pos h'] have hw3 : j + 3 < outW → w3 = ((j + 3) * inW).toUSize := fun h' => by show ((if j + 3 < outW then j + 3 else j) * inW).toUSize = _; rw [if_pos h'] simp only [r] by_cases hw : wide = true <;> simp only [hw, ite_true, ite_false, dite_true, dite_false, Bool.false_eq_true] · obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11⟩ := dot4Pw_spec IX X0 X1 X2 D w0 w1 w2 w3 nz hI h0 h1 h2 hr hDs 0 0 0 0 0 0 0 0 0 0 0 0 0 simp only [USize.toNat_zero] at e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ e0 e1 (Or.inl e2) · intro h1'; exact L (j + 1) h1' w1 (hw1 h1') _ _ _ e3 e4 (Or.inl e5) · intro h2'; exact L (j + 2) h2' w2 (hw2 h2') _ _ _ e6 e7 (Or.inl e8) · intro h3'; exact L (j + 3) h3' w3 (hw3 h3') _ _ _ e9 e10 (Or.inl e11) · obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7⟩ := dot4Pn_spec IX X0 X1 D w0 w1 w2 w3 nz hI h0 h1 hr hDs 0 0 0 0 0 0 0 0 0 simp only [USize.toNat_zero] at e0 e1 e2 e3 e4 e5 e6 e7 have h18 : lim ≤ 18 := hwl (by simpa using hw) apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ e0 e1 (Or.inr ⟨h18, rfl⟩) · intro h1'; exact L (j + 1) h1' w1 (hw1 h1') _ _ _ e2 e3 (Or.inr ⟨h18, rfl⟩) · intro h2'; exact L (j + 2) h2' w2 (hw2 h2') _ _ _ e4 e5 (Or.inr ⟨h18, rfl⟩) · intro h3'; exact L (j + 3) h3' w3 (hw3 h3') _ _ _ e6 e7 (Or.inr ⟨h18, rfl⟩) | case2 j out hj => unfold preBlocks; simp only [dite_cond_eq_false (eq_false hj)] rw [show min j outW = outW by omega] at h rw [show (i + 1) * outW = i * outW + outW by ring]; exact h theorem lim_le (wide : Bool) : (if wide then 39 else 18 : UInt32) ≤ 39 := by cases wide <;> decide theorem lim_ok (wide : Bool) : wide = false → (if wide then 39 else 18 : UInt32).toNat ≤ 18 := by intro h; subst h; decide theorem flyRowWith_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hoe : o.toNat = lw * 256) (lx lim : UInt32) (wide : Bool) (hlim : lim ≤ 39) (hwl : wide = false → lim.toNat ≤ 18) (hXsz : (i + 1) * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (out : Array Word) (h : Pref inW outW relu X W b out (i * outW)) : Pref inW outW relu X W b (flyRowWith inW outW relu X W b i o ho lw lx lim wide hXsz hWsz hXs hWs hin out) ((i + 1) * outW) := by unfold flyRowWith dsimp only split · rename_i hcond simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.or_eq_true, beq_iff_eq, bne_iff_ne, ne_eq] at hcond obtain ⟨⟨hok, hlx⟩, hguard⟩ := hcond have hn : inW.toUSize.toNat = inW := toUSize_toNat _ (by omega) have hxo : (i * inW).toUSize.toNat = i * inW := toUSize_toNat _ (by nlinarith) have hz : inW.toUSize.toNat ≤ (Array.replicate inW (0 : UInt32)).size := by simp [hn] have hXb : (i * inW).toUSize.toNat + inW.toUSize.toNat ≤ X.size := by rw [hn, hxo]; nlinarith obtain ⟨-, hcnt, -⟩ := decX_facts X (i * inW).toUSize inW.toUSize lx lim hXb hXs _ hz obtain ⟨_, hsem⟩ := decX_sem X (i * inW).toUSize inW.toUSize lx lim hXb hXs hlim 0 0 (Array.replicate inW 0) (Array.replicate inW 0) (Array.replicate inW 0) (Array.replicate inW 0) (by simp) ⟨hz, hz, hz, hz⟩ true (by simp) obtain ⟨-, hxok, -, hnew, hG⟩ := hsem hok simp only [USize.toNat_zero, hxo] at hxok hnew hG have hcnt' := le_of_le_of_eq hcnt hn have hlx1 : 1 ≤ lx.toNat := hlx apply flyBlocks_pref inW outW relu X W b i _ _ _ _ _ wide o ho _ _ _ _ _ _ hWsz hWs (by omega) hcnt' lx.toNat lw hlx1 hlw1 hlw hoe rfl lim.toNat hwl · intro t ht obtain ⟨p1, p2, p3⟩ := hnew t (Nat.zero_le _) ht exact ⟨p1, fun h18 => p2 h18, p3⟩ · intro k hk; exact hxok k (Nat.zero_le _) (by rw [hn]; exact hk) · intro G; exact (hG G).trans (by rw [hn]) · rcases hguard with hfull | hor · left; omega · right intro jj hjj k hk have hsz : (outW * inW).toUSize.toNat = outW * inW := toUSize_toNat _ (by omega) have hlt : jj * inW + k < outW * inW := by have : (jj + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hjj nlinarith exact (orAll_spec W (outW * inW).toUSize (by rw [hsz]; omega) 0 0 (by simp) hor).2 (jj * inW + k) (by simp) (by rw [hsz]; exact hlt) · simpa using h · exact slowRow_pref inW outW relu X W b i 0 out (Nat.zero_le _) (by simpa using h) theorem flyRow_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hoe : o.toNat = lw * 256) (hXsz : (i + 1) * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (out : Array Word) (h : Pref inW outW relu X W b out (i * outW)) : Pref inW outW relu X W b (flyRow inW outW relu X W b i o ho lw hXsz hWsz hXs hWs hin out) ((i + 1) * outW) := by unfold flyRow exact flyRowWith_pref inW outW relu X W b i o ho lw hlw1 hlw hoe _ _ _ (lim_le _) (lim_ok _) hXsz hWsz hXs hWs hin out h theorem flyRows_pref (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (o : USize) (ho : o.toNat ≤ 65280) (lw : Nat) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hoe : o.toNat = lw * 256) (hXsz : rows * inW ≤ X.size) (hWsz : outW * inW ≤ W.size) (hXs : X.size < 2 ^ 32) (hWs : W.size < 2 ^ 32) (hin : inW < 2048) (i : Nat) (out : Array Word) (hi : i ≤ rows) (h : Pref inW outW relu X W b out (i * outW)) : Pref inW outW relu X W b (flyRows rows inW outW relu X W b o ho lw hXsz hWsz hXs hWs hin i out) (rows * outW) := by induction i, out using flyRows.induct rows inW outW relu X W b o ho lw hXsz hWsz hXs hWs hin with | case1 i out hi' ih => unfold flyRows; simp only [dite_cond_eq_true (eq_true hi')] exact ih (by omega) (flyRow_pref inW outW relu X W b i o ho lw hlw1 hlw hoe _ hWsz hXs hWs hin out h) | case2 i out hi' => unfold flyRows; simp only [dite_cond_eq_false (eq_false hi')] have : i = rows := by omega subst this; exact h theorem preRows_pref (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (D : Array UInt32) (oks : Array Bool) (wo : USize) (hwo : wo.toNat ≤ 65280) (lw : Nat) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hD : ∀ jj, jj < outW → ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)).toUInt32) (hok : ∀ jj, jj < outW → oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (hXsz : rows * inW ≤ X.size) (hDsz : outW * inW ≤ D.size) (hXs : X.size < 2 ^ 32) (hDs : D.size < 2 ^ 32) (hin : inW < 2048) (IX P0 P1 P2 : Array UInt32) (hP : inW ≤ IX.size ∧ inW ≤ P0.size ∧ inW ≤ P1.size ∧ inW ≤ P2.size) (i : Nat) (out : Array Word) (hi : i ≤ rows) (h : Pref inW outW relu X W b out (i * outW)) : Pref inW outW relu X W b (preRows rows inW outW relu X W b D oks lw hXsz hDsz hXs hDs hin IX P0 P1 P2 hP i out) (rows * outW) := by induction IX, P0, P1, P2, hP, i, out using preRows.induct rows inW outW relu X W b D oks lw hXsz hDsz hXs hDs hin with | case1 IX P0 P1 P2 hP i out hi' xo n hn hX lh wide hs r hd hr out' ih => have heq : preRows rows inW outW relu X W b D oks lw hXsz hDsz hXs hDs hin IX P0 P1 P2 hP i out = preRows rows inW outW relu X W b D oks lw hXsz hDsz hXs hDs hin r.1 r.2.1 r.2.2.1 r.2.2.2.1 hr (i + 1) out' := by conv_lhs => unfold preRows simp only [dite_cond_eq_true (eq_true hi')] rfl rw [heq] apply ih (by omega) simp only [out'] split · rename_i hcond simp only [Bool.and_eq_true, decide_eq_true_eq] at hcond obtain ⟨hok', hlx⟩ := hcond have hxo : xo.toNat = i * inW := toUSize_toNat _ (by nlinarith) obtain ⟨_, hsem⟩ := decX_sem X xo n lh.1 (if wide then 39 else 18) hX hXs (lim_le _) 0 0 IX P0 P1 P2 (by simp) hs true (by simp) obtain ⟨-, hxok, -, hnew, hG⟩ := hsem hok' simp only [USize.toNat_zero, hxo] at hxok hnew hG have hcnt' : r.2.2.2.2.1.toNat ≤ inW := by rw [← hn]; exact hd.2.1 have hlx1 : 1 ≤ lh.1.toNat := hlx apply preBlocks_pref inW outW relu X W b i _ _ _ _ D oks _ wide _ _ _ _ _ _ hDsz hDs (by omega) wo hwo lh.1.toNat lw hlx1 hlw1 hlw hwoe rfl (if wide then 39 else 18 : UInt32).toNat (lim_ok wide) · intro t ht obtain ⟨p1, p2, p3⟩ := hnew t (Nat.zero_le _) ht exact ⟨p1, fun h18 => p2 h18, p3⟩ · intro k hk; exact hxok k (Nat.zero_le _) (by rw [hn]; exact hk) · intro G; exact (hG G).trans (by rw [hn]) · exact hD · exact hok · simpa using h · exact slowRow_pref inW outW relu X W b i 0 out (Nat.zero_le _) (by simpa using h) | case2 IX P0 P1 P2 hP i out hi' => unfold preRows; simp only [dite_cond_eq_false (eq_false hi')] have : i = rows := by omega subst this; exact h theorem dense_get (D : Array UInt32) (w t : USize) (n : Nat) (ht : t.toNat < n) (hw : w.toNat + n ≤ D.size) (hDs : D.size < 2 ^ 32) : (D.uget (w + t) (dense_idx D w t n ht hw hDs)).toUInt64 = (D[w.toNat + t.toNat]!).toUInt64 := by rw [uget_eq, usize_add_toNat _ _ (by omega)] theorem dotD4_spec (U0 U1 D : Array UInt32) (w0 w1 w2 w3 n : USize) (h0 : n.toNat ≤ U0.size) (h1 : n.toNat ≤ U1.size) (hW : DenseOk D n.toNat w0 w1 w2 w3) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 a1 b1 a2 b2 a3 b3 : UInt64) : let r := dotD4 U0 U1 D w0 w1 w2 w3 n h0 h1 hW hDs t a0 b0 a1 b1 a2 b2 a3 b3 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w0.toNat + s]!).toUInt64 ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w0.toNat + s]!).toUInt64 ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w1.toNat + s]!).toUInt64 ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w1.toNat + s]!).toUInt64 ∧ r.a2 = a2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w2.toNat + s]!).toUInt64 ∧ r.b2 = b2 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w2.toNat + s]!).toUInt64 ∧ r.a3 = a3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w3.toNat + s]!).toUInt64 ∧ r.b3 = b3 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w3.toNat + s]!).toUInt64 := by induction t, a0, b0, a1, b1, a2, b2, a3, b3 using dotD4.induct U0 U1 D w0 w1 w2 w3 n h0 h1 hW hDs with | case1 t a0 b0 a1 b1 a2 b2 a3 b3 h ht x0 x1 d0 d1 d2 d3 ih => unfold dotD4 simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5, e6, e7⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = (U0[t.toNat]!).toUInt64 := by simp only [x0, uget_eq] have hx1 : x1 = (U1[t.toNat]!).toUInt64 := by simp only [x1, uget_eq] have hd0 : d0 = (D[w0.toNat + t.toNat]!).toUInt64 := dense_get D w0 t _ ht hW.1 hDs have hd1 : d1 = (D[w1.toNat + t.toNat]!).toUInt64 := dense_get D w1 t _ ht hW.2.1 hDs have hd2 : d2 = (D[w2.toNat + t.toNat]!).toUInt64 := dense_get D w2 t _ ht hW.2.2.1 hDs have hd3 : d3 = (D[w3.toNat + t.toNat]!).toUInt64 := dense_get D w3 t _ ht hW.2.2.2 hDs rw [hsucc] at e0 e1 e2 e3 e4 e5 e6 e7 refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd1] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd2] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd2] · rw [e6, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd3] · rw [e7, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd3] | case2 t a0 b0 a1 b1 a2 b2 a3 b3 h => unfold dotD4 simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, and_self] theorem dotD2w_spec (U0 U1 U2 D : Array UInt32) (w0 w1 n : USize) (h0 : n.toNat ≤ U0.size) (h1 : n.toNat ≤ U1.size) (h2 : n.toNat ≤ U2.size) (hW : DenseOk D n.toNat w0 w1 w0 w1) (hDs : D.size < 2 ^ 32) (t : USize) (a0 b0 c0 a1 b1 c1 : UInt64) : let r := dotD2w U0 U1 U2 D w0 w1 n h0 h1 h2 hW hDs t a0 b0 c0 a1 b1 c1 r.a0 = a0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w0.toNat + s]!).toUInt64 ∧ r.b0 = b0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w0.toNat + s]!).toUInt64 ∧ r.c0 = c0 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U2[s]!).toUInt64 * (D[w0.toNat + s]!).toUInt64 ∧ r.a1 = a1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U0[s]!).toUInt64 * (D[w1.toNat + s]!).toUInt64 ∧ r.b1 = b1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U1[s]!).toUInt64 * (D[w1.toNat + s]!).toUInt64 ∧ r.c1 = c1 + ∑ s ∈ Finset.Ico t.toNat n.toNat, (U2[s]!).toUInt64 * (D[w1.toNat + s]!).toUInt64 := by induction t, a0, b0, c0, a1, b1, c1 using dotD2w.induct U0 U1 U2 D w0 w1 n h0 h1 h2 hW hDs with | case1 t a0 b0 c0 a1 b1 c1 h ht x0 x1 x2 d0 d1 ih => unfold dotD2w simp only [dite_cond_eq_true (eq_true h)] obtain ⟨e0, e1, e2, e3, e4, e5⟩ := ih have hsucc := usize_succ_toNat t n h have hx0 : x0 = (U0[t.toNat]!).toUInt64 := by simp only [x0, uget_eq] have hx1 : x1 = (U1[t.toNat]!).toUInt64 := by simp only [x1, uget_eq] have hx2 : x2 = (U2[t.toNat]!).toUInt64 := by simp only [x2, uget_eq] have hd0 : d0 = (D[w0.toNat + t.toNat]!).toUInt64 := dense_get D w0 t _ ht hW.1 hDs have hd1 : d1 = (D[w1.toNat + t.toNat]!).toUInt64 := dense_get D w1 t _ ht hW.2.1 hDs rw [hsucc] at e0 e1 e2 e3 e4 e5 refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ · rw [e0, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd0] · rw [e1, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd0] · rw [e2, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hd0] · rw [e3, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx0, hd1] · rw [e4, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx1, hd1] · rw [e5, Ico_succ _ _ _ ht, ← add_assoc]; simp only [hx2, hd1] | case2 t a0 b0 c0 a1 b1 c1 h => unfold dotD2w simp only [dite_cond_eq_false (eq_false h)] have : ¬ t.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico t.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) simp only [he, Finset.sum_empty, add_zero, and_self] def uv (u : UInt32) : Int := (u.toNat : Int) - 2 ^ 21 def pvD (U0 U1 U2 : Array UInt32) (t : Nat) : Int := uv U0[t]! + uv U1[t]! * 2 ^ 21 + uv U2[t]! * 2 ^ 42 def PartsOkD (U0 U1 U2 : Array UInt32) (t : Nat) : Prop := (uv U0[t]!).natAbs < 2 ^ 21 ∧ (uv U1[t]!).natAbs < 2 ^ 21 ∧ (uv U2[t]!).natAbs < 2 ^ 21 theorem offP_val (neg : Bool) (p : UInt64) (hp : p.toNat < 2 ^ 21) : uv (offP neg p).toUInt32 = if neg then -(p.toNat : Int) else p.toNat := by unfold uv offP have c : (0x200000 : UInt64).toNat = 2 ^ 21 := rfl cases neg · simp only [Bool.false_eq_true, ite_false, UInt64.toNat_toUInt32, UInt64.toNat_add, c] rw [Nat.mod_eq_of_lt (by omega), Nat.mod_eq_of_lt (by omega)]; push_cast; ring · have hle : p ≤ (0x200000 : UInt64) := UInt64.le_iff_toNat_le.mpr (by rw [c]; omega) simp only [ite_true, UInt64.toNat_toUInt32] rw [UInt64.toNat_sub_of_le _ _ hle, c, Nat.mod_eq_of_lt (by omega)] omega theorem mag_hi_zero (w lx lim : UInt32) (he0 : (expOf w).toNat ≠ 0) (hlx : lx ≤ (w >>> 23) &&& 0xFF) (hd : ((w >>> 23) &&& 0xFF) - lx ≤ lim) (h18 : lim.toNat ≤ 18) : ((((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< (((w >>> 23) &&& 0xFF) - lx).toUInt64) >>> 42).toNat = 0 := by have he : (w >>> 23) &&& 0xFF = expOf w := rfl have hdn : ((expOf w) - lx).toNat = (expOf w).toNat - lx.toNat := UInt32.toNat_sub_of_le _ _ (by rw [he] at hlx; exact hlx) have hd18 : (expOf w).toNat - lx.toNat ≤ 18 := by have : ((expOf w) - lx).toNat ≤ lim.toNat := by rw [he] at hd; exact hd omega have hml := mantOf_lt_24 w have hmant : mantOf w = 2 ^ 23 + (fracOf w).toNat := by have : expOf w ≠ 0 := fun h' => he0 (by simp [h']) simp [mantOf, this] have hfr : (fracOf w).toNat = (w &&& 0x7FFFFF).toNat := rfl have hf := fracOf_lt w have hm23 : ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64.toNat = mantOf w := by rw [UInt32.toNat_toUInt64, UInt32.toNat_or, hmant, hfr] rw [or_disj _ _ 23 hf (by norm_num)]; norm_num; omega rw [UInt64.toNat_shiftRight, UInt64.toNat_shiftLeft, hm23, he] have : ((expOf w - lx).toUInt64.toNat % 64) = (expOf w).toNat - lx.toNat := by simp only [UInt32.toNat_toUInt64, hdn]; omega rw [this, Nat.shiftLeft_eq, show (42 : UInt64).toNat % 64 = 42 from rfl, Nat.shiftRight_eq_div_pow] have hlt : mantOf w * 2 ^ ((expOf w).toNat - lx.toNat) < 2 ^ 42 := by calc mantOf w * 2 ^ ((expOf w).toNat - lx.toNat) < 2 ^ 24 * 2 ^ 18 := Nat.mul_lt_mul_of_lt_of_le hml (Nat.pow_le_pow_right (by norm_num) hd18) (by positivity) _ = 2 ^ 42 := by norm_num rw [Nat.mod_eq_of_lt (by omega)] exact Nat.div_eq_of_lt hlt theorem decXD_sem (X : Array UInt32) (o n : USize) (lx lim : UInt32) (hX : o.toNat + n.toNat ≤ X.size) (hXs : X.size < 2 ^ 32) (hlim : lim ≤ 39) (k : USize) (U0 U1 U2 : Array UInt32) (hs : n.toNat ≤ U0.size ∧ n.toNat ≤ U1.size ∧ n.toNat ≤ U2.size) (s0 s1 s2 : UInt64) (ok : Bool) (hkn : k.toNat ≤ n.toNat) : let r := decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok (∀ t, t < k.toNat → r.1[t]! = U0[t]! ∧ r.2.1[t]! = U1[t]! ∧ r.2.2.1[t]! = U2[t]!) ∧ r.2.2.2.1 = s0 + ∑ t ∈ Finset.Ico k.toNat n.toNat, (r.1[t]!).toUInt64 ∧ r.2.2.2.2.1 = s1 + ∑ t ∈ Finset.Ico k.toNat n.toNat, (r.2.1[t]!).toUInt64 ∧ r.2.2.2.2.2.1 = s2 + ∑ t ∈ Finset.Ico k.toNat n.toNat, (r.2.2.1[t]!).toUInt64 ∧ (r.2.2.2.2.2.2 = true → ok = true ∧ ∀ k', k.toNat ≤ k' → k' < n.toNat → XOk X o.toNat lx.toNat lim.toNat k' ∧ PartsOkD r.1 r.2.1 r.2.2.1 k' ∧ (lim.toNat ≤ 18 → uv r.2.2.1[k']! = 0) ∧ pvD r.1 r.2.1 r.2.2.1 k' = wv X[o.toNat + k']! lx.toNat) := by induction k, U0, U1, U2, hs, s0, s1, s2, ok using decXD.induct X o n lx lim hX hXs with | case1 k U0 U1 U2 hs s0 s1 s2 ok h hk w e d mag neg q0 q1 q2 ih => have hw : w = X[o.toNat + k.toNat]! := by simp only [w]; rw [uget_eq, usize_add_toNat _ _ (by omega)] have heq : decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok = decXD X o n lx lim hX hXs (k + 1) (U0.uset k q0 (by omega)) (U1.uset k q1 (by omega)) (U2.uset k q2 (by omega)) (by simp only [Array.size_uset]; exact hs) (s0 + q0.toUInt64) (s1 + q1.toUInt64) (s2 + q2.toUInt64) (ok && if _h : (e == 0) = true then (w &&& 0x7FFFFF) == 0 else e != 255 && decide (lx ≤ e) && decide (d ≤ lim)) := by conv_lhs => unfold decXD simp only [dite_cond_eq_true (eq_true h)] rfl rw [heq] have hk1 := usize_succ_toNat k n h obtain ⟨kept, sum0, sum1, sum2, sem⟩ := ih (by omega) rw [hk1] at kept sum0 sum1 sum2 sem have hat : ∀ (A : Array UInt32) (q : UInt32) (hA : k.toNat < A.size) (R : Array UInt32), (∀ t, t < k.toNat + 1 → R[t]! = (A.uset k q hA)[t]!) → R[k.toNat]! = q := by intro A q hA R hR; rw [hR _ (by omega), uset_get!, if_pos rfl] have r0 := hat U0 q0 (by omega) _ (fun t ht => (kept t ht).1) have r1 := hat U1 q1 (by omega) _ (fun t ht => (kept t ht).2.1) have r2 := hat U2 q2 (by omega) _ (fun t ht => (kept t ht).2.2) refine ⟨?_, ?_, ?_, ?_, ?_⟩ · intro t ht obtain ⟨o1, o2, o3⟩ := kept t (by omega) rw [o1, o2, o3, uset_get!, uset_get!, uset_get!, if_neg (by omega), if_neg (by omega), if_neg (by omega)] exact ⟨rfl, rfl, rfl⟩ · rw [sum0, Ico_succ _ _ _ hk]; simp only [r0, add_assoc] · rw [sum1, Ico_succ _ _ _ hk]; simp only [r1, add_assoc] · rw [sum2, Ico_succ _ _ _ hk]; simp only [r2, add_assoc] · intro hfin obtain ⟨hok', hrest⟩ := sem hfin simp only [Bool.and_eq_true] at hok' obtain ⟨hok, hcond⟩ := hok' refine ⟨hok, fun k' hk1' hk2' => ?_⟩ rcases Nat.lt_or_ge k' (k.toNat + 1) with hlt | hge · have hk' : k' = k.toNat := by omega subst hk' unfold XOk PartsOkD pvD rw [r0, r1, r2, ← hw] by_cases he0 : e = 0 · have he' : expOf w = 0 := he0 have hcz : (w &&& 0x7FFFFF) = 0 := by have := hcond; simp only [he0, beq_self_eq_true, dite_true, beq_iff_eq] at this; exact this have hmag : mag = 0 := by simp only [mag, he0, beq_self_eq_true, dite_true] have z0 : uv q0 = 0 := by simp only [q0, hmag, UInt64.zero_and]; rw [offP_val _ _ (by decide)]; split <;> simp have z1 : uv q1 = 0 := by simp only [q1, hmag, UInt64.zero_shiftRight, UInt64.zero_and]; rw [offP_val _ _ (by decide)]; split <;> simp have z2 : uv q2 = 0 := by simp only [q2, hmag, UInt64.zero_shiftRight]; rw [offP_val _ _ (by decide)]; split <;> simp have hfz : fracOf w = 0 := hcz refine ⟨⟨by rw [he']; decide, fun _ => by rw [hfz]; rfl, fun h' => absurd (by rw [he']; rfl) h'⟩, ⟨by rw [z0]; decide, by rw [z1]; decide, by rw [z2]; decide⟩, fun _ => z2, ?_⟩ rw [z0, z1, z2]; unfold wv; rw [sv_zero w he' hfz]; ring · have hne : (e == 0) = false := by simpa using he0 have hmag : mag = ((w &&& 0x7FFFFF) ||| 0x800000).toUInt64 <<< d.toUInt64 := by simp only [mag, hne, Bool.false_eq_true, dite_false] have hc' : (e != 255 && decide (lx ≤ e) && decide (d ≤ lim)) = true := by have := hcond; simp only [hne, Bool.false_eq_true, dite_false] at this; exact this simp only [Bool.and_eq_true, bne_iff_ne, ne_eq, decide_eq_true_eq] at hc' obtain ⟨⟨h255, hlx⟩, hdl⟩ := hc' have he0' : (expOf w).toNat ≠ 0 := by intro h0; apply he0 have : expOf w = 0 := UInt32.toNat_inj.mp (by simpa using h0) exact this obtain ⟨p0lt, p1lt, p2lt, hval⟩ := parts_wv w lx lim hlim he0' hlx hdl have p0lt' : (mag &&& 0x1FFFFF).toNat < 2 ^ 21 := by rw [hmag]; exact p0lt have p1lt' : ((mag >>> 21) &&& 0x1FFFFF).toNat < 2 ^ 21 := by rw [hmag]; exact p1lt have p2lt' : (mag >>> 42).toNat < 2 ^ 21 := by rw [hmag]; exact p2lt have v0 : uv q0 = if signOf w then -((mag &&& 0x1FFFFF).toNat : Int) else (mag &&& 0x1FFFFF).toNat := by simp only [q0]; rw [offP_val _ _ p0lt'] have v1 : uv q1 = if signOf w then -(((mag >>> 21) &&& 0x1FFFFF).toNat : Int) else ((mag >>> 21) &&& 0x1FFFFF).toNat := by simp only [q1]; rw [offP_val _ _ p1lt'] have v2 : uv q2 = if signOf w then -((mag >>> 42).toNat : Int) else (mag >>> 42).toNat := by simp only [q2]; rw [offP_val _ _ p2lt'] have hlx' : lx.toNat ≤ (expOf w).toNat := hlx have hdl' : ((expOf w) - lx).toNat ≤ lim.toNat := hdl have hdn : ((expOf w) - lx).toNat = (expOf w).toNat - lx.toNat := UInt32.toNat_sub_of_le _ _ hlx refine ⟨⟨fun h' => h255 (by have : expOf w = 255 := UInt32.toNat_inj.mp (by simpa using h'); exact this), fun h' => absurd h' he0', fun _ => ⟨hlx', by omega⟩⟩, ⟨?_, ?_, ?_⟩, ?_, ?_⟩ · rw [v0]; split <;> simp only [Int.natAbs_neg, Int.natAbs_natCast] <;> omega · rw [v1]; split <;> simp only [Int.natAbs_neg, Int.natAbs_natCast] <;> omega · rw [v2]; split <;> simp only [Int.natAbs_neg, Int.natAbs_natCast] <;> omega · intro h18 have hz : (mag >>> 42).toNat = 0 := by rw [hmag]; exact mag_hi_zero w lx lim he0' hlx hdl h18 rw [v2, hz]; split <;> simp · rw [v0, v1, v2, hmag]; exact hval · exact hrest k' (by omega) hk2' | case2 k U0 U1 U2 hs s0 s1 s2 ok h => have heq : decXD X o n lx lim hX hXs k U0 U1 U2 hs s0 s1 s2 ok = (U0, U1, U2, s0, s1, s2, ok) := by unfold decXD; simp only [dite_cond_eq_false (eq_false h)] rw [heq] have hkn' : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico k.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) refine ⟨fun t _ => ⟨rfl, rfl, rfl⟩, by simp [he], by simp [he], by simp [he], fun hfin => ⟨hfin, ?_⟩⟩ intro k' h1 h2; omega theorem decWRowD_sem (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (o n : USize) (hW : o.toNat + n.toNat ≤ W.size) (hWs : W.size < 2 ^ 32) (k : USize) (D : Array UInt32) (hD : W.size ≤ D.size) (c sd : UInt64) (hkn : k.toNat ≤ n.toNat) : let r := decWRowD W wo hwo o n hW hWs k D hD c sd (∀ i, ¬(o.toNat + k.toNat ≤ i ∧ i < o.toNat + n.toNat) → r.1[i]! = D[i]!) ∧ (∀ i, o.toNat + k.toNat ≤ i → i < o.toNat + n.toNat → r.1[i]! = (sgnW W[i]! (magW wo hwo W[i]!) + 0x80000000).toUInt32) ∧ r.2.2 = sd + ∑ t ∈ Finset.Ico k.toNat n.toNat, (r.1[o.toNat + t]!).toUInt64 ∧ (r.2.1.toNat < 2 ^ 31 → c.toNat < 2 ^ 31 ∧ ∀ i, o.toNat + k.toNat ≤ i → i < o.toNat + n.toNat → (magW wo hwo W[i]!).toNat < 2 ^ 31) := by induction k, D, hD, c, sd using decWRowD.induct W wo hwo o n hW hWs with | case1 k D hD c sd h hk hok v m u ih => have hv : v = W[o.toNat + k.toNat]! := by simp only [v]; rw [uget_eq, hok] have heq : decWRowD W wo hwo o n hW hWs k D hD c sd = decWRowD W wo hwo o n hW hWs (k + 1) (D.uset (o + k) u (by omega)) (by simp only [Array.size_uset]; exact hD) (c ||| m) (sd + u.toUInt64) := by conv_lhs => unfold decWRowD simp only [dite_cond_eq_true (eq_true h)] rfl rw [heq] have hk1 := usize_succ_toNat k n h obtain ⟨i1, i2, i3, i4⟩ := ih (by omega) rw [hk1] at i1 i2 i3 i4 have hat : (decWRowD W wo hwo o n hW hWs (k + 1) (D.uset (o + k) u (by omega)) (by simp only [Array.size_uset]; exact hD) (c ||| m) (sd + u.toUInt64)).1[o.toNat + k.toNat]! = u := by rw [i1 _ (by omega), uset_get!, if_pos hok.symm] refine ⟨?_, ?_, ?_, ?_⟩ · intro i hi rw [i1 i (by omega), uset_get!, if_neg (by omega)] · intro i h1 h2 rcases Nat.lt_or_ge i (o.toNat + k.toNat + 1) with hlt | hge · have : i = o.toNat + k.toNat := by omega subst this rw [hat]; simp only [u, m]; rw [hv] · exact i2 i (by omega) h2 · rw [i3, Ico_succ _ _ _ hk]; simp only [hat, add_assoc] · intro hc obtain ⟨hc1, hc2⟩ := i4 hc rw [or_lt] at hc1 refine ⟨hc1.1, fun i h1 h2 => ?_⟩ rcases Nat.lt_or_ge i (o.toNat + k.toNat + 1) with hlt | hge · have : i = o.toNat + k.toNat := by omega subst this; rw [← hv]; exact hc1.2 · exact hc2 i (by omega) h2 | case2 k D hD c sd h => unfold decWRowD; simp only [dite_cond_eq_false (eq_false h)] have : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') have he : Finset.Ico k.toNat n.toNat = ∅ := Finset.Ico_eq_empty (by omega) exact ⟨fun _ _ => by simp, fun i h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), by simp [he], fun hc => ⟨hc, fun i h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩⟩ theorem preDecodeD_sem (W : Array UInt32) (wo : USize) (hwo : wo.toNat ≤ 65280) (inW outW : Nat) (hWsz : outW * inW ≤ W.size) (hWs : W.size < 2 ^ 32) (j : Nat) (D : Array UInt32) (hD : W.size ≤ D.size) (oks : Array Bool) (sds : Array UInt64) : let r := preDecodeD W wo hwo inW outW hWsz hWs j D hD oks sds (∀ i, i < j * inW → r.1[i]! = D[i]!) ∧ (∀ j', j ≤ j' → j' < outW → ∀ k, k < inW → r.1[j' * inW + k]! = (sgnW W[j' * inW + k]! (magW wo hwo W[j' * inW + k]!) + 0x80000000).toUInt32) ∧ (∀ j', j' < oks.size → r.2.1[j']! = oks[j']!) ∧ (∀ j', j ≤ j' → j' < outW → r.2.1[oks.size + (j' - j)]! = true → ∀ k, k < inW → (magW wo hwo W[j' * inW + k]!).toNat < 2 ^ 31) ∧ (∀ j', j' < sds.size → r.2.2[j']! = sds[j']!) ∧ (∀ j', j ≤ j' → j' < outW → r.2.2[sds.size + (j' - j)]! = 0 + ∑ t ∈ Finset.Ico 0 inW, (r.1[j' * inW + t]!).toUInt64) := by induction j, D, hD, oks, sds using preDecodeD.induct W wo hwo inW outW hWsz hWs with | case1 j D hD oks sds hj h1 r ih => have heq : preDecodeD W wo hwo inW outW hWsz hWs j D hD oks sds = preDecodeD W wo hwo inW outW hWsz hWs (j + 1) r.1 (by rw [decWRowD_size]; exact hD) (oks.push (r.2.1 < 0x80000000)) (sds.push r.2.2) := by conv_lhs => unfold preDecodeD simp only [dite_cond_eq_true (eq_true hj)] rfl rw [heq] obtain ⟨p1, p2, p3, p4, p5, p6⟩ := ih have hji : (j * inW).toUSize.toNat = j * inW := toUSize_toNat _ (by nlinarith) have hni : inW.toUSize.toNat = inW := toUSize_toNat _ (by nlinarith) obtain ⟨d1, d2, d3, d4⟩ := decWRowD_sem W wo hwo (j * inW).toUSize inW.toUSize h1 hWs 0 D hD 0 0 (by simp) simp only [USize.toNat_zero, Nat.add_zero, hji, hni] at d1 d2 d3 d4 refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ · intro i hi rw [p1 i (by nlinarith), d1 i (by omega)] · intro j' hj1 hj2 k hk rcases Nat.lt_or_ge j' (j + 1) with hlt | hge · have : j' = j := by omega subst this rw [p1 _ (by nlinarith), d2 _ (by omega) (by nlinarith)] · exact p2 j' hge hj2 k hk · intro j' hj' rw [p3 j' (by simp; omega), push_get!_lt _ _ _ hj'] · intro j' hj1 hj2 hok k hk rcases Nat.lt_or_ge j' (j + 1) with hlt | hge · have hjj : j' = j := by omega have h3 := p3 oks.size (by simp) rw [push_get!_eq] at h3 have hidx : oks.size + (j' - j) = oks.size := by omega rw [hidx, h3] at hok have hc' : r.2.1.toNat < 2 ^ 31 := by simpa [UInt64.lt_iff_toNat_lt] using hok rw [hjj] exact (d4 hc').2 _ (by omega) (by nlinarith) · have := p4 j' hge hj2 (by rw [show (oks.push (r.2.1 < 0x80000000)).size + (j' - (j + 1)) = oks.size + (j' - j) by simp; omega] exact hok) exact this k hk · intro j' hj' rw [p5 j' (by simp; omega), push_get!_lt _ _ _ hj'] · intro j' hj1 hj2 rcases Nat.lt_or_ge j' (j + 1) with hlt | hge · have hjj : j' = j := by omega have h5 := p5 sds.size (by simp) rw [push_get!_eq] at h5 have hsum : r.2.2 = 0 + ∑ t ∈ Finset.Ico 0 inW, ((preDecodeD W wo hwo inW outW hWsz hWs (j + 1) r.1 (by rw [decWRowD_size]; exact hD) (oks.push (r.2.1 < 0x80000000)) (sds.push r.2.2)).1[j * inW + t]!).toUInt64 := by conv_lhs => rw [d3] congr 1 apply Finset.sum_congr rfl intro t ht rw [p1 _ (by have := (Finset.mem_Ico.mp ht).2; nlinarith)] rw [show sds.size + (j' - j) = sds.size by omega, h5, hjj] exact hsum · rw [show sds.size + (j' - j) = (sds.push r.2.2).size + (j' - (j + 1)) by simp; omega] exact p6 j' hge hj2 | case2 j D hD oks sds hj => unfold preDecodeD; simp only [dite_cond_eq_false (eq_false hj)] exact ⟨fun _ _ => by simp, fun j' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), fun _ _ => by simp, fun j' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega), fun _ _ => by simp, fun j' h1 h2 => absurd (lt_of_le_of_lt h1 h2) (by omega)⟩ theorem bv_ofInt_sub (x y : Int) : BitVec.ofInt 64 (x - y) = BitVec.ofInt 64 x - BitVec.ofInt 64 y := by rw [sub_eq_add_neg, BitVec.ofInt_add, BitVec.ofInt_neg, ← sub_eq_add_neg] theorem bv_of_toNat (x : UInt64) (z : Int) (h : (x.toNat : Int) = z) : x.toBitVec = BitVec.ofInt 64 z := by rw [← h, BitVec.ofInt_natCast] apply BitVec.eq_of_toNat_eq simp theorem fixD_val (N : Nat) (hN : N < 2048) (A B : Nat → UInt64) (v d : Nat → Int) (hA : ∀ t, t < N → ((A t).toNat : Int) = 2 ^ 21 + v t) (hB : ∀ t, t < N → ((B t).toNat : Int) = 2 ^ 31 + d t) (hv : ∀ t, t < N → (v t).natAbs < 2 ^ 21) (hd : ∀ t, t < N → (d t).natAbs < 2 ^ 31) : sv64 (fixD N.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 N, A s * B s) (0 + ∑ s ∈ Finset.Ico 0 N, A s) (0 + ∑ s ∈ Finset.Ico 0 N, B s)) = ∑ t ∈ Finset.range N, v t * d t := by have eA : ∀ t ∈ Finset.range N, (A t).toBitVec = BitVec.ofInt 64 (2 ^ 21 + v t) := fun t ht => bv_of_toNat _ _ (hA t (Finset.mem_range.mp ht)) have eB : ∀ t ∈ Finset.range N, (B t).toBitVec = BitVec.ofInt 64 (2 ^ 31 + d t) := fun t ht => bv_of_toNat _ _ (hB t (Finset.mem_range.mp ht)) have eN : (N.toUInt64).toBitVec = BitVec.ofInt 64 (N : Int) := bv_of_toNat _ _ (by simp; omega) have c31 : (2147483648 : UInt64).toBitVec = BitVec.ofInt 64 (2 ^ 31) := bv_of_toNat _ _ (by norm_num) have c21 : (2097152 : UInt64).toBitVec = BitVec.ofInt 64 (2 ^ 21) := bv_of_toNat _ _ (by norm_num) have c52 : (4503599627370496 : UInt64).toBitVec = BitVec.ofInt 64 (2 ^ 52) := bv_of_toNat _ _ (by norm_num) have hSAB : (∑ s ∈ Finset.range N, A s * B s).toBitVec = BitVec.ofInt 64 (∑ t ∈ Finset.range N, (2 ^ 21 + v t) * (2 ^ 31 + d t)) := by rw [bv_sum, ofInt_sum] apply Finset.sum_congr rfl intro t ht rw [UInt64.toBitVec_mul, eA t ht, eB t ht, BitVec.ofInt_mul] have hSA : (∑ s ∈ Finset.range N, A s).toBitVec = BitVec.ofInt 64 (∑ t ∈ Finset.range N, (2 ^ 21 + v t)) := by rw [bv_sum, ofInt_sum]; exact Finset.sum_congr rfl eA have hSB : (∑ s ∈ Finset.range N, B s).toBitVec = BitVec.ofInt 64 (∑ t ∈ Finset.range N, (2 ^ 31 + d t)) := by rw [bv_sum, ofInt_sum]; exact Finset.sum_congr rfl eB have key : ∑ t ∈ Finset.range N, (2 ^ 21 + v t) * (2 ^ 31 + d t) - (∑ t ∈ Finset.range N, (2 ^ 21 + v t)) * 2 ^ 31 - (∑ t ∈ Finset.range N, (2 ^ 31 + d t)) * 2 ^ 21 + (N : Int) * 2 ^ 52 = ∑ t ∈ Finset.range N, v t * d t := by have e1 : ∑ t ∈ Finset.range N, (2 ^ 21 + v t) * (2 ^ 31 + d t) = ∑ t ∈ Finset.range N, (v t * d t + (2 ^ 21 + v t) * 2 ^ 31 + (2 ^ 31 + d t) * 2 ^ 21 - 2 ^ 52) := Finset.sum_congr rfl (fun t _ => by ring) rw [e1, Finset.sum_sub_distrib, Finset.sum_add_distrib, Finset.sum_add_distrib, ← Finset.sum_mul, ← Finset.sum_mul, Finset.sum_const, Finset.card_range] simp only [nsmul_eq_mul]; ring have hbv : (fixD N.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 N, A s * B s) (0 + ∑ s ∈ Finset.Ico 0 N, A s) (0 + ∑ s ∈ Finset.Ico 0 N, B s)).toBitVec = BitVec.ofInt 64 (∑ t ∈ Finset.range N, v t * d t) := by unfold fixD rw [← Finset.range_eq_Ico, zero_add, zero_add, zero_add, UInt64.toBitVec_add, UInt64.toBitVec_sub, UInt64.toBitVec_sub, UInt64.toBitVec_mul, UInt64.toBitVec_mul, UInt64.toBitVec_mul, hSAB, hSA, hSB, eN, c31, c21, c52, ← BitVec.ofInt_mul, ← BitVec.ofInt_mul, ← BitVec.ofInt_mul, ← bv_ofInt_sub, ← bv_ofInt_sub, ← BitVec.ofInt_add, key] have hb : (∑ t ∈ Finset.range N, v t * d t).natAbs < 2 ^ 63 := by calc (∑ t ∈ Finset.range N, v t * d t).natAbs ≤ ∑ t ∈ Finset.range N, (v t * d t).natAbs := natAbs_sum_le _ _ _ ≤ ∑ _t ∈ Finset.range N, (2 ^ 52 : Nat) := by apply Finset.sum_le_sum intro t ht have h1 := hv t (Finset.mem_range.mp ht) have h2 := hd t (Finset.mem_range.mp ht) rw [Int.natAbs_mul] calc (v t).natAbs * (d t).natAbs ≤ 2 ^ 21 * 2 ^ 31 := Nat.mul_le_mul (by omega) (by omega) _ = 2 ^ 52 := by norm_num _ = N * 2 ^ 52 := by simp _ < 2 ^ 63 := by calc N * 2 ^ 52 < 2048 * 2 ^ 52 := Nat.mul_lt_mul_of_pos_right hN (by positivity) _ = 2 ^ 63 := by norm_num unfold sv64 rw [hbv, BitVec.toInt_ofInt] apply Int.bmod_eq_of_le <;> omega theorem dval (w : UInt32) (m : UInt64) (hm : m.toNat < 2 ^ 31) : (((sgnW w m + 0x80000000).toUInt32).toNat : Int) = 2 ^ 31 + sv64 (sgnW w m) := by rw [sgnW_sv w m hm] unfold sgnW have c : (0x80000000 : UInt64).toNat = 2147483648 := rfl have hm' : m.toNat < 2147483648 := by omega have h64 : (2 : Nat) ^ 64 = 18446744073709551616 := by norm_num have h32 : (2 : Nat) ^ 32 = 4294967296 := by norm_num by_cases hs : signOf w = true · rw [if_pos hs, if_pos hs] have e1 : (0 - m + 0x80000000 : UInt64).toNat = 2147483648 - m.toNat := by rw [UInt64.toNat_add, UInt64.toNat_sub, c, UInt64.toNat_zero, h64]; omega rw [UInt64.toNat_toUInt32, e1, h32, Nat.mod_eq_of_lt (by omega)] omega · rw [if_neg hs, if_neg hs] have e1 : (m + 0x80000000 : UInt64).toNat = m.toNat + 2147483648 := by rw [UInt64.toNat_add, c, h64]; omega rw [UInt64.toNat_toUInt32, e1, h32, Nat.mod_eq_of_lt (by omega)] push_cast; ring theorem lane_good_dense (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i jj : Nat) (hjj : jj < outW) (hin : inW < 2048) (D : Array UInt32) (oks : Array Bool) (sds : Array UInt64) (wo : USize) (hwo : wo.toNat ≤ 65280) (U0 U1 U2 : Array UInt32) (s0 s1 s2 : UInt64) (lx lw lim : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hparts : ∀ k, k < inW → PartsOkD U0 U1 U2 k ∧ (lim ≤ 18 → uv U2[k]! = 0) ∧ pvD U0 U1 U2 k = wv X[i * inW + k]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hs0 : s0 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U0[t]!).toUInt64) (hs1 : s1 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U1[t]!).toUInt64) (hs2 : s2 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U2[t]!).toUInt64) (hD : ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!) + 0x80000000).toUInt32) (hok : oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (hsd : sds[jj]! = 0 + ∑ t ∈ Finset.Ico 0 inW, (D[jj * inW + t]!).toUInt64) (wr : USize) (hwrd : wr = (jj * inW).toUSize) (hWj : (jj + 1) * inW < 2 ^ 32) (a bb c : UInt64) (ha : a = fixD inW.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 inW, (U0[s]!).toUInt64 * (D[wr.toNat + s]!).toUInt64) s0 sds[jj]!) (hb : bb = fixD inW.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 inW, (U1[s]!).toUInt64 * (D[wr.toNat + s]!).toUInt64) s1 sds[jj]!) (hc : c = fixD inW.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 inW, (U2[s]!).toUInt64 * (D[wr.toNat + s]!).toUInt64) s2 sds[jj]! ∨ (lim ≤ 18 ∧ c = 0)) : LaneGood inW relu X W b i jj (lx + lw - 2) (oks[jj]!) a bb c := by intro hk w hw have hm := hok hk have hspec := fun k hk' => magW_spec lw hlw wo hwoe hwo W[jj * inW + k]! (hm k hk') have hwv := fun k hk' => sgnW_wv lw hlw wo hwoe hwo W[jj * inW + k]! (hm k hk') have hwr : wr.toNat = jj * inW := by rw [hwrd, toUSize_toNat _ (by nlinarith)] have hB : ∀ k, k < inW → (((D[wr.toNat + k]!).toUInt64).toNat : Int) = 2 ^ 31 + sv64 (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)) := by intro k hk' rw [hwr, hD k hk', UInt32.toNat_toUInt64] exact dval _ _ (hm k hk') have hlane : ∀ (U : Array UInt32) (su : UInt64), su = 0 + ∑ t ∈ Finset.Ico 0 inW, (U[t]!).toUInt64 → (∀ k, k < inW → (uv U[k]!).natAbs < 2 ^ 21) → sv64 (fixD inW.toUInt64 (0 + ∑ s ∈ Finset.Ico 0 inW, (U[s]!).toUInt64 * (D[wr.toNat + s]!).toUInt64) su sds[jj]!) = ∑ k ∈ Finset.range inW, uv U[k]! * sv64 (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)) := by intro U su hsu hU conv_lhs => rw [hsu, hsd, ← hwr] exact fixD_val inW hin (fun s => (U[s]!).toUInt64) (fun s => (D[wr.toNat + s]!).toUInt64) (fun s => uv U[s]!) (fun k => sv64 (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!))) (fun t _ => by simp only [uv, UInt32.toNat_toUInt64]; ring) hB hU (fun k hk' => (hwv k hk').2) have hlanes : sv64 a + sv64 bb * 2 ^ 21 + sv64 c * 2 ^ 42 = ∑ k ∈ Finset.range inW, wv X[i * inW + k]! lx * wv W[jj * inW + k]! lw := by have e0 := hlane U0 s0 hs0 (fun k hk' => (hparts k hk').1.1) have e1 := hlane U1 s1 hs1 (fun k hk' => (hparts k hk').1.2.1) have hc' : sv64 c = ∑ k ∈ Finset.range inW, uv U2[k]! * sv64 (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!)) := by rcases hc with hc | ⟨h18, hc⟩ · rw [hc]; exact hlane U2 s2 hs2 (fun k hk' => (hparts k hk').1.2.2) · rw [hc] have : sv64 0 = 0 := by simp [sv64] rw [this]; symm apply Finset.sum_eq_zero; intro k hk'; rw [(hparts k (Finset.mem_range.mp hk')).2.1 h18]; ring rw [ha, hb, e0, e1, hc', Finset.sum_mul, Finset.sum_mul, ← Finset.sum_add_distrib, ← Finset.sum_add_distrib] apply Finset.sum_congr rfl intro k hk' have hk'' := Finset.mem_range.mp hk' have hp := (hparts k hk'').2.2 unfold pvD at hp rw [(hwv k hk'').1, ← hp]; ring unfold specOut rw [out_core inW relu X W b i jj lx lw hlx hlw1 ?_ ?_ ?_ a bb c hlanes w hw] · intro k hk' have hx := hxok k hk' unfold XOk at hx refine ⟨fun h' => hx.1 (by rw [h']; rfl), ?_⟩ by_cases he : (expOf X[i * inW + k]!).toNat = 0 · left exact ⟨UInt32.toNat_inj.mp (by simpa using he), UInt32.toNat_inj.mp (by simpa using hx.2.1 he)⟩ · right; exact (hx.2.2 he).1 · intro k hk' obtain ⟨_, hm2⟩ := hspec k hk' rcases hm2 with hm0 | ⟨_, h255, _⟩ · intro h'; unfold mantOf at hm0; rw [h'] at hm0; simp at hm0 · exact fun h' => h255 (by rw [h']; rfl) · intro k hk' _ obtain ⟨_, hm2⟩ := hspec k hk' rcases hm2 with hm0 | ⟨_, _, hle, _⟩ · left unfold mantOf at hm0 by_cases he : expOf W[jj * inW + k]! = 0 · rw [if_pos he] at hm0 exact ⟨he, UInt32.toNat_inj.mp (by simpa using hm0)⟩ · rw [if_neg he] at hm0; omega · right; exact hle theorem denseBlocks_pref (inW outW : Nat) (relu : Bool) (X W b : Array Word) (i : Nat) (U0 U1 U2 D : Array UInt32) (oks : Array Bool) (sds : Array UInt64) (s0 s1 s2 : UInt64) (wide : Bool) (base : Nat) (h0 : inW ≤ U0.size) (h1 : inW ≤ U1.size) (h2 : inW ≤ U2.size) (hin : inW < 2048) (hDsz : outW * inW ≤ D.size) (hDs : D.size < 2 ^ 32) (wo : USize) (hwo : wo.toNat ≤ 65280) (lx lw : Nat) (hlx : 1 ≤ lx) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hbase : base = lx + lw - 2) (lim : Nat) (hwl : wide = false → lim ≤ 18) (hparts : ∀ k, k < inW → PartsOkD U0 U1 U2 k ∧ (lim ≤ 18 → uv U2[k]! = 0) ∧ pvD U0 U1 U2 k = wv X[i * inW + k]! lx) (hxok : ∀ k, k < inW → XOk X (i * inW) lx lim k) (hs0 : s0 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U0[t]!).toUInt64) (hs1 : s1 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U1[t]!).toUInt64) (hs2 : s2 = 0 + ∑ t ∈ Finset.Ico 0 inW, (U2[t]!).toUInt64) (hD : ∀ jj, jj < outW → ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!) + 0x80000000).toUInt32) (hok : ∀ jj, jj < outW → oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (hsd : ∀ jj, jj < outW → sds[jj]! = 0 + ∑ t ∈ Finset.Ico 0 inW, (D[jj * inW + t]!).toUInt64) (j : Nat) (out : Array Word) (h : Pref inW outW relu X W b out (i * outW + min j outW)) : Pref inW outW relu X W b (denseBlocks inW outW relu X W b i U0 U1 U2 D oks sds s0 s1 s2 wide base h0 h1 h2 hin hDsz hDs j out) ((i + 1) * outW) := by induction j, out using denseBlocks.induct inW outW relu X W b i U0 U1 U2 D oks sds s0 s1 s2 wide base h0 h1 h2 hin hDsz hDs with | case1 j out hj n hn w0 w1 w2 w3 hr nn e0 e1 e2 e3 r ih => have heq : denseBlocks inW outW relu X W b i U0 U1 U2 D oks sds s0 s1 s2 wide base h0 h1 h2 hin hDsz hDs j out = denseBlocks inW outW relu X W b i U0 U1 U2 D oks sds s0 s1 s2 wide base h0 h1 h2 hin hDsz hDs (j + 4) (pushBlock inW outW relu X W b i j base r (fun jj => oks[jj]!) out) := by conv_lhs => unfold denseBlocks simp only [dite_cond_eq_true (eq_true hj)] rfl rw [heq] apply ih rw [show min j outW = j by omega] at h have L := fun (jj : Nat) (hjj : jj < outW) (wr : USize) (hwr : wr = (jj * inW).toUSize) (a bb c : UInt64) => lane_good_dense inW outW relu X W b i jj hjj hin D oks sds wo hwo U0 U1 U2 s0 s1 s2 lx lw lim hlx hlw1 hlw hwoe hparts hxok hs0 hs1 hs2 (hD jj hjj) (hok jj hjj) (hsd jj hjj) wr hwr (by have : (jj + 1) * inW ≤ outW * inW := Nat.mul_le_mul_right _ hjj; omega) a bb c rw [hbase] at * have hw0 : w0 = (j * inW).toUSize := rfl have hw1 : j + 1 < outW → w1 = ((j + 1) * inW).toUSize := fun h' => by show ((if j + 1 < outW then j + 1 else j) * inW).toUSize = _; rw [if_pos h'] have hw2 : j + 2 < outW → w2 = ((j + 2) * inW).toUSize := fun h' => by show ((if j + 2 < outW then j + 2 else j) * inW).toUSize = _; rw [if_pos h'] have hw3 : j + 3 < outW → w3 = ((j + 3) * inW).toUSize := fun h' => by show ((if j + 3 < outW then j + 3 else j) * inW).toUSize = _; rw [if_pos h'] have he1 : j + 1 < outW → e1 = sds[j + 1]! := fun h' => by show sds[if j + 1 < outW then j + 1 else j]! = _; rw [if_pos h'] have he2 : j + 2 < outW → e2 = sds[j + 2]! := fun h' => by show sds[if j + 2 < outW then j + 2 else j]! = _; rw [if_pos h'] have he3 : j + 3 < outW → e3 = sds[j + 3]! := fun h' => by show sds[if j + 3 < outW then j + 3 else j]! = _; rw [if_pos h'] have hnn : n.toNat = inW := hn simp only [r] by_cases hw : wide = true <;> simp only [hw, ite_true, ite_false, dite_true, dite_false, Bool.false_eq_true] · obtain ⟨p0, p1, p2, p3, p4, p5⟩ := dotD2w_spec U0 U1 U2 D w0 w1 n (by omega) (by omega) (by omega) (dense_ok01 _ _ _ _ _ _ hr) hDs 0 0 0 0 0 0 0 obtain ⟨q0, q1, q2, q3, q4, q5⟩ := dotD2w_spec U0 U1 U2 D w2 w3 n (by omega) (by omega) (by omega) (dense_ok23 _ _ _ _ _ _ hr) hDs 0 0 0 0 0 0 0 simp only [USize.toNat_zero, hnn] at p0 p1 p2 p3 p4 p5 q0 q1 q2 q3 q4 q5 apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ (by rw [p0] <;> rfl) (by rw [p1] <;> rfl) (Or.inl (by rw [p2] <;> rfl)) · intro h1' exact L (j + 1) h1' w1 (hw1 h1') _ _ _ (by rw [p3, he1 h1'] <;> rfl) (by rw [p4, he1 h1'] <;> rfl) (Or.inl (by rw [p5, he1 h1'] <;> rfl)) · intro h2' exact L (j + 2) h2' w2 (hw2 h2') _ _ _ (by rw [q0, he2 h2'] <;> rfl) (by rw [q1, he2 h2'] <;> rfl) (Or.inl (by rw [q2, he2 h2'] <;> rfl)) · intro h3' exact L (j + 3) h3' w3 (hw3 h3') _ _ _ (by rw [q3, he3 h3'] <;> rfl) (by rw [q4, he3 h3'] <;> rfl) (Or.inl (by rw [q5, he3 h3'] <;> rfl)) · obtain ⟨e0', e1', e2', e3', e4', e5', e6', e7'⟩ := dotD4_spec U0 U1 D w0 w1 w2 w3 n (by omega) (by omega) hr hDs 0 0 0 0 0 0 0 0 0 simp only [USize.toNat_zero, hnn] at e0' e1' e2' e3' e4' e5' e6' e7' have h18 : lim ≤ 18 := hwl (by simpa using hw) apply pushBlock_pref inW outW relu X W b i j (lx + lw - 2) _ _ out hj h · exact L j hj w0 hw0 _ _ _ (by rw [e0'] <;> rfl) (by rw [e1'] <;> rfl) (Or.inr ⟨h18, rfl⟩) · intro h1' exact L (j + 1) h1' w1 (hw1 h1') _ _ _ (by rw [e2', he1 h1'] <;> rfl) (by rw [e3', he1 h1'] <;> rfl) (Or.inr ⟨h18, rfl⟩) · intro h2' exact L (j + 2) h2' w2 (hw2 h2') _ _ _ (by rw [e4', he2 h2'] <;> rfl) (by rw [e5', he2 h2'] <;> rfl) (Or.inr ⟨h18, rfl⟩) · intro h3' exact L (j + 3) h3' w3 (hw3 h3') _ _ _ (by rw [e6', he3 h3'] <;> rfl) (by rw [e7', he3 h3'] <;> rfl) (Or.inr ⟨h18, rfl⟩) | case2 j out hj => unfold denseBlocks; simp only [dite_cond_eq_false (eq_false hj)] rw [show min j outW = outW by omega] at h rw [show (i + 1) * outW = i * outW + outW by ring]; exact h theorem denseRows_pref (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (D : Array UInt32) (oks : Array Bool) (sds : Array UInt64) (wo : USize) (hwo : wo.toNat ≤ 65280) (lw : Nat) (hlw1 : 1 ≤ lw) (hlw : lw ≤ 254) (hwoe : wo.toNat = lw * 256) (hD : ∀ jj, jj < outW → ∀ k, k < inW → D[jj * inW + k]! = (sgnW W[jj * inW + k]! (magW wo hwo W[jj * inW + k]!) + 0x80000000).toUInt32) (hok : ∀ jj, jj < outW → oks[jj]! = true → ∀ k, k < inW → (magW wo hwo W[jj * inW + k]!).toNat < 2 ^ 31) (hsd : ∀ jj, jj < outW → sds[jj]! = 0 + ∑ t ∈ Finset.Ico 0 inW, (D[jj * inW + t]!).toUInt64) (hXsz : rows * inW ≤ X.size) (hDsz : outW * inW ≤ D.size) (hXs : X.size < 2 ^ 32) (hDs : D.size < 2 ^ 32) (hin : inW < 2048) (U0 U1 U2 : Array UInt32) (hU : inW ≤ U0.size ∧ inW ≤ U1.size ∧ inW ≤ U2.size) (i : Nat) (out : Array Word) (hi : i ≤ rows) (h : Pref inW outW relu X W b out (i * outW)) : Pref inW outW relu X W b (denseRows rows inW outW relu X W b D oks sds lw hXsz hDsz hXs hDs hin U0 U1 U2 hU i out) (rows * outW) := by induction U0, U1, U2, hU, i, out using denseRows.induct rows inW outW relu X W b D oks sds lw hXsz hDsz hXs hDs hin with | case1 U0 U1 U2 hU i out hi' xo n hn hX lh wide r hr out' ih => have heq : denseRows rows inW outW relu X W b D oks sds lw hXsz hDsz hXs hDs hin U0 U1 U2 hU i out = denseRows rows inW outW relu X W b D oks sds lw hXsz hDsz hXs hDs hin r.1 r.2.1 r.2.2.1 hr (i + 1) out' := by conv_lhs => unfold denseRows simp only [dite_cond_eq_true (eq_true hi')] rfl rw [heq] apply ih (by omega) simp only [out'] split · rename_i hcond simp only [Bool.and_eq_true, decide_eq_true_eq] at hcond obtain ⟨hok', hlx⟩ := hcond have hni : n.toNat = inW := hn have hxo : xo.toNat = i * inW := toUSize_toNat _ (by nlinarith) obtain ⟨-, hsum0, hsum1, hsum2, hsem⟩ := decXD_sem X xo n lh.1 (if wide then 39 else 18) hX hXs (lim_le _) 0 U0 U1 U2 (by rw [hni]; exact hU) 0 0 0 true (by simp) obtain ⟨-, hrest⟩ := hsem hok' simp only [USize.toNat_zero, hxo, hni] at hsum0 hsum1 hsum2 hrest have hlx1 : 1 ≤ lh.1.toNat := hlx apply denseBlocks_pref inW outW relu X W b i _ _ _ D oks sds _ _ _ wide _ _ _ _ hin hDsz hDs wo hwo lh.1.toNat lw hlx1 hlw1 hlw hwoe rfl (if wide then 39 else 18 : UInt32).toNat (lim_ok wide) · intro k hk obtain ⟨_, p2, p3, p4⟩ := hrest k (Nat.zero_le _) hk exact ⟨p2, p3, p4⟩ · intro k hk; exact (hrest k (Nat.zero_le _) hk).1 · exact hsum0 · exact hsum1 · exact hsum2 · exact hD · exact hok · exact hsd · simpa using h · exact slowRow_pref inW outW relu X W b i 0 out (Nat.zero_le _) (by simpa using h) | case2 U0 U1 U2 hU i out hi' => unfold denseRows; simp only [dite_cond_eq_false (eq_false hi')] have : i = rows := by omega subst this; exact h theorem win_eq (lw : UInt32) (h : lw ≤ 254) : (lw.toUSize * 256).toNat = lw.toNat * 256 := by have h1 : lw.toNat ≤ 254 := h have h2 : (lw.toUSize).toNat = lw.toNat := UInt32.toNat_toUSize lw rw [USize.toNat_mul, h2] have : 2 ^ 32 ≤ USize.size := USize.le_size have h3 : USize.size = 2 ^ System.Platform.numBits := rfl simp only [USize.toNat_ofNat] rw [Nat.mod_eq_of_lt (by omega : 256 < 2 ^ System.Platform.numBits), Nat.mod_eq_of_lt (by omega)] theorem pref_full (rows inW outW : Nat) (relu : Bool) (X W b : Array Word) (out : Array Word) (h : Pref inW outW relu X W b out (rows * outW)) : out = linearWith dotOf rows inW outW relu X W b := by obtain ⟨hs, hq⟩ := h apply Array.ext · simp [linearWith, hs] · intro q h1 h2 rw [← getElem!_pos out q h1, hq q (by omega)] simp only [linearWith, Array.getElem_ofFn] rfl theorem lw_facts (lw0 : UInt32) : let lw : UInt32 := if lw0 == 0 || lw0 > 254 then 1 else lw0 1 ≤ lw.toNat ∧ lw ≤ 254 := by intro lw simp only [lw] split · exact ⟨by decide, by decide⟩ · rename_i h simp only [Bool.or_eq_true, beq_iff_eq, decide_eq_true_eq, not_or, not_lt] at h obtain ⟨h1, h2⟩ := h have h2' : ¬ (254 : UInt32).toNat < lw0.toNat := fun h' => h2 (UInt32.lt_iff_toNat_lt.mpr h') refine ⟨?_, UInt32.le_iff_toNat_le.mpr (by simp at h2' ⊢; omega)⟩ have : lw0.toNat ≠ 0 := fun h' => h1 (UInt32.toNat_inj.mp (by simpa using h')) omega theorem linFast_eq : linFast = linearWith dotOf := by funext rows inW outW relu X W b unfold linFast split · rename_i hg obtain ⟨hlw1, hlw⟩ := lw_facts ((topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7)) dsimp only split · split · apply pref_full set lw : UInt32 := if (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) == 0 || (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) > 254 then 1 else (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) with hlwd have hwoe : (lw.toUSize * 256).toNat = lw.toNat * 256 := win_eq lw hlw obtain ⟨-, d2, -, d4, -, d6⟩ := preDecodeD_sem W (lw.toUSize * 256) (win_ok lw hlw) inW outW hg.2.1 hg.2.2.2.2.2 0 (Array.replicate W.size 0) (by simp) #[] #[] apply denseRows_pref rows inW outW relu X W b _ _ _ (lw.toUSize * 256) (win_ok lw hlw) lw.toNat hlw1 hlw hwoe · intro jj hjj k hk; exact d2 jj (Nat.zero_le _) hjj k hk · intro jj hjj hok k hk exact d4 jj (Nat.zero_le _) hjj (by rw [show (#[] : Array Bool).size + (jj - 0) = jj by simp]; exact hok) k hk · intro jj hjj have := d6 jj (Nat.zero_le _) hjj rwa [show (#[] : Array UInt64).size + (jj - 0) = jj by simp] at this · exact Nat.zero_le _ · simp [Pref] apply pref_full set lw : UInt32 := if (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) == 0 || (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) > 254 then 1 else (topRows W inW outW hg.2.1 hg.2.2.2.2.2 0 4 0 - 7) with hlwd have hwoe : (lw.toUSize * 256).toNat = lw.toNat * 256 := win_eq lw hlw obtain ⟨-, d2, -, d4⟩ := preDecode_sem W (lw.toUSize * 256) (win_ok lw hlw) inW outW hg.2.1 hg.2.2.2.2.2 0 (Array.replicate W.size 0) (by simp) #[] apply preRows_pref rows inW outW relu X W b _ _ (lw.toUSize * 256) (win_ok lw hlw) lw.toNat hlw1 hlw hwoe · intro jj hjj k hk; exact d2 jj (Nat.zero_le _) hjj k hk · intro jj hjj hok k hk exact d4 jj (Nat.zero_le _) hjj (by rw [show (#[] : Array Bool).size + (jj - 0) = jj by simp]; exact hok) k hk · exact Nat.zero_le _ · simp [Pref] · apply pref_full apply flyRows_pref rows inW outW relu X W b _ (win_ok _ hlw) _ hlw1 hlw (win_eq _ hlw) · exact Nat.zero_le _ · simp [Pref] · rw [slowDot_eq] end def kernels : Kernels where core := ⟨sumX, dotX⟩ dense := ⟨linFast, gemmWith dotFn⟩ conv := ⟨conv2dWith dotFn, depthwiseWith dotFn⟩ elem := ⟨avgpoolWith dotFn, maxpoolW, addReluSpec, scaleShiftSpec⟩ norm := ⟨rmsnormWith dotFn, layernormWith dotFn⟩ theorem correct : Obligation kernels := by simp only [Obligation, kernels, specKernels, specCore, specDense, specConv, specElem, specNorm, linearSpec, gemmSpec, conv2dSpec, depthwiseSpec, avgpoolSpec, rmsnormSpec, layernormSpec, maxpoolW_eq, dotFn_eq_dotOf, linFast_eq, funext sumX_eq_spec, funext fun a => funext (dotX_eq_spec a)] end Submission