import Tablet.DecimalSeqRoundArbitraryEndpoint import Tablet.ParserSlowFirstRoundSeparation import Tablet.ParserSlowCarryRoundSeparation import Tablet.ParserExponentConstants import Tablet.ModelU64BitAnd import Tablet.ModelU64BitOr import Tablet.ModelU64Shl import Tablet.P804_T1_DecimalValueBounds import Tablet.DecimalRightShiftLoop0Execution import Tablet.num_imp_dec2flt_slow_parse_long_mantissa open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxRecDepth 100000 set_option maxHeartbeats 800000 namespace dec2flt_model -- [TABLET NODE: ParserSlowFinalEncoding] theorem ParserSlowFinalEncoding : forall (x : DecimalLexeme) (B : Real) (tr : num.imp.dec2flt.Dec2FltAssemblyTrace F64) (d d0 d1 d2 d3 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 exp1 expStart exp2 : Std.I32), num.imp.dec2flt.decimal_seq.parse_decimal_seq tr.body = ok d -> d.num_digits.val != 0 -> -324 <= d.decimal_point.val -> d.decimal_point.val < 310 -> num.imp.dec2flt.slow.parse_long_mantissa F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 tr.body = ok tr.selected_pair -> DecimalGoalVocabularyDefault.assembledWord tr.selected_pair tr.assembly_result -> num.imp.dec2flt.slow.parse_long_mantissa_loop0 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 0#i32 } d.num_digits d.decimal_point d.truncated d.digits 0#i32 none = ok (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none) -> num.imp.dec2flt.slow.parse_long_mantissa_loop1 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 2047#i32 } d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none = ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits, exp1, none) -> num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (d2, exp2) -> exp1 - 1#i32 = ok expStart -> expStart.val = exp1.val - 1 -> d1.decimal_point.val = 0 -> d1.num_digits.val != 0 -> 5 <= d1.digits.val[0].val -> -1079 <= exp1.val -> B / (2 : Real) ^ exp1.val <= 1 -> exp2.val = max expStart.val (-1022) -> (exp2.val - expStart.val).toNat <= 58 -> let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x let digits2 := (d2.digits.val.take d2.num_digits.val).map (fun z => z.val) let integer2 : Nat := digits2.foldl (fun n digit => 10 * n + digit) 0 let value2 : Real := integer2 * (10 : Real) ^ (d2.decimal_point.val - (d2.num_digits.val : Int)) d2.num_digits.val <= 768 /\ (forall z, z ∈ d2.digits.val.take d2.num_digits.val -> z.val < 10) /\ (exists z zs, digits2 = z :: zs /\ 1 <= z /\ z < 10 /\ digits2.length = d2.num_digits.val) /\ (exists eta2 u2 : Real, A / (2 : Real) ^ (exp2.val + 1) = value2 + eta2 /\ B / (2 : Real) ^ (exp2.val + 1) = value2 + u2 /\ 0 <= eta2 /\ eta2 < u2 /\ (d2.truncated = true <-> 0 < eta2)) -> num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d2 53#usize = ok d3 -> let digits3 := (d3.digits.val.take d3.num_digits.val).map (fun z => z.val) let integer3 : Nat := digits3.foldl (fun n digit => 10 * n + digit) 0 let value3 : Real := integer3 * (10 : Real) ^ (d3.decimal_point.val - (d3.num_digits.val : Int)) d3.num_digits.val <= 768 /\ (forall z, z ∈ d3.digits.val.take d3.num_digits.val -> z.val < 10) /\ (exists z zs, digits3 = z :: zs /\ 1 <= z /\ z < 10 /\ digits3.length = d3.num_digits.val /\ digits3.getLast? != some 0) /\ (exists eta3 u3 : Real, A / (2 : Real) ^ (exp2.val - 52) = value3 + eta3 /\ B / (2 : Real) ^ (exp2.val - 52) = value3 + u3 /\ 0 <= eta3 /\ eta3 < u3 /\ (d3.truncated = true <-> 0 < eta3)) /\ d3.decimal_point.val < 19 /\ 0 <= A / (2 : Real) ^ (exp2.val - 52) /\ A / (2 : Real) ^ (exp2.val - 52) < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) /\ (forall n : Nat, (n : Real) + (1 / 2 : Real) <= value3 \/ B / (2 : Real) ^ (exp2.val - 52) <= (n : Real) + (1 / 2 : Real)) -> (forall d4 : num.imp.dec2flt.decimal_seq.DecimalSeq, num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d3 1#usize = ok d4 -> d4.num_digits.val != 0 -> let digits4 := (d4.digits.val.take d4.num_digits.val).map (fun z => z.val) let integer4 : Nat := digits4.foldl (fun n digit => 10 * n + digit) 0 let value4 : Real := integer4 * (10 : Real) ^ (d4.decimal_point.val - (d4.num_digits.val : Int)) d4.num_digits.val <= 768 /\ (forall z, z ∈ d4.digits.val.take d4.num_digits.val -> z.val < 10) /\ (exists z zs, digits4 = z :: zs /\ 1 <= z /\ z < 10 /\ digits4.length = d4.num_digits.val /\ digits4.getLast? != some 0) /\ (exists eta4 u4 : Real, A / (2 : Real) ^ (exp2.val - 51) = value4 + eta4 /\ B / (2 : Real) ^ (exp2.val - 51) = value4 + u4 /\ 0 <= eta4 /\ eta4 < u4 /\ (d4.truncated = true <-> 0 < eta4)) /\ d4.decimal_point.val < 19 /\ 0 <= A / (2 : Real) ^ (exp2.val - 51) /\ A / (2 : Real) ^ (exp2.val - 51) < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) /\ forall n : Nat, (n : Real) + (1 / 2 : Real) <= value4 \/ B / (2 : Real) ^ (exp2.val - 51) <= (n : Real) + (1 / 2 : Real)) -> let unit : Nat := 2 ^ 52 let infinityBits : Nat := 2047 * unit let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) let raw : Nat := Nat.lor tr.selected_pair.m.val (((tr.selected_pair.p_biased.val.emod (2 ^ 64)).toNat * unit) % (2 ^ 64)) (raw = infinityBits /\ threshold <= A) \/ (exists j : Nat, j <= 2 ^ 53 /\ raw = j /\ A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\ A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) + (2 : Real) ^ (-(1075 : Int)) /\ (j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A + (2 : Real) ^ (-(1075 : Int)) /\ (A = ((j : Real) + (1 / 2 : Real)) * (2 : Real) ^ (-(1074 : Int)) \/ A = ((j : Real) - (1 / 2 : Real)) * (2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/ (exists p j : Nat, 2 <= p /\ p <= 2046 /\ 2 ^ 52 <= j /\ j <= 2 ^ 53 /\ (p < 2046 \/ j < 2 ^ 53) /\ raw = p * unit + (j - unit) /\ let delta : Real := (2 : Real) ^ ((p : Int) - 1075) (unit : Real) * delta <= A /\ A <= (2 ^ 53 : Nat) * delta /\ A < threshold /\ A <= (j : Real) * delta + delta / 2 /\ (j : Real) * delta <= A + delta / 2 /\ (A = ((j : Real) + (1 / 2 : Real)) * delta \/ A = ((j : Real) - (1 / 2 : Real)) * delta -> j % 2 = 0)) := by -- BODY dsimp only intro x B tr d d0 d1 d2 d3 exp0 exp1 expStart exp2 hparse hdne hdpLower hdpUpper hslow hassembled hloop0 hloop1 hloop2 hsub hstart hd1Point hd1ne hd1Lead hexp1Lower hBUpper hexp2 hshiftBound hstate2 hleft53 hstate3 hcarry classical let A := DecimalGoalVocabularyDefault.accumulatedMagnitude x let digits2 := (d2.digits.val.take d2.num_digits.val).map (fun z => z.val) let integer2 : Nat := digits2.foldl (fun n digit => 10 * n + digit) 0 let value2 : Real := integer2 * (10 : Real) ^ (d2.decimal_point.val - (d2.num_digits.val : Int)) let digits3 := (d3.digits.val.take d3.num_digits.val).map (fun z => z.val) let integer3 : Nat := digits3.foldl (fun n digit => 10 * n + digit) 0 let value3 : Real := integer3 * (10 : Real) ^ (d3.decimal_point.val - (d3.num_digits.val : Int)) rcases hstate2 with ⟨hcap2, hdigit2, hshape2, hend2⟩ rcases hshape2 with ⟨z2, zs2, hshape2, hz2lo, hz2hi, hlen2⟩ rcases hend2 with ⟨eta2, u2, hA2, hB2, heta2, hetau2, hsticky2⟩ rcases hstate3 with ⟨hcap3, hdigit3, hshape3, hend3, hpoint3, hA3nonneg, hA3bound, hsep3⟩ rcases hshape3 with ⟨z3, zs3, hshape3, hz3lo, hz3hi, hlen3, hlast3⟩ rcases hend3 with ⟨eta3, u3, hA3, hB3, heta3, hetau3, hsticky3⟩ have hround3 := DecimalSeqRoundArbitraryEndpoint d3 (A / (2 : Real) ^ (exp2.val - 52)) (B / (2 : Real) ^ (exp2.val - 52)) ⟨hcap3, hdigit3, Or.inr ⟨z3, zs3, hshape3, hz3lo, hz3hi, hlen3, hlast3⟩, ⟨eta3, u3, hA3, hB3, heta3, hetau3, hsticky3⟩⟩ hpoint3 hA3nonneg hA3bound hsep3 obtain ⟨mantissa, hround3Call, hround3Upper, hround3Lower, hround3Even⟩ := hround3 have hinteger2 : 0 < integer2 := by dsimp only [integer2, digits2] rw [hshape2] simp only [List.foldl_cons] have hrest (ys : List Nat) (a : Nat) : a <= ys.foldl (fun n digit => 10 * n + digit) a := by induction ys generalizing a with | nil => simp | cons q qs ih => simp only [List.foldl_cons] exact le_trans (by omega) (ih (10 * a + q)) have := hrest zs2 (10 * 0 + z2) omega have hvalue2pos : 0 < value2 := by exact mul_pos (by exact_mod_cast hinteger2) (zpow_pos (by norm_num) _) have hpow2pos : 0 < (2 : Real) ^ (exp2.val + 1) := zpow_pos (by norm_num) _ have hAB : A < B := by apply (div_lt_div_iff_of_pos_right hpow2pos).mp rw [hA2, hB2] linarith have hBpos : 0 < B := by have hquot : 0 < B / (2 : Real) ^ (exp2.val + 1) := by rw [hB2] linarith rcases div_pos_iff.mp hquot with h | h · exact h.1 · exact (not_lt_of_ge hpow2pos.le h.2).elim have hexpOrder : exp1.val <= exp2.val + 1 := by rw [hexp2, hstart] omega have hpowOrder : (2 : Real) ^ exp1.val <= (2 : Real) ^ (exp2.val + 1) := zpow_le_zpow_right₀ (by norm_num) hexpOrder have hB2Upper : B / (2 : Real) ^ (exp2.val + 1) <= 1 := by exact (div_le_div_of_nonneg_left hBpos.le (zpow_pos (by norm_num) _) hpowOrder).trans hBUpper have hA2Upper : A / (2 : Real) ^ (exp2.val + 1) < 1 := by have hlt : A / (2 : Real) ^ (exp2.val + 1) < B / (2 : Real) ^ (exp2.val + 1) := (div_lt_div_iff_of_pos_right hpow2pos).2 hAB exact hlt.trans_le hB2Upper have hscale53 (C : Real) : C / (2 : Real) ^ (exp2.val - 52) = (C / (2 : Real) ^ (exp2.val + 1)) * (2 : Real) ^ (53 : Nat) := by rw [show exp2.val + 1 = (exp2.val - 52) + 53 by omega] rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num only [zpow_natCast] field_simp have hA3Upper53 : A / (2 : Real) ^ (exp2.val - 52) < (2 : Real) ^ (53 : Nat) := by rw [hscale53] have hp53 : 0 < (2 : Real) ^ (53 : Nat) := by positivity nlinarith have hmantissaLe : mantissa.val <= 2 ^ 53 := by have hreal : (mantissa.val : Real) < ((2 ^ 53 : Nat) : Real) + 1 := calc (mantissa.val : Real) <= A / (2 : Real) ^ (exp2.val - 52) + 1 / 2 := hround3Lower _ < ((2 ^ 53 : Nat) : Real) + 1 := by norm_num at hA3Upper53 ⊢ linarith have hnat : mantissa.val < (2 ^ 53 : Nat) + 1 := by exact_mod_cast hreal omega have hexp2Lower : -1022 <= exp2.val := by rw [hexp2] exact le_max_right _ _ rcases ParserExponentConstants with ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin, hsmall, hlarge, hroundmin, hroundmax⟩ have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 = ok { m := 0#u64, p_biased := 0#i32 } := by rfl have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 = ok { m := 0#u64, p_biased := 2047#i32 } := by rfl have hdword : d.num_digits ≠ 0#usize := by intro h have := congrArg UScalar.val h simp at this have hdne' : d.num_digits.val ≠ 0 := by simpa [bne_iff_ne] using hdne exact hdne' this have hdpNotLow : ¬ d.decimal_point < (-324)#i32 := by change ¬ d.decimal_point.val < -324 omega have hdpNotHigh : ¬ d.decimal_point >= 310#i32 := by change ¬ 310 <= d.decimal_point.val omega have hnormalizedLower (he : -1022 < exp2.val) : (1 / 2 : Real) <= A / (2 : Real) ^ (exp2.val + 1) := by have hstartHigh : -1022 <= expStart.val := by rw [hexp2] at he omega have hbody : num.imp.dec2flt.slow.parse_long_mantissa_loop2.body F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (done (d1, expStart)) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2.body rw [hmin] simp only [bind_tc_ok] have hnot : ¬ ((-1022)#i32 : Std.I32) > expStart := by change ¬ (-1022 : Int) > expStart.val omega simp only [hnot, if_false] have hloopIdentity : num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart = ok (d1, expStart) := by unfold num.imp.dec2flt.slow.parse_long_mantissa_loop2 rw [Aeneas.Std.loop.eq_1] simpa only [hbody] have hpair := Result.ok.inj (hloopIdentity.symm.trans hloop2) have hd2eq : d2 = d1 := (congrArg Prod.fst hpair).symm have he2eq : exp2 = expStart := (congrArg Prod.snd hpair).symm subst d2 have hpoint2 : d1.decimal_point.val = 0 := by exact hd1Point have hnum2pos : 0 < d1.num_digits.val := by rw [← hlen2, hshape2] simp have hindexBound : (0 : Nat) < d1.digits.val.length := by rw [d1.digits.property] norm_num have htake : (d1.digits.val.take d1.num_digits.val)[0]? = some d1.digits.val[0] := by rw [List.getElem?_take, if_pos hnum2pos] exact List.getElem?_eq_getElem hindexBound have hhead := congrArg List.head? hshape2 rw [List.head?_eq_getElem?] at hhead simp only [List.getElem?_map, htake] at hhead have hz2eq : z2 = d1.digits.val[0].val := by exact (Option.some.inj hhead).symm have hz2five : 5 <= z2 := by rw [hz2eq] exact hd1Lead have hdigits2 : forall q, q ∈ digits2 -> q < 10 := by intro q hq simp only [digits2, List.mem_map] at hq rcases hq with ⟨q8, hq8, rfl⟩ exact hdigit2 q8 hq8 have hv := P804_T1_DecimalValueBounds digits2 z2 zs2 hshape2 hz2lo hdigits2 d1.decimal_point.val dsimp only at hv rw [hlen2] at hv have hhalfValue : (1 / 2 : Real) <= value2 := by calc (1 / 2 : Real) <= (z2 : Real) * (10 : Real) ^ (-1 : Int) := by have hzR : (5 : Real) <= (z2 : Real) := by exact_mod_cast hz2five rw [zpow_neg] norm_num linarith _ <= value2 := by simpa [value2, integer2, hpoint2] using hv.1 rw [hA2] exact hhalfValue.trans (by linarith) let infPower := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.INFINITE_POWER have hinf' : infPower = ok 2047#i32 := hinf let expMin := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.EXP_MIN have hmin' : expMin = ok (-1022)#i32 := hmin have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y, Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by intro α β r f y hr cases r with | fail err => simp [Bind.bind, Aeneas.Std.bind] at hr | div => simp [Bind.bind, Aeneas.Std.bind] at hr | ok q => exact ⟨q, rfl, hr⟩ have hflow := hslow unfold num.imp.dec2flt.slow.parse_long_mantissa at hflow rw [hzero] at hflow simp only [bind_tc_ok] at hflow change (do let i ← infPower; _) = _ at hflow rw [hinf'] at hflow simp only [bind_tc_ok] at hflow rw [hinfFp, hparse] at hflow simp only [bind_tc_ok, hdword, if_false, hdpNotLow, hdpNotHigh, hloop0] at hflow change (do let (i3, i4, b1, a1, exp21, loop_result1) ← num.imp.dec2flt.slow.parse_long_mantissa_loop1 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 { m := 0#u64, p_biased := 2047#i32 } d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none match loop_result1 with | none => _ | some result => ok result) = _ at hflow rw [hloop1] at hflow simp only [bind_tc_ok] at hflow change (do let exp22 ← exp1 - 1#i32 let (q2, exp23) ← num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 exp22 _) = _ at hflow rw [hsub] at hflow simp only [bind_tc_ok] at hflow change (do let (q2, exp23) ← num.imp.dec2flt.slow.parse_long_mantissa_loop2 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart _) = _ at hflow rw [hloop2] at hflow simp only [bind_tc_ok] at hflow change (do let i5 ← expMin; _) = _ at hflow rw [hmin'] at hflow simp only [bind_tc_ok] at hflow obtain ⟨expDiff, hdiff, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨biased, hbiased, hflow⟩ := bind_ok_inv _ _ _ hflow have hdiffVal : expDiff.val = exp2.val - (-1022) := by have hs := IScalar.sub_equiv exp2 (-1022)#i32 rw [hdiff] at hs simpa using hs.2.1 have hbiasedVal : biased.val = exp2.val + 1023 := by have hs := IScalar.add_equiv expDiff 1#i32 rw [hbiased] at hs have hv := hs.2.1 rw [hdiffVal] at hv norm_num at hv omega by_cases hsat : biased >= 2047#i32 · simp only [hsat, if_true] at hflow have hselected : tr.selected_pair = ({ m := 0#u64, p_biased := 2047#i32 } : num.imp.dec2flt.common.BiasedFp) := (Result.ok.inj hflow).symm rw [hselected] left constructor · norm_num rw [Nat.mod_eq_of_lt (by decide)] rfl · have he : 1024 <= exp2.val := by change biased.val >= 2047 at hsat omega have hnorm := hnormalizedLower (by omega) have hmag : (1 / 2 : Real) * (2 : Real) ^ (exp2.val + 1) <= A := (le_div_iff₀ hpow2pos).mp hnorm have hpow : (2 : Real) ^ (1025 : Int) <= (2 : Real) ^ (exp2.val + 1) := zpow_le_zpow_right₀ (by norm_num) (by omega) calc (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= (2 : Real) ^ (1024 : Nat) := by exact sub_le_self _ (by positivity) _ = (1 / 2 : Real) * (2 : Real) ^ (1025 : Int) := by rw [show (2 : Real) ^ (1025 : Int) = (2 : Real) ^ (1024 : Nat) * 2 by rw [show (1025 : Int) = (1024 : Int) + 1 by omega, zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num rfl] ring _ <= (1 / 2 : Real) * (2 : Real) ^ (exp2.val + 1) := by nlinarith _ <= A := hmag · simp only [hsat, if_false] at hflow let sigBits := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_BITS have hsig' : sigBits = ok 52#u32 := hsig change (do let i8 ← sigBits; _) = _ at hflow rw [hsig'] at hflow simp only [bind_tc_ok] at hflow obtain ⟨shiftWidth, hcastWidth, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨shiftCount, haddCount, hflow⟩ := bind_ok_inv _ _ _ hflow have hwidthVal : shiftWidth.val = 52 := by simp only [lift] at hcastWidth have heq := Result.ok.inj hcastWidth have hv := congrArg UScalar.val heq simpa [UScalar.cast_val_eq] using hv.symm have hcountVal : shiftCount.val = 53 := by have hs := UScalar.add_equiv shiftWidth 1#usize rw [haddCount] at hs have hv := hs.2.1 rw [hwidthVal] at hv norm_num at hv ⊢ exact hv have hcount : shiftCount = 53#usize := UScalar.eq_of_val_eq (by simpa using hcountVal) obtain ⟨shifted, hleft, hflow⟩ := bind_ok_inv _ _ _ hflow have hshifted : shifted = d3 := by rw [hcount] at hleft exact Result.ok.inj (hleft.symm.trans hleft53) subst shifted obtain ⟨rounded, hround, hflow⟩ := bind_ok_inv _ _ _ hflow have hrounded : rounded = mantissa := Result.ok.inj (hround.symm.trans hround3Call) subst rounded obtain ⟨sigCount, haddSig, hflow⟩ := bind_ok_inv _ _ _ hflow have hsigCountVal : sigCount.val = 53 := by have hs := UScalar.add_equiv (52#u32 : Std.U32) 1#u32 rw [haddSig] at hs simpa using hs.2.1 have hsigCount : sigCount = 53#u32 := UScalar.eq_of_val_eq (by simpa using hsigCountVal) obtain ⟨carryLimit, hshiftLimit, hflow⟩ := bind_ok_inv _ _ _ hflow have hcarryLimitVal : carryLimit.val = 2 ^ 53 := by have hs := UScalar.ShiftLeft_spec (1#u64) sigCount (2 ^ 64) (by rw [hsigCount]; norm_num [U64.numBits]) (by norm_num [U64.size_eq]) rw [hshiftLimit] at hs have hv := hs.1 simp only [WP.spec_ok] at hv rw [hsigCount, Nat.shiftLeft_eq] at hv norm_num at hv ⊢ exact hv have hcarryLimit : carryLimit = 9007199254740992#u64 := UScalar.eq_of_val_eq (by norm_num; exact hcarryLimitVal) rw [hcarryLimit] at hflow have hdeltaPos : 0 < (2 : Real) ^ (exp2.val - 52) := zpow_pos (by norm_num) _ have hnearestUpper : A <= (mantissa.val : Real) * (2 : Real) ^ (exp2.val - 52) + (2 : Real) ^ (exp2.val - 52) / 2 := by calc A = (A / (2 : Real) ^ (exp2.val - 52)) * (2 : Real) ^ (exp2.val - 52) := by field_simp _ <= ((mantissa.val : Real) + 1 / 2) * (2 : Real) ^ (exp2.val - 52) := by gcongr _ = _ := by ring have hnearestLower : (mantissa.val : Real) * (2 : Real) ^ (exp2.val - 52) <= A + (2 : Real) ^ (exp2.val - 52) / 2 := by have hq : (mantissa.val : Real) - 1 / 2 <= A / (2 : Real) ^ (exp2.val - 52) := by linarith calc (mantissa.val : Real) * (2 : Real) ^ (exp2.val - 52) = ((mantissa.val : Real) - 1 / 2) * (2 : Real) ^ (exp2.val - 52) + (2 : Real) ^ (exp2.val - 52) / 2 := by ring _ <= (A / (2 : Real) ^ (exp2.val - 52)) * (2 : Real) ^ (exp2.val - 52) + (2 : Real) ^ (exp2.val - 52) / 2 := by gcongr _ = _ := by field_simp have hnearestEven : (A = ((mantissa.val : Real) + 1 / 2) * (2 : Real) ^ (exp2.val - 52) ∨ A = ((mantissa.val : Real) - 1 / 2) * (2 : Real) ^ (exp2.val - 52) -> mantissa.val % 2 = 0) := by intro htie apply hround3Even rcases htie with htie | htie · left apply (div_eq_iff (ne_of_gt hdeltaPos)).2 simpa [mul_comm] using htie · right apply (div_eq_iff (ne_of_gt hdeltaPos)).2 simpa [mul_comm] using htie by_cases hfirstCarry : mantissa >= 9007199254740992#u64 · simp only [hfirstCarry, if_true] at hflow have hmantissaCarry : mantissa.val = 2 ^ 53 := by change mantissa.val >= 9007199254740992 at hfirstCarry norm_num at hfirstCarry ⊢ omega have hnum3ne : d3.num_digits ≠ 0#usize := by intro heq have hv := congrArg UScalar.val heq norm_num at hv rw [← hlen3, hshape3] at hv simp at hv have hpoint3nonneg : 0 <= d3.decimal_point.val := by by_contra hn have hpword : d3.decimal_point < 0#i32 := by change d3.decimal_point.val < 0 omega unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round at hround3Call simp only [hnum3ne, if_false, hpword, if_true] at hround3Call have hz := congrArg UScalar.val (Result.ok.inj hround3Call.symm) rw [hmantissaCarry] at hz norm_num at hz obtain ⟨read4, acc4, d4good, _hprefix4, _hread4lo, _hread4hi, _hacc4lo, _hacc4hi, _hpros4lo, _hpros4hi, hrightGood, _hpoint4, hnonzeroGood⟩ := DecimalRightShiftLoop0Execution d3 1#usize (by norm_num) hpoint3nonneg (by omega) ⟨hcap3, hdigit3, z3, zs3, hshape3, hz3lo, hz3hi, by rw [← hshape3]; exact hlen3⟩ obtain ⟨d4, hright4, hflow⟩ := bind_ok_inv _ _ _ hflow have hd4 : d4 = d4good := Result.ok.inj (hright4.symm.trans hrightGood) have hnonzero4 : d4.num_digits.val != 0 := by rw [hd4] exact hnonzeroGood have hstate4 := hcarry d4 hright4 hnonzero4 rcases hstate4 with ⟨hcap4, hdigit4, hshape4, hend4, hpoint4, hA4nonneg, hA4bound, hsep4⟩ rcases hshape4 with ⟨z4, zs4, hshape4, hz4lo, hz4hi, hlen4, hlast4⟩ rcases hend4 with ⟨eta4, u4, hA4, hB4, heta4, hetau4, hsticky4⟩ have hround4 := DecimalSeqRoundArbitraryEndpoint d4 (A / (2 : Real) ^ (exp2.val - 51)) (B / (2 : Real) ^ (exp2.val - 51)) ⟨hcap4, hdigit4, Or.inr ⟨z4, zs4, hshape4, hz4lo, hz4hi, hlen4, hlast4⟩, ⟨eta4, u4, hA4, hB4, heta4, hetau4, hsticky4⟩⟩ hpoint4 hA4nonneg hA4bound hsep4 obtain ⟨mantissa4, hround4Call, hround4Upper, hround4Lower, hround4Even⟩ := hround4 obtain ⟨exp24, haddExp24, hflow⟩ := bind_ok_inv _ _ _ hflow have hexp24Val : exp24.val = exp2.val + 1 := by have hs := IScalar.add_equiv exp2 1#i32 rw [haddExp24] at hs exact hs.2.1 obtain ⟨rounded4, hround4', hflow⟩ := bind_ok_inv _ _ _ hflow have hrounded4 : rounded4 = mantissa4 := Result.ok.inj (hround4'.symm.trans hround4Call) subst rounded4 obtain ⟨expDiff4, hdiff4, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨biased4, hbiased4, hflow⟩ := bind_ok_inv _ _ _ hflow have hdiff4Val : expDiff4.val = exp24.val - (-1022) := by have hs := IScalar.sub_equiv exp24 (-1022)#i32 rw [hdiff4] at hs simpa using hs.2.1 have hbiased4Val : biased4.val = exp2.val + 1024 := by have hs := IScalar.add_equiv expDiff4 1#i32 rw [hbiased4] at hs have hv := hs.2.1 rw [hdiff4Val, hexp24Val] at hv norm_num at hv omega have hscaleHalf : A / (2 : Real) ^ (exp2.val - 51) = (A / (2 : Real) ^ (exp2.val - 52)) / 2 := by rw [show exp2.val - 51 = (exp2.val - 52) + 1 by omega, zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] rw [div_eq_mul_inv] ring have hX4Lower : ((2 ^ 52 : Nat) : Real) - 1 / 4 <= A / (2 : Real) ^ (exp2.val - 51) := by rw [hscaleHalf] have hmreal : (mantissa.val : Real) = (2 ^ 53 : Nat) := by exact_mod_cast hmantissaCarry rw [hmreal] at hround3Lower norm_num at hround3Lower ⊢ linarith have hX4Upper : A / (2 : Real) ^ (exp2.val - 51) < ((2 ^ 52 : Nat) : Real) := by rw [hscaleHalf] norm_num at hA3Upper53 ⊢ linarith have hmantissa4Le : mantissa4.val <= 2 ^ 52 := by have hr : (mantissa4.val : Real) < ((2 ^ 52 : Nat) : Real) + 1 := hround4Lower.trans_lt (by linarith) have hrn : mantissa4.val < (2 ^ 52 : Nat) + 1 := by exact_mod_cast hr omega have hmantissa4Ge : 2 ^ 52 <= mantissa4.val := by by_contra hn have hle : mantissa4.val <= 2 ^ 52 - 1 := by omega have hleR : (mantissa4.val : Real) <= ((2 ^ 52 - 1 : Nat) : Real) := by exact_mod_cast hle have hround4Upper' : A / (2 : Real) ^ (exp2.val - 51) <= ((2 ^ 52 - 1 : Nat) : Real) + 1 / 2 := by calc _ <= (mantissa4.val : Real) + 1 / 2 := hround4Upper _ <= ((2 ^ 52 - 1 : Nat) : Real) + 1 / 2 := by gcongr norm_num at hround4Upper' hX4Lower linarith have hmantissa4Val : mantissa4.val = 2 ^ 52 := by omega by_cases hsecondSat : biased4 >= 2047#i32 · simp only [hsecondSat, if_true] at hflow have he1023 : exp2.val = 1023 := by change 2047 <= biased4.val at hsecondSat change ¬ 2047 <= biased.val at hsat omega have hselected : tr.selected_pair = ({ m := 0#u64, p_biased := 2047#i32 } : num.imp.dec2flt.common.BiasedFp) := (Result.ok.inj hflow).symm rw [hselected] left constructor · norm_num rw [Nat.mod_eq_of_lt (by decide)] rfl · have hden : 0 < (2 : Real) ^ (971 : Int) := zpow_pos (by norm_num) _ have hscaled : ((2 ^ 53 : Nat) : Real) - 1 / 2 <= A / (2 : Real) ^ (971 : Int) := by have hmreal : (mantissa.val : Real) = (2 ^ 53 : Nat) := by exact_mod_cast hmantissaCarry rw [hmreal, he1023] at hround3Lower norm_num at hround3Lower ⊢ linarith have hthresholdEq : (((2 ^ 53 : Nat) : Real) - 1 / 2) * (2 : Real) ^ (971 : Nat) = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by have hprod : (2 : Real) ^ (53 : Nat) * 2 ^ (971 : Nat) = (2 : Real) ^ (1024 : Nat) := by rw [← pow_add] have hhalf : (1 / 2 : Real) * 2 ^ (971 : Nat) = (2 : Real) ^ (970 : Nat) := by rw [show (2 : Real) ^ (971 : Nat) = 2 * (2 : Real) ^ (970 : Nat) by rw [show (971 : Nat) = 970 + 1 by omega, pow_succ] ring] ring have hcast53 : (((2 ^ 53 : Nat) : Real)) = (2 : Real) ^ (53 : Nat) := by norm_num rw [hcast53] rw [sub_mul, hprod, hhalf] rw [← hthresholdEq] exact (le_div_iff₀ hden).mp (by simpa using hscaled) · simp only [hsecondSat, if_false] at hflow obtain ⟨expDiff5, hdiff5, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨power2, hpower2, hflow⟩ := bind_ok_inv _ _ _ hflow have hdiff5Val : expDiff5.val = exp24.val - (-1022) := by have hs := IScalar.sub_equiv exp24 (-1022)#i32 rw [hdiff5] at hs simpa using hs.2.1 have hpower2Val : power2.val = exp2.val + 1024 := by have hs := IScalar.add_equiv expDiff5 1#i32 rw [hpower2] at hs have hv := hs.2.1 rw [hdiff5Val, hexp24Val] at hv norm_num at hv omega obtain ⟨unit4a, hunit4a, hflow⟩ := bind_ok_inv _ _ _ hflow have hunit4aVal : unit4a.val = 2 ^ 52 := by have hs := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num [U64.numBits]) (by norm_num [U64.size_eq]) rw [hunit4a] at hs have hv := hs.1 simp only [WP.spec_ok, Nat.shiftLeft_eq] at hv norm_num at hv ⊢ exact hv have hnotSmall4 : ¬ mantissa4 < unit4a := by change ¬ mantissa4.val < unit4a.val omega simp only [hnotSmall4, if_false, bind_tc_ok] at hflow obtain ⟨unit4b, hunit4b, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨mask4, hmask4, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨masked4, hmasked4, hflow⟩ := bind_ok_inv _ _ _ hflow have hunit4bVal : unit4b.val = 2 ^ 52 := by have hs := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num [U64.numBits]) (by norm_num [U64.size_eq]) rw [hunit4b] at hs have hv := hs.1 simp only [WP.spec_ok, Nat.shiftLeft_eq] at hv norm_num at hv ⊢ exact hv have hmask4Val : mask4.val = 2 ^ 52 - 1 := by have hs := UScalar.sub_equiv unit4b 1#u64 rw [hmask4] at hs have hv := hs.2.1 rw [hunit4bVal] at hv norm_num at hv ⊢ omega have hmasked4Val : masked4.val = 0 := by simp only [lift] at hmasked4 have heq := Result.ok.inj hmasked4 have hv := congrArg UScalar.val heq rw [UScalar.val_and, hmantissa4Val, hmask4Val, Nat.and_two_pow_sub_one_eq_mod] at hv simpa using hv.symm have hselected : tr.selected_pair = ({ m := masked4, p_biased := power2 } : num.imp.dec2flt.common.BiasedFp) := (Result.ok.inj hflow).symm have hpower2Nonneg : 0 <= power2.val := by omega have hpower2Upper : power2.val <= 2046 := by change ¬ 2047 <= biased4.val at hsecondSat omega have hraw : Nat.lor masked4.val (((power2.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) = (power2.val.toNat * (2 ^ 52)) := by have hzeroLor (n : Nat) : Nat.lor 0 n = n := by apply Nat.eq_of_testBit_eq intro k simp [Nat.testBit_lor] have hpower2Lt : power2.val < (2 ^ 64 : Int) := by norm_num omega have hem : power2.val.emod (2 ^ 64) = power2.val := Int.emod_eq_of_lt hpower2Nonneg hpower2Lt rw [hmasked4Val, hzeroLor, hem] have hpNat : power2.val.toNat <= 2046 := by rw [Int.toNat_le] omega apply Nat.mod_eq_of_lt calc power2.val.toNat * 2 ^ 52 <= 2046 * 2 ^ 52 := by gcongr _ < 2 ^ 64 := by norm_num rw [hselected] simp only [hraw] by_cases hminimum : exp2.val = -1022 · right left have hpowerNat : power2.val.toNat = 2 := by rw [hpower2Val, hminimum] rfl have hmreal : (mantissa.val : Real) = (2 ^ 53 : Nat) := by exact_mod_cast hmantissaCarry have hhalfMin : (2 : Real) ^ (-(1074 : Int)) / 2 = (2 : Real) ^ (-(1075 : Int)) := by rw [show (-(1074 : Int)) = -(1075 : Int) + 1 by omega, zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num have hlowMagnitude : A <= ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) := by have hd : 0 < (2 : Real) ^ (-(1074 : Int)) := zpow_pos (by norm_num) _ have hx := hA3Upper53 rw [hminimum] at hx have hm := (div_lt_iff₀ hd).mp hx norm_num only [Nat.cast_pow, Nat.cast_ofNat] at hm ⊢ exact hm.le refine ⟨2 ^ 53, le_rfl, ?_, hlowMagnitude, ?_, ?_, ?_⟩ · rw [hpowerNat] norm_num [pow_succ] · rw [← hhalfMin] simpa [hminimum, hmreal] using hnearestUpper · rw [← hhalfMin] simpa [hminimum, hmreal] using hnearestLower · simpa [hminimum, hmreal] using hnearestEven · right right let p : Nat := (exp2.val + 1023).toNat have heLower : -1022 < exp2.val := by omega have heUpper : exp2.val <= 1022 := by change ¬ 2047 <= biased4.val at hsecondSat omega have hpNonneg : 0 <= exp2.val + 1023 := by omega have hpCast : (p : Int) = exp2.val + 1023 := by dsimp only [p] exact Int.toNat_of_nonneg hpNonneg have hpowerNat : power2.val.toNat = p + 1 := by have hpowerNatCast : (power2.val.toNat : Int) = power2.val := Int.toNat_of_nonneg hpower2Nonneg have heq : (power2.val.toNat : Int) = (p + 1 : Nat) := by push_cast rw [hpowerNatCast, hpCast, hpower2Val] omega exact_mod_cast heq have hmreal : (mantissa.val : Real) = (2 ^ 53 : Nat) := by exact_mod_cast hmantissaCarry have hpExp : (p : Int) - 1075 = exp2.val - 52 := by omega have hnormalMagnitude : ((2 ^ 52 : Nat) : Real) * (2 : Real) ^ ((p : Int) - 1075) <= A := by have hnorm := hnormalizedLower heLower have hx : ((2 ^ 52 : Nat) : Real) <= A / (2 : Real) ^ (exp2.val - 52) := by rw [hscale53] norm_num at hnorm ⊢ nlinarith rw [hpExp] exact (le_div_iff₀ hdeltaPos).mp hx have hnormalUpper : A <= ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ ((p : Int) - 1075) := by rw [hpExp] simpa only [Nat.cast_pow, Nat.cast_ofNat] using ((div_lt_iff₀ hdeltaPos).mp hA3Upper53).le have hbelowThreshold : A < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by have hAupper : A < (2 : Real) ^ (exp2.val + 1) := (div_lt_one hpow2pos).mp hA2Upper have hp : (2 : Real) ^ (exp2.val + 1) <= (2 : Real) ^ (1023 : Int) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hgap : (2 : Real) ^ (1023 : Nat) < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by have hsmallPow : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) := pow_lt_pow_right₀ (by norm_num) (by omega) rw [show (2 : Real) ^ (1024 : Nat) = 2 * (2 : Real) ^ (1023 : Nat) by rw [show (1024 : Nat) = 1023 + 1 by omega, pow_succ] ring] rw [lt_sub_iff_add_lt] calc (2 : Real) ^ (1023 : Nat) + 2 ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) + 2 ^ (1023 : Nat) := by simpa [add_comm] using add_lt_add_left hsmallPow ((2 : Real) ^ (1023 : Nat)) _ = 2 * (2 : Real) ^ (1023 : Nat) := by ring exact hAupper.trans_le hp |>.trans hgap refine ⟨p, 2 ^ 53, by omega, by omega, by omega, le_rfl, Or.inl (by omega), ?_, hnormalMagnitude, hnormalUpper, hbelowThreshold, ?_, ?_, ?_⟩ · rw [hpowerNat] norm_num [pow_succ] omega · rw [hpExp] simpa [hmreal] using hnearestUpper · rw [hpExp] simpa [hmreal] using hnearestLower · rw [hpExp] simpa [hmreal] using hnearestEven · simp only [hfirstCarry, if_false] at hflow have hmantissaLt : mantissa.val < 2 ^ 53 := by change ¬ mantissa.val >= 9007199254740992 at hfirstCarry norm_num at hfirstCarry ⊢ omega obtain ⟨expDiff5, hdiff5, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨power2, hpower2, hflow⟩ := bind_ok_inv _ _ _ hflow have hdiff5Val : expDiff5.val = exp2.val - (-1022) := by have hs := IScalar.sub_equiv exp2 (-1022)#i32 rw [hdiff5] at hs simpa using hs.2.1 have hpower2Val : power2.val = exp2.val + 1023 := by have hs := IScalar.add_equiv expDiff5 1#i32 rw [hpower2] at hs have hv := hs.2.1 rw [hdiff5Val] at hv norm_num at hv omega obtain ⟨unit3a, hunit3a, hflow⟩ := bind_ok_inv _ _ _ hflow have hunit3aVal : unit3a.val = 2 ^ 52 := by have hs := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num [U64.numBits]) (by norm_num [U64.size_eq]) rw [hunit3a] at hs have hv := hs.1 simp only [WP.spec_ok, Nat.shiftLeft_eq] at hv norm_num at hv ⊢ exact hv obtain ⟨power21, hpower21, hflow⟩ := bind_ok_inv _ _ _ hflow have hpower21Val : power21.val = if mantissa < unit3a then exp2.val + 1022 else exp2.val + 1023 := by by_cases hsmall : mantissa < unit3a · simp only [hsmall, if_true] at hpower21 ⊢ have hs := IScalar.sub_equiv power2 1#i32 rw [hpower21] at hs have hv := hs.2.1 calc power21.val = power2.val - 1 := hv _ = exp2.val + 1022 := by rw [hpower2Val]; omega · simp only [hsmall, if_false] at hpower21 ⊢ have heq := Result.ok.inj hpower21 have hv := congrArg IScalar.val heq calc power21.val = power2.val := hv.symm _ = exp2.val + 1023 := hpower2Val obtain ⟨unit3b, hunit3b, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨mask3, hmask3, hflow⟩ := bind_ok_inv _ _ _ hflow obtain ⟨masked3, hmasked3, hflow⟩ := bind_ok_inv _ _ _ hflow have hunit3bVal : unit3b.val = 2 ^ 52 := by have hs := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num [U64.numBits]) (by norm_num [U64.size_eq]) rw [hunit3b] at hs have hv := hs.1 simp only [WP.spec_ok, Nat.shiftLeft_eq] at hv norm_num at hv ⊢ exact hv have hmask3Val : mask3.val = 2 ^ 52 - 1 := by have hs := UScalar.sub_equiv unit3b 1#u64 rw [hmask3] at hs have hv := hs.2.1 rw [hunit3bVal] at hv norm_num at hv ⊢ omega have hmasked3Val : masked3.val = mantissa.val % 2 ^ 52 := by simp only [lift] at hmasked3 have heq := Result.ok.inj hmasked3 have hv := congrArg UScalar.val heq rw [UScalar.val_and, hmask3Val, Nat.and_two_pow_sub_one_eq_mod] at hv exact hv.symm have hselected : tr.selected_pair = ({ m := masked3, p_biased := power21 } : num.imp.dec2flt.common.BiasedFp) := (Result.ok.inj hflow).symm have hpower21Nonneg : 0 <= power21.val := by split at hpower21Val <;> omega have hpower21Upper : power21.val <= 2046 := by change ¬ 2047 <= biased.val at hsat split at hpower21Val <;> omega have hraw : Nat.lor masked3.val (((power21.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) = power21.val.toNat * 2 ^ 52 + masked3.val := by have hpLt : power21.val < (2 ^ 64 : Int) := by norm_num omega have hem : power21.val.emod (2 ^ 64) = power21.val := Int.emod_eq_of_lt hpower21Nonneg hpLt rw [hem, Nat.mod_eq_of_lt] · have hmaskedLt : masked3.val < 2 ^ 52 := by rw [hmasked3Val] exact Nat.mod_lt _ (by positivity) calc Nat.lor masked3.val (power21.val.toNat * 2 ^ 52) = Nat.lor (2 ^ 52 * power21.val.toNat) masked3.val := by rw [Nat.mul_comm] exact Nat.lor_comm _ _ _ = 2 ^ 52 * power21.val.toNat + masked3.val := (Nat.two_pow_add_eq_or_of_lt hmaskedLt _).symm _ = power21.val.toNat * 2 ^ 52 + masked3.val := by omega · have hpNat : power21.val.toNat <= 2046 := by rw [Int.toNat_le] omega calc power21.val.toNat * 2 ^ 52 <= 2046 * 2 ^ 52 := by gcongr _ < 2 ^ 64 := by norm_num rw [hselected] simp only [hraw] by_cases hminimum : exp2.val = -1022 · have hrawMin : power21.val.toNat * 2 ^ 52 + masked3.val = mantissa.val := by by_cases hsmall : mantissa < unit3a · have hsmallVal : mantissa.val < 2 ^ 52 := by change mantissa.val < unit3a.val at hsmall simpa only [hunit3aVal] using hsmall have hpVal : power21.val = 0 := by simp only [hsmall, if_true, hminimum] at hpower21Val omega have hpNat : power21.val.toNat = 0 := by rw [hpVal]; rfl rw [hpNat, zero_mul, zero_add, hmasked3Val, Nat.mod_eq_of_lt hsmallVal] · have hlargeVal : 2 ^ 52 <= mantissa.val := by change ¬ mantissa.val < unit3a.val at hsmall rw [hunit3aVal] at hsmall omega have hpVal : power21.val = 1 := by simp only [hsmall, if_false, hminimum] at hpower21Val omega have hpNat : power21.val.toNat = 1 := by rw [hpVal]; rfl rw [hpNat, one_mul, hmasked3Val, Nat.mod_eq_sub_mod hlargeVal, Nat.mod_eq_of_lt (by omega)] omega have hhalfMin : (2 : Real) ^ (-(1074 : Int)) / 2 = (2 : Real) ^ (-(1075 : Int)) := by rw [show (-(1074 : Int)) = -(1075 : Int) + 1 by omega, zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num have hlowMagnitude : A <= ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) := by have hd : 0 < (2 : Real) ^ (-(1074 : Int)) := zpow_pos (by norm_num) _ have hx := hA3Upper53 rw [hminimum] at hx have hm := (div_lt_iff₀ hd).mp hx norm_num only [Nat.cast_pow, Nat.cast_ofNat] at hm ⊢ exact hm.le right left refine ⟨mantissa.val, hmantissaLt.le, hrawMin, hlowMagnitude, ?_, ?_, ?_⟩ · rw [← hhalfMin] simpa [hminimum] using hnearestUpper · rw [← hhalfMin] simpa [hminimum] using hnearestLower · simpa [hminimum] using hnearestEven · have heLower : -1022 < exp2.val := by omega have heUpper : exp2.val <= 1023 := by change ¬ 2047 <= biased.val at hsat omega have hnorm := hnormalizedLower heLower have hXLower : ((2 ^ 52 : Nat) : Real) <= A / (2 : Real) ^ (exp2.val - 52) := by rw [hscale53] norm_num at hnorm ⊢ nlinarith have hmantissaGe : 2 ^ 52 <= mantissa.val := by by_contra hn have hmLe : mantissa.val <= 2 ^ 52 - 1 := by omega have hmLeR : (mantissa.val : Real) <= ((2 ^ 52 - 1 : Nat) : Real) := by exact_mod_cast hmLe have hupper : A / (2 : Real) ^ (exp2.val - 52) <= ((2 ^ 52 - 1 : Nat) : Real) + 1 / 2 := by calc _ <= (mantissa.val : Real) + 1 / 2 := hround3Upper _ <= ((2 ^ 52 - 1 : Nat) : Real) + 1 / 2 := by gcongr norm_num at hupper hXLower linarith have hnotSmall : ¬ mantissa < unit3a := by change ¬ mantissa.val < unit3a.val rw [hunit3aVal] omega have hpower21Exact : power21.val = exp2.val + 1023 := by simpa only [hnotSmall, if_false] using hpower21Val let p : Nat := (exp2.val + 1023).toNat have hpNonneg : 0 <= exp2.val + 1023 := by omega have hpCast : (p : Int) = exp2.val + 1023 := by dsimp only [p] exact Int.toNat_of_nonneg hpNonneg have hpower21Nat : power21.val.toNat = p := by apply Nat.cast_injective (R := Int) rw [Int.toNat_of_nonneg hpower21Nonneg, hpCast, hpower21Exact] have hmaskedSub : masked3.val = mantissa.val - 2 ^ 52 := by rw [hmasked3Val, Nat.mod_eq_sub_mod hmantissaGe, Nat.mod_eq_of_lt (by omega)] have hrawNormal : power21.val.toNat * 2 ^ 52 + masked3.val = p * 2 ^ 52 + (mantissa.val - 2 ^ 52) := by rw [hpower21Nat, hmaskedSub] have hpExp : (p : Int) - 1075 = exp2.val - 52 := by omega have hnormalMagnitude : ((2 ^ 52 : Nat) : Real) * (2 : Real) ^ ((p : Int) - 1075) <= A := by rw [hpExp] exact (le_div_iff₀ hdeltaPos).mp hXLower have hnormalUpper : A <= ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ ((p : Int) - 1075) := by rw [hpExp] simpa only [Nat.cast_pow, Nat.cast_ofNat] using ((div_lt_iff₀ hdeltaPos).mp hA3Upper53).le have hthresholdEq : (((2 ^ 53 : Nat) : Real) - 1 / 2) * (2 : Real) ^ (971 : Nat) = (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by have hprod : (2 : Real) ^ (53 : Nat) * 2 ^ (971 : Nat) = (2 : Real) ^ (1024 : Nat) := by rw [← pow_add] have hhalf : (1 / 2 : Real) * 2 ^ (971 : Nat) = (2 : Real) ^ (970 : Nat) := by rw [show (2 : Real) ^ (971 : Nat) = 2 * (2 : Real) ^ (970 : Nat) by rw [show (971 : Nat) = 970 + 1 by omega, pow_succ] ring] ring have hcast53 : (((2 ^ 53 : Nat) : Real)) = (2 : Real) ^ (53 : Nat) := by norm_num rw [hcast53, sub_mul, hprod, hhalf] have hbelowThreshold : A < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by by_cases htopExp : exp2.val = 1023 · have hXle : A / (2 : Real) ^ (exp2.val - 52) <= ((2 ^ 53 : Nat) : Real) - 1 / 2 := by have hmLe : mantissa.val <= 2 ^ 53 - 1 := by omega have hmLeR : (mantissa.val : Real) <= ((2 ^ 53 - 1 : Nat) : Real) := by exact_mod_cast hmLe calc _ <= (mantissa.val : Real) + 1 / 2 := hround3Upper _ <= ((2 ^ 53 - 1 : Nat) : Real) + 1 / 2 := by gcongr _ = ((2 ^ 53 : Nat) : Real) - 1 / 2 := by norm_num have hXne : A / (2 : Real) ^ (exp2.val - 52) ≠ ((2 ^ 53 : Nat) : Real) - 1 / 2 := by intro heq have hmGeR : (9007199254740991 : Real) <= (mantissa.val : Real) := by calc (9007199254740991 : Real) = (((2 ^ 53 : Nat) : Real) - 1 / 2) - 1 / 2 := by rw [show (2 ^ 53 : Nat) = 9007199254740992 by norm_num] norm_num _ = A / (2 : Real) ^ (exp2.val - 52) - 1 / 2 := by rw [heq] _ <= (mantissa.val : Real) := (sub_le_iff_le_add).2 hround3Upper have hmGe : 9007199254740991 <= mantissa.val := by exact_mod_cast hmGeR have hmEq : mantissa.val = 2 ^ 53 - 1 := by norm_num at hmantissaLt ⊢ omega have htie : A / (2 : Real) ^ (exp2.val - 52) = (mantissa.val : Real) + 1 / 2 := by rw [hmEq] norm_num at heq ⊢ exact heq have heven := hround3Even (Or.inl htie) rw [hmEq] at heven norm_num at heven have hXlt : A / (2 : Real) ^ (exp2.val - 52) < ((2 ^ 53 : Nat) : Real) - 1 / 2 := lt_of_le_of_ne hXle hXne have hd971 : 0 < (2 : Real) ^ (971 : Int) := zpow_pos (by norm_num) _ have hm := (div_lt_iff₀ hd971).mp (by rw [htopExp] at hXlt simpa using hXlt) rw [← hthresholdEq] simpa using hm · have heSmall : exp2.val <= 1022 := by omega have hAupper : A < (2 : Real) ^ (exp2.val + 1) := (div_lt_one hpow2pos).mp hA2Upper have hp : (2 : Real) ^ (exp2.val + 1) <= (2 : Real) ^ (1023 : Int) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hsmallPow : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) := pow_lt_pow_right₀ (by norm_num) (by omega) have hgap : (2 : Real) ^ (1023 : Nat) < (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) := by rw [show (2 : Real) ^ (1024 : Nat) = 2 * (2 : Real) ^ (1023 : Nat) by rw [show (1024 : Nat) = 1023 + 1 by omega, pow_succ] ring, lt_sub_iff_add_lt] calc (2 : Real) ^ (1023 : Nat) + 2 ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) + 2 ^ (1023 : Nat) := by simpa [add_comm] using add_lt_add_left hsmallPow ((2 : Real) ^ (1023 : Nat)) _ = 2 * (2 : Real) ^ (1023 : Nat) := by ring exact hAupper.trans_le hp |>.trans hgap right right refine ⟨p, mantissa.val, by omega, by omega, hmantissaGe, hmantissaLt.le, Or.inr hmantissaLt, hrawNormal, hnormalMagnitude, hnormalUpper, hbelowThreshold, ?_, ?_, ?_⟩ · rw [hpExp] exact hnearestUpper · rw [hpExp] exact hnearestLower · rw [hpExp] exact hnearestEven end dec2flt_model