import ContractExact namespace Submission local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>mul_le_mul_of_nonneg_right local notation"В"=>Nat.and_two_pow_sub_one_eq_mod local notation"Г"=>Nat.mod_eq_of_lt local notation"Д"=>ite_eq_right local notation"Е"=>getElem!_pos local notation"Ж"=>UInt32.le_iff_toNat_le local notation"З"=>Nat.mul_le_mul_right local notation"И"=>UInt32.toNat_inj local notation"Й"=>Nat.zero_add local notation"К"=>mul_le_mul_of_nonneg_left local notation"Л"=>dite_eq_right local notation"М"=>ite_eq_left local notation"Н"=>dite_eq_left local notation"О"=>UInt32.toNat_toUSize local notation"П"=>zpow_add₀ local notation"Р"=>abs_nonneg local notation"С"=>Nat.pos_of_ne_zero local notation"Т"=>Nat.cast_nonneg local notation"У"=>abs_of_nonneg local notation"Ф"=>UInt32.toNat_and local notation"Х"=>two_ne_zero local notation"Ц"=>zpow_le_zpow_right₀ local notation"Ч"=>abs_add_le local notation"Ш"=>mul_nonneg local notation"Щ"=>mul_lt_mul_of_pos_right local notation"Ъ"=>UInt64.toNat_add local notation"Ы"=>Nat.mod_lt local notation"Ь"=>zpow_natCast local notation"Э"=>Nat.add_zero local notation"Ю"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"Я"=>Nat.shiftRight_eq_div_pow local notation"а"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"б"=>getElem!_neg local notation"в"=>mul_le_mul local notation"г"=>Nat.add_assoc local notation"д"=>Nat.pow_le_pow_right local notation"е"=>Nat.lt_or_eq_of_le local notation"ж"=>Nat.le_mul_of_pos_right local notation"з"=>Nat.pos_of_mul_pos_left local notation"и"=>UInt32.toNat_shiftRight local notation"й"=>UInt64.toNat_shiftRight local notation"к"=>List.mem_range local notation"л"=>Nat.mul_comm local notation"м"=>UInt32.lt_iff_toNat_lt local notation"н"=>UInt64.lt_iff_toNat_lt local notation"о"=>zpow_pos local notation"п"=>Nat.div_eq_of_lt local notation"р"=>Nat.shiftLeft_eq local notation"с"=>Bool.and_eq_true_iff local notation"т"=>Nat.add_mul local notation"у"=>Nat.div_lt_of_lt_mul local notation"ф"=>Nat.one_mul local notation"х"=>Nat.pow_add local notation"ц"=>Nat.lt_succ_iff local notation"ч"=>USize.toNat_one local notation"ш"=>UInt32.toNat_ofNat' local notation"щ"=>Nat.two_pow_add_eq_or_of_lt local notation"ъ"=>Array.ext local notation"ы"=>Finset.sum_congr local notation"ь"=>Nat.exists_eq_add_of_le local notation"э"=>UInt64.toNat_and local notation"ю"=>lt_of_mul_lt_mul_right local notation"я"=>Nat.div_div_eq_div_mul local notation"ذ"=>Nat.succ_mul local notation"ر"=>Nat.succ_pos local notation"ز"=>UInt32.toNat_sub_of_le local notation"س"=>List.mem_filter local notation"ش"=>Nat.mod_add_div local notation"ص"=>Array.getElem_push_eq local notation"ض"=>Array.getElem_push_lt local notation"ط"=>le_trans local notation"ظ"=>Rat.cast_zero 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⟩ lemma 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 _ _ _ א];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 _ _ _ א];push_cast;ring lemma Acc.total_empty:Acc.empty.total=0:=by decide section open FloatLib.Numerics lemma qCp (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:=ط (д ב א) 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 (ط (д ב א) 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 :=א;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) :=א;rw[ha,hb];dsimp only;rw[show a-j+1=(a+1)-j by omega];exact roundShiftRightEven_shiftRightJam m j (a+1) א . congr 1;rw[show m.log2-j-23=(m.log2-23)-j by omega];exact roundShiftRightEven_shiftRightJam m j _ א def winVal (a:Array Nat) (i:Nat):Nat->Nat | 0=>0 | k+1=>a[i]! + 2^32*winVal a (i+1) k lemma qAi (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 lemma qDe (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) (λ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) א),show a[i]! = b[j]! from by simpa using h 0 א] lemma qAa (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 :=ב;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 א . intro h;refine ⟨by simpa using h 0 א,Or.inr λt ht=>?_⟩;rw[show i+1+t=i+(t+1) by omega];exact h (t+1) א lemma qFd (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 א have h1:=ih (i+1) (λt ht=>by rw[show i+1+t=i+(t+1) by omega];exact h (t+1) א) simp only[Nat.add_zero] at h0;rw[winVal,show 32*(k+1)=32+32*k by ring,pow_add];nlinarith lemma qA5 (a:Array Nat) (t:Nat) (h:a.size<=t):a[t]! = 0:=by simp[getElem!_neg a t א] lemma qBz (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_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[qA5 a i א,List.drop_of_length_le (by simp;omega),List.drop_of_length_le (by simp;omega)];simp[limbVal] lemma qCx (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),<-qBz a 0 (lo+(n+a.size)) א,qAi,qAi];rw[(qAa a 0 lo).2 (λt ht=>by simpa using hlow t ht),(qAa a (0+lo+n) a.size).2 (λt _=>hhigh _ א)];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 lemma qB2 (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:=Ы _ ב have hta:ta%4294967296<4294967296:=Ы _ ב have ht2:t<2*4294967296 :=א have hq:t/4294967296<=1 :=א have hd:t%4294967296<2^32:=by have:=Ы 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 е (ц.mp hs) with hs | rfl . rw[Е _ s (by simp;omega),ض hs,<-Е out s hs];exact hout s hs . rw[Е _ _ ג,ص];exact hd obtain ⟨h1,h2,h3⟩:=ih (i+1) (ta/4294967296) (tb/4294967296) (1-t/4294967296) (out.push (t%4294967296)) א 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[qAi,qDe _ out 0 0 out.size (λs hs=>by rw[Е _ _ (by simp;omega),ض א, <-Е out _ א])] simp only[winVal,Nat.zero_add,Nat.mul_zero,Nat.add_zero] rw[Е _ _ ג,ص] 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:=ش ta 4294967296 have e2:=ش tb 4294967296 have e3:=ش t 4294967296 have e4:(1-t/4294967296:Nat)=1-(t/4294967296:Int) :=א have e5:(t:Int)=ta%4294967296+4294967296-tb%4294967296-br :=א 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 lemma qEb (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 е (ц.mp ht) with ht | rfl;. exact ih h t ht . simpa using hk lemma qCl (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,λt h1 h2=>by omega⟩ . rename_i hk;obtain ⟨a,b,c⟩:=ih hh;refine ⟨by omega,b,λt h1 h2=>?_⟩;rcases е (ц.mp h2) with h2 | rfl;. exact c t h1 h2 . simpa using hk lemma qB6 (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 е (ц.mp ht) with ht | rfl;. exact h1 t ht . exact h0 . intro h;exact ⟨h k א,λt ht=>h t א⟩ lemma qF5 (r:Array Nat) (k:Nat):anyGo r k=(winVal r 0 k != 0):=by have hz:=qAa r 0 k;simp only[Nat.zero_add] at hz;cases h:anyGo r k;. rw[hz.2 ((qB6 r k).1 h)];rfl . have:winVal r 0 k≠0:=λh0=>by have:=(qB6 r k).2 (hz.1 h0);simp_all simp[this] lemma qEx (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:=Ы x ב 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:=ش 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 _ _ א,п hA,Й,Nat.add_mul_mod_self_left,Г hA] by_cases hc:x%4≠0∨low≠0 . have:¬ (low+2^(32*h)*(x%4)=0):=λ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)⟩) lemma qBy (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 С hR cases neg<;>simp[finishWord,ofIntDyadic,hR,not_lt.mpr hpos.le,Nat.pos_of_ne_zero hR] lemma qBc (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[(qAa r 0 r.size).2 (λt ht=>by simpa using qEb r _ hnone t ht)];simp . rename_i hsome;obtain ⟨h1,h2,h3⟩:=qCl r _ 0 hsome;rw[show r.size=1+(r.size-1) by omega,qAi,(qAa r (0+1) (r.size-1)).2 (λt ht=>h3 _ א א)];simp[winVal] . rename_i h hsome;obtain ⟨h1,h2,h3⟩:=qCl 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,qAi,qAi,(qAa r (0+h+2) _).2 (λt ht=>h3 _ א א)];simp[winVal] have hlow:winVal r 0 h<2^(32*h):=qFd r 0 h (λt ht=>hr _ א) 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:=С 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]!:=С h2 calc 2^(32*h+2+25)<=2^(32*h)*2^32:=by rw[<-pow_add];exact д ב א _<=2^(32*h)*(r[h]! + 2^32*r[h+1]!):=by gcongr;nlinarith _<=_ :=א rw[qBy neg _ e negZero hR0,<-qCp neg _ e _ hlog,hR,qEx _ _ _ h hlow,qF5] 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 lemma qC5 (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 е (ц.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 lemma qC7 (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:=qC5 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 qCx a lo n . intro t ht;rcases ha with rfl | rfl;. exact (hlow t א ht).1 . exact (hlow t א ht).2 . intro t ht;by_cases hts:t< max pos.size neg.size . rcases ha with rfl | rfl;. exact (hhigh t א hts).1 . exact (hhigh t א hts).2 . rcases ha with rfl | rfl<;>exact qA5 _ t א 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:=qB2 pos neg lo 0 0 0 #[] n א ג have hd':=qB2 neg pos lo 0 0 0 #[] n א ג 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[qBc 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[qBc 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) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) else finishFast acc.pos acc.neg (-298) (n≠0 && acc.negZero) lemma qHz (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 lemma qCc (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 .. lemma qDk (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 א have hb:=hB index א 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[qCc] 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) א 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 :=א;subst this;rw[ha,hb];exact he' . exact hall k hk א . rw[hD,h5,dsum,ha,hb,qHz];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] lemma qAf (n:Nat) (f:Nat->UInt32) (k:Nat) (hk:k< n) : (Array.ofFn (n:=n) λi=>f i.val)[k]! = f k:=by rw[Е _ k (by simpa using hk),Array.getElem_ofFn] lemma qCa (n:Nat) (fa fb:Nat->UInt32) : dotFn n fa fb =dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i):=by unfold dotFn set A:=Array.ofFn (n:=n) λi=>fa i with hA set B:=Array.ofFn (n:=n) λ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⟩:=qDk fa fb A B n (qAf n fa) (qAf n fb) Acc.empty 0 n א (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[qC7,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 א א).1;rw[Е 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 א א).2;rw[Е B k hk'] at this;simp[hR,isFinite_ofUInt32,this] have hslice:Model.Reduction.Semantics.finiteDotSlice L R 0 L.size ג (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 א);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[<-а,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 א);have h1':(Model.Reduction.Internal.dotStateLoop L R ({}:Model.Reduction.Internal.State Fmt) 0 L.size ג (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[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]) lemma qAy (a:Array UInt32) (n:Nat) (h:n=a.size) : (Array.ofFn (n:=n) λi=>a[i.val]!)=a:=by subst h;apply ъ ג;intro i h1 h2;rw[Array.getElem_ofFn,Е a i h2] lemma qEv (n:Nat) (c:UInt32) : (Array.ofFn (n:=n) λ_=>c)=Array.replicate n c:=by apply ъ ג;intro i h1 h2;simp def dotX (a b:Array UInt32):UInt32 := if a.size=b.size then dotFn a.size (λi=>a[i]!) (λi=>b[i]!) else dotSpec a b def sumX (x:Array UInt32):UInt32:=dotFn x.size (λi=>x[i]!) (λ_=>oneW) lemma qDt (a b:Array UInt32):dotX a b=dotSpec a b:=by unfold dotX;split;. rename_i h;rw[qCa,qAy a _ rfl,qAy b _ h] . rfl lemma qDg (x:Array UInt32):sumX x=sumSpec x:=by rw[sumX,qCa,qAy x _ rfl,qEv,sumSpec] lemma qAc:dotFn=dotOf :=funext λn=>funext λfa=>funext λfb=>qCa n fa fb lemma qCk (w:Word):toWord (ofW w)=w:=by apply И.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact Г w.toBitVec.isLt lemma qCj (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) lemma qF2 (w:Word):keyOf w=sv w*2^idxOf w:=by unfold keyOf sv;split<;>simp[Nat.shiftLeft_eq] lemma qAg (w:Word) : (decodeD w).toReal=(keyOf w:ℝ)*2^(-149:Int):=by rw[<-Ю,decodeD_toRat,qF2];push_cast;rw[zpow_sub₀ Х,Ь];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) lemma qDw (a b:Word) : Model.cmpDyadic (decodeD a) (decodeD b)=compare (keyOf a) (keyOf b):=by have hs:(0:ℝ)<2^(-149:Int):=о two_pos _;cases h:compare (keyOf a) (keyOf b);. rw[Model.cmpDyadic_lt_iff,qAg,qAg];exact Щ (by exact_mod_cast compare_lt_iff_lt.mp h) hs . rw[Model.cmpDyadic_eq_iff,qAg,qAg,compare_eq_iff_eq.mp h] . rw[Model.cmpDyadic_gt_iff,qAg,qAg];exact Щ (by exact_mod_cast compare_gt_iff_gt.mp h) hs lemma qFp (a b:Word):ofW (maxW a b)=Model.maximum (ofW a) (ofW b):=by unfold maxW;split;. exact qCj _ . rename_i hs;have ha:=toDyadic_ofUInt32 a (λh=>hs (Or.inl h));have hb:=toDyadic_ofUInt32 b (λ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[qDw] 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[qCj] def maxpoolW (c h w k:Nat) (inp:Array Word):Array Word := Array.ofFn (n:=c*(h/k*(w/k))) λ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 (λacc t=>maxW acc (poolAt h w k inp ch y x t)) (poolAt h w k inp ch y x 0) lemma qEm (p:Nat->Word) (l:List Nat) (acc:Word) : l.foldl (λm t=>Model.maximum m (ofW (p t))) (ofW acc) =ofW (l.foldl (λa t=>maxW a (p t)) acc):=by induction l generalizing acc with | nil=>rfl | cons t l ih=>simp only[List.foldl_cons,<-qFp,ih] lemma qDy:maxpoolW=maxpoolSpec:=by funext c h w k inp;simp only[maxpoolW,maxpoolSpec,qEm,qCk] lemma usz (t n:USize) (h:t< n):(t+1).toNat=t.toNat+1:=by have h1:t.toNat< n.toNat:=А.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,ч];exact Г א lemma qAl (a b:USize) (h:a.toNat+b.toNat<2^32):(a+b).toNat=a.toNat+b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add];exact Г א lemma uk (k:Nat) (h:k<2^32):(USize.ofNat k).toNat=k:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_ofNat'];exact Г א lemma qB3 (a:FloatArray) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qAp (a:Array UInt32) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdF (a:FloatArray) (i:USize):Float := if h:i< a.usize then a.uget i (qB3 a i h) else 0 @[inline] def rdW (a:Array UInt32) (i:USize):UInt32 := if h:i< a.usize then a.uget i (qAp a i h) else 0 @[inline] def wrF (a:FloatArray) (i:USize) (v:Float):FloatArray := if h:i< a.usize then a.uset i v (qB3 a i h) else a @[inline] def wrW (a:Array UInt32) (i:USize) (v:UInt32):Array UInt32 := if h:i< a.usize then a.uset i v (qAp a i h) else a def pwEntry (i:Nat):Float := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then Float.ofBits s else Float.ofBits (s ||| ((e+873) <<< (52:UInt64))) def pw:FloatArray := (List.range 512).foldl (λa i=>a.push (pwEntry i)) (FloatArray.emptyWithCapacity 512) lemma qD8:pw.data=(List.range 512).toArray.map pwEntry:=by have key:∀ (l:List Nat) (a:FloatArray),(l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pw,key];simp[FloatArray.emptyWithCapacity] lemma qGu:pw.size=512:=by simp[FloatArray.size,qD8] lemma qHd (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[qGu];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega @[inline] def dv (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:UInt32)).toUSize (qHd w) @[inline] def good (w:UInt32):Bool := let v:=w &&& 0x7FFFFFFF v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32) def scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):UInt32 := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 0x7FFFFFFF let u:=v-1 scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else if 0x007FFFFF<=lo∧hi<0x7F800000 then hi else 0xFFFFFFFF termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega lemma qAj (a:Array UInt32):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ def pushZ (n k:USize) (a:FloatArray):FloatArray := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def zivW (A B:UInt64):UInt32 := let x:UInt64:=(A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let y:UInt64:=(B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let ex:UInt64:=(x >>> (23:UInt64)) &&& (0x7FF:UInt64) if x == y && ex-(897:UInt64)<(254:UInt64) then (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).toUInt32 else 0xFFFFFFFF @[inline] def zivS (s:Float):UInt32 := let a:=s.toBits zivW (a+2) (a-2) @[inline] def ziv (s e:Float):UInt32 := zivW (s+e).toBits (s-e).toBits structure F1 where (s t:Float) structure R1 where (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32) @[inline] def mx (a w:UInt32):UInt32 := let m:=w &&& 0x7FFFFFFF if m<=a then a else m def decR (X:Array UInt32) (o n t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32):R1 := if h:t< n then let w:=rdW X (o+t) if w &&& 0x7FFFFFFF == 0 then decR X o n (t+1) ix buf c hi else decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c (dv w)) (c+1) (mx hi w) else ⟨ix,buf,c,hi⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:USize) (W:Array UInt32) (hw:w.toNat+m.toNat<=W.size) (hs:W.size<2^32) (hi:i.toNat< m.toNat):(w+i).toNat< W.size:=by rw[qAl _ _ א];omega section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary noncomputable def R (f:Float):ℝ:=Model.toReal (toModel (ofFloat f)) noncomputable def wv (w:UInt32):ℝ:=Model.toReal (ofW w) noncomputable def exactS (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32):ℝ := (Finset.range n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:Nat):Float:=(n.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000 lemma qAm:FloatFormat.binary64.isIEEE=true:=by decide lemma qB4 (a:Model FloatFormat.binary64) : Model.isFinite a=(Model.toModel a).isFinite:=by have hd:=Model.toDyadic?_ieee_eq_model _ qAm a;cases hm:Model.toModel a with | notANumber => rw[hm] at hd;cases h:Model.isFinite a with | false=>rfl | true => obtain ⟨d,hd'⟩:=Model.exists_toDyadic?_of_isFinite h;rw[hd] at hd';cases hd' | infinity s => rw[hm] at hd;cases h:Model.isFinite a with | false=>rfl | true => obtain ⟨d,hd'⟩:=Model.exists_toDyadic?_of_isFinite h;rw[hd] at hd';cases hd' | zero s => rw[hm] at hd;exact Model.isFinite_eq_true_of_toDyadic?_some hd | finite s m e h => rw[hm] at hd;exact Model.isFinite_eq_true_of_toDyadic?_some hd abbrev A_M (x:Float):Model FloatFormat.binary64:=Model.ofFloatModel x.toModel lemma A_R (x:Float):R x=Model.toReal (A_M x):=rfl lemma qAb (x:Float):Model.isFinite (A_M x)=x.isFinite:=by rw[qB4,Model.toModel_ofFloatModel];rfl lemma qA9 (e:ℤ):Formats.Flocq.bpow FloatLib.Numerics.binaryRadix e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qA6:(2:ℝ)^(1000:ℤ)<=Model.toReal (Model.posMaxFinite FloatFormat.binary64):=by rw[Model.toReal_posMaxFinite] have h1:Model.pow2 FloatFormat.binary64.fracWidth+FloatFormat.binary64.maxFiniteFracField =2^53-1:=by decide +kernel have h2:FloatFormat.binary64.maxNormalExponent-Int.ofNat FloatFormat.binary64.fracWidth =971:=by decide +kernel rw[h1,h2];have h3:Model.bpow 971=(2:ℝ)^(971:ℤ):=by simp only[Model.bpow,qA9] rw[h3,show (1000:ℤ)=29+971 from rfl,П ב];apply Б _ (by positivity);norm_num lemma qIg (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb : |R x| + |R y| ≤ 2^(1000:ℤ)) : (x+y).isFinite=true ∧ R (x+y)=Model.roundAt FloatFormat.binary64 (R x+R y):=by have hm:A_M (x+y)=Model.add (A_M x) (A_M y) :=toModel_ofFloat_add_of_isFinite x y hx hy have hfx:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) . rw[A_R,hm];exact Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) lemma qId (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb : |R x| + |R y| ≤ 2^(1000:ℤ)) : (x-y).isFinite=true ∧ R (x-y)=Model.roundAt FloatFormat.binary64 (R x-R y):=by have hm:A_M (x-y)=Model.sub (A_M x) (A_M y) :=toModel_ofFloat_sub_of_isFinite x y hx hy have hfx:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) . rw[A_R,hm];exact Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) open Float.Model.UnpackedFloat in lemma qD6 (b:BitVec 64) (s m e h) (hu:Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 b=.finite s m e h) : (2^52<=m∧-1074<=e)∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at hu;simp only at hu;have hb:(Float.Model.Format.binary64.exponentBias:ℤ)=1023:=by decide rw[hb] at hu;split_ifs at hu with h1 h2 h3 h4 . injection hu with _ _ he;right;have:(unpackExponent (spec:=Float.Model.Format.binary64) b).toNat=0:=by simp[h3] rw[this] at he;omega . injection hu with _ hm he;left;have:(unpackExponent (spec:=Float.Model.Format.binary64) b).toNat≠0:=by intro h0;exact h3 (BitVec.eq_of_toNat_eq (by simpa using h0)) constructor;. rw[<-hm,BitVec.toNat_append];refine ط ?_ Nat.left_le_or;simp[Nat.shiftLeft_eq] . omega open Float.Model.UnpackedFloat in lemma qEk (spec:Float.Model.Format) (s:Sign) (m:ℕ) (e:ℤ) (h:e<=spec.targetExponent (Float.Model.totalExponent m e)) : Float.Model.UnpackedFloat.round spec s m e=roundWithAccuracy spec s m e .exact:=by unfold Float.Model.UnpackedFloat.round decreaseExponent;have h0:(e-spec.targetExponent (Float.Model.totalExponent m e)).toNat=0 :=א;simp[h0] open Float.Model.UnpackedFloat in lemma qIh (s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂) (b₁ b₂:BitVec 64) (hu₁:unpack Float.Model.Format.binary64 b₁=.finite s₁ m₁ e₁ h₁) (hu₂:unpack Float.Model.Format.binary64 b₂=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=Float.Model.Format.binary64.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have c₁:=qD6 _ _ _ _ _ hu₁ have c₂:=qD6 _ _ _ _ _ hu₂ have hm0:m₁*m₂≠0:=Nat.mul_ne_zero h₁.ne' h₂.ne' have hlog:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=λh=>(Nat.le_log2 hm0).2 h simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] rcases c₁ with ⟨c₁,_⟩ | c₁ . have:=hlog (ط c₁ (ж _ h₂));apply le_max_of_le_left;norm_num;omega . rcases c₂ with ⟨c₂,_⟩ | c₂;. have:=hlog (ط c₂ (Nat.le_mul_of_pos_left _ h₁));apply le_max_of_le_left;norm_num;omega . apply le_max_of_le_right;norm_num;omega open Float.Model.UnpackedFloat in lemma qDx (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) : A_M (x*y)=Model.mul (A_M x) (A_M y):=by have hX:Model.toModel (A_M x)=x.toModel.unpack:=Model.toModel_ofFloatModel _;have hY:Model.toModel (A_M y)=y.toModel.unpack:=Model.toModel_ofFloatModel _;change Model.ofFloatModel (Float.Model.mul x.toModel y.toModel)=_;rw[Float.Model.mul,Model.ofFloatModel_pack,Model.Proof.mul_eq_spec,Model.Spec.mul,Model.toDyadic?_ieee_eq_model _ qAm (A_M x),Model.toDyadic?_ieee_eq_model _ qAm (A_M y),hX,hY];change x.toModel.unpack.isFinite=true at hx;change y.toModel.unpack.isFinite=true at hy;cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,Bool.false_eq_true] at hx hy case zero.zero s₁ s₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case zero.finite s₁ s₂ m₂ e₂ h₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case finite.zero s₁ m₁ e₁ h₁ s₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];have hle:=qIh s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂ _ _ hxm hym;have h0:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] rw[<-qEk _ _ _ _ hle,h0];have hs:Model.modelSign (Model.modelSignBit s₁ ^^ Model.modelSignBit s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAm,ite_true,Model.ieeeRoundDyadic,hs];rfl lemma qC8 (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb : |R x| * |R y| ≤ 2^(1000:ℤ)) : (x*y).isFinite=true ∧ R (x*y)=Model.roundAt FloatFormat.binary64 (R x*R y):=by have hm:A_M (x*y)=Model.mul (A_M x) (A_M y):=qDx x y hx hy have hfx:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) . rw[A_R,hm];exact Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAm hfx hfy (hb.trans qA6) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qBj:0< A_z:=by unfold A_z;positivity lemma qCd:A_z<=1/2^200*(1/2^200):=by have:(1/2^200*(1/2^200):ℝ)=2^(-400:ℤ):=by rw[show (-400:ℤ)=-200+-200 from rfl,П ב];norm_num rw[this];exact Ц ב ב lemma qCw:0< A_B:=by unfold A_B;positivity lemma qAq:(2:ℝ)^256*2^42<=A_B:=by have:((2:ℝ)^256*2^42)=2^(298:ℤ):=by rw[show (298:ℤ)=256+42 from rfl,П ב];norm_num rw[this];exact Ц ב ב lemma qDm:A_B*2<=2^(1000:ℤ):=by have:A_B*2=2^(301:ℤ):=by unfold A_B;rw[show (301:ℤ)=300+1 from rfl,П ב];norm_num rw[this];exact Ц ב ב lemma qCm:A_B*A_B<=2^(1000:ℤ):=by have:A_B*A_B=2^(600:ℤ):=by unfold A_B;rw[show (600:ℤ)=300+300 from rfl,П ב] rw[this];exact Ц ב ב lemma qEf (x:ℝ) : |Model.roundAt FloatFormat.binary64 x-x| ≤ 1/2^53*(|x| + A_z):=by have ez:(1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ):=by have e1:(1/2^53:ℝ)=2^(-53:ℤ) :=ב rw[e1,show (-1074:ℤ)=-53+-1021 from rfl,П ב (-53),mul_add] rfl rw[ez] have hu:(0:ℝ)<=(2:ℝ)^(-53:ℤ) * |x|:=by positivity have ht:(0:ℝ)<(2:ℝ)^(-1074:ℤ):=by positivity by_cases hx:x=0 . subst hx;rw[Model.roundAt_zero];simp only[sub_self,abs_zero];linarith refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf FloatFormat.binary64) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x hx] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have hl:=Formats.Flocq.bpow_magnitude_sub_one_le FloatLib.Numerics.binaryRadix x hx rw[qA9] at hl rw[qA9,show ((FloatFormat.binary64.fracWidth:ℤ)+1)=53 from rfl,show FloatFormat.binary64.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude FloatLib.Numerics.binaryRadix x=M at hl ⊢ rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h];have e:(2:ℝ)^(M-53)/2=2^(-53:ℤ)*2^(M-1):=by rw[<-П ב,show (-53:ℤ)+(M-1)=M-53-1 by ring,zpow_sub₀ ב _ 1,zpow_one] rw[e];have:=К hl (show (0:ℝ)<=2^(-53:ℤ) by positivity);linarith . rw[max_eq_right h];generalize (2:ℝ)^(-1074:ℤ)=t at ht ⊢;generalize (2:ℝ)^(-53:ℤ) * |x| = v at hu ⊢;linarith lemma qBt (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z):=by obtain ⟨hf,he⟩:=qIg x y hx hy (by have:=qDm;linarith);exact ⟨hf,he ▸ qEf _⟩ lemma qHk (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z):=by obtain ⟨hf,he⟩:=qId x y hx hy (by have:=qDm;linarith);exact ⟨hf,he ▸ qEf _⟩ lemma qDh (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x*y).isFinite=true∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z):=by have hb : |R x| * |R y| ≤ 2^(1000:ℤ) :=(в h1 h2 (Р _) qCw.le).trans qCm;obtain ⟨hf,he⟩:=qC8 x y hx hy hb;exact ⟨hf,he ▸ qEf _⟩ lemma qAk (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k) : Model.roundAt FloatFormat.binary64 ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;apply (Formats.Flocq.generic_format_FLT_iff (FloatFormat.binary64.minSubnormalExponent) (FloatFormat.binary64.fracWidth+1) (by decide) _).2;refine ⟨by decide,⟨m,k⟩,?_,?_,?_⟩;. simp[Formats.Flocq.toReal,qA9] . exact hm . exact hk lemma qAs (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,Model.toReal_eq_unpackedToReal_toModel qAm,Model.toModel_ofFloatModel] lemma qFa (f:Float) (s m e h) (hu:f.toModel.unpack=.finite s m e h) : (Model.ofModel FloatFormat.binary64 (.finite .positive m e h)).toModel=.finite .positive m e h:=by have hm:Model.toModel (A_M f)=.finite s m e h:=by rw[Model.toModel_ofFloatModel,hu] cases s with | positive => rw[<-hm];exact Model.toModel_canonicalizeModel _ | negative => have hf:(Model.toModel (A_M f)).isFinite=true:=by rw[hm];rfl have hn:=Model.toModel_neg_of_isFinite qAm (A_M f) hf;rw[hm] at hn;have h2:Float.Model.UnpackedFloat.neg (.finite .negative m e h) =.finite .positive m e h:=rfl;rw[h2] at hn;rw[<-hn];exact Model.toModel_canonicalizeModel _ lemma qDa (f:Float) (hf:f.isFinite=true):f.abs.isFinite=true∧R f.abs = |R f|:=by have hA:A_M f.abs=Model.ofModel FloatFormat.binary64 f.toModel.unpack.abs :=Model.ofFloatModel_pack _;change f.toModel.unpack.isFinite=true at hf;cases hu:f.toModel.unpack with | notANumber=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | infinity s=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw[hu] at hA;constructor;. rw[<-qAb,hA];exact Model.isFinite_ofModel_zero _ qAm _ . rw[A_R f.abs,hA,qAs f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAm] | finite s m e h => rw[hu] at hA;have hk:=qFa f s m e h hu;change Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))=_ at hk;constructor;. rw[<-qAb,hA,qB4];change (Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))).isFinite=_;rw[hk];rfl . rw[A_R f.abs,hA,qAs f,hu];change Model.toReal (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))=_;rw[Model.toReal_eq_unpackedToReal_toModel qAm,hk];simp only[Model.unpackedToReal_finite,qA9];have h0:(0:ℝ)<=(m:ℝ)*2^e:=by positivity cases s<;>simp[Model.modelSignBit,abs_of_nonneg h0] lemma qD5 (f:Float) (k:ℕ) (hk:k<2^53) (hf:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat Float.Model.Format.binary64 k)) : f.isFinite=true∧R f=k:=by have hA:A_M f=Model.roundDyadic FloatFormat.binary64 (FloatLib.Numerics.Dyadic.ofScaledInt k 0):=by change Model.ofFloatModel f.toModel=_;rw[hf,Model.ofFloatModel_pack];exact Model.ofModel_ofInt_eq_roundDyadic _ qAm (k:ℤ) have hd:(FloatLib.Numerics.Dyadic.ofScaledInt k 0).toReal=k:=by rw[<-Ю,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have hkR:(k:ℝ)<=2^(1000:ℤ):=by have h1:(k:ℝ)<=2^53:=by exact_mod_cast hk.le have h2:(2:ℝ)^53<=2^256*2^42 :=ב calc (k:ℝ)<=2^53:=h1 _<=2^256*2^42:=h2 _<=A_B:=qAq _<=A_B*2:=by linarith [qCw] _<=_:=qDm have hfin:Model.isFinite (A_M f)=true:=by rw[hA];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAm;rw[hd,У (Т _)];exact hkR.trans qA6 refine ⟨by rw[<-qAb];exact hfin,?_⟩;rw[A_R,hA,Model.toReal_roundDyadic_eq_roundAt _ qAm _ (hA ▸ hfin),hd];have:=qAk k 0 (by simpa using hk) ב;simpa using this lemma qG1 (m:UInt32) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat :=qD5 _ _ h rfl lemma qGr (m:UInt64) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat :=qD5 _ _ h rfl lemma qB5:(0:Float).toModel.unpack=.zero .positive:=by rfl lemma qIv:(0:Float).isFinite=true∧R 0=0:=by refine ⟨?_,?_⟩;. change (0:Float).toModel.unpack.isFinite=true;rw[qB5];rfl . rw[qAs,qB5];simp def A_bits (i:ℕ):UInt64 := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:UInt64)) lemma qFe (i:ℕ):pwEntry i=Float.ofBits (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):Bool := match Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec with | .finite s m e _=>(Model.modelSignBit s == decide (256<=i)) && m == 2^52 && e == ((i%256:ℕ):ℤ)-202 && (i%256 != 0) | .zero _=>i%256 == 0 | _=>false lemma qFc:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qIp (i:ℕ) (hi:i<512) : (pwEntry i).isFinite=true∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have hc:=qFc i hi rw[qFe] have hU:(Float.ofBits (A_bits i)).toModel.unpack =Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec :=Model.unpack_pack_unpack _ _ unfold A_chk at hc rw[qAs,hU] change (Float.ofBits (A_bits i)).toModel.unpack.isFinite=true∧_ rw[hU] cases hu:Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec with | notANumber=>rw[hu] at hc;simp at hc | infinity s=>rw[hu] at hc;simp at hc | zero s => rw[hu] at hc simp only[beq_iff_eq] at hc refine ⟨rfl,?_⟩ simp[hc] | finite s m e h => rw[hu] at hc simp only[Bool.and_eq_true,beq_iff_eq,bne_iff_ne] at hc obtain ⟨⟨⟨h1,h2⟩,h3⟩,h4⟩:=hc refine ⟨rfl,?_⟩ have e:((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150):=by rw[Nat.cast_pow,Nat.cast_ofNat,<-Ь,<-П ב] congr 1 push_cast ring rw[Model.unpackedToReal_finite,qA9,h3,h1,h2,mul_assoc,e] simp only[decide_eq_true_eq,h4,↓reduceIte] lemma qF8 (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,qD8] lemma qA1 (w:UInt32):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[Ф];exact В w.toNat 31 lemma qBk (w:UInt32):(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[Ф];exact В w.toNat 23 lemma qGf (w:UInt32):(w >>> (23:UInt32)).toNat=w.toNat/2^23:=by rw[и,Я];rfl lemma qBl (w:UInt32):(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[Ф,qGf];exact В _ 8 lemma qDo (w:UInt32):signOf w=decide (2^31<=w.toNat):=by have hw:=w.toNat_lt;unfold signOf;have h:((w &&& 0x80000000).toNat)=(w.toNat/2^31%2)*2^31:=by rw[Ф];show w.toNat &&& 2^31=_;rw[Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];have h2:w.toNat/2^31%2=0∨w.toNat/2^31%2=1 :=א;rcases h2 with h2 | h2<;>rw[h2]<;>rfl rw[Bool.eq_iff_iff];simp only[bne_iff_ne,ne_eq,decide_eq_true_eq];rw[<-И,h];simp only[UInt32.toNat_zero];omega lemma qHa (w:UInt32) : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qBk,Nat.or_comm];have h:w.toNat%2^23<2^23:=Ы _ (by positivity);have:=щ h 1;simpa using this.symm lemma qBg (w:UInt32) (h:good w=true) : w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<0x7F800000):=by have hv:=qA1 w;have hlt:=(w &&& 0x7FFFFFFF).toNat_lt;unfold good at h;simp only[Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-hv,h];rfl . right;rw[Ж,UInt32.toNat_sub] at h;rw[<-hv];generalize (w &&& 0x7FFFFFFF).toNat=v at *;simp at h;omega noncomputable def A_mag (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qBa (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qBf (w:UInt32) (hg:good w=true) : wv w=(if 2^31<=w.toNat then -1 else 1)*A_mag (w.toNat%2^31):=by have hN:=w.toNat_lt;have hgd:=qBg w hg;have hE:=qBl w;have hF:(fracOf w).toNat=w.toNat%2^23:=qBk w;have h255:expOf w≠255:=by intro h;rw[h] at hE;change 255=_ at hE;omega have hv:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,toDyadic_ofUInt32 w h255] rw[hv];unfold decodeD A_mag;by_cases h0:w.toNat%2^31=0 . have e0:expOf w=0:=by apply И.mp;rw[hE];show _=0;omega have f0:fracOf w=0:=by apply И.mp;rw[hF];show _=0;omega simp only[e0,f0,h0,↓reduceIte];simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] . have e0:expOf w≠0:=by intro h;rw[h] at hE;change 0=_ at hE;omega simp only[e0,h0,↓reduceIte];have f1:w.toNat%2^31%2^23=(fracOf w).toNat:=by rw[hF];omega have f2:w.toNat%2^31/2^23=(expOf w).toNat:=by rw[hE];omega rw[f1,f2];simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,qDo];simp;split_ifs<;>ring lemma qF3 (v:ℕ) (h:v<0x7F800000):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . have h1:((2^23+v%2^23:ℕ):ℝ)<=2^24:=by have:2^23+v%2^23<=2^24 :=א;exact_mod_cast this have h2:(2:ℝ)^(((v/2^23:ℕ):ℤ)-150)<=2^(104:ℤ) :=Ц ב א;have h3:(2:ℝ)^(104:ℤ)=2^104 :=ב;rw[h3] at h2 calc _<=(2:ℝ)^24*2^104:=в h1 h2 (by positivity) (by positivity) _=2^128 :=ב lemma qCn (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):A_mag a<=A_mag b:=by rcases ha with rfl | ha;. simpa [A_mag] using qBa b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have hdiv:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases hdiv.eq_or_lt with he | hl;. rw[he];apply Б _ (by positivity);have:2^23+a%2^23<=2^23+b%2^23 :=א;exact_mod_cast this . have h1:((2^23+a%2^23:ℕ):ℝ)<=2^23*2:=by have:2^23+a%2^23<=2^23*2 :=א;exact_mod_cast this have h2:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ ב];exact Ц ב א have h3:(2:ℝ)^23<=((2^23+b%2^23:ℕ):ℝ):=by have:2^23<=2^23+b%2^23 :=א;exact_mod_cast this calc _<=((2:ℝ)^23*2)*2^(((a/2^23:ℕ):ℤ)-150) := Б h1 (by positivity) _=2^23*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by ring _<=_:=в h3 h2 (by positivity) (by positivity) lemma qA7 (w:UInt32) (hg:good w=true):(dv w).isFinite=true∧R (dv w)=wv w:=by have hN:=w.toNat_lt;have hgd:=qBg w hg;have hM:=qHa w;have hm:=qG1 ((w &&& 0x7FFFFF) ||| 0x800000) (by rw[hM];omega);have hI:(w >>> (23:UInt32)).toUSize.toNat=w.toNat/2^23:=by rw[О,qGf] have hp:=qIp (w.toNat/2^23) א;unfold dv;rw[qF8,hI];have hR1 : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| ≤ 2^24:=by rw[hm.2,hM,У (Т _)];have:2^23+w.toNat%2^23<=2^24 :=א;exact_mod_cast this have hR2 : |R (pwEntry (w.toNat/2^23))| ≤ 2^106:=by rw[hp.2];split_ifs;. simp all_goals simp only[abs_mul,abs_neg,abs_one,one_mul];rw[abs_of_pos (by positivity)];have:(2:ℝ)^(((w.toNat/2^23%256:ℕ):ℤ)-150)<=2^(106:ℤ) :=Ц ב א;simpa using this have hb : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| * |R (pwEntry (w.toNat/2^23))| ≤ 2^(1000:ℤ):=by have h1:=qAq;have e1:(2:ℝ)^24<=2^256*2^42 :=ב;have e2:(2:ℝ)^106<=2^256*2^42 :=ב;exact (в (hR1.trans (e1.trans h1)) (hR2.trans (e2.trans h1)) (Р _) qCw.le).trans qCm obtain ⟨hf,he⟩:=qC8 _ _ hm.1 hp.1 hb;refine ⟨hf,?_⟩;rw[he,hm.2,hp.2,qBf w hg,hM];rcases hgd with h0 | ⟨h1,h2⟩;. have e0:w.toNat/2^23%256=0 :=א;rw[h0];simp only[e0,↓reduceIte,mul_zero,Model.roundAt_zero];simp[A_mag] . have e0:¬ w.toNat/2^23%256=0 :=א;have e1:w.toNat%2^31/2^23=w.toNat/2^23%256 :=א;have e2:w.toNat%2^31%2^23=w.toNat%2^23 :=א;have e3:(256<=w.toNat/2^23) ↔ 2^31<=w.toNat :=א;unfold A_mag;rw[e1,e2];simp only[e0,e3,show ¬ w.toNat%2^31=0 by omega,↓reduceIte];have hk:-1074<=((w.toNat/2^23%256:ℕ):ℤ)-150 :=א;have hMl:2^23+w.toNat%2^23<2^53 :=א;generalize ((w.toNat/2^23%256:ℕ):ℤ)-150=k at hk ⊢;generalize 2^23+w.toNat%2^23=M at hMl ⊢;by_cases hs:2^31<=w.toNat;. have:=qAk (-(M:ℤ)) k (by simpa using hMl) hk;simp only[hs,↓reduceIte];push_cast at this;rw[show (M:ℝ)*(-1*2^k)=-(M:ℝ)*2^k by ring,show -1*((M:ℝ)*2^k)=-(M:ℝ)*2^k by ring];exact this . have:=qAk (M:ℤ) k (by simpa using hMl) hk;simp only[hs,↓reduceIte,one_mul];push_cast at this;exact this def A_fsum (l:List Float):Float:=l.foldl (.+·) 0 noncomputable def A_sa (l:List Float):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List Float):ℝ:=(l.map R).sum lemma qAw (l:List Float):0<=A_sa l:=by induction l with | nil=>simp[A_sa] | cons q l ih => simp only[A_sa,List.map_cons,List.sum_cons] at ih ⊢;positivity lemma qBb (l:List Float) (q:Float):A_sa (l ++ [q])=A_sa l + |R q|:=by simp[A_sa] lemma qBs (l:List Float) (q:Float):A_sr (l ++ [q])=A_sr l+R q:=by simp[A_sr] lemma qAv (l:List Float) (q:Float):A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_fsum] lemma qB7 (l:List Float) : |A_sr l| ≤ A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => simp only[A_sa,A_sr,List.map_cons,List.sum_cons] at ih ⊢;exact (Ч _ _).trans ד lemma qAu (l:List Float) (hf:∀ q∈l,q.isFinite=true) (hlen:l.length<=2^21) (hb:A_sa l<=A_B/4) : (A_fsum l).isFinite=true ∧ |R (A_fsum l)-A_sr l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil => simp only[A_fsum,List.foldl_nil,List.length_nil] refine ⟨qIv.1,?_⟩ simp[qIv.2,A_sr] | append_singleton l q ih => rw[qBb] at hb have hP:=qAw l have hq0:=Р (R q) obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx])) (by simp at hlen;omega) ד have hk:(l.length:ℝ)+1<=2^21:=by simp at hlen exact_mod_cast hlen have hk0:(0:ℝ)<=l.length:=Т _ have hS:=qB7 l have hz:=qBj have hz1:=qCd have hB:=qCw have hBl:=qAq rw[qAv,qBb,qBs,List.length_append,List.length_singleton] have hB1:(8:ℝ)<=A_B:=by have:(8:ℝ)<=2^256*2^42 :=ב linarith have hz':A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1 :=ב linarith push_cast set r:=R (A_fsum l) set a:=R q set P:=A_sa l set S:=A_sr l set k:ℝ:=(l.length:ℝ) set W:=(1/2^53:ℝ)*(P + |a| + A_z) with hWdef have hW:(1/2^53:ℝ)*(P+A_z)<=W:=by rw[hWdef] apply К _ ב linarith have hW0:0<=W:=by rw[hWdef];positivity have hD : |r-S| ≤ 2*k*W:=h2.trans (К hW (by positivity)) have hk2:2*k*(1/2^53)<=1:=by norm_num at hk ⊢ linarith have hUD:(1/2^53:ℝ) * |r-S| ≤ W := calc (1/2^53:ℝ) * |r-S| ≤ 1/2^53*(2*k*W) := К hD ב _=(2*k*(1/2^53))*W:=by ring _<=1*W:=Б hk2 hW0 _=W:=one_mul _ have hDP : |r-S| ≤ P+1 := calc |r-S| ≤ 2*k*(1/2^53*(P+A_z)):=h2 _=(2*k*(1/2^53))*(P+A_z):=by ring _<=1*(P+A_z):=Б hk2 (by positivity) _<=P+1 :=ד have hr : |r| ≤ A_B:=by have e:r=(r-S)+S:=by ring have:=Ч (r-S) S rw[<-e] at this linarith have ha : |a| ≤ A_B :=ד obtain ⟨hf3,he⟩:=qBt (A_fsum l) q h1 (hf q ג) hr ha refine ⟨hf3,?_⟩ have t1 : |R (A_fsum l+q)-(S+a)| ≤ |R (A_fsum l+q)-(r+a)| + |r-S|:=by have e:R (A_fsum l+q)-(S+a)=(R (A_fsum l+q)-(r+a))+(r-S):=by ring rw[e] exact Ч _ _ have t2 : |r+a| ≤ |r-S| + P + |a|:=by have e:r+a=(r-S)+S+a:=by ring rw[e] have h3:=Ч ((r-S)+S) a have h4:=Ч (r-S) S linarith have t3:(1/2^53:ℝ)*(|r+a| + A_z)<=1/2^53 * |r-S| + W:=by rw[hWdef,<-mul_add] apply К _ ב linarith calc |R (A_fsum l+q)-(S+a)| ≤ W+W+2*k*W :=ד _=2*(k+1)*W:=by ring lemma qIu (n:ℕ) (hn:n<2^20) : (nfOf n).isFinite=true∧R (nfOf n)=((n:ℝ)+8)/2^51:=by have ht:(n.toUInt64+8).toNat=n+8:=by have h8:(8:UInt64).toNat=8:=rfl;simp[UInt64.toNat_add,h8];omega have h1:=qGr (n.toUInt64+8) (by rw[ht];omega);have hu:(Float.ofBits 0x3CC0000000000000).toModel.unpack =.finite .positive (2^52) (-103) (by decide):=by rfl have h2:(Float.ofBits 0x3CC0000000000000).isFinite=true:=by change (Float.ofBits 0x3CC0000000000000).toModel.unpack.isFinite=true;rw[hu];rfl have h3:R (Float.ofBits 0x3CC0000000000000)=1/2^51:=by rw[qAs,hu,Model.unpackedToReal_finite,qA9];simp;norm_num have h4:(n:ℝ)+8<=2^21:=by have:n+8<=2^21 :=א;exact_mod_cast this obtain ⟨hf,he⟩:=qC8 _ _ h1.1 h2 (by rw[h1.2,h3,ht];have h5:(1:ℝ)<=2^256*2^42 :=ב;have h6:=qAq;have h7:(2:ℝ)^21<=2^256*2^42 :=ב;refine ط (в ?_ ?_ (Р _) qCw.le) qCm;. rw[Nat.cast_add,У (by positivity)];push_cast;linarith . rw[У (by positivity)];have:(1:ℝ)/2^51<=1 :=ב linarith) unfold nfOf;refine ⟨hf,?_⟩;rw[he,h1.2,h3,ht];have:=qAk ((n:ℤ)+8) (-51) א ב;push_cast at this ⊢;have e:(2:ℝ)^(-51:ℤ)=1/2^51 :=ב;rw[e] at this;rw[this];ring lemma qHg (c Y S T E z:ℝ) (hc1:8<=c) (hc2:c<=2^20+8) (hz1:z<=1/2^200*(1/2^200)) (hY:0<=Y) (hS : |S| ≤ 2*Y) (hT : |T-Y| ≤ Y/2^30+1/2^250) (hE : |E-c/2^51*T| ≤ 1/2^53*(|c/2^51*T| + z)) (hbig:1/2^127 ≤ |S+E| + 1/2^53*(|S+E| + z)) : 4*(c-7)*(1/2^53*Y)+1/2^53*(|S| + |E| + z)< E:=by have hc0:0< c :=ד;obtain ⟨hT1,hT2⟩:=abs_le.mp hT;have hT' : |T| ≤ 2*Y+1/2^250:=by apply abs_le.mpr;constructor<;>linarith have hcG:c/2^51<=1/2^30:=by have:(2:ℝ)^20+8<=2^21 :=ב calc c/2^51<=2^21/2^51:=by gcongr;linarith _=1/2^30 :=ב set G:=c/2^51*T with hGdef have hG : |G| ≤ (2*Y+1/2^250)/2^30:=by rw[hGdef,abs_mul,abs_of_pos (by positivity)] calc _<=(1/2^30:ℝ)*(2*Y+1/2^250) := в hcG hT' (Р _) (by positivity) _=_:=by ring have hE1 : |E| ≤ |E-G| + |G|:=by have e:E=(E-G)+G:=by ring have:=Ч (E-G) G;rwa [<-e] at this have hSE:=Ч S E;have hG0:=Р G;have hE0:=Р E;have hYl:1/2^129<=Y :=ד;have hT3:Y-Y/2^30-1/2^250<=T :=ד;have hT4:0<=Y-Y/2^30-1/2^250 :=ד;have hT5:0<=T :=ד;have hG1:0<=G:=by rw[hGdef];positivity rw[У hG1] at hE hE1 hG;have hE2:=(abs_le.mp hE).1;have hG2:c*(Y-Y/2^30-1/2^250)/2^51<=G:=by rw[hGdef,show c/2^51*T=c*T/2^51 by ring];gcongr have hw1:c*Y<=2^21*Y:=by have:c<=2^21:=by have:(2:ℝ)^20+8<=2^21 :=ב;linarith exact Б this hY have hG3:c*(Y-Y/2^30-1/2^250) =c*Y-c*Y/2^30-c*(1/2^250):=by ring rw[hG3] at hG2 have hc3:c*(1/2^250)<=2^21*(1/2^250):=by have:c<=2^21:=by have:(2:ℝ)^20+8<=2^21 :=ב;linarith exact Б this (by positivity) have hgoal:4*(c-7)*(1/2^53*Y)=c*Y/2^51-28*Y/2^53:=by ring rw[hgoal];linarith lemma qD7 (n:ℕ) (hn:n<2^20) (s t:Float) (hs:s.isFinite=true) (ht:t.isFinite=true) (X Y:ℝ) (hY0:0<=Y) (hYB:Y<=A_B/8) (h1 : |R s-X| ≤ 4*((n:ℝ)+1)*(1/2^53*Y)) (h2 : |R t-Y| ≤ Y/2^30+1/2^250) (h3 : |R s| ≤ 2*Y) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+nfOf n*t)|) : R (s-nfOf n*t)< X∧X< R (s+nfOf n*t):=by have hB:=qCw;have hB8:(8:ℝ)<=A_B:=by have h:(8:ℝ)<=2^256*2^42 :=ב;linarith [qAq] have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hn0:(0:ℝ)<=n:=Т n;obtain ⟨hnf,hnv⟩:=qIu n hn have hT : |R t| ≤ 3/8*A_B:=by have e:R t=(R t-Y)+Y:=by ring have:=Ч (R t-Y) Y;rw[<-e,У hY0] at this;have h:(1:ℝ)/2^250<=1 :=ב;have h':Y/2^30<=Y:=by apply div_le_self hY0;norm_num linarith have hT3 : |R t| ≤ A_B :=ד have hN : |R (nfOf n)| ≤ A_B:=by rw[hnv,У (by positivity)];have:((n:ℝ)+8)/2^51<=1:=by rw[div_le_one (by positivity)];norm_num at hnR ⊢;linarith linarith obtain ⟨hef,heE⟩:=qDh _ _ hnf ht hN hT3 have hNT : |R (nfOf n)*R t| ≤ |R t|:=by rw[abs_mul,hnv,У (show 0<=((n:ℝ)+8)/2^51 by positivity)];have:((n:ℝ)+8)/2^51<=1:=by rw[div_le_one (by positivity)];norm_num at hnR ⊢;linarith nlinarith [Р (R t)] have hz:=qBj;have hz1:=qCd;have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1 :=ב;linarith have hE : |R (nfOf n*t)| ≤ A_B:=by have e:R (nfOf n*t)=(R (nfOf n*t)-R (nfOf n)*R t)+R (nfOf n)*R t:=by ring have:=Ч (R (nfOf n*t)-R (nfOf n)*R t) (R (nfOf n)*R t);rw[<-e] at this;linarith have hSB : |R s| ≤ A_B :=ד;obtain ⟨hpf,hpE⟩:=qBt s _ hs hef hSB hE;obtain ⟨hmf,hmE⟩:=qHk s _ hs hef hSB hE;have hSb1 : |R s+R (nfOf n*t)| ≤ |R s| + |R (nfOf n*t)|:=Ч _ _;have hSb2 : |R s-R (nfOf n*t)| ≤ |R s| + |R (nfOf n*t)|:=abs_sub _ _;rw[hnv] at heE have hP : |R (s+nfOf n*t)| ≤ |R s+R (nfOf n*t)| + |R (s+nfOf n*t)-(R s+R (nfOf n*t))|:=by have e:R (s+nfOf n*t)=(R s+R (nfOf n*t)) + (R (s+nfOf n*t)-(R s+R (nfOf n*t))):=by ring have:=Ч (R s+R (nfOf n*t)) (R (s+nfOf n*t)-(R s+R (nfOf n*t))) rwa [<-e] at this have hbig':(1:ℝ)/2^127 ≤ |R s+R (nfOf n*t)| + 1/2^53*(|R s+R (nfOf n*t)| + A_z):=by have e:(2:ℝ)^(-127:ℤ)=1/2^127 :=ב;rw[e] at hbig;linarith have key:=qHg ((n:ℝ)+8) Y (R s) (R t) (R (nfOf n*t)) A_z ד (by norm_num at hnR ⊢;linarith) hz1 hY0 h3 h2 heE hbig';have e7:4*((n:ℝ)+8-7)=4*((n:ℝ)+1):=by ring rw[e7] at key;obtain ⟨h1a,h1b⟩:=abs_le.mp h1;obtain ⟨hpa,hpb⟩:=abs_le.mp hpE;obtain ⟨hma,hmb⟩:=abs_le.mp hmE;constructor<;>linarith lemma qHr (g h:ℕ->Float) (l:List ℕ) (a b:Float) : l.foldl (λ(acc:F1) t=>F1.mk (acc.s+g t) (acc.t+h t)) ⟨a,b⟩ =⟨(l.map g).foldl (.+·) a,(l.map h).foldl (.+·) b⟩:=by induction l generalizing a b with | nil=>rfl | cons x l ih=>simp only[List.foldl_cons,List.map_cons,ih] lemma qDr (l:List Float) (hf:∀ q∈l,q.isFinite=true) : A_sa (l.map Float.abs)=A_sa l∧A_sr (l.map Float.abs)=A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx]));have h3:=(qDa q (hf q ג)).2;simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.map_map] at h1 h2 ⊢;simp only[Function.comp_def,h3,abs_abs];simp only[Function.comp_def] at h1 h2;rw[h1,h2];simp lemma qD2 (ts:List ℕ) (q:ℕ->Float) (p:ℕ->ℝ) (hq:∀ t∈ts,|R (q t)-p t| ≤ 1/2^53*(|p t| + A_z)) : |A_sr (ts.map q)-(ts.map p).sum| ≤ 1/2^53*((ts.map λt=>|p t|).sum+ts.length*A_z) ∧ A_sa (ts.map q)<=(ts.map λt=>|p t|).sum + 1/2^53*((ts.map λt=>|p t|).sum+ts.length*A_z) ∧ (ts.map λt=>|p t|).sum<=2*A_sa (ts.map q)+ts.length*A_z:=by induction ts with | nil=>simp[A_sa,A_sr] | cons x ts ih => obtain ⟨h1,h2,h3⟩:=ih (λt ht=>hq t (by simp[ht])) have h4:=hq x ג simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.map_map,List.length_cons] at h1 h2 h3 ⊢ simp only[Function.comp_def] at h1 h2 h3 ⊢ have hz:=qBj obtain ⟨h5,h6⟩:=abs_le.mp h4 obtain ⟨h7,h8⟩:=abs_le.mp h1 have hpq : |p x| ≤ |R (q x)| + 1/2^53*(|p x| + A_z):=by have e:p x=R (q x)-(R (q x)-p x):=by ring have:=abs_sub (R (q x)) (R (q x)-p x) rw[<-e] at this linarith have hqp : |R (q x)| ≤ |p x| + 1/2^53*(|p x| + A_z):=by have e:R (q x)=p x+(R (q x)-p x):=by ring have:=Ч (p x) (R (q x)-p x) rw[<-e] at this linarith have hp0:=Р (p x) refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>nlinarith lemma qDd (ts:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈ts,f t<=M) : (ts.map f).sum<=ts.length*M:=by induction ts with | nil=>simp | cons x ts ih => have h1:=ih (λt ht=>h t (by simp[ht]));have h2:=h x ג;simp only[List.map_cons,List.sum_cons,List.length_cons];push_cast;linarith lemma qCb (w:UInt32) (hg:good w=true) : |wv w| = A_mag (w.toNat%2^31):=by rw[qBf w hg,abs_mul,У (qBa _)];split_ifs<;>simp lemma qAh (w:UInt32) (hg:good w=true) : |wv w| ≤ 2^128:=by rw[qCb w hg];apply qF3;rcases qBg w hg with h | h<;>omega lemma qBr (w:UInt32) (hg:good w=true) (h:w &&& 0x7FFFFFFF=0):wv w=0:=by rw[qBf w hg,<-qA1 w,h];simp[A_mag] lemma qAn:(2:ℝ)^128<=A_B:=by have h:(2:ℝ)^128<=2^256*2^42 :=ב;linarith [qAq] lemma qDq (a b:UInt32) (ha:good a=true) (hb:good b=true) : (dv a*dv b).isFinite=true ∧ |R (dv a*dv b)-wv a*wv b| ≤ 1/2^53*(|wv a*wv b| + A_z) ∧ |wv a*wv b| ≤ 2^256:=by obtain ⟨fa,ra⟩:=qA7 a ha obtain ⟨fb,rb⟩:=qA7 b hb have la:=qAh a ha have lb:=qAh b hb have hab : |wv a*wv b| ≤ 2^256:=by rw[abs_mul] calc |wv a| * |wv b| ≤ 2^128*2^128:=в la lb (Р _) (by positivity) _=2^256 :=ב obtain ⟨h1,h2⟩:=qDh _ _ fa fb (by rw[ra];exact la.trans qAn) (by rw[rb];exact lb.trans qAn) rw[ra,rb] at h2 exact ⟨h1,h2,hab⟩ lemma qD1 (n:ℕ) (xw ww:ℕ->UInt32) (bw:UInt32) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (h0:∀ t,t< n->t∉l->wv (xw t)*wv (ww t)=0) : exactS n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw:=by unfold exactS;congr 1;rw[<-List.sum_toFinset _ hl];symm;apply Finset.sum_subset;. intro t ht;simp only[List.mem_toFinset] at ht;simpa using hl2 t ht . intro t ht hnl;exact h0 t (by simpa using ht) (by simpa using hnl) lemma qC4 (n:ℕ) (xw ww:ℕ->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) : exactS n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw := qD1 n xw ww bw l hl hl2 λt h1 h2=>by rcases hz t h1 h2 with h | h . simp[qBr (xw t) (hx t h1) h] . simp[qBr (ww t) (hw t h1) h] lemma qGo (n:ℕ) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n):l.length<=n:=by have h:l.toFinset ⊆ Finset.range n:=by intro t ht;simp only[List.mem_toFinset] at ht;simpa using hl2 t ht have:=Finset.card_le_card h;rwa [List.toFinset_card_of_nodup hl,Finset.card_range] at this lemma qDf (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->Float) (p:ℕ->ℝ) (b:Float) (bv:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ 2^256) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl : |bv| ≤ 2^128) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs))|) : R (A_fsum (ts.map g ++ [b])-nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b]) + nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs)):=by have hfq:∀ q∈ts.map g ++ [b],q.isFinite=true:=by intro q hq simp only[List.mem_append,List.mem_map,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=qD2 ts g p (λt ht=>(hg t ht).2.1) have hAle:=qDd ts (λt=>|p t|) _ (λt ht=>(hg t ht).2.2) have hsa:=qBb (ts.map g) b have hsr:=qBs (ts.map g) b have hsag:=qAw (ts.map g) have hsa0:=qAw (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Т _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=qBj have hz1:=qCd have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1 :=ב linarith have hA0:0<=(ts.map λt=>|p t|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact Р _) set A:=(ts.map λt=>|p t|).sum set k:ℝ:=(ts.length:ℝ) have hkz:k*A_z<=2^20:=by nlinarith have hAl:A<=2^20*2^256:=by refine hAle.trans (Б ?_ (by positivity)) linarith have hBl:=qAq have hnum:(2:ℝ)^20*2^256*3+2^128+2^20+2<=2^256*2^42/8:=by norm_num have hsaB:A_sa (ts.map g ++ [b])+A_z<=A_B/8:=by have hU:(1/2^53:ℝ)*(A+k*A_z)<=A+k*A_z:=by have:0<=A+k*A_z:=by positivity nlinarith linarith have hB8:(1:ℝ)<=A_B/8:=by have:(1:ℝ)<=2^256*2^42/8 :=ב linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp;omega obtain ⟨hf1,he1⟩:=qAu _ hfq hlen1 ד obtain ⟨habs1,habs2⟩:=qDr _ hfq obtain ⟨hf2,he2⟩:=qAu ((ts.map g ++ [b]).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (qDa x (hfq x hx)).1) (by simpa using hlen1) (by rw[habs1];linarith) rw[habs1,habs2] at he2 have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] have hl2:(((ts.map g ++ [b]).map Float.abs).length:ℝ)=k+1:=by simp[k] rw[hl1] at he1 rw[hl2] at he2 generalize hsaD:A_sa (ts.map g ++ [b])=sa at * set Y:=sa+A_z with hYdef have hY0:0<=Y:=by positivity have hUY:0<=(1/2^53:ℝ)*Y:=by positivity have hKU:2*(k+1)*(1/2^53*Y)<=Y/2^30:=by have h:k+1<=2^20 :=ד have:=Б h hUY norm_num at this ⊢ linarith have hS1 : |A_sr (ts.map g ++ [b])| ≤ sa:=by rw[<-hsaD];exact qB7 _ obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨he2a,he2b⟩:=abs_le.mp he2 refine qD7 n hn _ _ hf1 hf2 _ Y hY0 ד ?_ ?_ ?_ hbig . have hKY:sa+k*A_z<=(k+1)*Y:=by rw[hYdef];nlinarith [Ш hk0 hsa0] have hU2:(1/2^53:ℝ)*(A+k*A_z)<=2*(k+1)*(1/2^53*Y):=by have h1:A+k*A_z<=2*(sa+k*A_z):=by linarith [Р bv] have h2:A+k*A_z<=2*((k+1)*Y) :=ד calc (1/2^53:ℝ)*(A+k*A_z)<=1/2^53*(2*((k+1)*Y)) := К h2 ב _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y) :=Б ד hUY;obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1;apply abs_le.mpr;constructor<;>linarith . apply abs_le.mpr;constructor . have:(0:ℝ)<=1/2^250 :=ב;have:A_z<=1/2^250:=by have:(1/2^200*(1/2^200):ℝ)<=1/2^250 :=ב;linarith linarith . linarith . have e:R (A_fsum (ts.map g ++ [b])) =(R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b]))+A_sr (ts.map g ++ [b]):=by ring have h1:=Ч (R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b]));rw[<-e] at h1;have h2:Y/2^30<=Y:=by apply div_le_self hY0;norm_num linarith lemma qB0 (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by set g:ℕ->Float:=λt=>dv (xw t)*dv (ww t) with hg set p:ℕ->ℝ:=λt=>wv (xw t)*wv (ww t) with hp have hfold:l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩ = ⟨(l.map g).foldl (.+·) 0,(l.map λt=>(g t).abs).foldl (.+·) 0⟩ := qHr g (λt=>(g t).abs) l 0 0 obtain ⟨hs1,hs2⟩:=hs rw[hfold] at hs1 hs2 have hlen:=qGo n l hl hl2 have hgt:∀ t∈l,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ 2^256:=λt ht=>qDq _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) obtain ⟨hbf,hbv⟩:=qA7 bw hb have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,qAv];rfl have hE:e=nfOf n*A_fsum ((l.map g ++ [dv bw]).map Float.abs):=by rw[hs2] congr 1 rw[List.map_append,List.map_map,List.map_singleton,qAv] rfl rw[hS,hE] at hbig ⊢ rw[qC4 n xw ww bw hx hw l hl hl2 hz] exact qDf n hn l hlen g p (dv bw) (wv bw) hgt hbf hbv (qAh bw hb) hbig lemma qEg (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e) := qB0 n hn xw ww bw hx hw hb l hl hl2 (λt h1 h2=>Or.inl (hz t h1 h2)) s e hs hbig lemma qGv (H J Rj Rg Rt b z k:ℝ) (hH0:0<=H) (hH:H<=2^128) (hJ:0<=J) (hb:0<=b) (hz0:0<=z) (hz1:z<=1/2^200*(1/2^200)) (hk:k<=2^20) (hj : |Rj-J| ≤ 2*k*(1/2^53*(J+z))) (hg : |Rg-H*Rj| ≤ 1/2^53*(|H*Rj| + z)) (ht : |Rt-(Rg+b)| ≤ 1/2^53*(|Rg+b| + z)) : |Rt-(H*J+b+z)| ≤ (H*J+b+z)/2^30+1/2^250:=by have e1 : |Rj-J| ≤ (J+z)/2^32:=by refine hj.trans ?_ have h:0<=(1/2^53:ℝ)*(J+z):=Ш ב (add_nonneg hJ hz0) have:=Б hk h norm_num at this ⊢ linarith have e2 : |H*Rj-H*J| ≤ (H*J+H*z)/2^32:=by rw[<-mul_sub,abs_mul,У hH0] calc H * |Rj-J| ≤ H*((J+z)/2^32):=К e1 hH0 _=_:=by ring have e3:H*z<=1/2^250:=by calc H*z<=2^128*(1/2^200*(1/2^200)) := в hH hz1 hz0 ב _<=1/2^250 :=ב have hW:0<=H*J:=Ш hH0 hJ have hZ0:0<=H*z:=Ш hH0 hz0 generalize H*J=W at * generalize H*z=Z at * generalize H*Rj=G at * obtain ⟨e2a,e2b⟩:=abs_le.mp e2 obtain ⟨hga,hgb⟩:=abs_le.mp hg obtain ⟨hta,htb⟩:=abs_le.mp ht have hG : |G| ≤ W+(W+Z)/2^32:=by apply abs_le.mpr constructor<;>linarith have hRg : |Rg| ≤ 2*W+1/2^200:=by apply abs_le.mpr constructor<;>linarith have hR : |Rg+b| ≤ 2*W+b+1/2^200:=by have:=Ч Rg b rw[У hb] at this linarith apply abs_le.mpr constructor<;>linarith lemma qEz (ts:List ℕ) (f g:ℕ->ℝ) (H:ℝ) (h:∀ t∈ts,f t<=H*g t) : (ts.map f).sum<=H*(ts.map g).sum:=by induction ts with | nil=>simp | cons x ts ih => have h1:=ih (λt ht=>h t (by simp[ht]));have h2:=h x ג;simp only[List.map_cons,List.sum_cons,mul_add];linarith lemma qDi (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->Float) (p:ℕ->ℝ) (b f a:Float) (bv H J K:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)) (hAJ:(ts.map λt=>|p t|).sum<=H*J) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl : |bv| ≤ 2^128) (hf:f.isFinite=true) (hH:R f=H) (hH0:0<=H) (hHl:H<=2^128) (hjf:a.isFinite=true) (hJ0:0<=J) (hJ2:J<=2^149) (hK:K<=2^20) (hje : |R a-J| ≤ 2*K*(1/2^53*(J+A_z))) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b])+nfOf n*(f*a+b.abs))|) : R (A_fsum (ts.map g ++ [b])-nfOf n*(f*a+b.abs))<(ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b])+nfOf n*(f*a+b.abs)):=by have hfq:∀ q∈ts.map g ++ [b],q.isFinite=true:=by intro q hq simp only[List.mem_append,List.mem_map,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=qD2 ts g p (λt ht=>(hg t ht).2) have hsa:=qBb (ts.map g) b have hsr:=qBs (ts.map g) b have hsa0:=qAw (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Т _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=qBj have hz1:=qCd have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1 :=ב linarith have hA0:0<=(ts.map λt=>|p t|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact Р _) set A:=(ts.map λt=>|p t|).sum set k:ℝ:=(ts.length:ℝ) have hBl:=qAq have hB8:(1:ℝ)<=A_B/8:=by have:(1:ℝ)<=2^256*2^42/8 :=ב linarith have hkR:k<=2^20 :=ד have hk2:2*K*(1/2^53)<=1:=by norm_num at hK ⊢ linarith have hje2 : |R a-J| ≤ J+1 := calc |R a-J| ≤ 2*K*(1/2^53*(J+A_z)):=hje _=(2*K*(1/2^53))*(J+A_z):=by ring _<=1*(J+A_z):=Б hk2 ד _<=J+1 :=ד have hRj : |R a| ≤ 2^151:=by have e:R a=(R a-J)+J:=by ring have h1:=Ч (R a-J) J rw[<-e,У hJ0] at h1 have:(2:ℝ)*2^149+1<=2^151 :=ב linarith have h151:(2:ℝ)^151<=A_B:=by have:(2:ℝ)^151<=2^256*2^42 :=ב linarith obtain ⟨hgf,hge⟩:=qDh f a hf hjf (by rw[hH,У hH0];linarith [qAn]) (hRj.trans h151) rw[hH] at hge have hHj : |H*R a| ≤ 2^128*2^151:=by rw[abs_mul,У hH0] exact в hHl hRj (Р _) ב have hgb : |R (f*a)| ≤ A_B:=by have e:R (f*a)=(R (f*a)-H*R a)+H*R a:=by ring have h1:=Ч (R (f*a)-H*R a) (H*R a) rw[<-e] at h1 have:(2:ℝ)^128*2^151*2+1<=2^256*2^42 :=ב have h2:(1/2^53:ℝ)*(|H*R a| + A_z) ≤ |H*R a| + 1:=by have:=Р (H*R a) linarith linarith obtain ⟨haf,hav⟩:=qDa b hbf rw[hbv] at hav obtain ⟨htf,hte⟩:=qBt (f*a) b.abs hgf haf hgb (by rw[hav,abs_abs];linarith [qAn]) rw[hav] at hte have key2:=qGv H J (R a) (R (f*a)) (R (f*a+b.abs)) |bv| A_z K hH0 hHl hJ0 (Р _) hz.le hz1 hK hje hge hte have hHJ:H*J<=2^128*2^149:=в hHl hJ2 hJ0 ב have hHJ0:0<=H*J:=Ш hH0 hJ0 have hbv0:=Р bv have hkz:k*A_z<=2^20*A_z:=Б hkR hz.le have hYB:H*J + |bv| + A_z<=A_B/8:=by have:(2:ℝ)^128*2^149+2^128+1<=2^256*2^42/8 :=ב linarith have hsaY:A_sa (ts.map g ++ [b])+A_z<=3/2*(H*J + |bv| + A_z):=by linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp;omega obtain ⟨hf1,he1⟩:=qAu _ hfq hlen1 ד have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] rw[hl1] at he1 have hS1:=qB7 (ts.map g ++ [b]) set Y:=H*J + |bv| + A_z with hYdef have hY0:0<=Y :=ד have hUY:0<=(1/2^53:ℝ)*Y:=Ш ב hY0 have hD1:2*(k+1)*(1/2^53*(A_sa (ts.map g ++ [b])+A_z)) ≤ 3*(k+1)*(1/2^53*Y) := calc 2*(k+1)*(1/2^53*(A_sa (ts.map g ++ [b])+A_z)) <=2*(k+1)*(1/2^53*(3/2*Y)):=by apply К _ ד exact К hsaY ב _=_:=by ring have hkY:A+k*A_z<=(k+1)*Y:=by have h1:A+A_z<=Y :=ד have h2:=К h1 (ד:0<=k+1) have h3:=Ш hk0 hA0 linarith have hD2:(1/2^53:ℝ)*(A+k*A_z)<=(k+1)*(1/2^53*Y) := calc (1/2^53:ℝ)*(A+k*A_z)<=1/2^53*((k+1)*Y) := К hkY ב _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y) :=Б ד hUY have hK3:3*(k+1)*(1/2^53*Y)<=Y/2:=by have h:k+1<=2^20 :=ד have:=Б h hUY norm_num at this ⊢ linarith obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1 refine qD7 n hn _ _ hf1 htf _ Y hY0 hYB ?_ key2 ?_ hbig . apply abs_le.mpr;constructor<;>linarith . have e:R (A_fsum (ts.map g ++ [b])) =(R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b]))+A_sr (ts.map g ++ [b]):=by ring have h1:=Ч (R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b]));rw[<-e] at h1;linarith lemma qC6 (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g v:ℕ->Float) (p a:ℕ->ℝ) (b f:Float) (bv H:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ H * |a t|) (hv:∀ t∈ts,(v t).isFinite=true∧R (v t)=a t∧|a t| ≤ 2^128) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl : |bv| ≤ 2^128) (hf:f.isFinite=true) (hH:R f=H) (hH0:0<=H) (hHl:H<=2^128) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs))|) : R (A_fsum (ts.map g ++ [b])-nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b]) + nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs)):=by have hkR:(ts.length:ℝ)<=2^20:=by have:ts.length<=2^20 :=א exact_mod_cast this have hJ0:0<=(ts.map λt=>|a t|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact Р _) have hJl:=qDd ts (λt=>|a t|) _ (λt ht=>(hv t ht).2.2) have hAJ:=qEz ts (λt=>|p t|) (λt=>|a t|) H (λt ht=>(hg t ht).2.2) have hvl:(ts.map λt=>(v t).abs)=(ts.map v).map Float.abs:=by simp[List.map_map,Function.comp_def] have hvf:∀ q∈ts.map v,q.isFinite=true:=by intro q hq obtain ⟨t,ht,rfl⟩:=List.mem_map.mp hq exact (hv t ht).1 obtain ⟨hv1,hv2⟩:=qDr (ts.map v) hvf have hJ1:A_sa (ts.map v)=(ts.map λt=>|a t|).sum:=by simp only[A_sa,List.map_map] congr 1 apply List.map_congr_left intro t ht simp[(hv t ht).2.1] rw[hvl] at hbig ⊢ rw[hJ1] at hv1 hv2 set J:=(ts.map λt=>|a t|).sum have hJ2:J<=2^20*2^128 :=hJl.trans (Б hkR ב) have hBl:=qAq have hlenv:((ts.map v).map Float.abs).length<=2^21:=by simp;omega obtain ⟨hjf,hje⟩:=qAu ((ts.map v).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (qDa x (hvf x hx)).1) hlenv (by rw[hv1] have:(2:ℝ)^20*2^128<=2^256*2^42/4 :=ב linarith) rw[hv1,hv2] at hje have hl3:(((ts.map v).map Float.abs).length:ℝ)=(ts.length:ℝ):=by simp rw[hl3] at hje exact qDi n hn ts hlen g p b f _ bv H J _ (λt ht=>⟨(hg t ht).1,(hg t ht).2.1⟩) hAJ hbf hbv hbl hf hH hH0 hHl hjf hJ0 (hJ2.trans ב) hkR hje hbig lemma qGt (hh:UInt32) (hg:good hh=true) (hp:hh<=0x7FFFFFFF) : 0<=wv hh∧wv hh<=2^128 ∧ ∀ w,good w=true->w &&& 0x7FFFFFFF<=hh->|wv w| ≤ wv hh:=by have hhN:hh.toNat<2^31:=by have:=Ж.mp hp change hh.toNat<=2147483647 at this omega have hHeq:wv hh=A_mag hh.toNat:=by rw[qBf hh hg,Г hhN] simp only[show ¬ 2^31<=hh.toNat by omega,↓reduceIte,one_mul] refine ⟨by rw[hHeq];exact qBa _,ط (le_abs_self _) (qAh hh hg),?_⟩ intro w hg1 hm have h1:=Ж.mp hm rw[qA1] at h1 rw[qCb _ hg1,hHeq] apply qCn _ _ h1 rcases qBg _ hg1 with h | h . exact Or.inl h . exact Or.inr h.1 lemma qDp (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by set g:ℕ->Float:=λt=>dv (xw t)*dv (ww t) with hgd set p:ℕ->ℝ:=λt=>wv (xw t)*wv (ww t) with hpd set v:ℕ->Float:=λt=>dv (ww t) with hvd obtain ⟨hs1,hs2⟩:=hs obtain ⟨hH0,hHl,hb1⟩:=qGt hh hg hp obtain ⟨hbf,hbv⟩:=qA7 bw hb obtain ⟨hhf,hhv⟩:=qA7 hh hg have hgt:∀ t∈l,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ wv hh * |wv (ww t)|:=by intro t ht have ht':=hl2 t ht obtain ⟨h1,h2,-⟩:=qDq _ _ (hx t ht') (hw t ht') refine ⟨h1,h2,?_⟩ simp only[hpd,abs_mul] exact Б (hb1 _ (hx t ht') (hm t ht')) (Р _) have hvt:∀ t∈l,(v t).isFinite=true∧R (v t)=wv (ww t)∧|wv (ww t)| ≤ 2^128:=by intro t ht obtain ⟨h1,h2⟩:=qA7 _ (hw t (hl2 t ht)) exact ⟨h1,h2,qAh _ (hw t (hl2 t ht))⟩ have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,qAv] simp[A_fsum,List.foldl_map,hgd] have hE:e=nfOf n*(dv hh*A_fsum (l.map λt=>(v t).abs)+(dv bw).abs):=by rw[hs2] simp[A_fsum,List.foldl_map,hvd] rw[hS,hE] at hbig ⊢ rw[qC4 n xw ww bw hx hw l hl hl2 hz] exact qC6 n hn l (qGo n l hl hl2) g v p (λt=>wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (qAh bw hb) hhf hhv hH0 hHl hbig end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary section open Float.Model Float.Model.UnpackedFloat lemma qEa (m K:Nat) : (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)) =⟨m/2^(K+1),(m/2^K)%2 != 0,decide (m%2^K≠0)⟩:=by show Nat.repeat ExtendedMantissa.shiftRightOne (K+1) _=_ induction K with | zero => simp[Nat.repeat,ExtendedMantissa.shiftRightOne,ExtendedMantissa.ofMantissaAndAccuracy,Nat.mod_one] | succ K ih => show ExtendedMantissa.shiftRightOne (Nat.repeat _ (K+1) _)=_ rw[ih] simp only[ExtendedMantissa.shiftRightOne,ExtendedMantissa.mk.injEq] refine ⟨?_,?_,?_⟩ . rw[я,<-Nat.pow_succ] . trivial . rw[Nat.mod_pow_succ];have h:0<2^K:=by positivity by_cases h1:m%2^K=0<;>by_cases h2:m/2^K%2=0<;>simp[h1,h2];omega lemma qBo (H a b:Nat) (hb:b< H):(H*a+b)/H=a∧(H*a+b)%H=b:=by have hH:0< H :=א;constructor;. rw[Nat.mul_add_div hH,п hb,Э] . rw[Nat.mul_add_mod,Г hb] lemma qE1 (m K r:Nat) (h1:2*r*2^K< m+2^K) (h2:m<(2*r+1)*2^K) : (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)).roundedMantissa=r:=by rw[qEa,Nat.pow_succ];have hH:0<2^K:=by positivity generalize 2^K=H at *;have hdm:=Nat.div_add_mod m (H*2);have hlt:m%(H*2)< H*2:=Ы _ א;generalize m/(H*2)=q at *;generalize m%(H*2)=t at *;subst hdm have hq:q=r∨q+1=r:=by rcases Nat.lt_trichotomy q r with h | h | h;. right;by_contra hne;have:q+2<=r :=א;have h3:(q+2)*H<=r*H:=З H this;nlinarith . exact Or.inl h . exfalso;have h3:(r+1)*H<=q*H:=З H h;nlinarith rcases hq with rfl | rfl . have ht:t< H:=by nlinarith obtain ⟨d1,d2⟩:=qBo H (2*q) t ht;rw[show H*2*q+t=H*(2*q)+t by ring,d1,d2] by_cases h0:t=0 <;> simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven,h0] . have ht:H< t:=by nlinarith obtain ⟨t',rfl⟩:∃ t',t=H+t':=⟨t-H,by omega⟩;have ht':t'< H :=א;obtain ⟨d1,d2⟩:=qBo H (2*q+1) t' ht';rw[show H*2*q+(H+t')=H*(2*q+1)+t' by ring,d1,d2];have h0:t'≠0 :=א;have h2':(2*q+1)%2=1 :=א;simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven,h0,h2'] lemma qBw (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma qBh (m L:Nat) (h1:2^L<=m) (h2:m<2^(L+1)):m.log2=L:=by have hne:m≠0:=by have:0<2^L:=by positivity omega apply le_antisymm;. have:=(Nat.log2_lt hne).2 h2;omega . exact (Nat.le_log2 hne).2 h1 lemma qA4 (s:Sign) (m:Nat) (e T:Int) (K r:Nat) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:Nat)) (h1:2*r*2^K< m+2^K) (h2:m<(2*r+1)*2^K) : UnpackedFloat.round Format.binary32 s m e =let p:=shiftToTargetExponent Format.binary32 r T .exact if h:p.1.mantissa=0 then .zero s else .finite s p.1.mantissa p.2 (С h):=by unfold UnpackedFloat.round decreaseExponent roundWithAccuracy simp only[hT] have h0:(e-T).toNat=0 :=א have hs:shiftToTargetExponent Format.binary32 m e .exact =(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1),T):=by unfold shiftToTargetExponent shiftToExponent simp only[hT] have h3:(T-e).toNat=K+1 :=א rw[h3,hK] simp only[h0,Nat.shiftLeft_zero,Int.natCast_zero,Int.sub_zero,hs,qE1 m K r h1 h2] lemma qGz (r:Nat) (T:Int) (h1:2^23<=r) (h2:r<2^24) (hT:-149<=T) : shiftToTargetExponent Format.binary32 r T .exact =(ExtendedMantissa.ofMantissaAndAccuracy r .exact,T):=by have hl:=qBh r 23 h1 h2 unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qBw] have h3:max (((23:Nat):Int)+1+T-24) (-149)=T :=א simp only[h3,sub_self,Int.toNat_zero,Int.natCast_zero,Int.add_zero] rfl lemma qF7 (T:Int) (hT:-150<=T) : shiftToTargetExponent Format.binary32 (2^24) T .exact =(ExtendedMantissa.ofMantissaAndAccuracy (2^23) .exact,T+1):=by have hl:(2^24:Nat).log2=24:=qBh _ 24 ב ב unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qBw] have h3:max (((24:Nat):Int)+1+T-24) (-149)-T=1 :=א rw[h3] rfl lemma qCu (s:Sign) (P:Nat) (E:Int) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hp:0< P) : (let p:=shiftToTargetExponent Format.binary32 P E .exact if h:p.1.mantissa=0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (С h))=.finite s P E hp:=by have hne:P≠0 :=א simp only[qGz P E hP1 hP2 hE,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false] lemma qD3 (s:Sign) (E:Int) (hE:-148<=E) (hp:0<2^23) : (let p:=shiftToTargetExponent Format.binary32 (2^24) (E-1) .exact if h:p.1.mantissa=0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (С h))=.finite s (2^23) E hp:=by have hne:(2^23:Nat)≠0 :=א simp only[qF7 (E-1) א,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false,sub_add_cancel] lemma qDj (s:Sign) (m:Nat) (e:Int) (P:Nat) (E:Int) (g:Nat) (hp:0< P) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hg:e+(g+2:Nat)=E) (hhi:2*m<(2*P+1)*2^(g+2)) (hlo1:P≠2^23->2*P*2^(g+2)<2*m+2^(g+2)) (hlo2:P=2^23->33554431*2^(g+2)<4*m) : UnpackedFloat.round Format.binary32 s m e=.finite s P E hp:=by have e1:2^(g+2)=4*2^g:=by rw[х];ring have e2:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have e3:2^(g+25)=2^25*2^g:=by rw[х];ring have e4:2^(g+25+1)=2^26*2^g:=by rw[х,х];ring have e5:2^(g+24)=2^24*2^g:=by rw[х];ring have hG:0<2^g:=by positivity rw[e1] at hhi;by_cases hc:2^(g+25)<=m . have hL:m.log2=g+25:=by apply qBh m _ hc;rw[e4];have:(2*P+1)*(4*2^g)<=(2^25-1)*(4*2^g) :=З _ א;omega have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,qBw];omega rw[qA4 s m e E (g+1) P hT א ?_ ?_];. exact qCu s P E hP1 hP2 hE hp . rw[e2];by_cases hP:P=2^23;. subst hP;rw[e3] at hc;omega . have:=hlo1 hP;rw[e1] at this;nlinarith . rw[e2];nlinarith . have hP:P=2^23:=by by_contra hne;have h1:=hlo1 hne;rw[e1] at h1;have h2:2*(2^23+1)*(4*2^g)<=2*P*(4*2^g) :=З _ א;rw[e3] at hc;omega have h1:=hlo2 hP;subst hP;rw[e1] at h1;rw[e3] at hc;have hL:m.log2=g+24:=by apply qBh m _ (by rw[e5];omega);rw[show g+24+1=g+25 from rfl,e3];omega by_cases hE':-148<=E . have hT:Format.binary32.targetExponent (totalExponent m e)=E-1:=by simp only[totalExponent,hL,qBw];omega rw[qA4 s m e (E-1) g (2^24) hT א א א];exact qD3 s E hE' hp . have hE2:E=-149 :=א;have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,qBw];omega rw[qA4 s m e E (g+1) (2^23) hT א (by rw[e2];omega) (by rw[e2];omega)];exact qCu s _ E ב ב hE hp lemma qA2 (neg:Bool) (m:Nat) (e:Int) : Model.roundDyadic Fmt ⟨neg,m,e⟩ =Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[Model.roundDyadic,Model.ieeeRoundDyadic] rfl lemma qEi (s:Sign):s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl lemma qAx (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:=qBh M 23 hM1 hM2 simp only[UnpackedFloat.pack,hl] have hbias:(E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat:=by simp only[Format.exponentBias];norm_num;ring_nf rw[hbias] have hno:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega simp only[hno,ite_false] have hnorm:(23+1=Format.binary32.mantissaBits):=rfl simp only[hnorm,ite_true,packComponents,BitVec.toNat_append,qEi,BitVec.toNat_ofNat] have h8:(E+150).toNat%2^8=(E+150).toNat:=Г א rw[h8,<-Nat.shiftLeft_add_eq_or_of_lt (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (Ы _ (by positivity):M%2^23<2^23)] split_ifs<;>simp[Nat.shiftLeft_eq] omega lemma qFk (neg:Bool) (m:Nat) (e:Int) (P:Nat) (E:Int) (hm:m≠0) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hE2:E+150<=254) (hhi:2*((m:ℝ)*2^e)<(2*P+1)*2^E) (hlo1:P≠2^23->(2*P-1)*2^E<2*((m:ℝ)*2^e)) (hlo2:P=2^23->33554431*2^E<4*((m:ℝ)*2^e)) : toWord (Model.roundDyadic Fmt ⟨neg,m,e⟩) =UInt32.ofNat ((if neg then 2^31 else 0)+(E+150).toNat*2^23+P%2^23):=by have hp:0< P :=א obtain ⟨k,hk⟩:∃ k:Nat,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,hg⟩:∃ g:Nat,(e-k)+(g+2:Nat)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have hb:(0:ℝ)<2^(e-k):=о ב _ have hV:(m:ℝ)*2^e=((m*2^k:Nat):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-Ь,<-П Х] congr 2;ring have hEE:(2:ℝ)^E=((2^(g+2):Nat):ℝ)*2^(e-k):=by push_cast rw[<-Ь,<-П Х,<-hg] congr 1;push_cast;ring rw[hV,hEE] at hhi have h1:2*(m*2^k)<(2*P+1)*2^(g+2):=by have:(2:ℝ)*((m*2^k:Nat):ℝ)<(2*P+1)*((2^(g+2):Nat):ℝ) :=ю ד hb.le exact_mod_cast this have h2:P≠2^23->2*P*2^(g+2)<2*(m*2^k)+2^(g+2):=by intro hne have h3:=hlo1 hne rw[hV,hEE] at h3 have:(2:ℝ)*P*((2^(g+2):Nat):ℝ) < 2*((m*2^k:Nat):ℝ)+((2^(g+2):Nat):ℝ) :=ю ד hb.le exact_mod_cast this have h3:P=2^23->33554431*2^(g+2)<4*(m*2^k):=by intro hne have h3:=hlo2 hne rw[hV,hEE] at h3 have:(33554431:ℝ)*((2^(g+2):Nat):ℝ)<4*((m*2^k:Nat):ℝ) :=ю ד hb.le exact_mod_cast this rw[qA2,<-ExactRuntime.round_scale _ _ m e k hm,qDj _ (m*2^k) (e-k) P E g hp hP1 hP2 hE hg h1 h2 h3] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qAx _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qH5 (f:Float) : Model.expField (toModel (ofFloat f))=f.toBits.toNat/2^52%2^11:=by show ((f.toBits.toBitVec >>> 52) &&& BitVec.ofNat 64 2047).toNat=_;rw[BitVec.toNat_and,BitVec.toNat_ushiftRight,BitVec.toNat_ofNat,Я];exact В _ 11 lemma qHl (f:Float):Model.fracField (toModel (ofFloat f))=f.toBits.toNat%2^52:=by show (f.toBits.toBitVec &&& BitVec.ofNat 64 (2^52-1)).toNat=_;rw[BitVec.toNat_and,BitVec.toNat_ofNat];exact В _ 52 lemma qIc (f:Float):Model.signBit (toModel (ofFloat f))=decide (2^63<=f.toBits.toNat):=by rw[Model.signBit_eq_msb];show f.toBits.toBitVec.msb=_;rw[BitVec.msb_eq_decide];rfl lemma qIs (f:Float) (s:Bool) (E F:Nat) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : Model.toDyadic? (toModel (ofFloat f))=some ⟨s,2^52+F,(E:Int)-1075⟩:=by have he:=qH5 f;have hf:=qHl f;have hs:=qIc f;have he':f.toBits.toNat/2^52%2^11=E:=by split at h<;>omega have hf':f.toBits.toNat%2^52=F:=by split at h<;>omega have hs':decide (2^63<=f.toBits.toNat)=s:=by cases s;. simp only[Bool.false_eq_true,ite_false] at h;simp;omega . simp only[ite_true] at h;simp;omega rw[he'] at he;rw[hf'] at hf;rw[hs'] at hs;have hx:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f));rw[he,hf,hs] at hx;rw[<-hx];have hfin:Model.isFinite (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ (by show E<2^11;omega) hF];show (E != 2047)=true;simp;omega rw[Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ (by show E<2^11;omega) hF hfin];have hE0:E≠0 :=א;simp only[hE0,ite_false,show B_D.exponentBias=1023 from rfl,show B_D.fracWidth=52 from rfl];simp only[show Model.pow2 52=2^52 from rfl];congr 2;simp only[Int.ofNat_eq_natCast];push_cast;ring lemma qEl (f:Float) (d:FloatLib.Numerics.Dyadic) (h:Model.toDyadic? (toModel (ofFloat f))=some d):f.isFinite=true:=by rw[Model.toDyadic?_ieee_eq_model _ (by decide)] at h;show (Model.toModel (toModel (ofFloat f))).isFinite=true;revert h;generalize Model.toModel (toModel (ofFloat f))=u;cases u<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.isFinite] lemma B_R (f:Float) (s:Bool) (E F:Nat) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : f.isFinite=true ∧ R f=(if s then -1 else 1)*((2^52+F:Nat):ℝ)*2^((E:Int)-1075):=by have hd:=qIs f s E F hE1 hE hF h refine ⟨qEl f _ hd,?_⟩ rw[R,Model.toReal_eq,hd] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A:Nat) (S:Bool) (ex fr:Nat):Prop := ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A x:Nat) (hA:A<2^64) (hx:x=(A+268435455)%2^64/2^29) (h1:897<=x/2^23%2^11) (h2:x/2^23%2^11<1151) : B_near A (decide (2^34<=x)) (x/2^23%2^11) (x%2^23):=by unfold B_near;by_cases h:2^34<=x;. simp only[h,decide_true,ite_true];exact ⟨A-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,Bool.false_eq_true,ite_false];exact ⟨A,by omega,by omega,by omega⟩ lemma qC0 (A:UInt64) : ((A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)).toNat=(A.toNat+268435455)%2^64/2^29:=by rw[й,Ъ];norm_num [Nat.shiftRight_eq_div_pow] lemma qEs (x:UInt64) : ((x >>> (23:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^23%2^11:=by rw[э,й];have:=В (x.toNat >>> 23) 11;norm_num [Nat.shiftRight_eq_div_pow] at this ⊢;exact this lemma qG8 (v:UInt64) (h:v-(897:UInt64)<(254:UInt64)) : 897<=v.toNat∧v.toNat<1151:=by rw[н,UInt64.toNat_sub] at h;have hv:=v.toNat_lt;norm_num at h hv;omega lemma qIb (x:UInt64) (hx:x.toNat<2^35) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).toUInt32 = UInt32.ofNat ((if decide (2^34<=x.toNat) then 2^31 else 0) + (x.toNat/2^23%2^11-896)*2^23+x.toNat%2^23):=by have ha:((x >>> (34:UInt64)) <<< (31:UInt64)).toNat=(x.toNat/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,й] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have hb:((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64))).toNat =x.toNat%2^34-896*2^23:=by have h2:(x &&& (0x3FFFFFFFF:UInt64)).toNat=x.toNat%2^34:=by rw[э] exact В _ 34 have h3:((896:UInt64) <<< (23:UInt64))=7516192768:=by decide rw[h3,UInt64.toNat_sub_of_le] . rw[h2];rfl . rw[UInt64.le_iff_toNat_le,h2];norm_num;omega have hc:(x.toNat/2^34)*2^31 ||| (x.toNat%2^34-896*2^23) =(x.toNat/2^34)*2^31+(x.toNat%2^34-896*2^23):=by rw[л (x.toNat/2^34)] exact (щ א _).symm apply И.mp rw[UInt64.toNat_toUInt32,UInt64.toNat_or,ha,hb,hc,ш] by_cases h:2^34<=x.toNat . simp only[h,decide_true,ite_true];omega . simp only[h,decide_false,Bool.false_eq_true,ite_false];omega lemma qDs (A B:UInt64) (h:zivW A B≠0xFFFFFFFF) : ∃ (S:Bool) (ex fr:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivW A B=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near A.toNat S ex fr∧B_near B.toNat S ex fr:=by unfold zivW at h ⊢ dsimp only at h ⊢ have hAn:=qC0 A have hBn:=qC0 B generalize (A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=x at h hAn ⊢ generalize (B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=y at h hBn ⊢ have hex:=qEs x generalize (x >>> (23:UInt64)) &&& (0x7FF:UInt64)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=с.mp hc;have hxy:x=y:=by simpa using hxy obtain ⟨h1,h2⟩:=qG8 ex (by simpa using hc);rw[hex] at h1 h2;subst hxy;have hA:=A.toNat_lt;have hB:=B.toNat_lt;have hx35:x.toNat<2^35 :=א;simp only[hc0,ite_true];exact ⟨_,_,_,h1,h2,by omega,qIb x hx35 h1 h2,B_x A.toNat x.toNat hA hAn h1 h2,B_x B.toNat x.toNat hB hBn h1 h2⟩ . exact absurd rfl h lemma qBq (s e:Float) (h:ziv s e≠0xFFFFFFFF) : ∃ (S:Bool) (ex fr:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near (s+e).toBits.toNat S ex fr∧B_near (s-e).toBits.toNat S ex fr := qDs (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qAo (f:Float) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:B_near f.toBits.toNat S ex fr) : f.isFinite=true∧∃ v:ℝ,R f=(if S then -v else v) ∧ v<=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< v) ∧ (2^53-2^28)*2^((ex:Int)-1076)< v:=by obtain ⟨M,hA,hl,hu⟩:=hn have hF:M%2^52<2^52:=Ы _ ב have hM:M=(M/2^52)*2^52+M%2^52 :=א generalize M/2^52=E at hM generalize M%2^52=F at hM hF subst hM have hb:(0:ℝ)<2^((ex:Int)-1076):=о ב _ have hE:E=ex∨(E+1=ex∧fr=0) :=א obtain ⟨hfin,hR⟩:=B_R f S E F א א hF (by rw[hA,г]) refine ⟨hfin,((2^52+F:Nat):ℝ)*2^((E:Int)-1075),?_,?_⟩ . rw[hR];cases S<;>simp;ring rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,П Х,zpow_one] rw[e];generalize (2:ℝ)^((E:Int)-1076)=b at hb ⊢;have h3:(F:ℝ)<=fr*2^29+2^28:=by exact_mod_cast (by omega:F<=fr*2^29+2^28) have h4':fr*2^29< F+2^28:=by have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl;exact Nat.lt_of_add_lt_add_left (by rw[г] at hl;exact hl) have h4:(fr:ℝ)*2^29< F+2^28:=by exact_mod_cast h4' have h5:(0:ℝ)<=F:=Т _;have h6:(0:ℝ)<=fr:=Т _;have h7:=Б h3 hb.le;have h8:=Щ h4 hb;have h9:=Ш h5 hb.le;have h10:=Ш h6 hb.le;push_cast;refine ⟨?_,?_,?_⟩;. linarith . intro _;linarith . linarith . have e:(2:ℝ)^(((E+1:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e] at hb ⊢;generalize (2:ℝ)^((E:Int)-1075)=b at hb ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52< F+2^28:=by exact_mod_cast (by omega:2^52< F+2^28) have h7:=Б h3 hb.le;have h8:=Щ h4 hb;push_cast;refine ⟨?_,?_,?_⟩;. linarith . intro h0;exact absurd rfl h0 . linarith lemma qCt (s e:Float) (h:ziv s e≠0xFFFFFFFF) : (s+e).isFinite=true∧(s-e).isFinite=true∧(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|:=by obtain ⟨S,ex,fr,h1,h2,h3,-,ha,hb⟩:=qBq s e h;obtain ⟨f1,v,hv,-,-,h5⟩:=qAo _ S ex fr h1 h2 h3 ha;obtain ⟨f2,-⟩:=qAo _ S ex fr h1 h2 h3 hb;refine ⟨f1,f2,?_⟩;have hb0:(2:ℝ)^(-179:ℤ)<=2^((ex:Int)-1076) :=Ц ב א;have hpos:(0:ℝ)<2^(-179:ℤ):=о ב _;have e1:(2:ℝ)^(-127:ℤ)=2^52*2^(-179:ℤ):=by rw[<-Ь,<-П Х];norm_num have hv1:(2:ℝ)^(-127:ℤ)<=v:=by rw[e1];nlinarith have hv0:0< v:=lt_of_lt_of_le (о ב _) hv1;have : |R (s+e)| = v:=by rw[hv];cases S<;>simp[abs_of_pos hv0] rwa [this] lemma qA8 (w:UInt32) (h:expOf w≠255):wv w=(rationalWord w:ℝ):=by rw[wv,Model.toReal_eq,ofW,toDyadic_ofUInt32 w h];simp only[rationalWord,FloatLib.Numerics.Dyadic.cast_toRat] lemma qAe (w:UInt32) (h:good w=true):expOf w≠255:=by intro he;have he':w.toNat/2^23%2^8=255:=by have:=congrArg UInt32.toNat he;rw[expOf,Ф,и,show (255:UInt32).toNat=2^8-1 from rfl,В] at this;simpa [Nat.shiftRight_eq_div_pow] using this have hv:(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[Ф,show (0x7FFFFFFF:UInt32).toNat=2^31-1 from rfl,В] simp only[good,Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. have:=congrArg UInt32.toNat h;rw[hv] at this;have h0:(0:UInt32).toNat=0:=rfl;omega . rw[Ж,UInt32.toNat_sub,hv] at h;norm_num at h;omega lemma qF0 (a b:UInt32) : ((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:Int) =rationalWord a*rationalWord b:=by simp only[rationalWord,decodeD_toRat] rw[show (-298:Int)=-149+-149 by norm_num,П Х] rw[zpow_sub₀ Х,zpow_sub₀ Х,Ь,Ь,pow_add] norm_num [zpow_neg,zpow_natCast] ring lemma qHm (a b:Array UInt32) (start n:Nat) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈Finset.range n,rationalWord a[start+j]! * rationalWord b[start+j]!:=by induction n generalizing start with | zero=>simp[dsum] | succ n ih => rw[dsum,Int.cast_add,Int.cast_mul,Int.cast_mul,Int.cast_pow,Int.cast_ofNat,add_mul,ih] conv_rhs=>rw[Finset.sum_range_succ'] simp only[Nat.add_zero] rw[qF0] have ht:(∑ j∈Finset.range n,rationalWord a[start+1+j]! * rationalWord b[start+1+j]!) = ∑ j∈Finset.range n,rationalWord a[start+(j+1)]! * rationalWord b[start+(j+1)]!:=by apply ы rfl intro j _ simp[Nat.add_comm,Nat.add_left_comm] rw[ht] ring lemma qE0 (a b:Array UInt32) (hs:a.size=b.size) (hf:∀ j,j< a.size->expOf a[j]!≠255∧expOf b[j]!≠255) (hn:(dsum a b 0 a.size:ℚ)*2^(-298:Int)≠0) : ∃ d:FloatLib.Numerics.Dyadic,d.toRat=(dsum a b 0 a.size:ℚ)*2^(-298:Int) ∧ dotSpec a b=toWord (Model.roundDyadic Fmt d):=by let L:=a.map (Model.ofUInt32 (fmt:=Fmt)) let K:=b.map (Model.ofUInt32 (fmt:=Fmt)) have hLK:L.size=K.size:=by simp[L,K,hs] have hLf:∀ j (hj:j< L.size),Model.isFinite L[j]=true:=by intro j hj have hj':j< a.size:=by simpa [L] using hj have he:=(hf j hj').1 rw[Е a j hj'] at he simp[L,isFinite_ofUInt32,he] have hKf:∀ j (hj:j< K.size),Model.isFinite K[j]=true:=by intro j hj have hj':j< b.size:=by simpa [K] using hj have he:=(hf j א).2 rw[Е b j hj'] at he simp[K,isFinite_ofUInt32,he] have hex:=Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L K hLK hLf hKf have hmath:Model.Reduction.Semantics.finiteDotSlice L K 0 L.size ג (by simp[hLK])=(dsum a b 0 a.size:ℚ)*2^(-298:Int):=by simpa [L,K] using finiteDotSlice_eq a b 0 a.size ג (by simp[hs]) (by intro j _ hj;exact hf j א) have hn':Model.Reduction.Semantics.finiteDot L.toList K.toList≠.ok 0:=by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L K hLK,hmath] simpa using hn refine ⟨(Model.Reduction.Internal.dotState L K hLK : Model.Reduction.Internal.State Fmt).exact,hex.trans hmath,?_⟩ unfold dotSpec Model.dot rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L K .nearestEven hLK hLf hKf hn'] rfl lemma qC9 (n:Nat) (fa fb:Nat->UInt32) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j))≠0) : ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j)) ∧ dotOf n fa fb=toWord (Model.roundDyadic Fmt d):=by have hsum:(dsum (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) 0 n:ℚ) *2^(-298:Int)=∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j):=by rw[qHm] apply ы rfl intro j hj have hj':=Finset.mem_range.mp hj rw[Й,qAf n fa j hj',qAf n fb j hj'] have hs:(Array.ofFn (n:=n) λi=>fa i).size=n:=by simp obtain ⟨d,hd,hw⟩:=qE0 (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) ג (by intro j hj have hj':j< n:=by simpa using hj rw[qAf n fa j hj',qAf n fb j hj'] exact hf j hj') (by rw[hs,hsum];exact hn) rw[hs,hsum] at hd exact ⟨d,hd,hw⟩ lemma qGg:rationalWord oneW=1:=by have he:expOf oneW=127:=by decide have hf:fracOf oneW=0:=by decide have hs:signOf oneW=false:=by decide norm_num [rationalWord,decodeD,he,hf,hs,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand] lemma qGh (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) : ((∑ j∈Finset.range (n+1),rationalWord (if j< n then xw j else oneW) * rationalWord (if j< n then ww j else bw):ℚ):ℝ)=exactS n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qGg,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qA8 bw (qAe bw hb)] congr 1 apply ы rfl intro t ht have ht':=Finset.mem_range.mp ht simp only[ht',ite_true] rw[qA8 _ (qAe _ (hx t ht')),qA8 _ (qAe _ (hw t ht')),Rat.cast_mul] lemma qCv (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qBq s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAo _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAo _ S ex fr h1 h2 h3 hb' have hsum:=qGh n xw ww bw hx hw hb generalize exactS n xw ww bw=V at hlo hhi hsum have hb0:(0:ℝ)<2^((ex:Int)-1076):=о ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-Ь,<-П Х] congr 1;push_cast;ring rw[hva] at hhi rw[hvb] at hlo obtain ⟨W,hVW,hW1,hW2,hW3⟩:∃ W:ℝ,V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by cases S . simp only[Bool.false_eq_true,ite_false] at hhi hlo ⊢;exact ⟨V,rfl,lt_of_lt_of_le hhi hia,λh=>(lob1 h).trans hlo,lob.trans hlo⟩ . simp only[ite_true] at hhi hlo ⊢;exact ⟨-V,by simp,by linarith,λh=>by linarith [loa1 h],by linarith⟩ have hW0:0< W:=by refine lt_of_le_of_lt ?_ hW3 exact Ш ב hb0.le have hne:(∑ j∈Finset.range (n+1),rationalWord (if j< n then xw j else oneW) * rationalWord (if j< n then ww j else bw):ℚ)≠0:=by intro h0 rw[h0,ظ] at hsum rw[<-hsum] at hVW cases S . simp only[Bool.false_eq_true,ite_false] at hVW;linarith . simp only[ite_true] at hVW;linarith obtain ⟨⟨neg,m,q⟩,hd,hw'⟩:=qC9 (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (by intro j _ have hone:expOf oneW≠255:=by decide by_cases hj:j< n . simp only[hj,ite_true];exact ⟨qAe _ (hx j hj),qAe _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨hone,qAe _ hb⟩) hne have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=V:=by rw[<-Ю,hd,hsum] have hq:(0:ℝ)<2^q:=о ב _ have hm0:(0:ℝ)<=m:=Т _ have hmq:=Ш hm0 hq.le have hMW:(m:ℝ)*2^q=W∧neg=S:=by simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] at hdV cases neg<;>cases S <;> simp only[Bool.false_eq_true,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV hVW . exact ⟨by linarith,rfl⟩ . exfalso;linarith . exfalso;linarith . exact ⟨by linarith,rfl⟩ obtain ⟨hMW,rfl⟩:=hMW have hm:m≠0:=by rintro rfl simp at hMW linarith have hword:=qFk neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hz,hw',hword,e1,e2] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma qDz (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qCt s e h obtain ⟨hlo,hhi⟩:=qB0 n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCv n hn xw ww bw hx hw hb s e hlo hhi h lemma qDl (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qCt s e h obtain ⟨hlo,hhi⟩:=qDp n hn xw ww bw hh hx hw hb hg hm hp l hl hl2 hz s e hs hbig exact qCv n hn xw ww bw hx hw hb s e hlo hhi h end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma qAd (k:Nat) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qE8 (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_mul];exact Г א lemma qCr {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAd _ h lemma qAt (a:Array UInt32) (i:USize) (hs:a.size<2^32):rdW a i=a[i.toNat]!:=by unfold rdW;split;. rename_i h;have h2:=qAp a i h;rw[Е a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,qCr a hs];exact h2 rw[б a i.toNat h'];rfl lemma qA0 (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qBd (a:Array UInt32) (i:USize) (v:UInt32) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size):(wrW a i v)[q]! = if q=i.toNat then v else a[q]!:=by have h:i< a.usize:=by rw[А,qCr a hs];exact hi unfold wrW;rw[Н h];by_cases hq:q< a.size . have h3:q<(a.uset i v (qAp a i h)).size:=by simpa using hq rw[Е _ q h3,Е a q hq];simp only[Array.uset,Array.getElem_set];by_cases e:q=i.toNat<;>simp[e,eq_comm] . have h3:¬ q<(a.uset i v (qAp a i h)).size:=by simpa using hq rw[б _ q h3,б a q hq,Д א] def C_Inv (N:Nat) (sp:Nat->Word) (S:Nat->Prop) (out:Array Word):Prop := out.size=N∧∀ q,q< N->S q->out[q]! = sp q lemma qGa {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp S out) {u:USize} {p:Nat} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h;refine ⟨by rw[qA0,h1],λq hq hS=>?_⟩;rw[qBd _ _ _ _ א א,hu];by_cases e:q=p;. rw[М e,e,hv] . rw[Д e];exact h2 q hq (hS.resolve_left e) lemma qCo {N:Nat} {sp:Nat->Word} {S S':Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S' q->S q):C_Inv N sp S' out :=⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qHw {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q) : out=Array.ofFn (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply ъ;. simp[h1] . intro q h3 h4;have h5:q< N :=א;have:=h2 q h5 (hS q h5);rw[Е out q h3] at this;simp[this] lemma qFq (v:UInt32) (h1:(0x7FFFFF:UInt32)<=v-1) (h2:v<0x7F800000) : (v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32))=true:=by have a1:(0x7FFFFF:UInt32).toNat=0x7FFFFF:=rfl;have a2:(0x7F800000:UInt32).toNat=0x7F800000:=rfl;have a3:(0x00800000:UInt32).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:UInt32).toNat=0x7EFFFFFF:=rfl;have a5:(1:UInt32).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:UInt32)<=v:=by rw[Ж];have:v.toNat≠0:=λh=>h0 (И.mp h);omega rw[Ж,ز _ _ h3] at h1;rw[м] at h2;have h4:(0x00800000:UInt32)<=v:=by rw[Ж];omega simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[Ж,ز _ _ h4];omega lemma qH4 (c u lo:UInt32) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qH3 (c v hi:UInt32) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qB1 (w:UInt32) (h:good w=true):good (w &&& 0x7FFFFFFF)=true:=by have e:(w &&& 0x7FFFFFFF) &&& 0x7FFFFFFF=w &&& 0x7FFFFFFF:=by rw[UInt32.and_assoc,UInt32.and_self] unfold good at h ⊢;simpa only [e] using h lemma qFi {h:UInt32} (h1:h<0x7F800000):h<=0x7FFFFFFF:=by have a1:(0x7F800000:UInt32).toNat=0x7F800000:=rfl;have a2:(0x7FFFFFFF:UInt32).toNat=0x7FFFFFFF:=rfl;rw[м] at h1;rw[Ж];omega def C_Hv (A:Array Word) (h:Word):Prop := good h=true∧h<=0x7FFFFFFF∧(∀ k,k< A.size->good A[k]! = true) ∧ ∀ k,k< A.size->A[k]! &&& 0x7FFFFFFF<=h lemma qFx (r v hi:UInt32) (h:(if hi< v then v else hi)<=r):v<=r∧hi<=r:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qHx (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi≠0xFFFFFFFF → (0x7FFFFF:UInt32)<=lo∧hi<0x7F800000∧hi<=scan A n i hn lo hi ∧ scan A n i hn lo hi<0x7F800000∧(good hi=true->good (scan A n i hn lo hi)=true) ∧ ∀ k,i.toNat<=k->k< n.toNat → good A[k]! = true∧A[k]! &&& 0x7FFFFFFF<=scan A n i hn lo hi:=by induction d with | zero => intro i lo hi he h have e:scan A n i hn lo hi =if 0x007FFFFF<=lo∧hi<0x7F800000 then hi else 0xFFFFFFFF:=by rw[scan,Л (by rw[А];omega)] rw[e] at h ⊢ by_cases c:0x007FFFFF<=lo∧hi<0x7F800000 . rw[М c];exact ⟨c.1,c.2,UInt32.le_refl _,c.2,id,λk h1 h2=>by omega⟩ . rw[Д c] at h;exact absurd rfl h | succ d ih => intro i lo hi he h have hi':i< n:=by rw[А];omega have hb:i.toNat< A.size :=א have e:scan A n i hn lo hi=scan A n (i+1) hn (if (A.uget i hb &&& 0x7FFFFFFF)-1< lo then (A.uget i hb &&& 0x7FFFFFFF)-1 else lo) (if hi< A.uget i hb &&& 0x7FFFFFFF then A.uget i hb &&& 0x7FFFFFFF else hi):=by rw[scan,Н hi'] rw[e] at h ⊢ obtain ⟨h1,h2,h3,h4,h5,h6⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h7,h8⟩:=qH4 _ _ _ h1 obtain ⟨h9,h10⟩:=qH3 _ _ _ h2 obtain ⟨h11,h12⟩:=qFx _ _ _ h3 have hg:good (A.uget i hb)=true:=qFq _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qB1 _ hg . exact g0 . by_cases e2:k=i.toNat;. subst e2;rw[Е A i.toNat hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qAz (A:Array UInt32) (hs:A.size<2^32) (h:scan A A.usize 0 (qAj A) 0xFFFFFFFF 0≠0xFFFFFFFF) : C_Hv A (scan A A.usize 0 (qAj A) 0xFFFFFFFF 0):=by have e:=qCr A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qHx A A.usize (qAj A) A.size 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< A.size->good A[k]! = true ∧ A[k]! &&& 0x7FFFFFFF<=scan A A.usize 0 (qAj A) 0xFFFFFFFF 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qFi h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qFg (w:UInt32):w &&& 0x7FFFFFFF<=0x7FFFFFFF:=by rw[Ж,Ф];exact Nat.and_le_right def C_hok (w:Nat->Word) (t:Nat) (h:Word):Prop := good h=true∧(∀ k,k< t->w k &&& 0x7FFFFFFF<=h)∧h<=0x7FFFFFFF lemma qEy {w:Nat->Word} {t:Nat} {h:Word} (hg:good (w t)=true) (H:C_hok w t h):C_hok w (t+1) (mx h (w t)):=by obtain ⟨h1,h2,hp⟩:=H;have hm:=qB1 _ hg;unfold mx;dsimp only;by_cases e:w t &&& 0x7FFFFFFF<=h . rw[М e];refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2];exact e . exact h2 k א . rw[Д e];refine ⟨hm,λk hk=>?_,qFg _⟩;by_cases e2:k=t;. rw[e2];exact UInt32.le_refl _ . have h3:=h2 k א;simp only[UInt32.le_iff_toNat_le] at *;omega lemma qEw {w:Nat->Word} {t:Nat} {h:Word} (hz:w t &&& 0x7FFFFFFF=0) (H:C_hok w t h):C_hok w (t+1) h:=by obtain ⟨h1,h2,hp⟩:=H;refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2,hz,Ж];exact Nat.zero_le _ . exact h2 k א lemma qEh {rows inW i t:Nat} (hi:i< rows) (ht:t< inW):i*inW+t< rows*inW:=by have h:(i+1)*inW<=rows*inW:=З _ hi;rw[т,ф] at h;omega lemma qGe {rows inW i t:Nat} (hi:i< rows) (ht:t<=inW) : i*inW+t<=rows*inW:=by have h:(i+1)*inW<=rows*inW:=З _ hi;rw[т,ф] at h;omega lemma qBm (x:FloatArray) (i:USize) (h:i.toNat< x.size) : x.uget i h=x.data[i.toNat]!:=by obtain ⟨ds⟩:=x;have h':i.toNat< ds.size:=h;rw[Е ds i.toNat h'];rfl lemma qEo (a:Array UInt32) (i:USize) (h:i.toNat< a.size) : a.uget i h=a[i.toNat]!:=by rw[Е a i.toNat h];rfl lemma qEj (a:FloatArray) (i:USize) (v:Float):(wrF a i v).size=a.size:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma qE2 (a:FloatArray) (i:USize) (v:Float) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size) : (wrF a i v).data[q]! = if q=i.toNat then v else a.data[q]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize];rwa [qAd _ hs] unfold wrF;rw[Н h];obtain ⟨ds⟩:=a;have hi':i.toNat< ds.size:=hi;show (ds.set i.toNat v hi')[q]! = if q=i.toNat then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.set i.toNat v hi').size:=by simpa using hq rw[Е _ q h3,Е ds q hq];simp only[Array.getElem_set];by_cases e:q=i.toNat<;>simp[e,eq_comm] . have h3:¬ q<(ds.set i.toNat v hi').size:=by simpa using hq rw[б _ q h3,б ds q hq,Д א] def C_keep (xw:Nat->Word) (t:Nat):List Nat := (List.range t).filter (λp=>xw p &&& 0x7FFFFFFF != 0) lemma qCh (xw:Nat->Word) (t:Nat):(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& 0x7FFFFFFF != 0) (List.range t);simpa [C_keep] using h lemma qD0 (xw:Nat->Word) (t:Nat) (h:xw t &&& 0x7FFFFFFF=0) : C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma qEe (xw:Nat->Word) (t:Nat) (h:¬ xw t &&& 0x7FFFFFFF=0) : C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] def C_Dec (xw:Nat->Word) (sz t:Nat) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Word):Prop := c.toNat=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:Nat,(C_keep xw t)[k]?=some p → (ix[k]! : UInt32).toNat=p∧buf.data[k]! = dv (xw p))∧C_hok xw t hh lemma qHu {X:Array UInt32} {o n:USize} (xw:Nat->Word) (hn:n.toNat<2^32) (hxw:∀ t:USize,t< n->rdW X (o+t)=xw t.toNat) (hg:∀ t,t< n.toNat->good (xw t)=true) (d:Nat) : ∀ (t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Word),t.toNat+d=n.toNat->C_Dec xw n.toNat t.toNat ix buf c hh → C_Dec xw n.toNat n.toNat (decR X o n t ix buf c hh).ix (decR X o n t ix buf c hh).buf (decR X o n t ix buf c hh).c (decR X o n t ix buf c hh).hi:=by induction d with | zero => intro t ix buf c hh he H rw[decR,Л (by rw[А];omega)] have e:t.toNat=n.toNat :=א rw[e] at H;exact H | succ d ih => intro t ix buf c hh he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht rw[decR,Н ht] dsimp only rw[hxw t ht] obtain ⟨h1,h2,h3,h4,h5⟩:=H by_cases hz:xw t.toNat &&& 0x7FFFFFFF=0 . rw[М (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,qD0 xw _ hz];exact ⟨h1,h2,h3,h4,qEw hz h5⟩ . rw[Д (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qCh xw t.toNat;have hc:c.toNat< t.toNat+1 :=א;rw[hu,C_Dec,qEe xw _ hz];have hcn:c.toNat< n.toNat :=א;refine ⟨?_,by rw[qA0];exact h2,by rw[qEj];exact h3,λk p hk2=>?_,qEy (hg _ א) h5⟩;. rw[qAl _ _ (by rw[ч];omega),ч,h1];simp . rw[qBd _ _ _ _ א א,qE2 _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=c.toNat;. subst e;simp at hk2;subst hk2;simp;omega . rw[Д e,Д e];by_cases e2:k< c.toNat;. rw[М e2] at hk2;exact h4 k p hk2 . rw[Д e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-c.toNat=m+1:=⟨k-c.toNat-1,by omega⟩;rw[hm] at hk2;simp at hk2 lemma qBv (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qG2 (n:USize) (d:Nat):∀ (k:USize) (a:FloatArray),k.toNat+d=n.toNat → (pushZ n k a).size=a.size+d:=by induction d with | zero => intro k a he;rw[pushZ,Л (by rw[А];omega)];rfl | succ d ih => intro k a he;have ht:k< n:=by rw[А];omega rw[pushZ,Н ht,ih (k+1) _ (by rw[usz k n ht];omega),qBv];omega lemma qAr {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qCo (qGa hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qH1 (xs:FloatArray) (v:Float) (q:Nat) : (xs.push v).data[q]! = if q=xs.size then v else xs.data[q]!:=by obtain ⟨ds⟩:=xs;show (ds.push v)[q]! = if q=ds.size then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.push v).size:=by simp;omega rw[Е _ q h3,Е ds q hq,Д א,ض] . by_cases e:q=ds.size . subst e;have h3:ds.size<(ds.push v).size:=by simp rw[Е (ds.push v) ds.size h3,М rfl,ص] . have h3:¬ q<(ds.push v).size:=by simp;omega rw[б _ q h3,б ds q hq,Д e] structure D_Sc where (lo hi:UInt32) def D_scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):D_Sc := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 0x7FFFFFFF let u:=v-1 D_scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else ⟨lo,hi⟩ termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega def D_fill (v:Float) (n k:USize) (a:FloatArray):FloatArray := if h:k< n then D_fill v n (k+1) (a.push v) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega def D_copy (X:Array UInt32) (o n t:USize) (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (a:FloatArray):FloatArray := if h:t< n then D_copy X o n (t+1) hs ho (a.push (dv (X.uget (o+t) (wi o t n X ho hs (А.mp h))))) else a termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega def D_img (s:ConvShape) (X:Array UInt32) (hs:X.size<2^32) (z:Float) (R r:Nat) (a:FloatArray):FloatArray := if hr:r< R then let hp:=s.h+2*s.pad let yy:=r%hp let o:=((r/hp*s.h+(yy-s.pad))*s.w).toUSize let pu:=s.pad.toUSize if hg:s.pad<=yy∧yy-s.pad< s.h∧o.toNat+s.w.toUSize.toNat<=X.size then D_img s X hs z R (r+1) (D_fill z pu 0 (D_copy X o s.w.toUSize 0 hs hg.2.2 (D_fill z pu 0 a))) else D_img s X hs z R (r+1) (D_fill z (s.w+2*s.pad).toUSize 0 a) else a termination_by R-r def D_off (s:ConvShape) (t:Nat):Nat := (t/(s.kh*s.kw)*(s.h+2*s.pad)+t/s.kw%s.kh)*(s.w+2*s.pad)+t%s.kw structure D_C where (dw relu f4:Bool) (s:ConvShape) (X W b:Array UInt32) (k C ho wo wp:Nat) (step:USize) (hi:UInt32) (xd:FloatArray) (nf:Float) (tab:Array UInt32) structure D_P where (g:D_C) (wd:FloatArray) (off:Array UInt32) (e l1 l4 l8:USize) (e0 bv ze:Float) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:g.xd.size<2^32) (h1:∀ b:USize,b< l1->∀ j (h:j< off.size),b.toNat+off[j].toNat+0< g.xd.size) (h4:∀ b:USize,b< l4->∀ j (h:j< off.size),b.toNat+off[j].toNat+3< g.xd.size) (h8:∀ b:USize,b< l8->∀ j (h:j< off.size),b.toNat+off[j].toNat+7< g.xd.size) (hz:ze=0) lemma qFy {n d:Nat} (b:USize) (u:UInt32) (hs:n<2^32) (h:b.toNat+u.toNat+d< n):(b+u.toUSize).toNat+d< n:=by rw[qAl _ _ (by rw[О];omega),О];exact h lemma qBe {n:Nat} (p:USize) (k:Nat) (hs:n<2^32) (h:p.toNat+k< n) : (p+USize.ofNat k).toNat< n:=by rw[qAl _ _ (by rw[uk k א];omega),uk k א];exact h structure D_R where (t:UInt8) (a b c d:Float) def D_go4R (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+3< xd.size) (a0 a1 a2 a3:Float) (r:D_R):D_R := if h:i< e then have ht:=А.mp h let p:=b+(off.uget i א).toUSize have hp:p.toNat+3< xd.size:=qFy _ _ hs (hb i.toNat א) let v:=wd.uget i א D_go4R xd wd off b e (i+1) he hw hs hb (a0+xd.uget p א*v) (a1+xd.uget (p+USize.ofNat 1) (qBe p 1 hs א)*v) (a2+xd.uget (p+USize.ofNat 2) (qBe p 2 hs א)*v) (a3+xd.uget (p+USize.ofNat 3) (qBe p 3 hs א)*v) r else match r with | ⟨t,_,_,_,_⟩=>⟨t,a0,a1,a2,a3⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega structure D_R8 where (t:UInt8) (a0 a1 a2 a3 a4 a5 a6 a7:Float) def D_go8R (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+7< xd.size) (a0 a1 a2 a3 a4 a5 a6 a7:Float) (r:D_R8):D_R8 := if h:i< e then have ht:=А.mp h let p:=b+(off.uget i א).toUSize have hp:p.toNat+7< xd.size:=qFy _ _ hs (hb i.toNat א) let v:=wd.uget i א D_go8R xd wd off b e (i+1) he hw hs hb (a0+xd.uget p א*v) (a1+xd.uget (p+USize.ofNat 1) (qBe p 1 hs א)*v) (a2+xd.uget (p+USize.ofNat 2) (qBe p 2 hs א)*v) (a3+xd.uget (p+USize.ofNat 3) (qBe p 3 hs א)*v) (a4+xd.uget (p+USize.ofNat 4) (qBe p 4 hs א)*v) (a5+xd.uget (p+USize.ofNat 5) (qBe p 5 hs א)*v) (a6+xd.uget (p+USize.ofNat 6) (qBe p 6 hs א)*v) (a7+xd.uget (p+USize.ofNat 7) (qBe p 7 hs א)*v) r else match r with | ⟨t,_,_,_,_,_,_,_,_⟩=>⟨t,a0,a1,a2,a3,a4,a5,a6,a7⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega def D_goT (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+0< xd.size) (a t:Float):F1 := if h:i< e then have ht:=А.mp h let q:=xd.uget (b+(off.uget i א).toUSize) (qFy (d:=0) _ _ hs (hb i.toNat א))*wd.uget i א D_goT xd wd off b e (i+1) he hw hs hb (a+q) (t+q.abs) else ⟨a,t⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega @[noinline] def D_slow (g:D_C) (o y x:Nat):UInt32 := act g.relu (dotFn (g.k+1) (λt=>if t< g.k then windowAt g.s g.X y x ((if g.dw then o*g.k else 0)+t) else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) @[inline] def D_finT (c:D_P) (o y x:Nat) (b:USize):UInt32 := if hb:b< c.l1 then let q:=D_goT c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h1 b hb) c.ze c.ze let r:=ziv (q.s+c.bv) (c.g.nf*(q.t+c.bv.abs)) if r == 0xFFFFFFFF then D_slow c.g o y x else act c.g.relu r else D_slow c.g o y x @[inline] def D_fin (c:D_P) (o y x:Nat) (b:USize) (a:Float):UInt32 := let r:=ziv (a+c.bv) c.e0 if r == 0xFFFFFFFF then D_finT c o y x b else act c.g.relu r @[inline] def D_w4 (c:D_P) (o y x:Nat) (b q:USize) (s0 s1 s2 s3:Float) (out:Array UInt32) : Array UInt32 := let o0:=D_fin c o y x b s0 let o1:=D_fin c o y (x+1) (b+1) s1 let o2:=D_fin c o y (x+2) (b+2) s2 let o3:=D_fin c o y (x+3) (b+3) s3 let out:=wrW out q o0 let out:=wrW out (q+1) o1 let out:=wrW out (q+2) o2 wrW out (q+3) o3 def D_cols (c:D_P) (o y x:Nat) (b q:USize) (r8:D_R8) (rr:D_R) (out:Array UInt32):Array UInt32 := if h8:x+8<=c.g.wo∧c.g.f4=true∧b< c.l8 then let r:=D_go8R c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h8 b h8.2.2) c.ze c.ze c.ze c.ze c.ze c.ze c.ze c.ze r8 let out:=D_w4 c o y x b q r.a0 r.a1 r.a2 r.a3 out let out:=D_w4 c o y (x+4) (b+4) (q+4) r.a4 r.a5 r.a6 r.a7 out D_cols c o y (x+8) (b+8) (q+8) r rr out else if h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 then let r:=D_go4R c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h4 b h4.2.2) c.ze c.ze c.ze c.ze rr D_cols c o y (x+4) (b+4) (q+4) r8 r (D_w4 c o y x b q r.a r.b r.c r.d out) else if h2:x+2<=c.g.wo∧c.g.wo<=x+4∧4<=c.g.wo∧c.g.f4=true ∧ b-(x+4-c.g.wo).toUSize< c.l4 then let r:=D_go4R c.g.xd c.wd c.off (b-(x+4-c.g.wo).toUSize) c.e 0 c.he c.hw c.hs (c.h4 _ h2.2.2.2.2) c.ze c.ze c.ze c.ze rr D_w4 c o y (c.g.wo-4) (b-(x+4-c.g.wo).toUSize) (q-(x+4-c.g.wo).toUSize) r.a r.b r.c r.d out else if x< c.g.wo then D_cols c o y (x+1) (b+c.g.step) (q+1) r8 rr (wrW out q (D_finT c o y x b)) else out termination_by c.g.wo-x def D_rows (c:D_P) (o y:Nat) (q:USize) (out:Array UInt32):Array UInt32 := if y< c.g.ho then D_rows c o (y+1) (q+c.g.wo.toUSize) (D_cols c o y 0 (y.toUSize*c.g.step*c.g.wp.toUSize) q ⟨0,0,0,0,0,0,0,0,0⟩ ⟨0,0,0,0,0⟩ out) else out termination_by c.g.ho-y structure D_T where (off:Array UInt32) (a:Float) def D_tap (g:D_C) (o:Nat) (ix:Array UInt32) (buf:FloatArray) (e i:USize) (off:Array UInt32) (a:Float):D_T := if h:i< e then D_tap g o ix buf e (i+1) (wrW off i ((if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0) + rdW g.tab (rdW ix i).toUSize)) (a+(rdF buf i).abs) else ⟨off,a⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega lemma qBu (a:Array UInt32) (j:Nat) (h:j< a.size) : a[j].toNat<=a.foldl (λm v=>max m v.toNat) 0:=by have key:∀ (l:List UInt32) (i:Nat),i<=l.foldl (λm v=>max m v.toNat) i ∧ ∀ v∈l,v.toNat<=l.foldl (λm v=>max m v.toNat) i:=by intro l;induction l with | nil=>intro i;simp | cons x l ih => intro i;obtain ⟨h1,h2⟩:=ih (max i x.toNat);simp only[List.foldl_cons,List.mem_cons,forall_eq_or_imp];exact ⟨by omega,by omega,h2⟩ rw[<-Array.foldl_toList];exact (key a.toList 0).2 _ ג lemma qEc {n m:Nat} (d:Nat) (off:Array UInt32) (hs:n<2^32) (hm:∀ j (h:j< off.size),off[j].toNat<=m) (b:USize) (hb:b<(n-(m+d)).toUSize) (j:Nat) (h:j< off.size):b.toNat+off[j].toNat+d< n:=by have h1:=А.mp hb;rw[Nat.toUSize_eq,uk _ א] at h1;have:=hm j h;omega def D_outs (g:D_C) (hs:g.xd.size<2^32) (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array UInt32):Array UInt32 := if o< g.C then let r:=decR g.W (o*g.k).toUSize g.k.toUSize 0 ix buf 0 0 let t:=D_tap g o r.ix r.buf r.c 0 off 0 let m:=t.off.foldl (λm v=>max m v.toNat) 0 if h:r.c.toNat<=t.off.size∧r.c.toNat<=r.buf.size then let c:D_P:=⟨g,r.buf,t.off,r.c,(g.xd.size-(m+0)).toUSize, (g.xd.size-(m+3)).toUSize,(g.xd.size-(m+7)).toUSize,g.nf*(dv g.hi*t.a+(dv (rdW g.b o.toUSize)).abs), dv (rdW g.b o.toUSize),0,h.1,h.2,hs, qEc 0 t.off hs (qBu t.off),qEc 3 t.off hs (qBu t.off), qEc 7 t.off hs (qBu t.off),rfl⟩ D_outs g hs (o+1) (q+(g.ho*g.wo).toUSize) r.ix t.off r.buf (D_rows c o 0 q out) else out else out termination_by g.C-o def D_F (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) (k C:Nat) : Array UInt32 := let hp:=s.h+2*s.pad let wp:=s.w+2*s.pad let rx:=D_scan X X.usize 0 (qAj X) 0xFFFFFFFF 0 if hg:X.size<2^32∧0x007FFFFF<=rx.lo∧rx.hi<0x7F800000∧X.size=s.cin*s.h*s.w ∧ W.size=C*k∧b.size=C∧0< s.stride∧s.stride<2^32∧0< k∧k<2^20 ∧ s.kh<=hp∧s.kw<=wp∧hp<2^32∧wp<2^32∧W.size<2^32∧b.size<2^32 ∧ C*(s.ho*s.wo)<2^32∧s.cin*hp*wp<2^32 ∧ scan W W.usize 0 (qAj W) 0xFFFFFFFF 0≠0xFFFFFFFF ∧ scan b b.usize 0 (qAj b) 0xFFFFFFFF 0≠0xFFFFFFFF then let tight:=(rx.hi >>> (23:UInt32))-((rx.lo+1) >>> (23:UInt32)) > 99 let R:=s.cin*hp let xd:=D_img s X hg.1 (dv 0) R 0 (FloatArray.emptyWithCapacity (R*wp)) if hs:xd.size<2^32 then let g:D_C:=⟨dw,relu,s.stride == 1 && !tight,s,X,W,b,k,C,s.ho,s.wo,wp, s.stride.toUSize,rx.hi,xd,(k.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000, Array.ofFn (n:=k) λt=>(D_off s t.val).toUInt32⟩ D_outs g hs 0 0 (Array.replicate k 0) (Array.replicate k 0) (pushZ k.toUSize 0 (FloatArray.emptyWithCapacity k)) (Array.replicate (C*(s.ho*s.wo)) 0) else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b def convF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F false s relu X W b (s.cin*s.kh*s.kw) s.cout def depthF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F true s relu X W b (s.kh*s.kw) s.cin def D_px (s:ConvShape) (X:Array Word) (c yy xx:Nat):Word := if yy< s.pad∨xx< s.pad∨yy-s.pad ≥ s.h∨xx-s.pad ≥ s.w then 0 else X[(c*s.h+(yy-s.pad))*s.w+(xx-s.pad)]! def D_Fx (s:ConvShape) (X:Array Word) (q:Nat):Float := dv (D_px s X (q/(s.w+2*s.pad)/(s.h+2*s.pad)) (q/(s.w+2*s.pad)%(s.h+2*s.pad)) (q%(s.w+2*s.pad))) def D_Pre (F:Nat->Float) (m:Nat) (a:FloatArray):Prop := a.size=m∧∀ q,q< m->a.data[q]! = F q lemma qCf {F:Nat->Float} {m:Nat} {a:FloatArray} {v:Float} (h:D_Pre F m a) (hv:v=F m):D_Pre F (m+1) (a.push v):=by refine ⟨by rw[qBv,h.1],λq hq=>?_⟩;rw[qH1,h.1];by_cases e:q=m;. rw[М e,e,hv] . rw[Д e];exact h.2 q א lemma qBp {F:Nat->Float} {v:Float} {n:USize} (d:Nat) : ∀ (k:USize) (m:Nat) (a:FloatArray),k.toNat+d=n.toNat->D_Pre F m a → (∀ i,m<=i->i< m+d->F i=v)->D_Pre F (m+d) (D_fill v n k a):=by induction d with | zero => intro k m a he H _ rw[D_fill,Л (by rw[А];omega)];exact H | succ d ih => intro k m a he H hv have ht:k< n:=by rw[А];omega rw[D_fill,Н ht,show m+(d+1)=m+1+d by omega] exact ih (k+1) (m+1) _ (by rw[usz k n ht];omega) (qCf H (hv m א א).symm) (λi h1 h2=>hv i א א) lemma qEd {F:Nat->Float} {X:Array UInt32} {o n:USize} (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (d:Nat) : ∀ (t:USize) (m:Nat) (a:FloatArray),t.toNat+d=n.toNat->D_Pre F m a → (∀ j,j< d->F (m+j)=dv X[o.toNat+t.toNat+j]!) → D_Pre F (m+d) (D_copy X o n t hs ho a):=by induction d with | zero => intro t m a he H _ rw[D_copy,Л (by rw[А];omega)];exact H | succ d ih => intro t m a he H hv have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א rw[D_copy,Н ht,show m+(d+1)=m+1+d by omega] have e1:=hv 0 א rw[Э,Э] at e1 refine ih (t+1) (m+1) _ (by rw[usz t n ht];omega) (qCf H ?_) (λj hj=>?_) . rw[qEo,qAl _ _ א,e1] . have:=hv (j+1) א;rw[usz t n ht,show m+1+j=m+(j+1) by omega,show o.toNat+(t.toNat+1)+j=o.toNat+t.toNat+(j+1) by omega];exact this lemma qA3 {c:Nat} (a:Nat) {b:Nat} (h:b< c):(a*c+b)/c=a∧(a*c+b)%c=b:=by have h0:0< c :=א;rw[л a c,Nat.mul_add_div h0,Nat.mul_add_mod,п h,Г h];exact ⟨Э _,rfl⟩ lemma qFr (s:ConvShape) (X:Array Word) (r:Nat) {i:Nat} (hi:i< s.w+2*s.pad) : D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=by unfold D_Fx;rw[(qA3 r hi).1,(qA3 r hi).2] lemma qE6 (s:ConvShape) (X:Array UInt32) (hs:X.size<2^32) (hX:X.size=s.cin*s.h*s.w) (hh:s.h+2*s.pad<2^32) (hw:s.w+2*s.pad<2^32) {R:Nat} (hR:R=s.cin*(s.h+2*s.pad)) (d:Nat) : ∀ (r:Nat) (a:FloatArray),r+d=R → D_Pre (D_Fx s X) (r*(s.w+2*s.pad)) a → D_Pre (D_Fx s X) (R*(s.w+2*s.pad)) (D_img s X hs (dv 0) R r a):=by induction d with | zero => intro r a he H rw[D_img,Л א] rw[(א:R=r)];exact H | succ d ih => intro r a he H have hr:r< R :=א have hP:=С (λe:s.h+2*s.pad=0=>by rw[hR,e] at hr;simp at hr) have hc:r/(s.h+2*s.pad)< s.cin :=у (by rw[л,<-hR];exact hr) have hy:=Ы r hP have e0:(0:USize).toNat=0:=rfl have ep:s.pad.toUSize.toNat=s.pad:=qAd _ א have ew:s.w.toUSize.toNat=s.w:=qAd _ א have eW:(s.w+2*s.pad).toUSize.toNat=s.w+2*s.pad:=qAd _ hw have hF:∀ i,i< s.w+2*s.pad->D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i) :=λi hi=>qFr s X r hi have hz:∀ i,i< s.w+2*s.pad->(r%(s.h+2*s.pad)< s.pad∨i< s.pad ∨ r%(s.h+2*s.pad)-s.pad ≥ s.h∨i-s.pad ≥ s.w) → D_Fx s X (r*(s.w+2*s.pad)+i)=dv 0:=by intro i hi hc;rw[hF i hi,D_px,М hc] have hO:r%(s.h+2*s.pad)-s.pad< s.h->(r/(s.h+2*s.pad)*s.h + (r%(s.h+2*s.pad)-s.pad))*s.w+s.w<=X.size:=λg1=>by rw[hX];exact qGe (qEh hc g1) (le_refl _) have hstep:(r+1)*(s.w+2*s.pad)=r*(s.w+2*s.pad)+s.pad+s.w+s.pad:=by rw[т,ф];omega rw[D_img,Н hr] dsimp only split . rename_i hg;obtain ⟨g1,g2,g3⟩:=hg;have h1:=hO g2;apply ih (r+1) _ א;rw[hstep];generalize hm:r*(s.w+2*s.pad)=m at H hF hz have P1:=qBp (F:=D_Fx s X) (v:=dv 0) (n:=s.pad.toUSize) s.pad 0 m a (by rw[e0,ep,Й]) H (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1 exact hz u א (Or.inr (Or.inl א))) have P2:=qEd (F:=D_Fx s X) hs g3 s.w 0 _ _ (by rw[e0,ew,Й]) P1 (λj hj=>by rw[г m,hF _ א,D_px,Д א,e0,Э,qAd _ א,Nat.add_sub_cancel_left]) exact qBp (F:=D_Fx s X) (v:=dv 0) (n:=s.pad.toUSize) s.pad 0 _ _ (by rw[e0,ep,Й]) P2 (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1;rw[г m,г m] exact hz _ א (Or.inr (Or.inr (Or.inr א)))) . rename_i hg;apply ih (r+1) _ א;have hnr:r%(s.h+2*s.pad)< s.pad∨r%(s.h+2*s.pad)-s.pad ≥ s.h:=by by_contra hn;apply hg;have g1:r%(s.h+2*s.pad)-s.pad< s.h :=א;have h1:=hO g1;refine ⟨by omega,g1,?_⟩;rw[qAd _ א,ew];exact h1 rw[т,ф] exact qBp (F:=D_Fx s X) (v:=dv 0) (n:=(s.w+2*s.pad).toUSize) _ 0 _ _ (by rw[e0,eW,Й]) H (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1 exact hz u א א) def D_t0 (g:D_C) (o:Nat):Nat:=if g.dw then o*g.k else 0 def D_xw (g:D_C) (o y x t:Nat):Word:=windowAt g.s g.X y x (D_t0 g o+t) def D_out (g:D_C) (o y x:Nat):Word := act g.relu (dotOf (g.k+1) (λt=>if t< g.k then D_xw g o y x t else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) lemma qC2 (g:D_C) (o y x:Nat):D_slow g o y x=D_out g o y x:=by unfold D_slow D_out D_xw D_t0;rw[qAc] def D_B (g:D_C) (y x:Nat):Nat:=y*g.s.stride*g.wp+x*g.s.stride structure D_Ok (g:D_C):Prop where hk:g.k=if g.dw then g.s.kh*g.s.kw else g.s.cin*g.s.kh*g.s.kw hC:g.C=if g.dw then g.s.cin else g.s.cout ho:g.ho=g.s.ho wo:g.wo=g.s.wo wp:g.wp=g.s.w+2*g.s.pad st:g.step.toNat=g.s.stride nf:g.nf=nfOf g.k f4:g.f4=true->g.s.stride=1 p0:0< g.s.stride pk:0< g.k nk:g.k<2^20 kh:g.s.kh<=g.s.h+2*g.s.pad kw:g.s.kw<=g.s.w+2*g.s.pad xd:D_Pre (D_Fx g.s g.X) (g.s.cin*(g.s.h+2*g.s.pad)*(g.s.w+2*g.s.pad)) g.xd xs:g.xd.size<2^32 Hx:C_Hv g.X g.hi HW:∀ q:Nat,good g.W[q]! = true Hb:∀ q:Nat,good g.b[q]! = true sW:g.W.size=g.C*g.k sW2:g.W.size<2^32 sb:g.b.size<2^32 sN:g.C*(g.ho*g.wo)<2^32 wp2:g.wp<2^32 tb:g.tab=Array.ofFn (n:=g.k) λt=>(D_off g.s t.val).toUInt32 lemma qFh {A:Array Word} {h:Word}:good (0:Word)=true∧(0:Word) &&& 0x7FFFFFFF<=h:=by refine ⟨rfl,?_⟩;rw[Ж];exact Nat.le_trans (Nat.le_of_eq rfl) (Nat.zero_le _) lemma qGl {A:Array Word} (H:∀ k:Nat,k< A.size->good A[k]! = true) (q:Nat) : good A[q]! = true:=by by_cases h:q< A.size;. exact H q h . rw[б A q h];rfl lemma qCq {s:ConvShape} {X:Array Word} {hi:Word} (H:C_Hv X hi) (c yy xx:Nat) : good (D_px s X c yy xx)=true∧D_px s X c yy xx &&& 0x7FFFFFFF<=hi:=by unfold D_px;split;. exact qFh (A:=X) . rename_i q;generalize (c*s.h+(yy-s.pad))*s.w+(xx-s.pad)=n;by_cases h:n< X.size;. exact ⟨H.2.2.1 n h,H.2.2.2 n h⟩ . rw[б X n h];exact qFh (A:=X) lemma qHc {g:D_C} (ok:D_Ok g) {o t:Nat} (hC:o< g.C) (ht:t< g.k) : D_t0 g o+t< g.s.cin*(g.s.kh*g.s.kw):=by have h1:=ok.hk;have h2:=ok.hC;unfold D_t0;cases hd:g.dw;. simp only[hd,Bool.false_eq_true,if_false] at h1 h2 ⊢;rw[h1] at ht;rw[Й,<-Nat.mul_assoc];exact ht . simp only[hd,if_true] at h1 h2 ⊢;rw[h2] at hC;rw[h1] at ht ⊢;exact qEh hC ht lemma qGm {a b st n:Nat} (h0:0< st) (hk:b<=a) (hy:n<(a-b)/st+1) : n*st+b<=a:=by have h1:n<=(a-b)/st :=א;have h2:=З st h1;have h3:=Nat.div_mul_le_self (a-b) st;omega lemma qCe {g:D_C} (ok:D_Ok g) {o y x t:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo) (ht:t< g.k) : D_B g y x+D_off g.s (D_t0 g o+t)< g.xd.size ∧ g.xd.data[D_B g y x+D_off g.s (D_t0 g o+t)]! = dv (D_xw g o y x t):=by have hb:=qHc ok hC ht have hkk:0< g.s.kh*g.s.kw:=С (λe=>by rw[e] at hb;simp at hb) have hkh:0< g.s.kh:=Nat.pos_of_mul_pos_right hkk have hkw:0< g.s.kw:=з hkk have hY:=qGm ok.p0 ok.kh (by have:=hy;rw[ok.ho] at this;exact this) have hX:=qGm ok.p0 ok.kw (by have:=hx;rw[ok.wo] at this;exact this) unfold D_xw generalize D_t0 g o+t=u at hb ⊢ have hc:u/(g.s.kh*g.s.kw)< g.s.cin :=у (by rw[л];exact hb) have hdy:=Ы (u/g.s.kw) hkh have hdx:=Ы u hkw have e:D_B g y x+D_off g.s u=(u/(g.s.kh*g.s.kw)*(g.s.h+2*g.s.pad) + (y*g.s.stride+u/g.s.kw%g.s.kh))*(g.s.w+2*g.s.pad) + (x*g.s.stride+u%g.s.kw):=by unfold D_B D_off;rw[ok.wp];ring have hyy:y*g.s.stride+u/g.s.kw%g.s.kh< g.s.h+2*g.s.pad :=א have hxx:x*g.s.stride+u%g.s.kw< g.s.w+2*g.s.pad :=א rw[e] have hq:=qEh (qEh hc hyy) hxx refine ⟨by rw[ok.xd.1];exact hq,?_⟩ rw[ok.xd.2 _ hq,D_Fx,(qA3 _ hxx).1,(qA3 _ hxx).2,(qA3 _ hyy).1,(qA3 _ hyy).2] rfl lemma qFu {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+0< xd.size) (xf wf:Nat->Float) (L:List Nat):∀ (i:USize) (a t:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat]! = xf p ∧ wd.data[i.toNat+j]! = wf p) → D_goT xd wd off b e i he hw hs hb a t=L.foldl (λ(acc:F1) p =>let q:=xf p*wf p F1.mk (acc.s+q) (acc.t+q.abs)) ⟨a,t⟩:=by induction L with | nil => intro i a t h0 _ rw[D_goT,Л (by rw[А];simp at h0;omega)] rfl | cons p L ih => intro i a t h0 H simp only[List.length_cons] at h0 have ht:i< e:=by rw[А];omega have htn:i.toNat< e.toNat :=א have hit:i.toNat< off.size :=א obtain ⟨h1,h2⟩:=H 0 p rfl rw[Э] at h1 h2 have e1:off.uget i hit=off[i.toNat]!:=qEo _ _ _ have hbd:=hb i.toNat hit have e2:∀ pf,xd.uget (b+(off.uget i hit).toUSize) pf=xf p:=by intro pf have hbd':b.toNat+(off.uget i hit).toNat+0< xd.size:=hbd rw[qBm,qAl _ _ (by rw[О];omega),О,e1,h1] have e3:wd.uget i א=wf p:=by rw[qBm,h2] rw[D_goT,Н ht] dsimp only rw[e2,e3,ih (i+1) _ _ (by rw[usz i e ht];omega),List.foldl_cons] intro j q hq have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this lemma qH8 (a:FloatArray) (i:USize) (hs:a.size<2^32) (hi:i.toNat< a.size) : rdF a i=a.data[i.toNat]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize,qAd _ hs];exact hi unfold rdF;rw[Н h,qBm] lemma qDn {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+3< xd.size) (r:D_R) (xf:Nat->Nat->Float) (wf:Nat->Float) (L:List Nat):∀ (i:USize) (a0 a1 a2 a3:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->wd.data[i.toNat+j]! = wf p∧∀ d,d<4 → xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat+d]! = xf d p) → D_go4R xd wd off b e i he hw hs hb a0 a1 a2 a3 r = ⟨r.t,L.foldl (λacc p=>acc+xf 0 p*wf p) a0,L.foldl (λacc p=>acc+xf 1 p*wf p) a1,L.foldl (λacc p=>acc+xf 2 p*wf p) a2,L.foldl (λacc p=>acc+xf 3 p*wf p) a3⟩:=by induction L with | nil => intro i a0 a1 a2 a3 h0 _ rw[D_go4R,Л (by rw[А];simp at h0;omega)] cases r rfl | cons p L ih => intro i a0 a1 a2 a3 h0 H simp only[List.length_cons] at h0 have ht:i< e:=by rw[А];omega have htn:i.toNat< e.toNat :=א have hit:i.toNat< off.size :=א obtain ⟨h1,h2⟩:=H 0 p rfl rw[Э] at h1 h2 have e1:off.uget i hit=off[i.toNat]!:=qEo _ _ _ have hbd:b.toNat+(off.uget i hit).toNat+3< xd.size:=hb i.toNat hit have ep:(b+(off.uget i hit).toUSize).toNat=b.toNat+(off[i.toNat]! : UInt32).toNat:=by rw[qAl _ _ (by rw[О];omega),О,e1] rw[e1] at hbd have e2:∀ d,d<4->∀ pf,xd.uget ((b+(off.uget i hit).toUSize)+USize.ofNat d) pf=xf d p:=by intro d hd pf rw[qBm,qAl _ _ (by rw[ep,uk d א];omega),ep,uk d א,(h2 d hd)] have e0:∀ pf,xd.uget (b+(off.uget i hit).toUSize) pf=xf 0 p:=by intro pf rw[qBm,ep];exact h2 0 א have e3:wd.uget i א=wf p:=by rw[qBm,h1] rw[D_go4R,Н ht] dsimp only rw[e0,e2 1 א,e2 2 א,e2 3 א,e3] refine ih (i+1) _ _ _ _ (by rw[usz i e ht];omega) λj q hq=>?_ have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this lemma qFw {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+7< xd.size) (r:D_R8) (xf:Nat->Nat->Float) (wf:Nat->Float) (L:List Nat):∀ (i:USize) (a0 a1 a2 a3 a4 a5 a6 a7:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->wd.data[i.toNat+j]! = wf p∧∀ d,d<8 → xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat+d]! = xf d p) → D_go8R xd wd off b e i he hw hs hb a0 a1 a2 a3 a4 a5 a6 a7 r = ⟨r.t,L.foldl (λacc p=>acc+xf 0 p*wf p) a0,L.foldl (λacc p=>acc+xf 1 p*wf p) a1,L.foldl (λacc p=>acc+xf 2 p*wf p) a2,L.foldl (λacc p=>acc+xf 3 p*wf p) a3,L.foldl (λacc p=>acc+xf 4 p*wf p) a4,L.foldl (λacc p=>acc+xf 5 p*wf p) a5,L.foldl (λacc p=>acc+xf 6 p*wf p) a6,L.foldl (λacc p=>acc+xf 7 p*wf p) a7⟩:=by induction L with | nil => intro i a0 a1 a2 a3 a4 a5 a6 a7 h0 _ rw[D_go8R,Л (by rw[А];simp at h0;omega)] cases r rfl | cons p L ih => intro i a0 a1 a2 a3 a4 a5 a6 a7 h0 H simp only[List.length_cons] at h0 have ht:i< e:=by rw[А];omega have htn:i.toNat< e.toNat :=א have hit:i.toNat< off.size :=א obtain ⟨h1,h2⟩:=H 0 p rfl rw[Э] at h1 h2 have e1:off.uget i hit=off[i.toNat]!:=qEo _ _ _ have hbd:b.toNat+(off.uget i hit).toNat+7< xd.size:=hb i.toNat hit have ep:(b+(off.uget i hit).toUSize).toNat=b.toNat+(off[i.toNat]! : UInt32).toNat:=by rw[qAl _ _ (by rw[О];omega),О,e1] rw[e1] at hbd have e2:∀ d,d<8->∀ pf,xd.uget ((b+(off.uget i hit).toUSize)+USize.ofNat d) pf=xf d p:=by intro d hd pf rw[qBm,qAl _ _ (by rw[ep,uk d א];omega),ep,uk d א,(h2 d hd)] have e0:∀ pf,xd.uget (b+(off.uget i hit).toUSize) pf=xf 0 p:=by intro pf rw[qBm,ep];exact h2 0 א have e3:wd.uget i א=wf p:=by rw[qBm,h1] rw[D_go8R,Н ht] dsimp only rw[e0,e2 1 א,e2 2 א,e2 3 א,e2 4 א,e2 5 א,e2 6 א,e2 7 א,e3] refine ih (i+1) _ _ _ _ _ _ _ _ (by rw[usz i e ht];omega) λj q hq=>?_ have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this structure D_PO (c:D_P) (o:Nat) (L:List Nat):Prop where ok:D_Ok c.g hC:o< c.g.C len:c.e.toNat=L.length nd:L.Nodup lt:∀ t∈L,t< c.g.k hz:∀ t,t< c.g.k->t∉L->c.g.W[o*c.g.k+t]! &&& 0x7FFFFFFF=0 rd:∀ j p:Nat,L[j]?=some p → (c.off[j]! : UInt32).toNat=D_off c.g.s (D_t0 c.g o+p) ∧ c.wd.data[j]! = dv c.g.W[o*c.g.k+p]! e0:c.e0=nfOf c.g.k*(dv c.g.hi*L.foldl (λacc t=>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0 + (dv c.g.b[o]!).abs) bv:c.bv=dv c.g.b[o]! lemma qGj {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {j p:Nat} (hj:L[j]?=some p):p< c.g.k:=h.lt p (List.mem_of_getElem? hj) lemma qC3 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) : D_finT c o y x b=D_out c.g o y x:=by unfold D_finT;split . rename_i hl have e:=qFu c.he c.hw c.hs (c.h1 b hl) (λp=>dv (D_xw c.g o y x p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,Й];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj;rw[show (0:USize).toNat=0 from rfl,Й,h1.1,hb] exact ⟨(qCe h.ok h.hC hy hx (qGj h hj)).2,h1.2⟩) dsimp only;rw[c.hz,e,h.bv,h.ok.nf] by_cases hz:ziv ((L.foldl (λ(acc:F1) p =>let q:=dv (D_xw c.g o y x p)*dv c.g.W[o*c.g.k+p]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩).s+dv c.g.b[o]!) (nfOf c.g.k*((L.foldl (λ(acc:F1) p => let q:=dv (D_xw c.g o y x p)*dv c.g.W[o*c.g.k+p]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩).t+(dv c.g.b[o]!).abs))=0xFFFFFFFF . rw[М (by simpa using hz)];exact qC2 _ _ _ _ . rw[Д (by simpa using hz)];unfold D_out;congr 1;exact qDz c.g.k h.ok.nk (D_xw c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! (λt _=>(qCq h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) L h.nd h.lt (λt ht hn=>Or.inr (h.hz t ht hn)) _ _ ⟨rfl,rfl⟩ hz . exact qC2 _ _ _ _ lemma qBn {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) {a:Float} (ha:a=L.foldl (λacc t=>acc+dv (D_xw c.g o y x t)*dv c.g.W[o*c.g.k+t]!) 0) : D_fin c o y x b a=D_out c.g o y x:=by unfold D_fin;dsimp only;rw[h.bv,h.e0];by_cases hz:ziv (a+dv c.g.b[o]!) (nfOf c.g.k*(dv c.g.hi*L.foldl (λacc t =>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0+(dv c.g.b[o]!).abs))=0xFFFFFFFF;. rw[М (by simpa using hz)];exact qC3 h hy hx hb . rw[Д (by simpa using hz)];unfold D_out;congr 1;exact qDl c.g.k h.ok.nk (D_xw c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! c.g.hi (λt _=>(qCq h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) h.ok.Hx.1 (λt _=>(qCq h.ok.Hx _ _ _).2) h.ok.Hx.2.1 L h.nd h.lt (λt ht hn=>Or.inr (h.hz t ht hn)) _ _ ⟨by rw[ha],rfl⟩ hz def D_sp (g:D_C) (q:Nat):Word := D_out g (q/(g.ho*g.wo)) (q/g.wo%g.ho) (q%g.wo) lemma qD4 (g:D_C) {o y x:Nat} (hy:y< g.ho) (hx:x< g.wo) : D_out g o y x=D_sp g ((o*g.ho+y)*g.wo+x):=by unfold D_sp;rw[л g.ho g.wo,<-я,(qA3 _ hx).1,(qA3 _ hx).2,(qA3 _ hy).1,(qA3 _ hy).2] lemma qFt {g:D_C} (ok:D_Ok g) {o y x:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo):D_B g y x<2^32:=by have h:=(qCe ok hC hy hx ok.pk).1;have:=ok.xs;omega def D_sum (c:D_P) (o:Nat) (L:List Nat) (y x:Nat):Float := L.foldl (λacc p=>acc+dv (D_xw c.g o y x p)*dv c.g.W[o*c.g.k+p]!) 0 lemma qFz {g:D_C} (ok:D_Ok g) (hf:g.f4=true) (y x k:Nat) : D_B g y (x+k)=D_B g y x+k:=by have st1:=ok.f4 hf;unfold D_B;rw[st1];simp only[Nat.mul_one];omega lemma qCy {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hf:c.g.f4=true) (hb:b.toNat=D_B c.g y x) {k:Nat} (hk:x+k< c.g.wo):(b+USize.ofNat k).toNat=D_B c.g y (x+k):=by have hB:=qFz h.ok hf y x k;have hl:=qFt h.ok h.hC hy hk;rw[hB] at hl ⊢;rw[qAl _ _ (by rw[uk k א,hb];exact hl),hb,uk k א] lemma qCs {N Q x k:Nat} {q:USize} (hq:q.toNat=Q+x) (hN:Q+N<=2^32-1) (hk:x+k<=N):(q+USize.ofNat k).toNat=Q+(x+k):=by rw[qAl _ _ (by rw[uk k א,hq];omega),hq,uk k א];omega lemma qEu {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x w:Nat} {b:USize} (hy:y< c.g.ho) (hx:x+w<=c.g.wo) (hf:c.g.f4=true) (hb:b.toNat=D_B c.g y x) (j p:Nat) (hj:L[j]?=some p) : c.wd.data[(0:USize).toNat+j]! = dv c.g.W[o*c.g.k+p]!∧∀ d,d< w → c.g.xd.data[b.toNat+(c.off[(0:USize).toNat+j]! : UInt32).toNat+d]! =dv (D_xw c.g o y (x+d) p):=by have h1:=h.rd j p hj rw[show (0:USize).toNat=0 from rfl,Й,h1.1] refine ⟨h1.2,λk hk=>?_⟩ rw[show b.toNat+D_off c.g.s (D_t0 c.g o+p)+k =D_B c.g y (x+k)+D_off c.g.s (D_t0 c.g o+p) by rw[qFz h.ok hf,hb];omega] exact (qCe h.ok h.hC hy א (qGj h hj)).2 lemma qBx {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y Q:Nat} (hy:y< c.g.ho) (hQ:Q=(o*c.g.ho+y)*c.g.wo) (hN:Q+c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) {x:Nat} {b q:USize} {out:Array Word} (hx:x+4<=c.g.wo) (hf:c.g.f4=true) (hb:b.toNat=D_B c.g y x) (hq:q.toNat=Q+x) (H:C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+x) out) : C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+(x+4)) (D_w4 c o y x b q (D_sum c o L y x) (D_sum c o L y (x+1)) (D_sum c o L y (x+2)) (D_sum c o L y (x+3)) out):=by have sN:=h.ok.sN have hsp:∀ x',x'< c.g.wo->D_out c.g o y x'=D_sp c.g (Q+x'):=by intro x' hx';rw[hQ];exact qD4 c.g hy hx' have hN':Q+c.g.wo<=2^32-1 :=א have a1:(b+1).toNat=D_B c.g y (x+1):=qCy h hy hf hb (k:=1) א have a2:(b+2).toNat=D_B c.g y (x+2):=qCy h hy hf hb (k:=2) א have a3:(b+3).toNat=D_B c.g y (x+3):=qCy h hy hf hb (k:=3) א have o0:=qBn h hy (א:x< c.g.wo) hb (a:=D_sum c o L y x) rfl have o1:=qBn h hy (א:x+1< c.g.wo) a1 (a:=D_sum c o L y (x+1)) rfl have o2:=qBn h hy (א:x+2< c.g.wo) a2 (a:=D_sum c o L y (x+2)) rfl have o3:=qBn h hy (א:x+3< c.g.wo) a3 (a:=D_sum c o L y (x+3)) rfl have q1:(q+1).toNat=Q+x+1:=by have e:(q+1).toNat=Q+(x+1):=qCs hq hN' (k:=1) א omega have q2:(q+2).toNat=Q+x+1+1:=by have e:(q+2).toNat=Q+(x+2):=qCs hq hN' (k:=2) א omega have q3:(q+3).toNat=Q+x+1+1+1:=by have e:(q+3).toNat=Q+(x+3):=qCs hq hN' (k:=3) א omega have s1:D_out c.g o y (x+1)=D_sp c.g (Q+x+1):=by rw[г];exact hsp _ א have s2:D_out c.g o y (x+2)=D_sp c.g (Q+x+1+1):=by rw[show Q+x+1+1=Q+(x+2) by omega];exact hsp _ א have s3:D_out c.g o y (x+3)=D_sp c.g (Q+x+1+1+1):=by rw[show Q+x+1+1+1=Q+(x+3) by omega];exact hsp _ א unfold D_w4 dsimp only rw[o0,o1,o2,o3] rw[show Q+(x+4)=Q+x+1+1+1+1 by omega] exact qAr sN (qAr sN (qAr sN (qAr sN H hq א (hsp _ א)) q1 א s1) q2 א s2) q3 א s3 lemma qE5 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y Q:Nat} (hy:y< c.g.ho) (hQ:Q=(o*c.g.ho+y)*c.g.wo) (hN:Q+c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:Nat) : ∀ (x:Nat) (b q:USize) (r8:D_R8) (rr:D_R) (out:Array Word),x+d=c.g.wo → (x< c.g.wo->b.toNat=D_B c.g y x)->q.toNat=Q+x → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+x) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+c.g.wo) (D_cols c o y x b q r8 rr out):=by induction d using Nat.strong_induction_on with | _ d ih => intro x b q r8 rr out he hb hq H have sN:=h.ok.sN have hN':Q+c.g.wo<=2^32-1 :=א have hsp:∀ x',x'< c.g.wo->D_out c.g o y x'=D_sp c.g (Q+x'):=by intro x' hx';rw[hQ];exact qD4 c.g hy hx' by_cases h8:x+8<=c.g.wo∧c.g.f4=true∧b< c.l8 . have hb0:=hb א;have R:=qFw c.he c.hw c.hs (c.h8 b h8.2.2) r8 (λk p=>dv (D_xw c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 0 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,Й];exact h.len.symm) (qEu h hy h8.1 h8.2.1 hb0);have w1:=qBx h hy hQ hN (x:=x) א h8.2.1 hb0 hq H;have w2:=qBx h hy hQ hN (x:=x+4) א h8.2.1 (qCy h hy h8.2.1 hb0 (k:=4) א) (qCs hq hN' (k:=4) א) w1;rw[D_cols,Н h8];dsimp only;rw[c.hz,R];dsimp only;refine ih (d-8) א (x+8) _ _ _ _ _ א ?_ ?_ w2;. exact λhx=>qCy h hy h8.2.1 hb0 (k:=8) hx . exact qCs hq hN' (k:=8) א by_cases h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 . have hb0:=hb א;have R:=qDn c.he c.hw c.hs (c.h4 b h4.2.2) rr (λk p=>dv (D_xw c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,Й];exact h.len.symm) (qEu h hy h4.1 h4.2.1 hb0);rw[D_cols,Л h8,Н h4];dsimp only;rw[c.hz,R];dsimp only;refine ih (d-4) א (x+4) _ _ _ _ _ א ?_ ?_ (qBx h hy hQ hN h4.1 h4.2.1 hb0 hq H);. exact λhx=>qCy h hy h4.2.1 hb0 (k:=4) hx . exact qCs hq hN' (k:=4) א . by_cases h2:x+2<=c.g.wo∧c.g.wo<=x+4∧4<=c.g.wo∧c.g.f4=true ∧ b-(x+4-c.g.wo).toUSize< c.l4 . have hx2:=h2.1;have hw4:=h2.2.1;have hw5:=h2.2.2.1;have hf:=h2.2.2.2.1;have hl:=h2.2.2.2.2;have hb0:=hb א;have hB:=qFz h.ok hf y (c.g.wo-4) (x+4-c.g.wo);rw[show c.g.wo-4+(x+4-c.g.wo)=x by omega] at hB;have eS:(x+4-c.g.wo).toUSize.toNat=x+4-c.g.wo:=qAd _ א;have hb1:(b-(x+4-c.g.wo).toUSize).toNat=D_B c.g y (c.g.wo-4):=by rw[USize.toNat_sub_of_le _ _ (USize.le_iff_toNat_le.2 (by rw[eS,hb0];omega)),eS,hb0];omega have hq1:(q-(x+4-c.g.wo).toUSize).toNat=Q+(c.g.wo-4):=by rw[USize.toNat_sub_of_le _ _ (USize.le_iff_toNat_le.2 (by rw[eS,hq];omega)),eS,hq];omega have R:=qDn c.he c.hw c.hs (c.h4 _ hl) rr (λk p=>dv (D_xw c.g o y (c.g.wo-4+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,Й];exact h.len.symm) (qEu h hy (x:=c.g.wo-4) א hf hb1);have W:=qBx h hy hQ hN (x:=c.g.wo-4) א hf hb1 hq1 (qCo H λp _ (hp:p< Q+(c.g.wo-4))=>show p< Q+x by omega);rw[(א:c.g.wo-4+4=c.g.wo)] at W;rw[D_cols,Л h8,Л h4,Н h2];dsimp only;rw[c.hz,R];exact W by_cases h1:x< c.g.wo . have hb0:=hb h1;rw[D_cols,Л h8,Л h4,Л h2,М h1];refine ih (d-1) א (x+1) _ _ _ _ _ א ?_ ?_ ?_ . intro hx;have hbb:=qFt h.ok h.hC hy hx;have e:D_B c.g y (x+1)=D_B c.g y x+c.g.s.stride:=by unfold D_B;rw[т,ф];omega rw[e] at hbb ⊢;rw[qAl _ _ (by rw[hb0,h.ok.st];exact hbb),hb0,h.ok.st] . rw[qAl _ _ (by rw[ч,hq];omega),hq,ч];omega . rw[qC3 h hy h1 hb0];exact qAr sN H hq א (hsp _ h1) . rw[D_cols,Л h8,Л h4,Л h2,Д h1];have e:x=c.g.wo :=א;rw[e] at H;exact H lemma qFo {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) (hN:(o*c.g.ho+c.g.ho)*c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:Nat) : ∀ (y:Nat) (q:USize) (out:Array Word),y+d=c.g.ho → q.toNat=(o*c.g.ho+y)*c.g.wo → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (D_rows c o y q out):=by induction d with | zero => intro y q out he _ H rw[D_rows,Д א] rw[(א:y=c.g.ho)] at H;exact H | succ d ih => intro y q out he hq H have hy:y< c.g.ho :=א have sN:=h.ok.sN have hwo:0< c.g.wo:=by rw[h.ok.wo];exact ر _ have hA:(o*c.g.ho+(y+1))*c.g.wo<=(o*c.g.ho+c.g.ho)*c.g.wo :=З _ א have hA2:(o*c.g.ho+(y+1))*c.g.wo=(o*c.g.ho+y)*c.g.wo+c.g.wo:=by rw[<-г,т,ф] have hY:y<=(o*c.g.ho+y)*c.g.wo:=Nat.le_trans א (ж _ hwo) have eW:c.g.wo.toUSize.toNat=c.g.wo:=qAd _ א have eY:y.toUSize.toNat=y:=qAd _ א have bB:=qFt h.ok h.hC hy hwo have hwp:0< c.g.wp:=by have:=h.ok.wp have hkw:0< c.g.s.kw:=by have hb:=qHc h.ok h.hC h.ok.pk have hkk:0< c.g.s.kh*c.g.s.kw:=С (λe=>by rw[e] at hb;simp at hb) exact з hkk have:=h.ok.kw omega have e1:D_B c.g y 0=y*c.g.s.stride*c.g.wp:=by unfold D_B;simp rw[e1] at bB have hS:y*c.g.s.stride<=y*c.g.s.stride*c.g.wp:=ж _ hwp have eS:(y.toUSize*c.g.step).toNat=y*c.g.s.stride:=by rw[qE8 _ _ (by rw[eY,h.ok.st];omega),eY,h.ok.st] have eP:c.g.wp.toUSize.toNat=c.g.wp:=qAd _ h.ok.wp2 have ebase:(y.toUSize*c.g.step*c.g.wp.toUSize).toNat=D_B c.g y 0:=by rw[qE8 _ _ (by rw[eS,eP];exact bB),eS,eP,e1] rw[D_rows,М hy] refine ih (y+1) _ _ א ?_ ?_ . rw[qAl _ _ (by rw[eW,hq];omega),hq,eW,hA2] . rw[hA2];exact qE5 h hy rfl א c.g.wo 0 _ _ _ _ _ (Й _) (λ_=>ebase) (by rw[Э];exact hq) (by rw[Э];exact H) lemma qEr {g:D_C} {c:D_P} {o:Nat} {L:List Nat} (hg:c.g=g) (h:D_PO c o L) (hN:(o*g.ho+g.ho)*g.wo<=g.C*(g.ho*g.wo)) {q:USize} {out:Array Word} (hq:q.toNat=(o*g.ho+0)*g.wo) (H:C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p<(o*g.ho+0)*g.wo) out) : C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p<(o*g.ho+g.ho)*g.wo) (D_rows c o 0 q out):=by subst hg exact qFo h hN c.g.ho 0 q out (Й _) hq H lemma qGx (s:ConvShape) (o:Nat) {p:Nat} (hp:p< s.kh*s.kw) : D_off s (o*(s.kh*s.kw)+p) =o*((s.h+2*s.pad)*(s.w+2*s.pad))+D_off s p:=by have hkk:0< s.kh*s.kw :=א have hkh:0< s.kh:=Nat.pos_of_mul_pos_right hkk have hkw:0< s.kw:=з hkk have e1:(o*(s.kh*s.kw)+p)/s.kw=o*s.kh+p/s.kw:=by rw[<-Nat.mul_assoc,Nat.add_comm,Nat.add_mul_div_right _ _ hkw,Nat.add_comm] have e2:(o*(s.kh*s.kw)+p)%s.kw=p%s.kw:=by rw[<-Nat.mul_assoc,Nat.add_comm,Nat.add_mul_mod_self_right] unfold D_off rw[(qA3 o hp).1,п hp,e1,e2,Nat.add_comm (o*s.kh),Nat.add_mul_mod_self_right] ring lemma qIe (a b:Nat):a.toUInt32+b.toUInt32=(a+b).toUInt32 :=(UInt32.ofNat_add a b).symm lemma qHp {g:D_C} (ok:D_Ok g) (o:Nat) {p:Nat} (hp:p< g.k) {u:UInt32} (hu:u.toNat=p) : (if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0)+rdW g.tab u.toUSize =(D_off g.s (D_t0 g o+p)).toUInt32:=by have hz:g.tab.size=g.k:=by rw[ok.tb];simp have e:g.tab[p]! = (D_off g.s p).toUInt32:=by rw[ok.tb,Е _ p (by simpa using hp),Array.getElem_ofFn] rw[qAt _ _ (by have:=ok.nk;omega),О,hu,e] have h1:=ok.hk unfold D_t0 cases hd:g.dw . simp only[Bool.false_eq_true,if_false,UInt32.zero_add,Nat.zero_add] . simp only[hd,if_true] at h1 ⊢;rw[h1,qGx g.s o (by rw[<-h1];exact hp),ok.wp,qIe] def D_tv (g:D_C) (o:Nat) (L:List Nat) (j:Nat):Word := (D_off g.s (D_t0 g o+L[j]?.getD 0)).toUInt32 lemma qF9 {g:D_C} (ok:D_Ok g) (o:Nat) {ix:Array UInt32} {buf:FloatArray} {e:USize} {L:List Nat} {N:Nat} (wf:Nat->Float) (hk:∀ p∈L,p< g.k) (hN:N<2^32) (hix:ix.size<2^32) (hbuf:buf.size<2^32) (hL:e.toNat=L.length) (he:e.toNat<=buf.size) (heN:e.toNat<=N) (hr:∀ j p:Nat,L[j]?=some p->(ix[j]! : UInt32).toNat=p∧buf.data[j]! = wf p) (d:Nat) : ∀ (i:USize) (off:Array UInt32) (a:Float),i.toNat+d=e.toNat → C_Inv N (D_tv g o L) (λq=>q< i.toNat) off → C_Inv N (D_tv g o L) (λq=>q< e.toNat) (D_tap g o ix buf e i off a).off ∧ (D_tap g o ix buf e i off a).a =(L.drop i.toNat).foldl (λacc t=>acc+(wf t).abs) a:=by induction d with | zero => intro i off a h0 H rw[D_tap,Л (by rw[А];omega)] rw[(א:i.toNat=e.toNat)] at H ⊢ exact ⟨H,by rw[List.drop_eq_nil_of_le א];rfl⟩ | succ d ih => intro i off a h0 H have ht:i< e:=by rw[А];omega have hl:i.toNat< L.length :=א have hp:L[i.toNat]?=some L[i.toNat]:=List.getElem?_eq_getElem hl obtain ⟨h1,h2⟩:=hr _ _ hp have hi2:=ih (i+1) (wrW off i (D_off g.s (D_t0 g o+L[i.toNat])).toUInt32) (a+(wf L[i.toNat]).abs) (by rw[usz i e ht];omega) (by rw[usz i e ht] exact qAr hN H rfl א (by rw[D_tv,hp];rfl)) rw[D_tap,Н ht,qAt _ _ hix,qHp ok o (hk _ (List.getElem_mem hl)) h1,qH8 _ _ hbuf א,h2,List.drop_eq_getElem_cons hl,List.foldl_cons] rw[usz i e ht] at hi2 exact hi2 lemma qFf {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) (d:Nat) : ∀ (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array Word),o+d=g.C->q.toNat=o*(g.ho*g.wo)->ix.size=g.k->off.size=g.k->buf.size=g.k → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< o*(g.ho*g.wo)) out → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< g.C*(g.ho*g.wo)) (D_outs g hs o q ix off buf out):=by induction d with | zero => intro o q ix off buf out he _ _ _ _ H rw[D_outs,Д א] rw[(א:o=g.C)] at H;exact H | succ d ih => intro o q ix off buf out he hq h1 h2 h3 H have hC:o< g.C :=א have hkn:g.k<2^32:=lt_trans ok.nk ב have sW:=ok.sW have sW2:=ok.sW2 have sN:=ok.sN have hP:o*g.k+g.k<=g.W.size:=by rw[sW];exact qGe hC (le_refl _) have eo:(o*g.k).toUSize.toNat=o*g.k:=qAd _ א have ek:g.k.toUSize.toNat=g.k:=qAd _ hkn have hO:(o+1)*(g.ho*g.wo)<=g.C*(g.ho*g.wo):=З _ hC have hO2:(o+1)*(g.ho*g.wo)=o*(g.ho*g.wo)+g.ho*g.wo:=by rw[т,ф] have hpos:0< g.ho*g.wo:=by rw[ok.ho,ok.wo];exact Nat.mul_pos (ر _) (ر _) have eH:(g.ho*g.wo).toUSize.toNat=g.ho*g.wo:=qAd _ א have eOo:o.toUSize.toNat=o:=by have:o<=o*(g.ho*g.wo):=ж _ hpos exact qAd _ א have D:=qHu (X:=g.W) (o:=(o*g.k).toUSize) (n:=g.k.toUSize) (λp=>g.W[o*g.k+p]!) (by rw[ek];exact hkn) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAt _ _ sW2,qAl _ _ (by rw[eo];omega),eo]) (λt _=>ok.HW _) g.k 0 ix buf 0 0 (by rw[show (0:USize).toNat=0 from rfl,ek,Й]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h3,λk p hk=>by simp[C_keep] at hk,rfl,λk hk=>absurd hk (Nat.not_lt_zero _),(by decide)⟩ rw[ek] at D obtain ⟨d1,d2,d3,d4,-⟩:=D rw[D_outs,М hC] dsimp only have hlen:=qCh (λp=>g.W[o*g.k+p]!) g.k have T:=qF9 ok o (N:=g.k) (L:=C_keep (λp=>g.W[o*g.k+p]!) g.k) (λp=>dv g.W[o*g.k+p]!) (λt ht=>к.mp (س.mp ht).1) hkn א א d1 א א d4 (C_keep (λp=>g.W[o*g.k+p]!) g.k).length 0 off 0 (by rw[show (0:USize).toNat=0 from rfl,Й];exact d1.symm) ⟨h2,λq _ hq=>absurd hq (Nat.not_lt_zero _)⟩ obtain ⟨⟨T1,T3⟩,T2⟩:=T rw[Н (by rw[T1,d3,d1];exact ⟨hlen,hlen⟩)] refine ih (o+1) _ _ _ _ _ א ?_ d2 T1 d3 ?_ . rw[qAl _ _ (by rw[eH,hq];omega),hq,eH,hO2] . have E:(o+1)*(g.ho*g.wo)=(o*g.ho+g.ho)*g.wo:=by ring rw[E] refine qEr (L:=C_keep (λp=>g.W[o*g.k+p]!) g.k) rfl ⟨ok,hC,d1,(List.nodup_range).filter _,λt ht=>к.mp (س.mp ht).1,λt ht hn=>?_,λj p hj=>?_,?_,?_⟩ (by rw[<-E];exact hO) (by rw[Э,hq];ring) (by rw[show (o*g.ho+0)*g.wo=o*(g.ho*g.wo) by ring];exact H) . by_contra hc;exact hn (س.mpr ⟨к.mpr ht,by simpa using hc⟩) . have hjl:j<(C_keep (λp=>g.W[o*g.k+p]!) g.k).length :=(List.getElem?_eq_some_iff.mp hj).1;have hpk:p< g.k:=к.mp (س.mp (List.mem_of_getElem? hj)).1;have e1:=T3 j א א;rw[D_tv,hj] at e1;have e2:=(qCe ok hC (y:=0) (x:=0) (by rw[ok.ho];exact ر _) (by rw[ok.wo];exact ر _) hpk).1;have e3:D_off g.s (D_t0 g o+p)<2^32:=by have:=ok.xs;omega refine ⟨?_,(d4 j p hj).2⟩;rw[e1];simpa using Nat.mod_eq_of_lt e3 . rw[show (0:USize).toNat=0 from rfl,List.drop_zero] at T2;show g.nf*(dv g.hi*_+(dv (rdW g.b o.toUSize)).abs)=_;rw[qAt _ _ ok.sb,eOo,ok.nf];exact congrArg (λa=>nfOf g.k*(dv g.hi*a+(dv g.b[o]!).abs)) T2 . show dv (rdW g.b o.toUSize)=_;rw[qAt _ _ ok.sb,eOo] lemma qE9 (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi = if 0x007FFFFF<=(D_scan A n i hn lo hi).lo∧(D_scan A n i hn lo hi).hi<0x7F800000 then (D_scan A n i hn lo hi).hi else 0xFFFFFFFF:=by induction d with | zero => intro i lo hi he have h:¬ i< n:=by rw[А];omega rw[scan,D_scan] simp only[dite_eq_right h] | succ d ih => intro i lo hi he have h:i< n:=by rw[А];omega rw[scan,D_scan] simp only[dite_eq_left h] exact ih (i+1) _ _ (by rw[usz i n h];omega) lemma qG7 {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) : D_outs g hs 0 0 (Array.replicate g.k 0) (Array.replicate g.k 0) (pushZ g.k.toUSize 0 (FloatArray.emptyWithCapacity g.k)) (Array.replicate (g.C*(g.ho*g.wo)) 0) =if g.dw then depthwiseSpec g.s g.relu g.X g.W g.b else conv2dSpec g.s g.relu g.X g.W g.b:=by have e:=qAd g.k (lt_trans ok.nk ב) refine (qHw (qFf ok hs g.C 0 0 _ _ _ _ (Й _) (by rw[Nat.zero_mul];rfl) ג ג ?_ ⟨by simp,λq _ h=>?_⟩) (λq h=>h)).trans ?_ . rw[qG2 g.k.toUSize g.k 0 _ (by rw[e,show (0:USize).toNat=0 from rfl,Й])];exact Й _ . rw[Nat.zero_mul] at h;exact absurd h (Nat.not_lt_zero _) . obtain ⟨dw,relu,f4,s,X,W,b,k,C,ho,wo,wp,step,hi,xd,nf,tab⟩:=g;have h1:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw:=ok.hk;have h2:C=if dw then s.cin else s.cout:=ok.hC;have h3:ho=s.ho:=ok.ho;have h4:wo=s.wo:=ok.wo;subst h1 h2 h3 h4;cases dw;. refine congrArg (Array.ofFn (n:=s.cout*(s.ho*s.wo))) (funext λq=>?_);simp[D_sp,D_out,D_xw,D_t0] . refine congrArg (Array.ofFn (n:=s.cin*(s.ho*s.wo))) (funext λq=>?_);simp[D_sp,D_out,D_xw,D_t0] lemma qE4 (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) {k C:Nat} (hk:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw) (hC:C=if dw then s.cin else s.cout) : D_F dw s relu X W b k C =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by have hfb:(if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b) =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by rw[qAc];rfl unfold D_F dsimp only rw[<-hfb] generalize (if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b)=R at hfb ⊢ split . rename_i hg;split . rename_i hs;obtain ⟨g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20⟩:=hg;have e:=qE9 X X.usize (qAj X) (X.usize.toNat) 0 0xFFFFFFFF 0 (by rw[show (0:USize).toNat=0 from rfl,Й]);rw[М ⟨g2,g3⟩] at e;have Hx:=qAz X g1 (by rw[e];intro h;rw[h] at g3;exact absurd g3 (by decide));rw[e] at Hx;refine (qG7 ?_ _).trans hfb.symm exact ⟨hk,hC,rfl,rfl,rfl,qAd _ g8,rfl,λh=>beq_iff_eq.mp (с.mp h).1,g7,g9,g10,g11,g12, qE6 s X g1 g4 g13 g14 rfl _ 0 _ (Й _) ⟨by rw[Nat.zero_mul];rfl,λq hq=>by rw[Nat.zero_mul] at hq;omega⟩, hs,Hx,qGl (qAz W g15 g19).2.2.1,qGl (qAz b g16 g20).2.2.1, g5,g15,g16,g17,g14,rfl⟩ . rfl . rfl lemma qCg:convF=conv2dSpec:=by funext s relu X W b;exact qE4 false s relu X W b rfl rfl lemma qDv:depthF=depthwiseSpec:=by funext s relu X W b;exact qE4 true s relu X W b rfl rfl end @[inline] def sm (w:UInt32):Bool:=good w && (w &&& 0x7FFFFFFF)<=0x70FFFFFF section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat lemma qHy (s:Sign) (m:Nat) (e T:Int) (K r:Nat) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:Nat)) (he:(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)).roundedMantissa=r) : UnpackedFloat.round Format.binary32 s m e =let p:=shiftToTargetExponent Format.binary32 r T .exact if h:p.1.mantissa=0 then .zero s else .finite s p.1.mantissa p.2 (С h):=by unfold UnpackedFloat.round decreaseExponent roundWithAccuracy simp only[hT] have h0:(e-T).toNat=0 :=א have hs:shiftToTargetExponent Format.binary32 m e .exact =(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1),T):=by unfold shiftToTargetExponent shiftToExponent simp only[hT] have h3:(T-e).toNat=K+1 :=א rw[h3,hK] simp only[h0,Nat.shiftLeft_zero,Int.natCast_zero,Int.sub_zero,hs,he] def T_r (N:Nat):Nat := N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma qIn (N:Nat) : (ExtendedMantissa.ofMantissaAndAccuracy N .exact >>> (38+1)).roundedMantissa=T_r N:=by rw[qEa];have d:=Nat.div_add_mod N (2^39);have l:N%2^39<2^39:=Ы _ ב;unfold T_r;simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven];generalize hb:(N/274877906944%2 == 1)=b;generalize hc:(!decide (N%274877906944=0))=c;cases b<;>cases c<;>simp at hb hc ⊢<;>(try split_ifs)<;>omega lemma qHq (neg:Bool) (N:Nat) (e':Int) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253) : toWord (Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) N e')) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by have hr1:2^23<=T_r N:=by unfold T_r;have:2^23<=N/2^39 :=א split<;>omega have hr2:T_r N<=2^24:=by unfold T_r;have:N/2^39<2^24 :=א split<;>omega have hL:N.log2=62:=qBh N 62 hN1 hN2 have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,hL,qBw];omega rw[qHy _ N e' _ 38 _ hT (by push_cast;ring) (qIn N)] by_cases hc:T_r N=2^24 . rw[hc,show e'+39=e'+40-1 by ring,qD3 _ (e'+40) א (by norm_num:0<2^23)];simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[qAx _ _ _ _ ב ב א א];have e:(e'+40+150).toNat=(e'+40-1+149).toNat+2 :=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega . have hp:0< T_r N :=א;rw[qCu _ _ _ hr1 א א hp];simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[qAx _ _ _ _ hr1 א א א];have e:(e'+39+150).toNat=(e'+39+149).toNat+1 :=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega @[inline] def T_rd (neg:Bool) (mag e0 L:UInt64):UInt32 := let n:=mag <<< (62-L) let q:=n >>> 39 let rm:=n &&& 0x7FFFFFFFFF let w:=((e0+L-24) <<< 23)+q + (if rm > 0x4000000000 || (rm == 0x4000000000 && q &&& 1 == 1) then 1 else 0) (if neg then w ||| 0x80000000 else w).toUInt32 lemma qGw (neg:Bool) (mag e0:UInt64) (h0:mag.toNat≠0) (h1:mag.toNat<2^63) (h2:24< e0.toNat+mag.toNat.log2) (h3:e0.toNat+mag.toNat.log2<277) : T_rd neg mag e0 mag.log2=toWord (Model.roundDyadic Fmt ⟨neg,mag.toNat,(e0.toNat:Int)-150⟩):=by have l0:2^mag.toNat.log2<=mag.toNat:=Nat.log2_self_le h0;have l1:mag.toNat<2^(mag.toNat.log2+1):=Nat.lt_log2_self;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt h0).2 h1;omega obtain ⟨L,hLd⟩:∃ L,L=mag.toNat.log2:=⟨_,rfl⟩;rw[<-hLd] at l0 l1 hL h2 h3;have eL:mag.log2.toNat=L:=by rw[hLd];rfl obtain ⟨N,hN⟩:∃ N,N=mag.toNat*2^(62-L):=⟨_,rfl⟩;have p:2^L*2^(62-L)=2^62:=by rw[<-х];congr 1;omega have hN1:2^62<=N:=by rw[hN,<-p];exact З _ l0 have hN2:N<2^63:=by have:mag.toNat*2^(62-L)<2^(L+1)*2^(62-L) :=Nat.mul_lt_mul_of_pos_right l1 (by positivity);rw[hN] calc _< _:=this _=2^63:=by rw[<-х];congr 1;omega rw[qA2,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qHq neg N _ hN1 hN2 א א];have eT:((e0.toNat:Int)-150-((62-L:Nat):Int)+39+149).toNat=e0.toNat+L-24:=by omega rw[eT];unfold T_rd;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[eL];exact hL)),eL];rfl have a1:(mag <<< (62-mag.log2)).toNat=N:=by rw[UInt64.toNat_shiftLeft,e1,Г (by omega:62-L<64),р,<-hN,Г א] generalize mag <<< (62-mag.log2)=n at a1;have a2:(n >>> 39).toNat=N/2^39:=by rw[й,a1];simp[Nat.shiftRight_eq_div_pow] have a3:(n &&& 0x7FFFFFFFFF).toNat=N%2^39:=by rw[э,a1];exact В N 39 generalize n >>> 39=q at a2;generalize n &&& 0x7FFFFFFFFF=rm at a3;have a4:(q &&& 1).toNat=q.toNat%2:=by rw[э];exact В _ 1 have a5:(e0+mag.log2-24).toNat=e0.toNat+L-24:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[Ъ,eL,Г א];show 24<=_;omega)), Ъ,eL,Г א] rfl have a6:((e0+mag.log2-24) <<< 23).toNat=(e0.toNat+L-24)*2^23:=by rw[UInt64.toNat_shiftLeft,a5,р];show _*2^23%_=_;rw[Г א] generalize (e0+mag.log2-24) <<< 23=hi at a6;have hup:(decide (rm > 0x4000000000) || (rm == 0x4000000000 && q &&& 1 == 1)) =decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=by apply Bool.eq_iff_iff.2;simp only[Bool.or_eq_true,Bool.and_eq_true,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup] have hw:(hi+q+(if decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1))=true then (1:UInt64) else 0)).toNat=(e0.toNat+L-24)*2^23+T_r N:=by unfold T_r;simp only[decide_eq_true_eq];have hq:N/2^39<2^24 :=א;have b1:(hi+q).toNat=(e0.toNat+L-24)*2^23+N/2^39:=by rw[Ъ,a6,a2];exact Г א by_cases hc:2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1);. rw[if_pos hc,if_pos hc,Ъ,b1];show (_+1)%2^64=_;rw[Г א];omega . rw[if_neg hc,if_neg hc,Ъ,b1];show (_+0)%2^64=_;rw[Г א];omega generalize (hi+q+(if decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1))=true then (1:UInt64) else 0))=w at hw have hw2:w.toNat<2^31:=by have:T_r N<=2^24:=by unfold T_r;have:N/2^39<2^24 :=א;split<;>omega omega apply И.mp;rw[ш];cases neg;. simp only[Bool.false_eq_true,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:w.toNat ||| (2147483648:UInt64).toNat=2^31+w.toNat:=by have:=щ hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma qIo (w:UInt32) (h:(w &&& 0x7FFFFFFF)=0):rationalWord w=0:=by have h1:=qA1 w;rw[h] at h1;have h2:=w.toNat_lt have h3:w=0∨w=0x80000000:=by have:w.toNat=0∨w.toNat=2147483648:=by simp at h1;omega rcases this with e | e;. exact Or.inl (И.mp (by simpa using e)) . exact Or.inr (И.mp (by simpa using e)) have h4:expOf w=0∧fracOf w=0:=by rcases h3 with rfl | rfl<;>decide simp[rationalWord,decodeD,h4,FloatLib.Numerics.Dyadic.toRat_eq_zero_iff] @[inline] def T_e (w:UInt32):UInt64 := if w &&& 0x7FFFFFFF == 0 then 255 else (expOf w).toUInt64 @[inline] def T_m (w:UInt32):UInt64 := if w &&& 0x7FFFFFFF == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def T_s (e0:UInt64) (w:UInt32):UInt64:=T_m w <<< (T_e w-e0) @[inline] def T_ok (e0:UInt64) (w:UInt32):Bool := good w && (w &&& 0x7FFFFFFF == 0 || (e0<=T_e w && T_e w-e0<=37)) @[inline] def T_p (e0:UInt64) (w:UInt32) (p:UInt64):UInt64 := if signOf w then p else p+T_s e0 w @[inline] def T_n (e0:UInt64) (w:UInt32) (n:UInt64):UInt64 := if signOf w then n+T_s e0 w else n lemma qH7 (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true) : (T_s e0 w).toNat<2^61∧rationalWord w = (if signOf w then -((T_s e0 w).toNat:ℚ) else ((T_s e0 w).toNat:ℚ)) * 2^((e0.toNat:Int)-150):=by unfold T_ok at h simp only[Bool.and_eq_true,Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& 0x7FFFFFFF=0 . have hs:T_s e0 w=0:=by unfold T_s T_m;simp[h0] rw[hs,qIo w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=qBg w hg;have a0:(w &&& 0x7FFFFFFF).toNat≠0:=λe=>h0 (И.mp (by simpa using e));rw[qA1] at a0;have hx:=qBl w;have hf:(fracOf w).toNat=w.toNat%2^23:=qBk w;have hl:=w.toNat_lt;have x1:1<=(expOf w).toNat :=א;have x2:(expOf w).toNat<=254 :=א;have f1:(fracOf w).toNat<2^23 :=א;have ee:(T_e w).toNat=(expOf w).toNat:=by unfold T_e;simp[h0] have em:(T_m w).toNat=2^23+(fracOf w).toNat:=by unfold T_m;simp only[h0,beq_iff_eq,if_false,UInt32.toNat_toUInt64];rw[UInt32.toNat_add,Г (by show _+8388608< _;omega)];show _+8388608=_;omega have l1:=UInt64.le_iff_toNat_le.1 h1;have l2:=UInt64.le_iff_toNat_le.1 h2;rw[UInt64.toNat_sub_of_le _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-e0.toNat<=37:=l2 have es:(T_s e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat):=by unfold T_s;have sh:(T_e w-e0).toNat%64=(expOf w).toNat-e0.toNat:=by rw[UInt64.toNat_sub_of_le _ _ h1,ee];exact Г א rw[UInt64.toNat_shiftLeft,em,sh,р];apply Г;have:2^((expOf w).toNat-e0.toNat)<=2^37:=д ב l3;have:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=2^24*2^37 :=Nat.mul_le_mul א this;omega refine ⟨?_,?_⟩;. have a:2^((expOf w).toNat-e0.toNat)<=2^37:=д ב l3;have b:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=(2^24-1)*2^37 :=Nat.mul_le_mul א a;rw[es];omega . have nz:expOf w≠0:=λe=>by rw[e] at x1;simp at x1 have ew:(2:ℚ)^(((expOf w).toNat:Int)-150) =2^((expOf w).toNat-e0.toNat)*2^((e0.toNat:Int)-150):=by rw[<-Ь,<-П Х];congr 1;omega rw[es];simp only[rationalWord,decodeD,nz,ite_false,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand,ew];cases signOf w<;>simp<;>ring lemma qEp (e0:UInt64) (w:UInt32) (p n:UInt64) (V:ℚ) (B C:Nat) (hC:C=2^61-1) (hB:B+C<2^63) (hw:T_ok e0 w=true) (hp:p.toNat<=B) (hn:n.toNat<=B) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) : (T_p e0 w p).toNat<=B+C∧(T_n e0 w n).toNat<=B+C ∧ V+rationalWord w=(((T_p e0 w p).toNat:ℚ)-(T_n e0 w n).toNat) * 2^((e0.toNat:Int)-150):=by obtain ⟨h1,h2⟩:=qH7 e0 w hw have h3:(T_s e0 w).toNat<=C :=א unfold T_p T_n rw[h2,hV] cases signOf w . simp only[Bool.false_eq_true,ite_false];have e:(p+T_s e0 w).toNat=p.toNat+(T_s e0 w).toNat:=by rw[Ъ];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+T_s e0 w).toNat=n.toNat+(T_s e0 w).toNat:=by rw[Ъ];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def T_fin (p n e0:UInt64) (f:Unit->UInt32):UInt32 := if p == n then f () else let mag:=if p< n then n-p else p-n let L:=mag.log2 if e0+L<=24 || e0+L ≥ 277 then f () else T_rd (decide (p< n)) mag e0 L lemma qDc (p n e0:UInt64) (f:Unit->UInt32) (hp:p.toNat<2^63) (hn:n.toNat<2^63) (he:e0.toNat<=255) (R:UInt32) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150) ∧ R=toWord (Model.roundDyadic Fmt d)):T_fin p n e0 f=R:=by unfold T_fin split . exact hf . rename_i hne;have hne':p≠n:=λe=>hne (by simp[e]);have hnn:p.toNat≠n.toNat:=λe=>hne' (UInt64.toNat_inj.mp e);obtain ⟨d,hd,hR⟩:=hR hne';dsimp only;obtain ⟨mag,hm⟩:∃ m,m=if p< n then n-p else p-n:=⟨_,rfl⟩;rw[<-hm] have hm1:mag.toNat≠0∧mag.toNat<2^63 ∧ (((if decide (p< n)=true then -(mag.toNat:ℚ) else (mag.toNat:ℚ))) =(p.toNat:ℚ)-n.toNat):=by by_cases c:p< n . have c':=н.1 c;have:mag.toNat=n.toNat-p.toNat:=by rw[hm,if_pos c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c'.le)] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'.le];simp[c] . have c':n.toNat<=p.toNat:=by have:=н.not.1 c;omega have:mag.toNat=p.toNat-n.toNat:=by rw[hm,if_neg c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c')] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'];simp[c] obtain ⟨m0,m1,m2⟩:=hm1;split;. exact hf . rename_i hb;simp only[Bool.or_eq_true,decide_eq_true_eq,not_or,ge_iff_le] at hb;obtain ⟨b1,b2⟩:=hb;have eL:mag.log2.toNat=mag.toNat.log2:=rfl;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt m0).2 m1;omega have eA:(e0+mag.log2).toNat=e0.toNat+mag.toNat.log2:=by rw[Ъ,eL];exact Г א have b3:¬ (e0+mag.log2).toNat<=(24:UInt64).toNat:=λh=>b1 (UInt64.le_iff_toNat_le.2 h);have b4:¬ (277:UInt64).toNat<=(e0+mag.log2).toNat:=λh=>b2 (UInt64.le_iff_toNat_le.2 h);rw[eA] at b3 b4;have t1:(24:UInt64).toNat=24:=rfl;have t2:(277:UInt64).toNat=277:=rfl;rw[qGw _ _ _ m0 m1 א א,hR];congr 1 have s0:d.significand≠0:=by intro h0;have:d.toRat=0:=(а d).2 h0;rw[hd] at this;have q:(p.toNat:ℚ)-n.toNat≠0:=by rw[sub_ne_zero];exact_mod_cast hnn exact (mul_ne_zero q (zpow_ne_zero _ Х)) this refine roundDyadic_congr Fmt (by decide) _ _ m0 s0 ?_;rw[hd,<-m2];simp only[FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand];cases decide (p< n)<;>simp lemma qE7 (w:UInt32):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qBl w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:UInt64):UInt64:=if x< y then x else y lemma qB9 (x y:UInt64):(T_min x y).toNat<=x.toNat:=by unfold T_min;split;. exact le_rfl . have:=н.not.1 ‹_›;omega lemma qD9 (e0:UInt64) (w0 w1:UInt32) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 0)).toNat<=2*(2^61-1)∧(T_n e0 w1 (T_n e0 w0 0)).toNat<=2*(2^61-1) ∧ rationalWord w0+rationalWord w1=(((T_p e0 w1 (T_p e0 w0 0)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 0)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEp e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qEp e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qFv (e0:UInt64) (w0 w1:UInt32) (p n:UInt64) (V:ℚ) (hp:p.toNat<=2*(2^61-1)) (hn:n.toNat<=2*(2^61-1)) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 p)).toNat<2^63∧(T_n e0 w1 (T_n e0 w0 n)).toNat<2^63 ∧ V+rationalWord w0+rationalWord w1=(((T_p e0 w1 (T_p e0 w0 p)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 n)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEp e0 w0 p n V _ _ rfl ב h0 hp hn hV obtain ⟨b1,b2,b3⟩:=qEp e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 exact ⟨by omega,by omega,b3⟩ lemma qBi (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true):expOf w≠255:=by unfold T_ok at h;simp only[Bool.and_eq_true] at h;exact qAe w h.1 end @[noinline] def addSlow (relu:Bool) (x y:Word):Word := act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:Bool) (x y:Word):Word := let e0:=T_min (T_e x) (T_e y) if T_ok e0 x && T_ok e0 y then act relu (T_fin (T_p e0 y (T_p e0 x 0)) (T_n e0 y (T_n e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma qGy (relu:Bool) (x y:Word):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=qD9 _ x y hx hy;have ex:=qBi _ x hx;have ey:=qBi _ y hy;unfold addSlow;congr 1 refine qDc _ _ _ _ א א ((qB9 _ _).trans (qE7 x)) _ rfl λ_ =>⟨Model.addDyadic (decodeD x) (decodeD y),?_,?_⟩ . rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat];exact v . rw[Model.Proof.add_eq_spec];have e:=toDyadic_ofUInt32 x ex;have f:=toDyadic_ofUInt32 y ey;simp only[Model.Spec.add,ofW,e,f] . rfl end @[inline] def addOne (relu:Bool) (x y:Word):Word := if sm x && sm y then let r:=zivS (dv x+dv y) if r == 0xFFFFFFFF then addT relu x y else act relu r else addSlow relu x y section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma qHh (w:UInt32) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by unfold sm at h;simp only[Bool.and_eq_true,decide_eq_true_eq] at h;obtain ⟨hg,hl⟩:=h;refine ⟨hg,?_⟩;have h1:w.toNat%2^31<=0x70FFFFFF:=by have:=Ж.mp hl;rwa [qA1] at this have h2:=qBg w hg;rw[qCb w hg];refine (qCn _ _ h1 א).trans ?_;have:A_mag 0x70FFFFFF=(16777215:ℝ)*2^(75:ℤ):=by unfold A_mag;norm_num rw[this];norm_num lemma qIk (A P Q T g:Nat) (hg:g=0∨g=2^63) (ha:A<2^64) (e1:(A+2)%2^64=g+P) (e2:(2^64-2+A)%2^64=g+Q) (t1:2^61<=T) (t2:T<2^63-2^60) (l1:T< P+2^28) (u1:P<=T+2^28) (l2:T< Q+2^28) (u2:Q<=T+2^28) : A=g+(Q+2)∧T+2< Q+2+2^28∧Q+2+2<=T+2^28:=by omega lemma qIl (G f F:Nat) (hl:G+f+2< G+F+2^28) (hu:G+F+2<=G+f+2^28) : F+2<=f+2^28∧f+2< F+2^28:=by omega lemma qHi (s:Float) (h:zivS s≠0xFFFFFFFF) : ∃ (S:Bool) (ex fr M:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivS s=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ s.toBits.toNat=(if S then 2^63 else 0)+M ∧ (ex*2^23+fr)*2^29+2< M+2^28 ∧ M+2<=(ex*2^23+fr)*2^29+2^28:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,⟨P,e1,l1,u1⟩,⟨Q,e2,l2,u2⟩⟩:=qDs _ _ h rw[Ъ] at e1 rw[UInt64.toNat_sub] at e2 obtain ⟨a,b,c⟩:=qIk _ P Q ((ex*2^23+fr)*2^29) _ (by cases S<;>simp) s.toBits.toNat_lt e1 e2 א א l1 u1 l2 u2 exact ⟨S,ex,fr,Q+2,h1,h2,h3,hz,a,b,c⟩ lemma qH9 (v W z u:ℝ) (hu:0< u) (hz:z<=u) (h0:0<=v) (hv:v<=2^53*u) (h : |v-W| ≤ 1/2^53*(|W| + z)):v-2*u< W∧W< v+2*u:=by have h1 : |W| ≤ v + |v-W|:=by have:=Ч (v-(v-W)) (-(v-W)+(v-W));have e:W=v-(v-W):=by ring have a:=abs_sub_abs_le_abs_sub W v;rw[abs_sub_comm W v,У h0] at a;linarith have h2:=abs_lt.mp (show |v-W| < 2*u by nlinarith);constructor<;>linarith [h2.1,h2.2] set_option maxRecDepth 8192 in lemma qIr (ex fr E F:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hF:F<2^52) (hl:(ex*2^23+fr)*2^29+2< E*2^52+F+2^28) (hu:E*2^52+F+2<=(ex*2^23+fr)*2^29+2^28) (W:ℝ) (h : |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z)) : W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by have hE:E=ex∨(E+1=ex∧fr=0) :=א have hu0:(0:ℝ)<2^((E:Int)-1075):=о ב _ have hz:A_z<=2^((E:Int)-1075):=by have e:A_z=2^(-1021:ℤ):=rfl rw[e] exact Ц ב א have hN:((2^52+F:Nat):ℝ)<=2^53:=by have:2^52+F<=2^53 :=א exact_mod_cast this have hN0:(0:ℝ)<=((2^52+F:Nat):ℝ):=Т _ obtain ⟨a,b⟩:=qH9 _ W A_z _ hu0 hz (Ш hN0 hu0.le) (Б hN hu0.le) h push_cast at a b rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,П Х,zpow_one] rw[e] at a b;have hb:(0:ℝ)<2^((E:Int)-1076):=о ב _;generalize (2:ℝ)^((E:Int)-1076)=c at a b hb ⊢;have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl hu;obtain ⟨n3,n4⟩:=qIl _ _ _ hl hu;have h3:(F:ℝ)+2<=fr*2^29+2^28:=by exact_mod_cast n3 have h4:(fr:ℝ)*2^29+2< F+2^28:=by exact_mod_cast n4 have h5:(0:ℝ)<=F:=Т _;have h6:(0:ℝ)<=fr:=Т _;have h7:=Б h3 hb.le;have h8:=Щ h4 hb;have h9:=Ш h5 hb.le;have h10:=Ш h6 hb.le;refine ⟨?_,?_,?_⟩;. nlinarith . intro _;nlinarith . nlinarith . have e:(2:ℝ)^(((E+1:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e];generalize (2:ℝ)^((E:Int)-1075)=c at a b hu0 ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52+2< F+2^28:=by exact_mod_cast (by omega:2^52+2< F+2^28) have h7:=Б h3 hu0.le;have h8:=Щ h4 hu0;refine ⟨?_,?_,?_⟩;. nlinarith . intro h0;exact absurd rfl h0 . nlinarith lemma qG6 (d:FloatLib.Numerics.Dyadic) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (W:ℝ) (hd:(d.toRat:ℝ)=if S then -W else W) (hW1:W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) (hW3:(2^53-2^28)*2^((ex:Int)-1076)< W) : toWord (Model.roundDyadic Fmt d) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hb0:(0:ℝ)<2^((ex:Int)-1076):=о ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-Ь,<-П Х] congr 1;push_cast;ring have hW0:0< W:=lt_of_le_of_lt (Ш ב hb0.le) hW3 obtain ⟨neg,m,q⟩:=d have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=if S then -W else W:=by rw[<-Ю,hd] have hq:(0:ℝ)<2^q:=о ב _ have hm0:(0:ℝ)<=m:=Т _ have hmq:=Ш hm0 hq.le have hMW:(m:ℝ)*2^q=W∧neg=S:=by simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] at hdV cases neg<;>cases S <;> simp only[Bool.false_eq_true,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV . exact ⟨by linarith,rfl⟩ . exfalso;linarith . exfalso;linarith . exact ⟨by linarith,rfl⟩ obtain ⟨hMW,rfl⟩:=hMW have hm:m≠0:=by rintro rfl simp at hMW linarith have hword:=qFk neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hword,e1,e2] lemma qFb (s:Float) (d:FloatLib.Numerics.Dyadic) (hV : |R s-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z)) (h:zivS s≠0xFFFFFFFF):zivS s=toWord (Model.roundDyadic Fmt d):=by obtain ⟨S,ex,fr,M,h1,h2,h3,hz,hb,hl,hu⟩:=qHi s h;have hM:M=(M/2^52)*2^52+M%2^52 :=א;have hF:M%2^52<2^52:=Ы _ ב;generalize M/2^52=E at hM;generalize M%2^52=F at hM hF;subst hM;obtain ⟨-,hR⟩:=B_R s S E F א א hF (by rw[hb,г]);rw[hR] at hV obtain ⟨W,hVW,hW⟩:∃ W:ℝ,(d.toRat:ℝ)=(if S then -W else W) ∧ |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z):=by cases S . simp only[Bool.false_eq_true,ite_false,one_mul] at hV ⊢;exact ⟨_,rfl,hV⟩ . simp only[ite_true] at hV ⊢;refine ⟨-(d.toRat:ℝ),by simp,?_⟩;rw[abs_neg,show ((2^52+F:Nat):ℝ)*2^((E:Int)-1075) - -(d.toRat:ℝ) =-(-1*((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-(d.toRat:ℝ)) by ring,abs_neg];exact hV obtain ⟨a,b,c⟩:=qIr ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qG6 d S ex fr h1 h2 h3 W hVW a b c).symm lemma qFj (w:UInt32) (hg:good w=true) : ∃ (m k:ℤ),wv w=(m:ℝ)*2^k∧m.natAbs<2^25∧-160<=k:=by have hN:=w.toNat_lt;rw[qBf w hg];unfold A_mag;by_cases h0:w.toNat%2^31=0;. exact ⟨0,0,by rw[if_pos h0];simp,by norm_num,by norm_num⟩ . simp only[h0,ite_false];have hk:-160<=(((w.toNat%2^31/2^23:ℕ):ℤ)-150) :=א;have hM:2^23+w.toNat%2^31%2^23<2^25 :=א;generalize ((w.toNat%2^31/2^23:ℕ):ℤ)-150=k at hk ⊢;generalize 2^23+w.toNat%2^31%2^23=M at hM ⊢;by_cases hs:2^31<=w.toNat;. exact ⟨-(M:ℤ),k,by rw[if_pos hs];push_cast;ring,by simpa using hM,hk⟩ . exact ⟨(M:ℤ),k,by rw[if_neg hs];push_cast;ring,by simpa using hM,hk⟩ lemma qHb (a b:UInt32) (ha:good a=true) (hb:good b=true) : (dv a*dv b).isFinite=true∧R (dv a*dv b)=wv a*wv b:=by obtain ⟨fa,ra⟩:=qA7 a ha;obtain ⟨fb,rb⟩:=qA7 b hb;have la:=(qAh a ha).trans qAn;have lb:=(qAh b hb).trans qAn obtain ⟨h1,h2⟩:=qC8 _ _ fa fb (by rw[ra,rb] exact (в la lb (Р _) qCw.le).trans qCm) refine ⟨h1,?_⟩;obtain ⟨m,k,e1,h3,h4⟩:=qFj a ha;obtain ⟨n,l,e2,h5,h6⟩:=qFj b hb;have e:wv a*wv b=((m*n:ℤ):ℝ)*2^(k+l):=by rw[e1,e2,П Х];push_cast;ring rw[h2,ra,rb,e];refine qAk _ _ ?_ א;rw[Int.natAbs_mul];have:=Nat.mul_lt_mul'' h3 h5;omega lemma S_E (x y z:UInt32) (hx:good x=true) (hy:good y=true) (hz:good z=true) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=wv x*wv y+wv z) : |R (dv x*dv y+dv z)-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z):=by obtain ⟨f1,r1⟩:=qHb x y hx hy obtain ⟨-,-,l⟩:=qDq x y hx hy obtain ⟨f2,r2⟩:=qA7 z hz have h256:(2:ℝ)^256<=A_B:=by have:(2:ℝ)^256<=2^256*2^42 :=ב linarith [qAq] obtain ⟨-,he⟩:=qBt _ _ f1 f2 (by rw[r1];exact l.trans h256) (by rw[r2];exact (qAh z hz).trans qAn) rw[r1,r2] at he rw[hd] exact he lemma qC1 (relu:Bool) (x y:Word) : addOne relu x y=act relu (toWord (Model.add (ofW x) (ofW y))):=by unfold addOne;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨gx,-⟩:=qHh x hc.1;obtain ⟨gy,-⟩:=qHh y hc.2;dsimp only;split;. exact qGy relu x y . rename_i hr;have hn:zivS (dv x+dv y)≠0xFFFFFFFF:=λh=>hr (by simp[h]);have ex:=qAe _ gx;have ey:=qAe _ gy;have hd:((Model.addDyadic (decodeD x) (decodeD y)).toRat:ℝ)=wv x+wv y:=by rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,qA8 x ex,qA8 y ey];simp only[rationalWord,Rat.cast_add] obtain ⟨fx,rx⟩:=qA7 x gx;obtain ⟨fy,ry⟩:=qA7 y gy;obtain ⟨-,he⟩:=qBt _ _ fx fy (by rw[rx];exact (qAh x gx).trans qAn) (by rw[ry];exact (qAh y gy).trans qAn);rw[rx,ry,<-hd] at he;rw[qFb _ _ he hn];congr 2;rw[Model.Proof.add_eq_spec];have e:=toDyadic_ofUInt32 x ex;have f:=toDyadic_ofUInt32 y ey;simp only[Model.Spec.add,ofW,e,f] . rfl lemma qFn {L:USize->Array UInt32->Array UInt32} (f:USize->UInt32->UInt32) {e:USize} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:Nat) : ∀ (i:USize) (x:Array UInt32),i.toNat+d=e.toNat->e.toNat<=x.size->x.size<2^32 → (L i x).size=x.size∧∀ q,(L i x)[q]! =if i.toNat<=q∧q< e.toNat then f (USize.ofNat q) x[q]! else x[q]!:=by induction d with | zero => intro i x h1 _ _ rw[hL,if_neg (by rw[А];omega)] exact ⟨rfl,λq=>(Д א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=by rw[А];omega have hs:=qA0 x i (f i (rdW x i)) rw[hL,if_pos hi] obtain ⟨h4,h5⟩:=ih (i+1) (wrW x i (f i (rdW x i))) (by rw[usz i e hi];omega) א א refine ⟨h4.trans hs,λq=>?_⟩ rw[h5,qBd _ _ _ _ h3 א,usz i e hi,qAt _ _ h3] by_cases h6:q=i.toNat . subst h6;rw[if_neg א,if_pos rfl,if_pos ⟨Nat.le_refl _,by omega⟩,USize.ofNat_toNat] . rw[if_neg h6];by_cases h7:i.toNat+1<=q∧q< e.toNat;. rw[if_pos h7,if_pos ⟨by omega,h7.2⟩] . rw[if_neg h7,if_neg א] def addGo (relu:Bool) (y:Array Word) (e i:USize) (x:Array Word) : Array Word := if h:i< e then addGo relu y e (i+1) (wrW x i (addOne relu (rdW x i) (rdW y i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def addF (relu:Bool) (a b:Array Word):Array Word := if a.size=b.size∧a.size<2^32 then addGo relu b a.usize 0 a else Array.ofFn (n:=a.size) λi=>addOne relu a[i] b[i.val]! lemma qGc:addF=addReluSpec:=by funext relu a b;unfold addF;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=qAd _ h2;have hg:=qFn (L:=addGo relu b a.usize) (λi w=>addOne relu w (rdW b i)) (λ_ _=>by rw[addGo];rfl) a.size 0 a (by rw[hu,show (0:USize).toNat=0 from rfl,Й]) א h2;apply ъ (by rw[hg.1];simp[addReluSpec]);intro i h3 h4;have hi:i< a.size:=by rwa [hg.1] at h3 rw[<-Е _ i h3,hg.2 i,if_pos ⟨Nat.zero_le _,by omega⟩,qC1,Е a i hi,qAt _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qC1] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary @[noinline] def E_fma (relu:Bool) (w sw tw:UInt32):UInt32 := act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def E_act (relu:Bool) (r:UInt32):UInt32 := if relu && signOf r then 0 else r @[inline] def E_el (relu g:Bool) (sw tw:UInt32) (y z:Float) (w:UInt32):UInt32 := if g && good w then let r:=zivS (dv w*y+z) if r == 0xFFFFFFFF then E_fma relu w sw tw else E_act relu r else E_fma relu w sw tw @[specialize] def E_map (f:UInt32->UInt32) (e i:USize) (x:Array UInt32):Array UInt32 := if h:i< e then E_map f e (i+1) (wrW x i (f (rdW x i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def E_chs (relu:Bool) (c hw:Nat) (s t:Array UInt32) (ch:Nat) (x:Array UInt32):Array UInt32 := if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw E_chs relu c hw s t (ch+1) (E_map (E_el relu (good sw && good tw) sw tw y z) ((ch+1)*hw).toUSize (ch*hw).toUSize x) else x termination_by c-ch def scaleF (relu:Bool) (c hw:Nat) (x s t:Array UInt32):Array UInt32 := if x.size=c*hw∧x.size<2^32 then E_chs relu c hw s t 0 x else scaleShiftSpec relu c hw x s t @[noinline] def E_dot (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := dotFn (k*k) (λt=>X[b.toNat+(t/k*w+t%k)]!) (λ_=>oneW) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat def E_dotT (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := if k=2 then let i:=b.toNat let a0:=X[i]! let a1:=X[i+1]! let a2:=X[i+w]! let a3:=X[i+(w+1)]! let e0:=T_min (T_min (T_e a0) (T_e a1)) (T_min (T_e a2) (T_e a3)) if T_ok e0 a0 && T_ok e0 a1 && T_ok e0 a2 && T_ok e0 a3 then T_fin (T_p e0 a3 (T_p e0 a2 (T_p e0 a1 (T_p e0 a0 0)))) (T_n e0 a3 (T_n e0 a2 (T_n e0 a1 (T_n e0 a0 0)))) e0 λ_=>E_dot X b k w else E_dot X b k w else E_dot X b k w lemma qFl (X:Array UInt32) (b:USize) (k w:Nat):E_dotT X b k w=E_dot X b k w:=by unfold E_dotT;split . rename_i hk;subst hk;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨⟨⟨h0,h1⟩,h2⟩,h3⟩:=hc;generalize hi:b.toNat=i at *;obtain ⟨p1,n1,v⟩:=qD9 _ _ _ h0 h1;obtain ⟨p2,n2,v2⟩:=qFv _ _ _ _ _ _ p1 n1 v h2 h3;refine qDc _ _ _ _ p2 n2 ((qB9 _ _).trans ((qB9 _ _).trans (qE7 _))) _ rfl λhne=>?_;have q0:b.toNat+(0/2*w+0%2)=i:=by simp[hi] have q1:b.toNat+(1/2*w+1%2)=i+1:=by simp[hi] have q2:b.toNat+(2/2*w+2%2)=i+w:=by simp[hi] have q3:b.toNat+(3/2*w+3%2)=i+(w+1):=by simp[hi] have hsum:(∑ j∈Finset.range (2*2),rationalWord X[b.toNat+(j/2*w+j%2)]! * rationalWord oneW)=rationalWord X[i]! + rationalWord X[i+1]! + rationalWord X[i+w]! + rationalWord X[i+(w+1)]!:=by rw[show 2*2=4 from rfl] simp only[Finset.sum_range_succ,Finset.sum_range_zero,qGg,mul_one,zero_add,q0,q1,q2,q3] have o:expOf oneW≠255:=by decide have hf:∀ j,j<2*2->expOf X[b.toNat+(j/2*w+j%2)]!≠255∧expOf oneW≠255:=by intro j hj;rcases (by omega:j=0∨j=1∨j=2∨j=3) with rfl | rfl | rfl | rfl;. rw[q0];exact ⟨qBi _ _ h0,o⟩ . rw[q1];exact ⟨qBi _ _ h1,o⟩ . rw[q2];exact ⟨qBi _ _ h2,o⟩ . rw[q3];exact ⟨qBi _ _ h3,o⟩ obtain ⟨d,h4,h5⟩:=qC9 (2*2) (λj=>X[b.toNat+(j/2*w+j%2)]!) (λ_=>oneW) hf (by rw[hsum,v2] exact mul_ne_zero (sub_ne_zero.2 λe=>hne (UInt64.toNat_inj.mp (Nat.cast_injective e))) (zpow_ne_zero _ Х)) refine ⟨d,?_,?_⟩;. rw[h4,hsum];exact v2 . unfold E_dot;rw[qAc];exact h5 . rfl . rfl end @[noinline] def E_div (n:Nat) (s:UInt32):UInt32 := toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def E_quo (n:Nat) (j s:UInt32):UInt32 := if j< expOf s∧expOf s≠255 then s-(j <<< (23:UInt32)) else E_div n s @[inline] def E_tl (X:Array UInt32) (b:USize) (k w n:Nat) (j z:UInt32) (nf zb s a:Float) (o:UInt32):UInt32 := if o=0 then z else let r:=ziv (s+zb) (nf*(a+zb.abs)) E_quo n j (if r == 0xFFFFFFFF then E_dotT X b k w else r) def E_win (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (u nf zb:Float) (b m t:USize) (s a:Float) (o:UInt32):UInt32 := if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u E_win X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else E_tl X b k w n j z nf zb s a o termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega @[specialize] def G_x (f:USize->UInt32) (sx:USize) (W x:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if x< W then G_x f sx W (x+1) (b+sx) (q+1) (wrW out q (f b)) else out termination_by W-x @[specialize] def G_y (f:USize->UInt32) (sy sx:USize) (W H y:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if y< H then G_y f sy sx W H (y+1) (b+sy) (q+W.toUSize) (G_x f sx W 0 b q out) else out termination_by H-y @[specialize] def G_c (f:USize->UInt32) (sc sy sx:USize) (W H c ch:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if ch< c then G_c f sc sy sx W H c (ch+1) (b+sc) (q+(H*W).toUSize) (G_y f sy sx W H 0 b q out) else out termination_by c-ch def E_off (k w:Nat):Array UInt32 := Array.ofFn (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def E_grid (c h w k n:Nat) (X off:Array UInt32) (j z:UInt32) (u nf zb zr:Float) : Array UInt32 := G_c (λb=>E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Array.replicate (c*(h/k*(w/k))) 0) def avgF (c h w k:Nat) (X:Array UInt32):Array UInt32 := if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (qAj X) 0xFFFFFFFF 0≠0xFFFFFFFF then let n:=k*k let off:=E_off k w let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:UInt32:=if 0< k∧k<17 then 0 else E_div n (dotFn n (λ_=>0) (λ_=>oneW)) E_grid c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X lemma qHe (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠0xFFFFFFFF):V≠0:=by obtain ⟨S,ex,fr,h1,h2,h3,-,ha,hb⟩:=qBq s e h;obtain ⟨-,va,hva,-,-,loa⟩:=qAo _ S ex fr h1 h2 h3 ha;obtain ⟨-,vb,hvb,-,-,lob⟩:=qAo _ S ex fr h1 h2 h3 hb;have h0:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=Ш ב (zpow_nonneg ב _);rintro rfl;rw[hva] at hhi;rw[hvb] at hlo;cases S;. simp only[Bool.false_eq_true,ite_false] at hlo;linarith . simp only[ite_true] at hhi;linarith lemma qH6 (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=exactS n xw ww bw) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠0xFFFFFFFF):ziv s e=toWord (Model.roundDyadic Fmt d):=by have hV:=qHe s e _ hlo hhi h;have hsum:=qGh n xw ww bw hx hw hb have e0:∀ c:FloatLib.Numerics.Dyadic,(c.toRat:ℝ)=exactS n xw ww bw → c.significand≠0:=λc hc h0=>hV (by rw[<-hc,(а c).mpr h0,ظ]) obtain ⟨d',hd',hw'⟩:=qC9 (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (λj _=>by by_cases hj:j< n;. simp only[hj,ite_true];exact ⟨qAe _ (hx j hj),qAe _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAe _ hb⟩) (λh0=>hV (by rw[<-hsum,h0,ظ])) rw[qCv n hn xw ww bw hx hw hb s e hlo hhi h,hw',roundDyadic_congr Fmt (by decide) d' d (e0 d' (by rw[hd',hsum])) (e0 d hd) (Rat.cast_injective (α:=ℝ) (by rw[hd',hsum,hd]))] def E_d (x y z:UInt32):FloatLib.Numerics.Dyadic := Model.addDyadic ⟨Bool.xor (decodeD x).negative (decodeD y).negative, (decodeD x).significand*(decodeD y).significand, (decodeD x).exponent+(decodeD y).exponent⟩ (decodeD z) lemma qF6 (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : Model.fma (ofW x) (ofW y) (ofW z)=Model.roundDyadic Fmt (E_d x y z):=by rw[Model.Proof.fma_eq_spec];have e:=toDyadic_ofUInt32 x hx;have f:=toDyadic_ofUInt32 y hy;have g:=toDyadic_ofUInt32 z hz have o:Model.chooseNaN3 (Model.ofUInt32 x:Model Fmt) (Model.ofUInt32 y) (Model.ofUInt32 z) =none:=(Model.chooseNaN3_eq_none_iff _ _ _).2 ⟨Model.isNaN_eq_false_of_toDyadic?_some e,Model.isNaN_eq_false_of_toDyadic?_some f,Model.isNaN_eq_false_of_toDyadic?_some g⟩ simp only[Model.Spec.fma,ofW,o,Model.isInf_eq_false_of_toDyadic?_some e,Model.isInf_eq_false_of_toDyadic?_some f,Model.isInf_eq_false_of_toDyadic?_some g,Bool.or_false,Bool.false_eq_true,ite_false,e,f,g];rfl lemma qG5 (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : ((E_d x y z).toRat:ℝ)=wv x*wv y+wv z:=by have j:=FloatLib.Numerics.Dyadic.mul_toRat (decodeD x) (decodeD y);simp only[FloatLib.Numerics.Dyadic.mul,FloatLib.Numerics.Dyadic.mulFields] at j;rw[E_d,Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,j,qA8 x hx,qA8 y hy,qA8 z hz];simp only[rationalWord,Rat.cast_add,Rat.cast_mul] lemma qGs (relu:Bool) (r:UInt32) (hf:expOf r≠255):E_act relu r=act relu r:=by cases relu;. rfl . show (if signOf r then 0 else r) =if signOf r && !(expOf r=255 && fracOf r != 0) then 0 else r;simp only[hf,decide_false,Bool.false_and,Bool.not_false,Bool.and_true] lemma qIi (relu:Bool) (s:Float) (h:zivS s≠0xFFFFFFFF) : E_act relu (zivS s)=act relu (zivS s):=by apply qGs;obtain ⟨S,ex,fr,h1,h2,h3,hz,-,-⟩:=qDs _ _ h;have hz':zivS s=_:=hz;intro h0;have h4:(expOf (zivS s)).toNat=255:=by rw[h0];rfl rw[qBl,hz',ш] at h4;split at h4<;>omega lemma qG0 (relu g:Bool) (sw tw w:UInt32) (hg:g=true->good sw=true∧good tw=true) : E_el relu g sw tw (dv sw) (dv tw) w=E_fma relu w sw tw:=by unfold E_el;split . rename_i hc;obtain ⟨h1,h2⟩:=с.mp hc;obtain ⟨h3,h4⟩:=hg h1;dsimp only;split;. rfl . rename_i hr;have hr':zivS (dv w*dv sw+dv tw)≠0xFFFFFFFF:=λh=>hr (beq_iff_eq.mpr h);have hf:=qAe _ h2;have hg1:=qAe _ h3;have hg2:=qAe _ h4;rw[qIi _ _ hr',qFb _ (E_d w sw tw) (S_E w sw tw h2 h3 h4 _ (qG5 _ _ _ hf hg1 hg2)) hr',E_fma,qF6 _ _ _ hf hg1 hg2] . rfl lemma qEn (relu:Bool) (c hw:Nat) (s t:Array UInt32) (d:Nat) : ∀ (ch:Nat) (x:Array UInt32),ch+d=c->x.size=c*hw->x.size<2^32 → (E_chs relu c hw s t ch x).size=x.size∧∀ q,q< c*hw → (E_chs relu c hw s t ch x)[q]! =if ch*hw<=q then E_fma relu x[q]! s[q/hw]! t[q/hw]! else x[q]!:=by induction d with | zero => intro ch x h1 _ _ rw[E_chs,if_neg א] refine ⟨rfl,λq hq=>?_⟩ rw[if_neg (by rw[(א:ch=c)];omega)] | succ d ih => intro ch x h1 h2 h3 rw[E_chs,if_pos א] dsimp only have h4:(ch+1)*hw<=c*hw:=З _ א have h5:(ch+1)*hw=ch*hw+hw:=ذ _ _ have e1:((ch+1)*hw).toUSize.toNat=(ch+1)*hw:=qAd _ א have e0:(ch*hw).toUSize.toNat=ch*hw:=qAd _ א obtain ⟨h6,h7⟩:=qFn (λ_=>E_el relu (good s[ch]! && good t[ch]!) s[ch]! t[ch]! (dv s[ch]!) (dv t[ch]!)) (L:=E_map _ ((ch+1)*hw).toUSize) (λ_ _=>by rw[E_map];rfl) hw (ch*hw).toUSize x א א h3 obtain ⟨h8,h9⟩:=ih (ch+1) _ א (h6.trans h2) א refine ⟨h8.trans h6,λq hq=>?_⟩ rw[h9 q hq,h7,e0,e1,qG0 _ _ _ _ _ (λh=>с.mp h)] by_cases a:(ch+1)*hw<=q . rw[if_pos a,if_neg א,if_pos א] . rw[if_neg a];by_cases b:ch*hw<=q;. rw[if_pos ⟨b,by omega⟩,if_pos b,Nat.div_eq_of_lt_le b א] . rw[if_neg א,if_neg b] lemma qFm:scaleF=scaleShiftSpec:=by funext relu c hw x s t;unfold scaleF;split . rename_i h;obtain ⟨h1,h2⟩:=h;obtain ⟨h3,h4⟩:=qEn relu c hw s t c 0 x א h1 h2;apply ъ;. simp[scaleShiftSpec,h3,h1] . intro i hi hj;have hi':i< c*hw:=by rwa [h3,h1] at hi rw[<-Е _ i hi,h4 i hi',if_pos ג];simp[scaleShiftSpec,E_fma] . rfl lemma qF1 (w:UInt32):toWord (ofW w)=w:=by apply И.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact Г w.toBitVec.isLt lemma qCi (w:UInt32) (h:expOf w≠255) : Model.toReal (ofW w)=(sv w:ℝ)*2^((idxOf w:ℤ)-149):=by rw[show Model.toReal (ofW w)=wv w from rfl,qA8 w h,rationalWord,decodeD_toRat];push_cast;rfl lemma qIj (s j:UInt32) (hj:j.toNat<=6) (a:j.toNat<(expOf s).toNat) : (expOf (s-(j <<< (23:UInt32)))).toNat=(expOf s).toNat-j.toNat ∧ fracOf (s-(j <<< (23:UInt32)))=fracOf s ∧ signOf (s-(j <<< (23:UInt32)))=signOf s:=by rw[qBl] at a have hs:=s.toNat_lt have e:(j <<< (23:UInt32)).toNat=j.toNat*2^23:=by rw[UInt32.toNat_shiftLeft,р] exact Г (by simp;omega) have f:j <<< (23:UInt32)<=s:=by rw[Ж,e];omega have d:(s-(j <<< (23:UInt32))).toNat=s.toNat-j.toNat*2^23:=by rw[ز _ _ f,e] refine ⟨?_,?_,?_⟩ . rw[qBl,qBl,d];omega . apply И.mp;rw[fracOf,fracOf,qBk,qBk,d];omega . rw[qDo,qDo,d];simp;omega lemma qCz (s y j:UInt32) (f:j.toNat<=6) (a:expOf s≠255) (b:j.toNat<(expOf s).toNat) (d:expOf y≠255) (g:signOf y=false) (e:mantOf y=2^23) (k:idxOf y=126+j.toNat) : toWord (Model.div (ofW s) (ofW y))=s-(j <<< (23:UInt32)):=by have A:sv y=2^23:=by unfold sv;rw[g,e];rfl have B:Model.isZero (ofW y)=false:=by rw[ofW,Model.isZero_eq_beq_zero_of_toDyadic?_some (toDyadic_ofUInt32 y d),decodeD_sig_zero,e];decide obtain ⟨C,D,E⟩:=qIj s j f b;generalize s-(j <<< (23:UInt32))=r at C D E ⊢;have N:=expOf_toNat_lt s;have O:(expOf s).toNat≠255:=λF=>a (И.mp (by simpa using F));have l:expOf r≠255:=by intro h;have n:=congrArg UInt32.toNat h;simp at n;omega have q:expOf r≠0:=by intro h;have n:=congrArg UInt32.toNat h;simp at n;omega have t:expOf s≠0:=by intro h;rw[h] at b;simp at b have G:sv r=sv s:=by unfold sv mantOf;rw[E,D,if_neg q,if_neg t] have H:(idxOf r:ℤ)=(idxOf s:ℤ)-j.toNat:=by unfold idxOf;rw[if_neg q,if_neg t,C];omega have o:Model.isFinite (ofW s)=true:=by simp[ofW,isFinite_ofUInt32,a] have u:Model.isFinite (ofW y)=true:=by simp[ofW,isFinite_ofUInt32,d] have v:Model.isFinite (ofW r)=true:=by simp[ofW,isFinite_ofUInt32,l] have x:Model.toReal (ofW y)=2^j.toNat:=by rw[qCi _ d,A,k];push_cast;rw[show ((126:ℤ)+j.toNat-149)=(j.toNat:ℤ)+(-23) by ring,П Х,Ь];field_simp;norm_num have I:Model.toReal (ofW r)=Model.toReal (ofW s)/2^j.toNat:=by rw[qCi _ l,qCi _ a,G,H,show ((idxOf s:ℤ)-j.toNat-149) =((idxOf s:ℤ)-149)+-(j.toNat:ℤ) by ring,П Х,zpow_neg,Ь];field_simp have J : |Model.toReal (ofW s)/Model.toReal (ofW y)| ≤ Model.toReal (Model.posMaxFinite Fmt):=by rw[x,abs_div,abs_of_pos (by positivity:(0:ℝ)<2^j.toNat)];exact (div_le_self (Р _) (one_le_pow₀ ב)).trans (Model.abs_toReal_le_posMaxFinite_of_isIEEE_of_isFinite _ (by decide) o) have z:Model.isFinite (Model.div (ofW s) (ofW y))=true :=Model.isFinite_div_of_abs_div_le_posMaxFinite _ _ (by decide) o u B J;have p:=Model.toReal_div_eq_roundAt _ _ (by decide) o u B z;rw[x,<-I,Model.roundAt_toReal_eq _ v] at p have K:Model.toReal (Model.div (ofW s) (ofW y))≠0:=by rw[p,qCi _ l];apply mul_ne_zero _ (zpow_ne_zero _ Х);have:sv r≠0:=by unfold sv mantOf;split<;>omega exact_mod_cast this rw[Model.eq_of_toReal_eq_of_nonzero z v p K,qF1] lemma qF4 (n:Nat) (j s:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) : E_quo n j s=E_div n s:=by unfold E_quo;split . rename_i h;obtain ⟨h1,h2⟩:=h;have h3:=expOf_toNat_lt s;rw[м] at h1;rcases hj with rfl | ⟨rfl,rfl⟩ | ⟨rfl,rfl⟩;. exfalso;simp at h1;omega . exact (qCz s _ 2 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . exact (qCz s _ 6 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . rfl def E_f (xw:Nat->UInt32) (l:List Nat) (c:F1):F1 := l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv oneW F1.mk (acc.s+q) (acc.t+q.abs)) c lemma qGb (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (nf zb:Float) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat):∀ (t:USize) (s a:Float) (o:UInt32),t.toNat+d=m.toNat → E_win X off k w n j z (dv oneW) nf zb b m t s a o=E_tl X b k w n j z nf zb (E_f xw (List.range' t.toNat d) ⟨s,a⟩).s (E_f xw (List.range' t.toNat d) ⟨s,a⟩).t ((List.range' t.toNat d).foldl (λc i=>c ||| xw i) o):=by induction d with | zero => intro t s a o h rw[E_win,Л (by rw[А];omega)] rfl | succ d ih => intro t s a o h have ht:t< m:=by rw[А];omega rw[E_win,Н ht] dsimp only rw[hx t ht,ih (t+1) _ _ _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ] rfl lemma qIa (xw:Nat->UInt32) (l:List Nat):∀ o:UInt32,l.foldl (λc i=>c ||| xw i) o=0->o=0∧∀ i∈l,xw i=0:=by induction l with | nil=>exact λo h=>⟨h,by simp⟩ | cons i l ih => intro o h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=UInt32.or_eq_zero_iff.mp h1;exact ⟨h3,List.forall_mem_cons.mpr ⟨h4,h2⟩⟩ lemma qG9 (n:Nat) (hn:n<2^20) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (r:ziv ((E_f xw (List.range n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (List.range n) ⟨0,0⟩).t+(dv 0).abs))≠0xFFFFFFFF) : ziv ((E_f xw (List.range n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (List.range n) ⟨0,0⟩).t+(dv 0).abs))=dotOf n xw (λ_=>oneW):=by obtain ⟨-,-,hbig⟩:=qCt _ _ r obtain ⟨hlo,hhi⟩:=qEg n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) (List.range n) List.nodup_range ג (λt h1 h2=>absurd (к.mpr h1) h2) _ _ ⟨rfl,rfl⟩ hbig have hsum:((∑ j∈Finset.range n,rationalWord (xw j)*rationalWord oneW:ℚ):ℝ) =exactS n xw (λ_=>oneW) 0:=by rw[exactS,qBr 0 (by decide) (by decide),add_zero,Rat.cast_sum] refine ы rfl λt ht=>?_ rw[Rat.cast_mul,qA8 _ (qAe _ (hg t (Finset.mem_range.mp ht))),qA8 oneW (by decide)] obtain ⟨d,hd,hw⟩:=qC9 n xw (λ_=>oneW) (λj hj=>⟨qAe _ (hg j hj),by decide⟩) λh=>qHe _ _ _ hlo hhi r (by rw[<-hsum,h,ظ]) rw[hw] exact qH6 n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) _ _ d (by rw[hd,hsum]) hlo hhi r lemma qGp (X:Array UInt32) (b:USize) (k w n:Nat) (hn:n<2^20) (j:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (hs:E_dot X b k w=dotOf n xw (λ_=>oneW)) : E_tl X b k w n j (E_div n (dotFn n (λ_=>0) (λ_=>oneW))) (nfOf n) (dv 0) (E_f xw (List.range n) ⟨0,0⟩).s (E_f xw (List.range n) ⟨0,0⟩).t ((List.range n).foldl (λc i=>c ||| xw i) 0)=E_div n (dotOf n xw (λ_=>oneW)):=by unfold E_tl split . rename_i h1;obtain ⟨-,h5⟩:=qIa xw _ _ h1;have e:dotOf n xw (λ_=>oneW)=dotOf n (λ_=>0) (λ_=>oneW):=by unfold dotOf;congr 2;funext i;exact h5 _ (к.mpr i.isLt) rw[e,qAc] . dsimp only;rw[qF4 _ _ _ hj];congr 1;split;. rw[qFl];exact hs . rename_i hr;exact qG9 _ hn xw hg (λh=>hr (beq_iff_eq.mpr h)) lemma qEt {c h w k ch y x t:Nat} (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (ht:t< k*k):(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w:=by have hk:0< k:=by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at hy . exact h0 have h1:t/k< k:=у ht;have h2:t%k< k:=Ы _ hk;have h3:(y+1)*k<=h:=(Nat.le_div_iff_mul_le hk).mp hy;have h4:(x+1)*k<=w:=(Nat.le_div_iff_mul_le hk).mp hx;rw[ذ] at h3 h4;have h5:=З h (show ch+1<=c from hc);rw[ذ] at h5;have h6:=З w (א:ch*h+y*k+t/k+1<=c*h);rw[ذ] at h6;omega def G_P (N:Nat) (sp:Nat->UInt32) (out:Array UInt32) (p:Nat):Prop := out.size=N∧N<2^32∧∀ i,i< p->out[i]! = sp i lemma qIt {N p:Nat} {sp:Nat->UInt32} {out:Array UInt32} (h:G_P N sp out p) (hp:p< N) (q:USize) (hq:q.toNat=p):G_P N sp (wrW out q (sp p)) (p+1):=by obtain ⟨h1,h2,h3⟩:=h;refine ⟨(qA0 _ _ _).trans h1,h2,λi hi=>?_⟩;rw[qBd _ _ _ _ א א,hq];by_cases e:i=p;. rw[if_pos e,e] . rw[if_neg e];exact h3 i א lemma qHt (a s i:Nat):(a+(i+1)*s).toUSize=(a+i*s).toUSize+s.toUSize:=by rw[show a+(i+1)*s=a+i*s+s by ring];simp only[Nat.toUSize_eq,USize.ofNat_add] lemma qEq {L:Nat->USize->USize->Array UInt32->Array UInt32} {g:USize->USize->Array UInt32->Array UInt32} {sb S n N q0 b0:Nat} {sp:Nat->UInt32} (hL:∀ i b q o,L i b q o =if i< n then L (i+1) (b+sb.toUSize) (q+S.toUSize) (g b q o) else o) (hs:∀ i o,i< n->G_P N sp o (q0+i*S) → G_P N sp (g (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+(i+1)*S)) (d:Nat) : ∀ i o,i+d=n->G_P N sp o (q0+i*S) → G_P N sp (L i (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+n*S):=by induction d with | zero => intro i o h1 h2 rw[hL,if_neg א,<-h1] exact h2 | succ d ih => intro i o h1 h2 rw[hL,if_pos א,<-qHt,<-qHt] exact ih (i+1) _ א (hs i o א h2) lemma qHv {f:USize->UInt32} {sc sy sx W H c N:Nat} {sp:Nat->UInt32} (hN:c*(H*W)=N) (hs:∀ ch y t,ch< c->y< H->t< W → f (ch*sc+y*sy+t*sx).toUSize=sp (ch*(H*W)+y*W+t)) (o:Array UInt32) (ho:G_P N sp o 0) : G_P N sp (G_c f sc.toUSize sy.toUSize sx.toUSize W H c 0 0 0 o) N:=by have:=qEq (L:=G_c f sc.toUSize sy.toUSize sx.toUSize W H c) (sb:=sc) (S:=H*W) (n:=c) (N:=N) (q0:=0) (b0:=0) (sp:=sp) (λ_ _ _ _=>by rw[G_c]) (λch o a1 h=>by have h4:(ch+1)*(H*W)<=N:=hN ▸ З _ a1 have:=qEq (L:=G_y f sy.toUSize sx.toUSize W H) (sb:=sy) (S:=W) (n:=H) (N:=N) (q0:=ch*(H*W)) (b0:=ch*sc) (sp:=sp) (λ_ _ _ _=>by rw[G_y]) (λy o a2 h=>by have h5:(y+1)*W<=H*W:=З _ a2 rw[ذ] at h4 h5 have:=qEq (L:=G_x f sx.toUSize W) (g:=λb q o=>wrW o q (f b)) (sb:=sx) (S:=1) (n:=W) (N:=N) (q0:=ch*(H*W)+y*W) (b0:=ch*sc+y*sy) (sp:=sp) (λ_ _ _ _=>by rw[G_x];rfl) (λi o a3 h=>by have e1:ch*(H*W)+y*W+(i+1)*1 =ch*(H*W)+y*W+i*1+1 :=א have e2:ch*(H*W)+y*W+i=ch*(H*W)+y*W+i*1 :=א rw[e1,hs ch y i a1 a2 a3,e2] exact qIt h א _ (qAd _ (by have:=h.2.1;omega))) W 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) H 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) c 0 o א (by simpa using ho) simp only[Nat.zero_mul,Nat.zero_add,Nat.add_zero,hN] at this exact this lemma qHs {c H W q:Nat} (hq:q< c*(H*W)) : q/(H*W)< c∧q/W%H< H∧q%W< W:=by have hp:0< H*W:=С (by rintro e;simp[e] at hq);exact ⟨у (by rwa [л]),Ы _ (Nat.pos_of_mul_pos_right hp),Ы _ (з hp)⟩ lemma qHf (f:USize->UInt32) (c h w k:Nat) (hs:c*h*w<2^32) : G_c f (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Array.replicate (c*(h/k*(w/k))) 0) = Array.ofFn (n:=c*(h/k*(w/k))) λq =>let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) f ((ch*h+y*k)*w+x*k).toUSize:=by have hN:c*(h/k*(w/k))<2^32:=by have h1:h/k*(w/k)<=h*w:=Nat.mul_le_mul (Nat.div_le_self _ _) (Nat.div_le_self _ _) exact lt_of_le_of_lt ((Nat.mul_le_mul_left c h1).trans (Nat.mul_assoc c h w).symm.le) hs generalize hH:h/k=H at * generalize hW:w/k=W at * have hp:=qHv (f:=f) (sc:=h*w) (sy:=k*w) (sx:=k) (W:=W) (H:=H) (c:=c) (sp:=λq=>f ((q/(H*W)*h+q/W%H*k)*w+q%W*k).toUSize) rfl (λch y t h1 h2 h3=>by obtain ⟨d1,d2⟩:=qBo W (H*ch+y) t h3 obtain ⟨d3,d4⟩:=qBo H ch y h2 have e:ch*(H*W)+y*W+t=W*(H*ch+y)+t:=by ring rw[e,d2,л H W,<-я,d1,d3,d4] congr 2 ring) (Array.replicate (c*(H*W)) 0) ⟨by simp,hN,by simp⟩ apply ъ (by rw[hp.1,Array.size_ofFn]) intro i h1 h2 rw[<-Е _ i h1,hp.2.2 i (by simpa using h2),Array.getElem_ofFn] lemma qGk {c h w k ch y x:Nat} {X:Array UInt32} (h1:c*h*w<2^32) (h2:X.size<2^32) (h3:k*k<2^32) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (t:USize) (ht:t.toNat< k*k) : rdW X (((ch*h+y*k)*w+x*k).toUSize+(rdW (E_off k w) t).toUSize) =poolAt h w k X ch y x t.toNat:=by have h4:=qEt hc hy hx ht have e0:(ch*h+y*k+t.toNat/k)*w+x*k+t.toNat%k =(ch*h+y*k)*w+x*k+(t.toNat/k*w+t.toNat%k):=by ring rw[e0] at h4 have hz:(E_off k w).size<2^32:=by rw[E_off,Array.size_ofFn];exact h3 rw[qAt _ _ hz,E_off,qAf (k*k) (λt=>(t/k*w+t%k).toUInt32) _ ht,qAt _ _ h2] have e1:((t.toNat/k*w+t.toNat%k).toUInt32.toUSize).toNat =t.toNat/k*w+t.toNat%k:=by rw[О,Nat.toUInt32_eq,ш] exact Г א have e2:((ch*h+y*k)*w+x*k).toUSize.toNat=(ch*h+y*k)*w+x*k :=qAd _ א show _=X[(ch*h+y*k+t.toNat/k)*w+x*k+t.toNat%k]! rw[qAl _ _ (by rw[e1,e2];omega),e1,e2,e0] lemma qIf {c h w k ch y x:Nat} {X:Array UInt32} (h1:c*h*w<=X.size) (h2:X.size<2^32) (h3:k*k<2^20) (hg:∀ i,i< X.size->good X[i]! = true) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (j:UInt32) (hj:j=255∨(j=2∧k*k=4)∨(j=6∧k*k=64)) : E_win X (E_off k w) k w (k*k) j (E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))) (dv oneW) (nfOf (k*k)) (dv 0) ((ch*h+y*k)*w+x*k).toUSize (k*k).toUSize 0 0 0 0 = E_div (k*k) (dotOf (k*k) (poolAt h w k X ch y x) (λ_=>oneW)):=by have hn:(k*k).toUSize.toNat=k*k:=qAd _ א have hk:0< k*k:=by rcases Nat.eq_zero_or_pos k with rfl | h0 . simp at hy . exact Nat.mul_pos h0 h0 have hI:∀ t,t< k*k->(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w :=λt ht=>qEt hc hy hx ht have e2:((ch*h+y*k)*w+x*k).toUSize.toNat=(ch*h+y*k)*w+x*k :=qAd _ (by have:=hI 0 hk;simp at this;omega) rw[qGb X _ k w (k*k) j _ _ _ _ _ (poolAt h w k X ch y x) (λt ht=>qGk א h2 א hc hy hx t (by rw[А,hn] at ht;exact ht)) (k*k) 0 0 0 0 (by rw[hn,show (0:USize).toNat=0 from rfl,Й]), show (0:USize).toNat=0 from rfl,<-List.range_eq_range'] refine qGp X _ k w _ h3 j hj _ (λt ht=>hg _ (by have:=hI t ht;omega)) ?_ unfold E_dot rw[qAc] congr 1 funext t rw[e2] show _=X[(ch*h+y*k+t/k)*w+x*k+t%k]! congr 1 ring lemma qIq:∀ k,k<17->0< k->E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))=0:=by decide +kernel lemma qGq:avgF=avgpoolSpec:=by funext c h w k X;unfold avgF;split . rename_i hc;obtain ⟨h1,h2,h3,h4⟩:=hc;simp only[avgpoolSpec,avgpoolWith] rw[show (if 0< k∧k<17 then (0:UInt32) else E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)))=E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)) by split;. rename_i e;exact (qIq k e.2 e.1).symm . rfl] rw[E_grid,qHf _ c h w k א];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHs hq;dsimp only;refine qIf h1 h2 h3 (qAz X h2 h4).2.2.1 a1 a2 a3 _ ?_;split_ifs with e1 e2;. exact Or.inr (Or.inl ⟨rfl,e1⟩) . exact Or.inr (Or.inr ⟨rfl,e2⟩) . exact Or.inl rfl . rfl end section open ExactRuntime def linR (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32):Array UInt32 := if rows=1 then convF ⟨inW,1,1,outW,1,1,1,0⟩ relu X W b else linearWith dotFn rows inW outW relu X W b lemma qIm (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) : linearSpec 1 inW outW relu X W b=conv2dSpec ⟨inW,1,1,outW,1,1,1,0⟩ relu X W b:=by simp only[linearSpec,linearWith,conv2dSpec,conv2dWith,ConvShape.ho,ConvShape.wo];apply ъ;. simp . intro q h1 h2;have hq:q< outW:=by simpa using h1 simp only[Array.getElem_ofFn];have a1:q/outW=0:=п hq;have a2:q%outW=q:=Г hq;have e2:(1+2*0-1)/1+1=1:=by simp simp only[a1,a2,e2,windowAt,Nat.mul_one,Nat.div_one,Nat.mod_one,Nat.zero_mul,Nat.zero_add,Nat.add_zero,Nat.sub_zero];congr 2 lemma qG3:linR=linearSpec:=by funext rows inW outW relu X W b;unfold linR;split;. subst_vars;rw[qIm,qCg] . rw[qAc];rfl end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats @[inline] def ordW (w:Word):UInt32 := let m:=w &&& 0x7FFFFFFF if w >>> (31:UInt32) == 0 then 0x80000000+m else 0x80000000-m @[inline] def maxF (a b:Word):Word := if a &&& 0x7F800000 == 0x7F800000 || b &&& 0x7F800000 == 0x7F800000 then maxW a b else let p:=ordW a let q:=ordW b if p< q then b else if q< p then a else if a == b then a else maxW a b def M_K (m:Nat):Nat := if m/2^23=0 then m%2^23 else (2^23+m%2^23)*2^(m/2^23-1) lemma qB8:StrictMono M_K:=by refine strictMono_nat_of_lt_succ λm=>?_;unfold M_K;by_cases hc:m%2^23+1<2^23 . rw[show (m+1)/2^23=m/2^23 by omega,show (m+1)%2^23=m%2^23+1 by omega];split;. omega . exact Щ א (Nat.two_pow_pos _) . rw[show (m+1)/2^23=m/2^23+1 by omega,show (m+1)%2^23=0 by omega,show m%2^23=2^23-1 by omega];cases m/2^23 with | zero=>norm_num | succ e => have:=Nat.two_pow_pos e;rw[Д א,Д א,Nat.add_sub_cancel,Nat.add_sub_cancel,pow_succ];omega lemma qDb (w:Word):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31 ∧ (expOf w).toNat=w.toNat%2^31/2^23∧(fracOf w).toNat=w.toNat%2^31%2^23:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x7FFFFFFF:UInt32).toNat=2^31-1:=rfl;have a2:(0xFF:UInt32).toNat=2^8-1:=rfl;have a3:(0x7FFFFF:UInt32).toNat=2^23-1:=rfl;have a4:(23:UInt32).toNat%32=23:=rfl;unfold expOf fracOf;rw[Ф,Ф,Ф,и,a1,a2,a3,a4,В,В,В,Я];omega lemma qE3 (w:Word):signOf w=true ↔ 2^31<=w.toNat:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x80000000:UInt32).toNat=2^31:=rfl;unfold signOf;rw[bne_iff_ne,ne_eq,<-И,Ф,a1,Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];by_cases c:w.toNat/2^31%2=1;. rw[decide_eq_true c];simp only[Bool.toNat_true,UInt32.toNat_zero];omega . rw[decide_eq_false c];simp only[Bool.toNat_false,UInt32.toNat_zero];omega lemma qHo (w:Word):mantOf w <<< idxOf w=M_K (w.toNat%2^31):=by obtain ⟨-,h1,h2⟩:=qDb w;unfold mantOf idxOf M_K;rw[р];by_cases h:expOf w=0;. rw[М h,М h,М (by rw[<-h1,h];rfl),h2,pow_zero,Nat.mul_one] . rw[Д h,Д h,Д (λe=>h (И.mp (by rw[h1,e];rfl))),h1,h2] lemma qGd (w:Word):keyOf w=if w.toNat<2^31 then (M_K (w.toNat%2^31):Int) else -(M_K (w.toNat%2^31):Int):=by unfold keyOf;rw[qHo];by_cases c:w.toNat<2^31;. rw[М c,Д (λe=>by have:=(qE3 w).mp e;omega)] . rw[Д c,М ((qE3 w).mpr א)] lemma qGn (w:Word):(ordW w).toNat =if w.toNat<2^31 then 2^31+w.toNat%2^31 else 2^31-w.toNat%2^31:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x80000000:UInt32).toNat=2^31:=rfl;have a2:(31:UInt32).toNat%32=31:=rfl;have e:(w >>> (31:UInt32)).toNat=w.toNat/2^31:=by rw[и,a2,Я] have hm:=(qDb w).1;unfold ordW;dsimp only;by_cases c:w.toNat<2^31;. have e0:w >>> (31:UInt32)=0:=И.mp (by rw[e,UInt32.toNat_zero];omega);rw[e0,М (show ((0:UInt32) == 0)=true from rfl),М c,UInt32.toNat_add,hm,a1];omega . have e0:(w >>> (31:UInt32) == 0)=false:=by rw[beq_eq_false_iff_ne];intro h0;rw[h0,UInt32.toNat_zero] at e;omega rw[e0,Д ג,Д c,UInt32.toNat_sub,hm,a1];omega lemma qHn (a b:Word) (h:ordW a< ordW b):keyOf a< keyOf b:=by have h':=м.mp h;rw[qGn,qGn] at h';rw[qGd,qGd];have ha:a.toNat<2^32:=a.toNat_lt;have hb:b.toNat<2^32:=b.toNat_lt;have p0:∀ m,0< m->0< M_K m:=λm hm=>by have:=qB8 hm;omega by_cases c1:a.toNat<2^31<;>by_cases c2:b.toNat<2^31 <;>simp only[c1,c2,ite_true,ite_false] at h' ⊢;. have:=qB8 (א:a.toNat%2^31< b.toNat%2^31);omega . omega . by_cases hm:0< a.toNat%2^31;. have:=p0 _ hm;omega . have:=p0 (b.toNat%2^31) א;omega . have:=qB8 (א:b.toNat%2^31< a.toNat%2^31);omega lemma qH2 (x m n:Nat):x &&& (m <<< n)=((x >>> n) &&& m) <<< n:=by apply Nat.eq_of_testBit_eq;intro i;simp only[Nat.testBit_and,Nat.testBit_shiftLeft,Nat.testBit_shiftRight];by_cases h:n<=i;. simp[h,Nat.add_sub_cancel' h] . simp[h] lemma qGi (w:Word) (h:¬ (w &&& 0x7F800000 == 0x7F800000)=true):expOf w≠255:=by intro e;apply h;have e1:=(qDb w).2.1;rw[e,show (255:UInt32).toNat=255 from rfl] at e1;rw[beq_iff_eq,<-И,Ф,show (0x7F800000:UInt32).toNat=255 <<< 23 from rfl,qH2,show (255:Nat)=2^8-1 from rfl,В,Я];have:w.toNat<2^32:=w.toNat_lt;rw[(א:w.toNat/2^23%2^8=2^8-1)] lemma qHj (a:Word) (hz:Model.isZero (ofW a)=true):a=0∨a=0x80000000:=by obtain ⟨-,h1,h2⟩:=qDb a;have h:a.toNat<2^32:=a.toNat_lt;have e:ofW a=_:=ofUInt32_eq_ofFields a;rw[e,Model.isZero_ofFields Fmt _ _ _ (expOf_toNat_lt a) (fracOf_toNat_lt a)] at hz;have hz':((expOf a).toNat == 0 && (fracOf a).toNat == 0)=true:=hz;simp only[Bool.and_eq_true,beq_iff_eq] at hz';rw[h1,h2] at hz';by_cases c:a.toNat<2^31;. exact Or.inl (И.mp (by rw[UInt32.toNat_zero];omega)) . exact Or.inr (И.mp (by rw[show (0x80000000:UInt32).toNat=2^31 from rfl];omega)) lemma qH0 (a:Word) (ha:expOf a≠255):maxW a a=a:=by unfold maxW;rw[Д (by simpa using ha),compare_eq_iff_eq.mpr rfl];dsimp only;by_cases hz:Model.isZero (ofW a)=true;. rcases qHj a hz with rfl | rfl<;>decide . rw[Д (by simpa using hz)];exact qCk a lemma qG4 (a b:Word):maxF a b=maxW a b:=by unfold maxF;by_cases g:(a &&& 0x7F800000 == 0x7F800000 || b &&& 0x7F800000 == 0x7F800000)=true;. rw[М g] . rw[Д g];simp only[Bool.or_eq_true,not_or] at g;have ha:=qGi a g.1;have hb:=qGi b g.2;have hn:¬ (expOf a=255∨expOf b=255):=by simp[ha,hb] dsimp only;by_cases p:ordW a< ordW b;. rw[М p];unfold maxW;rw[Д hn,compare_lt_iff_lt.mpr (qHn a b p)] . rw[Д p];by_cases q:ordW b< ordW a;. rw[М q];unfold maxW;rw[Д hn,compare_gt_iff_gt.mpr (qHn b a q)] . rw[Д q];by_cases e:(a == b)=true;. rw[М e,eq_of_beq e,qH0 b hb] . rw[Д e] def mpWin (X off:Array UInt32) (b m t:USize) (acc:UInt32):UInt32 := if h:t< m then mpWin X off b m (t+1) (maxF acc (rdW X (b+(rdW off t).toUSize))) else acc termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega def maxpoolF (c h w k:Nat) (inp:Array Word):Array Word := if c*h*w<2^32∧inp.size<2^32∧k*k<2^32 then let off:=E_off k w G_c (λb=>mpWin inp off b (k*k).toUSize 0 (rdW inp b)) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Array.replicate (c*(h/k*(w/k))) 0) else maxpoolW c h w k inp lemma qFs (X off:Array UInt32) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat) : ∀ (t:USize) (a:UInt32),t.toNat+d=m.toNat → mpWin X off b m t a=(List.range' t.toNat d).foldl (λa i=>maxF a (xw i)) a:=by induction d with | zero => intro t a h rw[mpWin,Л (by rw[А];omega)] rfl | succ d ih => intro t a h have ht:t< m:=by rw[А];omega rw[mpWin,Н ht,hx t ht,ih (t+1) _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ,List.foldl_cons] lemma qDu:maxpoolF=maxpoolSpec:=by rw[<-qDy];funext c h w k inp;unfold maxpoolF;split . rename_i hc;obtain ⟨h1,h2,h3⟩:=hc;rw[qHf _ c h w k h1];simp only[maxpoolW];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHs hq;dsimp only;have hn:(k*k).toUSize.toNat=k*k:=qAd _ h3 have e:∀ {ch y x:Nat},ch< c->y< h/k->x< w/k → rdW inp ((ch*h+y*k)*w+x*k).toUSize=poolAt h w k inp ch y x 0:=by intro ch y x b1 b2 b3 have h4:=qEt (t:=0) b1 b2 b3 (by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at b2 . exact Nat.mul_pos h0 h0) simp only[Nat.zero_div,Nat.zero_mod,Nat.add_zero] at h4;rw[qAt _ _ h2,qAd _ א];simp[poolAt] rw[e a1 a2 a3,qFs inp _ _ _ (poolAt h w k inp _ _ _) (λt ht=>qGk h1 h2 h3 a1 a2 a3 t (by rw[А,hn] at ht;exact ht)) (k*k) 0 _ (by rw[hn,show (0:USize).toNat=0 from rfl,Й]),show (0:USize).toNat=0 from rfl,<-List.range_eq_range'];simp only[qG4] . rfl end def kernels:Kernels where core:=⟨sumX,dotX⟩ dense:=⟨linR,gemmWith dotFn⟩ conv:=⟨convF,depthF⟩ elem:=⟨avgF,maxpoolF,addF,scaleF⟩ norm:=⟨rmsnormWith dotFn,layernormWith dotFn⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specCore,specDense,specConv,specElem,specNorm,gemmSpec,avgpoolSpec,rmsnormSpec,layernormSpec,qG3,qCg,qDv,qGc,qFm,qGq,qDu,qAc,funext qDg,funext λa=>funext (qDt a)] end Submission