import Tablet.DecimalGoalVocabularyDefault import Tablet.Binary64AdjacentNormalValues import Tablet.Binary64EncoderCarryArithmetic import Tablet.Binary64FiniteMagnitudeStrictMono import Tablet.Binary64NormalEncoderRounds import Tablet.F64 import Tablet.IntegerPowerTableCertificate import Tablet.IntegerProductApproximation import Tablet.ModelU64BitAnd import Tablet.ModelU64BitOr import Tablet.ModelU64Shl import Tablet.ParserExponentConstants import Tablet.num_imp_dec2flt_lemire_compute_float import Mathlib.Tactic.FieldSimp open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false set_option maxHeartbeats 1500000 set_option maxRecDepth 1000000 namespace dec2flt_model -- [TABLET NODE: IntegerCoreRounding] theorem IntegerCoreRounding : ∀ (q : Std.I64) (w : Std.U64) (fp : num.imp.dec2flt.common.BiasedFp), 0 < w.val → -342 ≤ q.val → q.val ≤ 308 → 0 ≤ fp.p_biased.val → num.imp.dec2flt.lemire.compute_float F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok fp → ∃ target : F64, DecimalGoalVocabularyDefault.assembledWord fp target ∧ DecimalGoalVocabularyDefault.roundsMagnitude ((w.val : Real) * (10 : Real) ^ q.val) target := by -- BODY set_option maxRecDepth 1000000 in intro q w fp hw hqlo hqhi hfp hrun 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 as_aux_lemma => intro α β r f y hr cases r <;> simp_all [Bind.bind, Aeneas.Std.bind] 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 herr : num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32 = ok { m := 0#u64, p_biased := (-1)#i32 } := by rfl have hcastSmall : lift (IScalar.cast .I64 (-342)#i32) = ok (-342)#i64 := by rfl have hcastLarge : lift (IScalar.cast .I64 308#i32) = ok 308#i64 := by rfl have hsigop : (53#u32 - 1#u32) = ok 52#u32 := by rfl unfold num.imp.dec2flt.lemire.compute_float at hrun rw [hzero, hinf] at hrun simp only [bind_tc_ok] at hrun rw [hinfFp, herr] at hrun simp only [bind_tc_ok] at hrun have hwne : w ≠ 0#u64 := by as_aux_lemma => intro h have : w.val = 0 := UScalar.val_eq_of_eq h omega simp only [hwne, if_false] at hrun rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def, hcastSmall] at hrun simp only [bind_tc_ok] at hrun have hnsmall : ¬ q < (-342)#i64 := by as_aux_lemma => simpa only [IScalar.lt_equiv] using (show ¬ q.val < -342 by omega) simp only [hnsmall, if_false] at hrun rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def, hcastLarge] at hrun simp only [bind_tc_ok] at hrun have hnlarge : ¬ q > 308#i64 := by as_aux_lemma => simpa only [IScalar.lt_equiv] using (show ¬ 308 < q.val by omega) simp only [hnlarge, if_false] at hrun simp only [lift, bind_tc_ok] at hrun let l : Std.U32 := core.num.U64.leading_zeros w cases hW : (w <<< l) with | fail e => simp only [l, hW, bind_tc_fail] at hrun; contradiction | div => simp only [l, hW, bind_tc_div] at hrun; contradiction | ok W => simp only [l, hW, bind_tc_ok] at hrun rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def] at hrun simp only [core.f64.F64.MANTISSA_DIGITS, ModelF64MantissaDigits, Binary64ModelDefault] at hrun rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def] at hrun simp only [bind_tc_ok] at hrun rw [hsigop] at hrun simp only [bind_tc_ok] at hrun obtain ⟨precision, hprecision, hrun⟩ := bind_ok_inv _ _ _ hrun have hprecisionEq : precision = 55#usize := by have hs := UScalar.add_equiv (UScalar.cast .Usize 52#u32) 3#usize rw [hprecision] at hs apply UScalar.eq_of_val_eq norm_num at hs ⊢ exact hs.2.1 subst precision obtain ⟨pair, hproduct, hrun⟩ := bind_ok_inv _ _ _ hrun rcases pair with ⟨lo, hi⟩ simp only [Aeneas.Std.uncurry_apply_pair] at hrun have hwneVal : w.val ≠ 0 := by omega have hwbv : w.bv ≠ 0 := by intro h apply hwneVal change w.bv.toNat = 0 rw [h] rfl have hwlt : w.val < 2 ^ 64 := w.bv.isLt have hloglt : Nat.log 2 w.val < 64 := Nat.log_lt_of_lt_pow hwneVal hwlt have hlval : l.val = 63 - Nat.log 2 w.val := by simp only [l, core.num.U64.leading_zeros, BitVec.leadingZeros, if_neg hwbv, UScalar.val] change (BitVec.ofNat 32 (64 - Nat.log 2 w.bv.toNat - 1)).toNat = 63 - Nat.log 2 w.bv.toNat rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] omega have hl : l.val ≤ 63 := by omega have hpowlo : 2 ^ Nat.log 2 w.val ≤ w.val := Nat.pow_log_le_self 2 hwneVal have hpowhi : w.val < 2 ^ (Nat.log 2 w.val + 1) := Nat.lt_pow_succ_log_self (by omega) w.val have hmul_lt : w.val * 2 ^ l.val < 2 ^ 64 := by rw [hlval] have heq : Nat.log 2 w.val + 1 + (63 - Nat.log 2 w.val) = 64 := by omega calc w.val * 2 ^ (63 - Nat.log 2 w.val) < 2 ^ (Nat.log 2 w.val + 1) * 2 ^ (63 - Nat.log 2 w.val) := Nat.mul_lt_mul_of_pos_right hpowhi (by positivity) _ = 2 ^ 64 := by rw [← pow_add, heq] have hshiftSpec := UScalar.ShiftLeft_spec w l (2 ^ 64) (by change l.val < 64 omega) (by norm_num [U64.size_eq]) rw [hW] at hshiftSpec have hWval : W.val = w.val * 2 ^ l.val := by as_aux_lemma => have hmul_lt' : w.val * 2 ^ l.val < 18446744073709551616 := by norm_num at hmul_lt ⊢ exact hmul_lt simpa [WP.spec_ok, Nat.shiftLeft_eq, Nat.mod_eq_of_lt hmul_lt'] using hshiftSpec.1 have hWlower : 2 ^ 63 ≤ W.val := by as_aux_lemma => rw [hWval, hlval] calc 2 ^ 63 = 2 ^ Nat.log 2 w.val * 2 ^ (63 - Nat.log 2 w.val) := by rw [← pow_add] congr 1 omega _ ≤ w.val * 2 ^ (63 - Nat.log 2 w.val) := Nat.mul_le_mul_right _ hpowlo let coreRun : Result num.imp.dec2flt.common.BiasedFp := do let i8 ← hi >>> 63#i32 let upperbit := UScalar.hcast .I32 i8 let i9 ← upperbit + 64#i32 let sigbits := UScalar.hcast .I32 52#u32 let i11 ← i9 - sigbits let i12 ← i11 - 3#i32 let mantissa ← hi >>> i12 let i14 ← num.imp.dec2flt.lemire.power (IScalar.cast .I32 q) let i15 ← i14 + upperbit let i17 ← i15 - UScalar.hcast .I32 (core.num.U64.leading_zeros w) let i18 ← do let i ← do let i ← do let i ← do let i2 ← 64#u32 - 52#u32 i2 - 1#u32 let i1 ← 1#u32 <<< i i1 - 1#u32 i >>> 1#i32 let i2 ← UScalar.hcast .I32 i - 1#i32 -. i2 let i19 ← i17 - i18 let power2 ← i19 + 1#i32 if power2 ≤ 0#i32 then do let i20 ← -. power2 let i21 ← i20 + 1#i32 if i21 ≥ 64#i32 then ok { m := 0#u64, p_biased := 0#i32 } else do let i22 ← i20 + 1#i32 let mantissa1 ← mantissa >>> i22 let mantissa2 ← mantissa1 + (mantissa1 &&& 1#u64) let mantissa3 ← mantissa2 >>> 1#i32 let i24 ← 1#u64 <<< 52#u32 ok { m := mantissa3, p_biased := IScalar.cast_fromBool .I32 (decide (mantissa3 ≥ i24)) } else do let mantissa1 ← if lo ≤ 1#u64 then if q ≥ IScalar.cast .I64 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MIN_EXPONENT_ROUND_TO_EVEN then if q ≤ IScalar.cast .I64 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MAX_EXPONENT_ROUND_TO_EVEN then if mantissa &&& 3#u64 = 1#u64 then do let i26 ← i9 - sigbits let i27 ← i26 - 3#i32 let i28 ← mantissa <<< i27 if i28 = hi then ok (mantissa &&& ~~~1#u64) else ok mantissa else ok mantissa else ok mantissa else ok mantissa else ok mantissa let mantissa2 ← mantissa1 + (mantissa1 &&& 1#u64) let mantissa3 ← mantissa2 >>> 1#i32 let i21 ← 2#u64 <<< 52#u32 let (mantissa4, power21) ← if mantissa3 ≥ i21 then do let mantissa5 ← 1#u64 <<< 52#u32 let power22 ← power2 + 1#i32 ok (mantissa5, power22) else ok (mantissa3, power2) let i22 ← 1#u64 <<< 52#u32 if power21 ≥ 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 } else ok { m := mantissa4 &&& ~~~i22, p_biased := power21 } have hsent : lo.val ≠ 2 ^ 64 - 1 ∨ (-27 ≤ q.val ∧ q.val ≤ 55) := by as_aux_lemma => by_cases hlo : lo = 18446744073709551615#u64 · right have hqneg : q ≥ (-27)#i64 := by by_contra hn simp only [hlo, if_true, hn, bind_tc_ok, if_false] at hrun cases hrun norm_num at hfp have hq55 : q ≤ 55#i64 := by by_contra hn have hbfalse : decide (q ≤ 55#i64) = false := by simpa using hn simp only [hlo, if_true, hqneg, bind_tc_ok, hbfalse] at hrun cases hrun norm_num at hfp simpa only [IScalar.le_equiv] using And.intro hqneg hq55 · left intro hv apply hlo apply UScalar.eq_of_val_eq norm_num at hv ⊢ exact hv have hrunTail : coreRun = ok fp := by as_aux_lemma => by_cases hlo : lo = 18446744073709551615#u64 · have hs := hsent.resolve_left (by intro hv apply hv rw [hlo] norm_num) have hqneg : q ≥ (-27)#i64 := by simpa only [IScalar.le_equiv] using hs.1 have hq55 : q ≤ 55#i64 := by simpa only [IScalar.le_equiv] using hs.2 have hbtrue : decide (q ≤ 55#i64) = true := by simpa using hq55 simp only [hlo, if_true, hqneg, bind_tc_ok, hbtrue] at hrun simpa only [coreRun, hlo] using hrun · simp only [hlo, if_false] at hrun simpa only [coreRun] using hrun clear hrun have hrun := hrunTail clear hrunTail dsimp only [coreRun] at hrun rcases IntegerProductApproximation q W lo hi hqlo hqhi hWlower hproduct hsent with ⟨delta, index, a, b, hdelta, hindex, htable, happrox⟩ rcases IntegerPowerTableCertificate q hqlo hqhi with ⟨q32, power, delta', index', a', b', hq32, hpower, hpowerVal, hdelta', hindex', htable', htableFacts⟩ simp only [lift] at hq32 have hq32' : IScalar.cast .I32 q = q32 := Result.ok.inj hq32 let expMinRun : Result Std.I32 := do let i ← do let i ← do let i ← do let i2 ← 64#u32 - 52#u32 i2 - 1#u32 let i1 ← 1#u32 <<< i i1 - 1#u32 i >>> 1#i32 let i2 ← UScalar.hcast .I32 i - 1#i32 -. i2 let tailRun (G64 : Std.U64) (i9 i12 p : Std.I32) : Result num.imp.dec2flt.common.BiasedFp := if p ≤ 0#i32 then do let i20 ← -. p let i21 ← i20 + 1#i32 if i21 ≥ 64#i32 then ok { m := 0#u64, p_biased := 0#i32 } else do let i22 ← i20 + 1#i32 let mantissa1 ← G64 >>> i22 let mantissa2 ← mantissa1 + (mantissa1 &&& 1#u64) let mantissa3 ← mantissa2 >>> 1#i32 let i24 ← 1#u64 <<< 52#u32 ok { m := mantissa3, p_biased := IScalar.cast_fromBool .I32 (decide (mantissa3 ≥ i24)) } else do let mantissa1 ← if lo ≤ 1#u64 then if q ≥ IScalar.cast .I64 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MIN_EXPONENT_ROUND_TO_EVEN then if q ≤ IScalar.cast .I64 F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MAX_EXPONENT_ROUND_TO_EVEN then if G64 &&& 3#u64 = 1#u64 then do let i26 ← i9 - UScalar.hcast .I32 52#u32 let i27 ← i26 - 3#i32 let i28 ← G64 <<< i27 if i28 = hi then ok (G64 &&& ~~~1#u64) else ok G64 else ok G64 else ok G64 else ok G64 else ok G64 let mantissa2 ← mantissa1 + (mantissa1 &&& 1#u64) let mantissa3 ← mantissa2 >>> 1#i32 let i21 ← 2#u64 <<< 52#u32 let (mantissa4, power21) ← if mantissa3 ≥ i21 then do let mantissa5 ← 1#u64 <<< 52#u32 let power22 ← p + 1#i32 ok (mantissa5, power22) else ok (mantissa3, p) let i22 ← 1#u64 <<< 52#u32 if power21 ≥ 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 } else ok { m := mantissa4 &&& ~~~i22, p_biased := power21 } have hcoreInv : ∃ (i8 : Std.U64) (i9 i11 i12 : Std.I32) (G64 : Std.U64) (i15 i17 i18 i19 p : Std.I32), hi >>> 63#i32 = ok i8 ∧ UScalar.hcast .I32 i8 + 64#i32 = ok i9 ∧ i9 - UScalar.hcast .I32 52#u32 = ok i11 ∧ i11 - 3#i32 = ok i12 ∧ hi >>> i12 = ok G64 ∧ power + UScalar.hcast .I32 i8 = ok i15 ∧ i15 - UScalar.hcast .I32 (core.num.U64.leading_zeros w) = ok i17 ∧ expMinRun = ok i18 ∧ i17 - i18 = ok i19 ∧ i19 + 1#i32 = ok p ∧ tailRun G64 i9 i12 p = ok fp := by as_aux_lemma => have hr := hrun rw [hq32', hpower] at hr simp only [bind_tc_ok] at hr change (do let i8 ← hi >>> 63#i32 let i9 ← UScalar.hcast .I32 i8 + 64#i32 let i11 ← i9 - UScalar.hcast .I32 52#u32 let i12 ← i11 - 3#i32 let G64 ← hi >>> i12 let i15 ← power + UScalar.hcast .I32 i8 let i17 ← i15 - UScalar.hcast .I32 (core.num.U64.leading_zeros w) let i18 ← expMinRun let i19 ← i17 - i18 let p ← i19 + 1#i32 tailRun G64 i9 i12 p) = ok fp at hr obtain ⟨i8, hi8, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i9, hi9, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i11, hi11, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i12, hi12, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨G64, hG64, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i15, hi15, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i17, hi17, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i18, hi18, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨i19, hi19, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨p, hp, hr⟩ := bind_ok_inv _ _ _ hr exact ⟨i8, i9, i11, i12, G64, i15, i17, i18, i19, p, hi8, hi9, hi11, hi12, hG64, hi15, hi17, hi18, hi19, hp, hr⟩ rcases hcoreInv with ⟨i8, i9, i11, i12, G64, i15, i17, i18, i19, p, hi8, hi9, hi11, hi12, hG64, hi15, hi17, hi18, hi19, hp, hrun⟩ dsimp only [tailRun] at hrun dsimp only at happrox let e : Int := 217706 * q.val / 65536 - q.val let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val let R : Real := (W.val : Real) * T / (2 : Real) ^ (64 : Nat) let u : Nat := hi.val / 2 ^ 63 let G : Nat := hi.val / 2 ^ (9 + u) have hu : u ≤ 1 := by simpa only [u] using happrox.1 have hi8Spec := UScalar.ShiftRight_IScalar_spec hi (63#i32) (by norm_num) (by norm_num) rw [hi8] at hi8Spec have hi8val : i8.val = u := by as_aux_lemma => simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, u] using hi8Spec.1 have hi8bound : i8.val ≤ IScalar.max .I32 := by as_aux_lemma => rw [hi8val] rw [IScalar.max_I32_eq] omega have huCast : (UScalar.hcast .I32 i8).val = (u : Int) := by as_aux_lemma => have hs := UScalar.hcast_inBounds_spec .I32 i8 hi8bound simp only [lift, WP.spec_ok] at hs rw [hs, hi8val] have hi9val : i9.val = (u : Int) + 64 := by as_aux_lemma => have hs := IScalar.add_equiv (UScalar.hcast .I32 i8) 64#i32 rw [hi9] at hs norm_num at hs omega have hi11val : i11.val = (u : Int) + 12 := by as_aux_lemma => have hs := IScalar.sub_equiv i9 (UScalar.hcast .I32 52#u32) rw [hi11] at hs have h52 : (UScalar.hcast .I32 52#u32).val = 52 := by rfl rw [h52] at hs omega have hi12val : i12.val = (u : Int) + 9 := by as_aux_lemma => have hs := IScalar.sub_equiv i11 3#i32 rw [hi12] at hs norm_num at hs omega have hi12nat : i12.toNat = 9 + u := by as_aux_lemma => apply Nat.cast_injective (R := Int) rw [Int.toNat_of_nonneg (by rw [hi12val]; omega), hi12val] norm_num omega have hGSpec := UScalar.ShiftRight_IScalar_spec hi i12 (by change 0 ≤ i12.val; rw [hi12val]; omega) (by change i12.val < 64 rw [hi12val] omega) rw [hG64] at hGSpec have hGval : G64.val = G := by as_aux_lemma => simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, hi12nat, G] using hGSpec.1 have hi15val : i15.val = power.val + (u : Int) := by as_aux_lemma => have hs := IScalar.add_equiv power (UScalar.hcast .I32 i8) rw [hi15] at hs omega have hlCast : (UScalar.hcast .I32 l).val = (l.val : Int) := by as_aux_lemma => have hs := UScalar.hcast_inBounds_spec .I32 l (by rw [IScalar.max_I32_eq] omega) simp only [lift, WP.spec_ok] at hs exact hs have hi17val : i17.val = power.val + (u : Int) - l.val := by as_aux_lemma => have hs := IScalar.sub_equiv i15 (UScalar.hcast .I32 l) rw [hi17] at hs omega have hi18eq : i18 = (-1022)#i32 := by as_aux_lemma => have hc := hmin have htotal' := htotal dsimp only [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64] at hc dsimp only [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64] at htotal' rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def, htotal'] at hc simp only [bind_tc_ok, lift] at hc have hh := hc.symm.trans hi18 exact (Result.ok.inj hh).symm have hi19val : i19.val = power.val + (u : Int) - l.val + 1022 := by as_aux_lemma => have hs := IScalar.sub_equiv i17 i18 rw [hi19, hi18eq] at hs norm_num at hs omega have hpval : p.val = power.val + (u : Int) - l.val + 1023 := by as_aux_lemma => have hs := IScalar.add_equiv i19 1#i32 rw [hp] at hs norm_num at hs omega have hpformula : p.val = q.val + e + (u : Int) - l.val + 1086 := by as_aux_lemma => dsimp only [e] rw [hpval, hpowerVal] omega let x : Real := (w.val : Real) * (10 : Real) ^ q.val let k : Int := q.val + e - (l.val : Int) - 63 have hx : x = R * (2 : Real) ^ k := by as_aux_lemma => dsimp only [x, R, T, k] rw [hWval] push_cast rw [show (10 : Real) = 2 * 5 by norm_num, mul_zpow] rw [show (2 : Real) ^ (l.val : Nat) = (2 : Real) ^ (l.val : Int) by rw [zpow_natCast]] rw [show (2 : Real) ^ (64 : Nat) = (2 : Real) ^ (64 : Int) by rfl] rw [div_eq_mul_inv, ← zpow_neg] have hpow : (2 : Real) ^ (l.val : Int) * (2 : Real) ^ (127 - e) * (2 : Real) ^ (-64 : Int) * (2 : Real) ^ (q.val + e - (l.val : Int) - 63) = (2 : Real) ^ q.val := by rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] congr 1 ring rw [← hpow] ring have hk : (73 : Int) + (u : Int) + k = p.val - 1076 := by as_aux_lemma => dsimp only [k] rw [hpformula] omega dsimp only at htableFacts have hTlo : (2 : Real) ^ (127 : Nat) ≤ T := htableFacts.1.1 have hThi : T < (2 : Real) ^ (128 : Nat) := htableFacts.1.2 have hWlowerR : (2 : Real) ^ (63 : Nat) ≤ (W.val : Real) := by exact_mod_cast hWlower have hWupperR : (W.val : Real) < (2 : Real) ^ (64 : Nat) := by exact_mod_cast W.bv.isLt have hRlower : (2 : Real) ^ (126 : Nat) ≤ R := by as_aux_lemma => have hm : (2 : Real) ^ (63 : Nat) * (2 : Real) ^ (127 : Nat) ≤ (W.val : Real) * T := mul_le_mul hWlowerR hTlo (by positivity) (by positivity) dsimp only [R] calc (2 : Real) ^ (126 : Nat) = ((2 : Real) ^ (63 : Nat) * (2 : Real) ^ (127 : Nat)) / (2 : Real) ^ (64 : Nat) := by rw [show 126 = 63 + 63 by omega, show 127 = 63 + 64 by omega, pow_add, pow_add] field_simp _ ≤ (W.val : Real) * T / (2 : Real) ^ (64 : Nat) := (div_le_div_iff_of_pos_right (by positivity)).2 hm have hRupper : R < (2 : Real) ^ (128 : Nat) := by as_aux_lemma => have hm : (W.val : Real) * T < (2 : Real) ^ (64 : Nat) * (2 : Real) ^ (128 : Nat) := mul_lt_mul hWupperR hThi.le (by positivity) (by positivity) dsimp only [R] calc (W.val : Real) * T / (2 : Real) ^ (64 : Nat) < ((2 : Real) ^ (64 : Nat) * (2 : Real) ^ (128 : Nat)) / (2 : Real) ^ (64 : Nat) := (div_lt_div_iff_of_pos_right (by positivity)).2 hm _ = (2 : Real) ^ (128 : Nat) := by field_simp have hcellLo : (G : Real) * (2 : Real) ^ (73 + u) ≤ R := happrox.2.2.1 have hcellHi : R < ((G + 1 : Nat) : Real) * (2 : Real) ^ (73 + u) := happrox.2.2.2.1 have hscalePos : 0 < (2 : Real) ^ k := zpow_pos (by norm_num) _ have hscaleEq : (2 : Real) ^ (73 + u : Nat) * (2 : Real) ^ k = (2 : Real) ^ (p.val - 1076) := by as_aux_lemma => rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] congr 1 have hxcellLo : (G : Real) * (2 : Real) ^ (p.val - 1076) ≤ x := by as_aux_lemma => calc (G : Real) * (2 : Real) ^ (p.val - 1076) = ((G : Real) * (2 : Real) ^ (73 + u : Nat)) * (2 : Real) ^ k := by rw [← hscaleEq]; ring _ ≤ R * (2 : Real) ^ k := mul_le_mul_of_nonneg_right hcellLo hscalePos.le _ = x := hx.symm have hxcellHi : x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := by as_aux_lemma => calc x = R * (2 : Real) ^ k := hx _ < (((G + 1 : Nat) : Real) * (2 : Real) ^ (73 + u : Nat)) * (2 : Real) ^ k := mul_lt_mul_of_pos_right hcellHi hscalePos _ = ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := by rw [← hscaleEq] ring have hxLowerEq : x = (G : Real) * (2 : Real) ^ (p.val - 1076) ↔ R = (G : Real) * (2 : Real) ^ (73 + u : Nat) := by as_aux_lemma => rw [hx, ← hscaleEq] constructor · intro heq apply mul_right_cancel₀ hscalePos.ne' simpa only [mul_assoc] using heq · intro heq rw [heq] ring have htie : (x = (G : Real) * (2 : Real) ^ (p.val - 1076) ∧ G % 4 = 1) ↔ (-4 ≤ q.val ∧ q.val ≤ 23 ∧ G % 4 = 1 ∧ lo.val ≤ 1 ∧ G * 2 ^ (9 + u) = hi.val) := by as_aux_lemma => rw [hxLowerEq] exact happrox.2.2.2.2 have hpLower : -114 ≤ p.val := by dsimp only [e] at hpformula omega have hpUpper : p.val ≤ 2110 := by dsimp only [e] at hpformula omega have hGLower : 2 ^ 53 ≤ G := by as_aux_lemma => rcases (Nat.le_one_iff_eq_zero_or_eq_one.mp hu) with hu0 | hu1 · have hstrict : (2 : Real) ^ (126 : Nat) < ((G + 1 : Nat) : Real) * (2 : Real) ^ (73 : Nat) := by exact hRlower.trans_lt (by simpa only [hu0, Nat.add_zero] using hcellHi) have hpEq : (2 : Real) ^ (126 : Nat) = (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (73 : Nat) := by rw [← pow_add] rw [hpEq, Nat.cast_add, Nat.cast_one] at hstrict have hc : (2 : Real) ^ (53 : Nat) < (G : Real) + 1 := by nlinarith [show 0 < (2 : Real) ^ (73 : Nat) by positivity] have hn : 2 ^ 53 < G + 1 := by exact_mod_cast hc omega · have hu1' : 1 ≤ hi.val / 2 ^ 63 := by simpa only [u] using hu1.ge have hhi : 2 ^ 63 ≤ hi.val := by have := (Nat.le_div_iff_mul_le (by positivity : 0 < 2 ^ 63)).mp hu1' simpa only [one_mul] using this dsimp only [G] rw [hu1] apply (Nat.le_div_iff_mul_le (by positivity : 0 < 2 ^ (9 + 1))).2 norm_num at hhi ⊢ exact hhi have hGUpper : G < 2 ^ 54 := by as_aux_lemma => rcases (Nat.le_one_iff_eq_zero_or_eq_one.mp hu) with hu0 | hu1 · have hhi : hi.val < 2 ^ 63 := by apply Nat.lt_of_div_eq_zero (by positivity : 0 < 2 ^ 63) simpa only [u] using hu0 dsimp only [G] rw [hu0] apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ (9 + 0))).2 norm_num at hhi ⊢ exact hhi · have hhi : hi.val < 2 ^ 64 := hi.bv.isLt dsimp only [G] rw [hu1] apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ (9 + 1))).2 norm_num at hhi ⊢ exact hhi have andOne (z : Std.U64) : (z &&& 1#u64).val = z.val % 2 := by as_aux_lemma => change (z.bv &&& 1#64).toNat = z.bv.toNat % 2 rw [show (1#64) = 2#64 ^ 1 - 1#64 by decide, BitVec.and_two_pow_sub_one_eq_mod] simp have andThree (z : Std.U64) : (z &&& 3#u64).val = z.val % 4 := by as_aux_lemma => change (z.bv &&& 3#64).toNat = z.bv.toNat % 4 rw [show (3#64) = 2#64 ^ 2 - 1#64 by decide, BitVec.and_two_pow_sub_one_eq_mod] simp have clearLow (z : Std.U64) (hz : z.val % 2 = 1) : (z &&& ~~~1#u64).val = z.val - 1 := by as_aux_lemma => have clearNat (n : Nat) (hn : n < 2 ^ 64) : n &&& (2 ^ 64 - 2) = n - n % 2 := by have hn2 : n / 2 < 2 ^ 63 := by apply (Nat.div_lt_iff_lt_mul (by omega)).2 norm_num at hn ⊢ exact hn conv_lhs => rw [← Nat.bit_bodd_div2 n] rw [show 2 ^ 64 - 2 = Nat.bit false (2 ^ 63 - 1) by simp [Nat.bit]] rw [Nat.land_bit] simp only [Bool.and_false, Nat.bit_false] rw [Nat.and_two_pow_sub_one_eq_mod, Nat.div2_val, Nat.mod_eq_of_lt hn2] have hd := Nat.div_add_mod n 2 omega change (z.bv &&& ~~~(1#64)).toNat = z.bv.toNat - 1 rw [BitVec.toNat_and, BitVec.toNat_not, BitVec.toNat_ofNat] norm_num have hc := clearNat z.bv.toNat z.bv.isLt norm_num at hc rw [hc] omega have clearHidden (z one : Std.U64) (hone : one.val = 2 ^ 52) (hlo : 2 ^ 52 ≤ z.val) (hhi : z.val < 2 ^ 53) : (z &&& ~~~one).val = z.val - 2 ^ 52 := by as_aux_lemma => have clear52Nat (n : Nat) (hnlo : 2 ^ 52 ≤ n) (hnhi : n < 2 ^ 53) : n &&& (2 ^ 64 - 1 - 2 ^ 52) = n - 2 ^ 52 := by let mask := 2 ^ 64 - 1 - 2 ^ 52 have hlandlt : n &&& mask < 2 ^ 53 := lt_of_le_of_lt Nat.and_le_left hnhi have hm : mask % 2 ^ 53 = 2 ^ 52 - 1 := by norm_num [mask] have hc := Nat.and_mod_two_pow (a := n) (b := mask) (n := 53) rw [Nat.mod_eq_of_lt hlandlt, Nat.mod_eq_of_lt hnhi, hm, Nat.and_two_pow_sub_one_eq_mod] at hc have hloDiv : 1 ≤ n / 2 ^ 52 := (Nat.le_div_iff_mul_le (by positivity)).2 (by simpa using hnlo) have hhiDiv : n / 2 ^ 52 < 2 := (Nat.div_lt_iff_lt_mul (by positivity)).2 (by simpa using hnhi) have hdiv : n / 2 ^ 52 = 1 := by omega have hd := Nat.div_add_mod n (2 ^ 52) have hnmod : n % 2 ^ 52 = n - 2 ^ 52 := by omega exact hc.trans hnmod have oneEq : one = 4503599627370496#u64 := by apply UScalar.eq_of_val_eq norm_num at hone ⊢ exact hone rw [oneEq] change (z.bv &&& ~~~(BitVec.ofNat 64 4503599627370496)).toNat = z.bv.toNat - 2 ^ 52 rw [BitVec.toNat_and, BitVec.toNat_not, BitVec.toNat_ofNat] norm_num exact clear52Nat z.val hlo hhi have fieldOr (a ef : Nat) (ha : a < 2 ^ 52) (hef : ef * 2 ^ 52 < 2 ^ 64) : a ||| (ef * 2 ^ 52) = a + ef * 2 ^ 52 := by as_aux_lemma => have hland : a &&& (ef * 2 ^ 52) = 0 := by apply Nat.eq_of_testBit_eq intro j rw [Nat.testBit_land, Nat.testBit_mul_two_pow] by_cases hj : j < 52 · simp [hj] · have haj : a.testBit j = false := by apply Nat.testBit_eq_false_of_lt exact lt_of_lt_of_le ha (Nat.pow_le_pow_right (by omega) (by omega)) simp [haj] let av : BitVec 64 := BitVec.ofNat 64 a let ev : BitVec 64 := BitVec.ofNat 64 (ef * 2 ^ 52) have hlandBV : av &&& ev = 0#64 := by apply BitVec.eq_of_toNat_eq simp only [av, ev, BitVec.toNat_and, BitVec.toNat_ofNat, BitVec.toNat_zero] rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef, hland] calc a ||| (ef * 2 ^ 52) = (av ||| ev).toNat := by simp only [av, ev, BitVec.toNat_or, BitVec.toNat_ofNat] rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef] _ = (av + ev).toNat := by rw [BitVec.add_eq_or_of_and_eq_zero av ev hlandBV] _ = a + ef * 2 ^ 52 := by rw [BitVec.toNat_add_of_and_eq_zero hlandBV] simp only [av, ev, BitVec.toNat_ofNat] rw [Nat.mod_eq_of_lt (ha.trans (by norm_num)), Nat.mod_eq_of_lt hef] let unit : Nat := 2 ^ 52 let top : Nat := 2047 * unit let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) let value : Nat → Real := fun n => let field := n / unit let fraction := n % unit if field = 0 then (fraction : Real) / (2 : Real) ^ (1074 : Nat) else if 1075 ≤ field then ((unit + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075) else ((unit + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field) have hvalueMono : ∀ a b : Nat, a < b → b < top → value a < value b := by simpa only [unit, top, value] using Binary64FiniteMagnitudeStrictMono have hxpos : 0 < x := by as_aux_lemma => dsimp only [x] exact mul_pos (by exact_mod_cast hw) (zpow_pos (by norm_num) _) have honeThreshold : (1 : Real) < threshold := by as_aux_lemma => have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity have heq : (2 : Real) ^ (1024 : Nat) = (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by rw [← pow_add] dsimp only [threshold] rw [heq] have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by norm_num rw [h54] have hp1 : (1 : Real) ≤ (2 : Real) ^ (970 : Nat) := one_le_pow₀ (by norm_num) rw [show 18014398509481984 * (2 : Real) ^ (970 : Nat) - (2 : Real) ^ (970 : Nat) = 18014398509481983 * (2 : Real) ^ (970 : Nat) by ring] calc (1 : Real) < 18014398509481983 := by norm_num _ ≤ 18014398509481983 * (2 : Real) ^ (970 : Nat) := by simpa only [mul_one] using mul_le_mul_of_nonneg_left hp1 (by norm_num : (0 : Real) ≤ 18014398509481983) have cellRounds : ∀ (t : F64) (low bits : Nat), t.val = bits → low + 1 < top → value low ≤ x → x ≤ value (low + 1) → (bits = low ∨ bits = low + 1) → (bits = low → abs (x - value low) ≤ abs (x - value (low + 1))) → (bits = low + 1 → abs (x - value (low + 1)) ≤ abs (x - value low)) → (bits = low → abs (x - value low) = abs (x - value (low + 1)) → bits % 2 = 0) → (bits = low + 1 → abs (x - value (low + 1)) = abs (x - value low) → bits % 2 = 0) → x < threshold → DecimalGoalVocabularyDefault.roundsMagnitude x t := by as_aux_lemma => intro t low bits ht hlow hxl hxh hchoice hnearLow hnearHigh htieLow htieHigh hthreshold dsimp only [DecimalGoalVocabularyDefault] change 0 ≤ x ∧ t.val ≤ top ∧ (if t.val = top then threshold ≤ x else x < threshold ∧ ∀ v : F64, v.val < top → v ≠ t → abs (x - value t.val) ≤ abs (x - value v.val) ∧ (abs (x - value t.val) = abs (x - value v.val) → t.val % 2 = 0)) have hbits : bits < top := by rcases hchoice with h | h <;> omega have htBound : t.val < top := by omega refine ⟨hxpos.le, htBound.le, ?_⟩ rw [if_neg (by omega)] refine ⟨hthreshold, ?_⟩ intro v hv hne have hvne : v.val ≠ bits := by intro heq apply hne apply UScalar.eq_of_val_eq omega rcases hchoice with hbitsLow | hbitsHigh · have htlow : t.val = low := ht.trans hbitsLow rw [htlow] have hnear := hnearLow hbitsLow have htie := htieLow hbitsLow by_cases hvBelow : v.val < low · have hvValue : value v.val < value low := hvalueMono _ _ hvBelow (by omega) have hvx : value v.val < x := hvValue.trans_le hxl have hstrict : abs (x - value low) < abs (x - value v.val) := by rw [abs_of_nonneg (sub_nonneg.mpr hxl), abs_of_nonneg (sub_nonneg.mpr hvx.le)] linarith exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩ · have hvAbove : low < v.val := by omega have hhighLe : low + 1 ≤ v.val := by omega have hhighValue : value (low + 1) ≤ value v.val := by rcases hhighLe.eq_or_lt with heq | hlt · exact le_of_eq (congrArg value heq) · exact (hvalueMono _ _ hlt hv).le have hxv : x ≤ value v.val := hxh.trans hhighValue have hhighDist : abs (x - value (low + 1)) ≤ abs (x - value v.val) := by rw [abs_of_nonpos (sub_nonpos.mpr hxh), abs_of_nonpos (sub_nonpos.mpr hxv)] linarith constructor · exact hnear.trans hhighDist · intro heq have hreverse : abs (x - value (low + 1)) ≤ abs (x - value low) := hhighDist.trans_eq heq.symm have heqCell := le_antisymm hnear hreverse simpa only [hbitsLow] using htie heqCell · have hthigh : t.val = low + 1 := ht.trans hbitsHigh rw [hthigh] have hnear := hnearHigh hbitsHigh have htie := htieHigh hbitsHigh by_cases hvAbove : low + 1 < v.val · have hvValue : value (low + 1) < value v.val := hvalueMono _ _ hvAbove hv have hxv : x < value v.val := hxh.trans_lt hvValue have hstrict : abs (x - value (low + 1)) < abs (x - value v.val) := by rw [abs_of_nonpos (sub_nonpos.mpr hxh), abs_of_nonpos (sub_nonpos.mpr hxv.le)] linarith exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩ · have hvBelow : v.val < low + 1 := by omega have hvLow : v.val ≤ low := by omega have hvValue : value v.val ≤ value low := by rcases hvLow.eq_or_lt with heq | hlt · exact le_of_eq (congrArg value heq) · exact (hvalueMono _ _ hlt (by omega)).le have hvx : value v.val ≤ x := hvValue.trans hxl have hlowDist : abs (x - value low) ≤ abs (x - value v.val) := by rw [abs_of_nonneg (sub_nonneg.mpr hxl), abs_of_nonneg (sub_nonneg.mpr hvx)] linarith constructor · exact hnear.trans hlowDist · intro heq have hreverse : abs (x - value low) ≤ abs (x - value (low + 1)) := hlowDist.trans_eq heq.symm have heqCell := le_antisymm hnear hreverse simpa only [hbitsHigh] using htie heqCell let expBits : Nat := ((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64) let target : F64 := ⟨fp.m.bv ||| BitVec.ofNat 64 expBits⟩ have htargetAssembly : DecimalGoalVocabularyDefault.assembledWord fp target := by dsimp only [DecimalGoalVocabularyDefault, target, expBits] change (fp.m.bv ||| BitVec.ofNat 64 ((fp.p_biased.val.emod (2 ^ 64)).toNat * 2 ^ 52 % 2 ^ 64)).toNat = _ rw [BitVec.toNat_or, BitVec.toNat_ofNat] simp only [Nat.mod_mod] rfl refine ⟨target, htargetAssembly, ?_⟩ by_cases hp0 : p ≤ 0#i32 · as_aux_lemma => simp only [hp0, if_true] at hrun obtain ⟨negp, hnegp, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨d32, hd32, hrun⟩ := bind_ok_inv _ _ _ hrun have hnegpVal : negp.val = -p.val := by have hnotmin : p.val ≠ IScalar.min .I32 := by rw [IScalar.min_I32_eq] omega have hs := IScalar.neg_step p hnotmin change IScalar.neg p = ok negp at hnegp rw [hnegp] at hs simpa only [WP.spec_ok] using hs have hdVal : d32.val = 1 - p.val := by have hs := IScalar.add_equiv negp 1#i32 rw [hd32] at hs norm_num at hs omega have hdPos : 0 < d32.val := by have := (show p.val ≤ 0 by simpa only [IScalar.le_equiv] using hp0) omega have hpNonpos : p.val ≤ 0 := by simpa only [IScalar.le_equiv] using hp0 have hxltOne : x < 1 := by have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤ (2 : Real) ^ (-1076 : Int) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega calc x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi _ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1076 : Int) := by exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound (zpow_pos (by norm_num) _).le (by positivity) _ = (2 : Real) ^ (-1022 : Int) := by rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num _ < 1 := by rw [zpow_neg] exact inv_lt_one_of_one_lt₀ (by norm_num) have noOddHalf : ∀ odd : Nat, odd % 2 = 1 → x ≠ (odd : Real) * (2 : Real) ^ (-1075 : Int) := by intro odd hodd heq have hqNeg : q.val < 0 := by by_contra hn have hq0 : 0 ≤ q.val := by omega have hw1 : (1 : Real) ≤ w.val := by exact_mod_cast hw have hpow1 : (1 : Real) ≤ (10 : Real) ^ q.val := by rw [← zpow_zero (10 : Real)] exact zpow_le_zpow_right₀ (by norm_num) hq0 have hx1 : 1 ≤ x := by dsimp only [x] nlinarith [mul_le_mul hw1 hpow1 (by norm_num : (0 : Real) ≤ 1) (by positivity : (0 : Real) ≤ (w.val : Real))] linarith obtain ⟨n, z, hqn, hn1, hz1, hzlo, hzhi, heqz, hTneg⟩ := htableFacts.2.1 hqNeg have hn342 : n ≤ 342 := by omega have hEq' : (w.val : Real) * (10 : Real) ^ (-(n : Int)) = (odd : Real) * (2 : Real) ^ (-1075 : Int) := by simpa only [x, hqn] using heq rw [zpow_neg, zpow_neg] at hEq' have hcross0 := hEq' field_simp at hcross0 have hcross : (w.val : Real) * (2 : Real) ^ (1075 : Nat) = (odd : Real) * (10 : Real) ^ n := by simpa [mul_comm] using hcross0 have hcrossNat : w.val * 2 ^ 1075 = odd * 10 ^ n := by exact_mod_cast hcross have hten : 10 ^ n = 2 ^ n * 5 ^ n := by rw [show (10 : Nat) = 2 * 5 by norm_num, mul_pow] have htwo : 2 ^ 1075 = 2 ^ n * 2 ^ (1075 - n) := by rw [← pow_add] congr 1 omega rw [hten, htwo] at hcrossNat have hcancel : w.val * 2 ^ (1075 - n) = odd * 5 ^ n := by have hc : 2 ^ n * (w.val * 2 ^ (1075 - n)) = 2 ^ n * (odd * 5 ^ n) := by simpa [mul_assoc, mul_left_comm, mul_comm] using hcrossNat exact Nat.eq_of_mul_eq_mul_left (by positivity) hc have hleft : (w.val * 2 ^ (1075 - n)) % 2 = 0 := by rw [Nat.mul_mod] have heven : 2 ^ (1075 - n) % 2 = 0 := by rw [show 1075 - n = (1075 - n - 1) + 1 by omega, pow_succ] simp simp [heven] have hright : (odd * 5 ^ n) % 2 = 1 := by rw [Nat.mul_mod, hodd] have hfive : 5 ^ n % 2 = 1 := by rw [Nat.pow_mod] norm_num simp [hfive] rw [hcancel] at hleft omega by_cases hd64 : d32 ≥ 64#i32 · simp only [hd64, if_true] at hrun have hfpEq : fp = { m := 0#u64, p_biased := 0#i32 } := (Result.ok.inj hrun).symm have hdGe : 64 ≤ d32.val := by simpa only [IScalar.le_equiv] using hd64 have hpLe : p.val ≤ -63 := by omega have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤ (2 : Real) ^ (-1139 : Int) := zpow_le_zpow_right₀ (by norm_num) (by omega) have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega have hxTiny : x < (2 : Real) ^ (-1085 : Int) := by calc x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi _ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1139 : Int) := by exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound (zpow_pos (by norm_num) _).le (by positivity) _ = (2 : Real) ^ (-1085 : Int) := by rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num have hxHalf : x < (2 : Real) ^ (-1075 : Int) := hxTiny.trans (zpow_lt_zpow_right₀ (by norm_num) (by omega)) subst fp have htargetZero : target.val = 0 := by dsimp only [target, expBits, UScalar.val] norm_num [BitVec.toNat_or, BitVec.toNat_ofNat, Int.toNat] rfl have hvalueZero : value 0 = 0 := by dsimp only [value, unit] norm_num have hvalueOne : value 1 = (2 : Real) ^ (-1074 : Int) := by dsimp only [value, unit] norm_num rfl have htwiceHalf : 2 * (2 : Real) ^ (-1075 : Int) = (2 : Real) ^ (-1074 : Int) := by calc 2 * (2 : Real) ^ (-1075 : Int) = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by norm_num _ = (2 : Real) ^ (-1074 : Int) := by rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num have hxOne : x ≤ value 1 := by rw [hvalueOne, ← htwiceHalf] linarith [hxHalf] have hxOne' : x ≤ (2 : Real) ^ (-1074 : Int) := by simpa only [hvalueOne] using hxOne apply cellRounds target 0 0 htargetZero · dsimp only [top, unit] norm_num · rw [hvalueZero] exact hxpos.le · exact hxOne · exact Or.inl rfl · intro _ rw [hvalueZero, hvalueOne] rw [sub_zero] rw [abs_of_nonneg hxpos.le, abs_of_nonpos (sub_nonpos.mpr hxOne')] rw [← htwiceHalf] linarith · intro h omega · intro _ _ norm_num · intro h omega · exact hxltOne.trans honeThreshold · simp only [hd64, if_false] at hrun obtain ⟨d32', hd32', hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨M1, hM1, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨M2, hM2, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨M3, hM3, hrun⟩ := bind_ok_inv _ _ _ hrun obtain ⟨oneSub, honeSub, hrun⟩ := bind_ok_inv _ _ _ hrun have hdLt : d32.val < 64 := by have hn : ¬64 ≤ d32.val := by simpa only [IScalar.le_equiv] using hd64 omega have hdEq : d32' = d32 := by exact Result.ok.inj (hd32'.symm.trans hd32) subst d32' let dNat : Nat := d32.toNat have hdNatCast : (dNat : Int) = d32.val := by dsimp only [dNat] exact Int.toNat_of_nonneg hdPos.le have hdNatPos : 0 < dNat := by have hi : (0 : Int) < (dNat : Int) := by rw [hdNatCast] exact hdPos exact_mod_cast hi have hM1spec := UScalar.ShiftRight_IScalar_spec G64 d32 hdPos.le hdLt rw [hM1] at hM1spec have hM1val : M1.val = G / 2 ^ dNat := by rw [hGval] at hM1spec simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, dNat] using hM1spec.1 have hM2val : M2.val = M1.val + M1.val % 2 := by have hs := UScalar.add_equiv M1 (M1 &&& 1#u64) rw [hM2, andOne] at hs exact hs.2.1 have hM3spec := UScalar.ShiftRight_IScalar_spec M2 (1#i32) (by norm_num) (by norm_num) rw [hM3] at hM3spec have hM3val : M3.val = (M1.val + M1.val % 2) / 2 := by have hs := hM3spec.1 rw [hM2val] at hs simpa [Nat.shiftRight_eq_div_pow] using hs have honeSpec := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num) (by norm_num [U64.size_eq]) rw [honeSub] at honeSpec have honeVal : oneSub.val = unit := by dsimp only [unit] norm_num [WP.spec_ok, Nat.shiftLeft_eq] at honeSpec ⊢ exact honeSpec.1 have hpowDGe : 2 ≤ 2 ^ dNat := by rw [show dNat = (dNat - 1) + 1 by omega, pow_succ] have : 1 ≤ 2 ^ (dNat - 1) := Nat.one_le_iff_ne_zero.mpr (by positivity) omega have hM1Upper : M1.val < 2 ^ 53 := by rw [hM1val] apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ dNat)).2 calc G < 2 ^ 54 := hGUpper _ = 2 ^ 53 * 2 := by norm_num [pow_succ] _ ≤ 2 ^ 53 * 2 ^ dNat := Nat.mul_le_mul_left _ hpowDGe have hM1rem : M1.val % 2 ≤ 1 := Nat.le_pred_of_lt (Nat.mod_lt _ (by omega)) have hM3Upper : M3.val ≤ unit := by rw [hM3val] dsimp only [unit] omega let lowSub : Nat := M1.val / 2 let chooseSub : Prop := M1.val % 2 = 1 have hM3choice : M3.val = if chooseSub then lowSub + 1 else lowSub := by rw [hM3val] dsimp only [chooseSub, lowSub] by_cases hc : M1.val % 2 = 1 · rw [if_pos hc, hc] omega · have hz : M1.val % 2 = 0 := by omega rw [if_neg hc, hz] omega simp only [hM1, hM2, hM3, honeSub, bind_tc_ok] at hrun have htargetM3 : target.val = M3.val := by by_cases hc : M3 ≥ oneSub · have hge : unit ≤ M3.val := by rw [← honeVal] simpa only [UScalar.le_equiv] using hc have hM3Eq : M3.val = unit := by omega have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) = 1#i32 := by simp [hc, IScalar.cast_fromBool] rfl rw [hpbi] at hrun have hfpEq : fp = { m := M3, p_biased := 1#i32 } := (Result.ok.inj hrun).symm have ha := htargetAssembly dsimp only [DecimalGoalVocabularyDefault] at ha rw [hfpEq] at ha norm_num [Int.toNat, hM3Eq, unit] at ha omega · have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) = 0#i32 := by simp [hc, IScalar.cast_fromBool] rfl rw [hpbi] at hrun have hfpEq : fp = { m := M3, p_biased := 0#i32 } := (Result.ok.inj hrun).symm have ha := htargetAssembly dsimp only [DecimalGoalVocabularyDefault] at ha rw [hfpEq] at ha norm_num [Int.toNat] at ha exact ha let base : Nat := 2 ^ dNat let deltaSub : Real := (2 : Real) ^ (p.val - 1076) let subUnit : Real := (2 : Real) ^ (-1074 : Int) have hbasePos : 0 < base := by dsimp only [base]; positivity have hM1LowerNat : M1.val * base ≤ G := by rw [hM1val] dsimp only [base] exact Nat.div_mul_le_self G (2 ^ dNat) have hM1UpperNat : G < (M1.val + 1) * base := by rw [hM1val] dsimp only [base] simpa only [Nat.mul_comm] using Nat.lt_mul_div_succ G (by positivity : 0 < 2 ^ dNat) have hM1decomp : M1.val = 2 * lowSub + M1.val % 2 := by dsimp only [lowSub] have hd := Nat.mod_add_div M1.val 2 omega have hscale : (base : Real) * deltaSub = (2 : Real) ^ (-1075 : Int) := by dsimp only [base, deltaSub] push_cast rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] congr 1 rw [hdNatCast, hdVal] omega have hsubUnitEq : subUnit = 2 * (base : Real) * deltaSub := by have hp : (2 : Real) ^ (-1074 : Int) = 2 * (2 : Real) ^ (-1075 : Int) := by calc (2 : Real) ^ (-1074 : Int) = (2 : Real) ^ (1 + (-1075 : Int)) := by norm_num _ = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] _ = 2 * (2 : Real) ^ (-1075 : Int) := by norm_num dsimp only [subUnit] rw [hp, ← hscale] ring have hlowSubUpper : lowSub < unit := by dsimp only [lowSub, unit] apply (Nat.div_lt_iff_lt_mul (by omega : 0 < 2)).2 norm_num at hM1Upper ⊢ exact hM1Upper have hlowSucc : lowSub + 1 ≤ unit := by omega have subValue : ∀ n : Nat, n ≤ unit → value n = (n : Real) * subUnit := by intro n hn by_cases hlt : n < unit · dsimp only [value] rw [Nat.div_eq_of_lt hlt, Nat.mod_eq_of_lt hlt, if_pos rfl] dsimp only [subUnit] rw [zpow_neg] exact div_eq_mul_inv _ _ · have heq : n = unit := by omega subst n dsimp only [value] rw [Nat.div_self (by dsimp only [unit]; positivity), Nat.mod_self, if_neg (by omega), if_neg (by omega)] dsimp only [subUnit] rw [zpow_neg] norm_num exact div_eq_mul_inv _ _ have hvalueLowEq : value lowSub = (lowSub : Real) * subUnit := subValue lowSub hlowSubUpper.le have hvalueHighEq : value (lowSub + 1) = ((lowSub + 1 : Nat) : Real) * subUnit := subValue (lowSub + 1) hlowSucc have hxLowSub : (lowSub : Real) * subUnit ≤ x := by have hc0 : 2 * lowSub * base ≤ G := (Nat.mul_le_mul_right base (by omega : 2 * lowSub ≤ M1.val)).trans hM1LowerNat have hc0R : ((2 * lowSub * base : Nat) : Real) ≤ (G : Real) := by exact_mod_cast hc0 push_cast at hc0R have hc : (2 : Real) * lowSub * base * deltaSub ≤ (G : Real) * deltaSub := mul_le_mul_of_nonneg_right hc0R (by dsimp only [deltaSub] exact (zpow_pos (by norm_num) _).le) have hxLo' : (G : Real) * deltaSub ≤ x := by simpa only [deltaSub] using hxcellLo calc (lowSub : Real) * subUnit = (2 : Real) * lowSub * base * deltaSub := by rw [hsubUnitEq] ring _ ≤ (G : Real) * deltaSub := hc _ ≤ x := hxLo' have hxHighSub : x ≤ ((lowSub + 1 : Nat) : Real) * subUnit := by have hcNat : G + 1 ≤ 2 * (lowSub + 1) * base := by have h1 : G + 1 ≤ (M1.val + 1) * base := by omega have h2 : M1.val + 1 ≤ 2 * (lowSub + 1) := by omega exact h1.trans (Nat.mul_le_mul_right base h2) have hc : ((G + 1 : Nat) : Real) * deltaSub ≤ ((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := by exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by dsimp only [deltaSub] exact (zpow_pos (by norm_num) _).le) have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by simpa only [deltaSub] using hxcellHi calc x ≤ ((G + 1 : Nat) : Real) * deltaSub := hxHi'.le _ ≤ ((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := hc _ = ((lowSub + 1 : Nat) : Real) * subUnit := by rw [hsubUnitEq] push_cast ring have hmidHigh (hc : chooseSub) : ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub ≤ x := by have hodd : M1.val % 2 = 1 := hc have hEq : M1.val = 2 * lowSub + 1 := by omega rw [← hEq] have hcast : (M1.val : Real) * (base : Real) ≤ G := by exact_mod_cast hM1LowerNat have hxLo' : (G : Real) * deltaSub ≤ x := by simpa only [deltaSub] using hxcellLo exact (mul_le_mul_of_nonneg_right hcast (by dsimp only [deltaSub] exact (zpow_pos (by norm_num) _).le)).trans hxLo' have hmidLow (hc : ¬chooseSub) : x < ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by have hzero : M1.val % 2 = 0 := by dsimp only [chooseSub] at hc omega have hEq : M1.val = 2 * lowSub := by omega have hcNat : G + 1 ≤ (2 * lowSub + 1) * base := by rw [← hEq] omega have hcReal : ((G + 1 : Nat) : Real) * deltaSub ≤ ((2 * lowSub + 1) * base : Nat) * deltaSub := by exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by dsimp only [deltaSub] exact (zpow_pos (by norm_num) _).le) have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by simpa only [deltaSub] using hxcellHi calc x < ((G + 1 : Nat) : Real) * deltaSub := hxHi' _ ≤ (((2 * lowSub + 1) * base : Nat) : Real) * deltaSub := hcReal _ = ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by push_cast ring have hLowNearSub (hc : ¬chooseSub) : abs (x - (lowSub : Real) * subUnit) ≤ abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) := by rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub), abs_of_nonpos (sub_nonpos.mpr hxHighSub)] have hm := hmidLow hc rw [hsubUnitEq] push_cast at hm ⊢ nlinarith only [hm] have hHighNearSub (hc : chooseSub) : abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) ≤ abs (x - (lowSub : Real) * subUnit) := by rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub), abs_of_nonneg (sub_nonneg.mpr hxLowSub)] have hm := hmidHigh hc rw [hsubUnitEq] push_cast at hm ⊢ nlinarith only [hm] have hchoiceSub : M3.val = lowSub ∨ M3.val = lowSub + 1 := by rw [hM3choice] split <;> simp apply cellRounds target lowSub M3.val htargetM3 · dsimp only [top, unit] omega · rw [hvalueLowEq] exact hxLowSub · rw [hvalueHighEq] exact hxHighSub · exact hchoiceSub · intro hsel rw [hvalueLowEq, hvalueHighEq] apply hLowNearSub by_contra hc rw [hM3choice, if_pos hc] at hsel omega · intro hsel rw [hvalueLowEq, hvalueHighEq] apply hHighNearSub by_contra hc rw [hM3choice, if_neg hc] at hsel omega · intro hsel heq rw [hvalueLowEq, hvalueHighEq] at heq have hc : ¬chooseSub := by by_contra hc rw [hM3choice, if_pos hc] at hsel omega have hm := hmidLow hc rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub), abs_of_nonpos (sub_nonpos.mpr hxHighSub)] at heq rw [hsubUnitEq] at heq push_cast at heq hm nlinarith only [heq, hm] · intro hsel heq rw [hvalueLowEq, hvalueHighEq] at heq have hc : chooseSub := by by_contra hc rw [hM3choice, if_neg hc] at hsel omega have hm := hmidHigh hc rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub), abs_of_nonneg (sub_nonneg.mpr hxLowSub)] at heq have hxmid : x = ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by have hxmidSub : 2 * x = ((2 * lowSub + 1 : Nat) : Real) * subUnit := by push_cast at heq ⊢ linarith only [heq] rw [hsubUnitEq] at hxmidSub push_cast at hxmidSub ⊢ ring_nf at hxmidSub ⊢ linarith only [hxmidSub] have hodd : M1.val % 2 = 1 := hc have hM1Eq : M1.val = 2 * lowSub + 1 := by omega exfalso apply (noOddHalf M1.val hodd) rw [hxmid, ← hM1Eq] calc (M1.val : Real) * (base : Real) * deltaSub = (M1.val : Real) * ((base : Real) * deltaSub) := by ring _ = (M1.val : Real) * (2 : Real) ^ (-1075 : Int) := by rw [hscale] · exact hxltOne.trans honeThreshold · as_aux_lemma => let adjustRun : Result Std.U64 := if lo ≤ 1#u64 then if q ≥ IScalar.cast .I64 (-4)#i32 then if q ≤ IScalar.cast .I64 23#i32 then if G64 &&& 3#u64 = 1#u64 then do let i26 ← i9 - UScalar.hcast .I32 52#u32 let i27 ← i26 - 3#i32 let i28 ← G64 <<< i27 if i28 = hi then ok (G64 &&& ~~~1#u64) else ok G64 else ok G64 else ok G64 else ok G64 else ok G64 let carryRun (S64 twoU : Std.U64) : Result (Std.U64 × Std.I32) := if S64 ≥ twoU then do let mantissa5 ← 1#u64 <<< 52#u32 let power22 ← p + 1#i32 ok (mantissa5, power22) else ok (S64, p) have hposInv : ∃ (Gc Ga S64 twoU Sf : Std.U64) (pf : Std.I32) (oneU : Std.U64), adjustRun = ok Gc ∧ Gc + (Gc &&& 1#u64) = ok Ga ∧ Ga >>> 1#i32 = ok S64 ∧ 2#u64 <<< 52#u32 = ok twoU ∧ carryRun S64 twoU = ok (Sf, pf) ∧ 1#u64 <<< 52#u32 = ok oneU ∧ (if pf ≥ 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 } else ok { m := Sf &&& ~~~oneU, p_biased := pf }) = ok fp := by as_aux_lemma => have hr := hrun simp only [hp0, if_false] at hr rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MIN_EXPONENT_ROUND_TO_EVEN.eq_def, F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.MAX_EXPONENT_ROUND_TO_EVEN.eq_def] at hr change (do let Gc ← adjustRun let Ga ← Gc + (Gc &&& 1#u64) let S64 ← Ga >>> 1#i32 let twoU ← 2#u64 <<< 52#u32 let pair ← carryRun S64 twoU let (Sf, pf) := pair let oneU ← 1#u64 <<< 52#u32 if pf ≥ 2047#i32 then ok { m := 0#u64, p_biased := 2047#i32 } else ok { m := Sf &&& ~~~oneU, p_biased := pf }) = ok fp at hr obtain ⟨Gc, hGc, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨Ga, hGa, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨S64, hS64, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨twoU, htwoU, hr⟩ := bind_ok_inv _ _ _ hr obtain ⟨pair, hcarry, hr⟩ := bind_ok_inv _ _ _ hr rcases pair with ⟨Sf, pf⟩ simp only [Aeneas.Std.uncurry_apply_pair] at hr obtain ⟨oneU, honeU, hr⟩ := bind_ok_inv _ _ _ hr exact ⟨Gc, Ga, S64, twoU, Sf, pf, oneU, hGc, hGa, hS64, htwoU, hcarry, honeU, hr⟩ rcases hposInv with ⟨Gc, Ga, S64, twoU, Sf, pf, oneU, hGc, hGa, hS64, htwoU, hcarry, honeU, hfinal⟩ dsimp only [adjustRun] at hGc dsimp only [carryRun] at hcarry have hcastNeg4 : IScalar.cast .I64 (-4)#i32 = (-4)#i64 := rfl have hcast23 : IScalar.cast .I64 23#i32 = 23#i64 := rfl have hshiftWP := UScalar.ShiftLeft_IScalar_spec G64 i12 (2 ^ 64) (by change 0 ≤ i12.val; rw [hi12val]; omega) (by change i12.val < 64 rw [hi12val] omega) (by norm_num [U64.size_eq]) obtain ⟨H, hH, hHspec⟩ := Aeneas.Std.WP.spec_imp_exists hshiftWP have hGshiftBound : G * 2 ^ (9 + u) < 2 ^ 64 := by interval_cases u <;> norm_num at hGUpper ⊢ <;> omega have hHval : H.val = G * 2 ^ (9 + u) := by have hb : G * 2 ^ (9 + u) < 18446744073709551616 := by norm_num at hGshiftBound ⊢ exact hGshiftBound have hv : H.val = (G * 2 ^ (9 + u)) % 18446744073709551616 := by simpa only [hGval, hi12nat, Nat.shiftLeft_eq] using hHspec.1 rw [Nat.mod_eq_of_lt hb] at hv exact hv have hshiftIff : H = hi ↔ G * 2 ^ (9 + u) = hi.val := by as_aux_lemma => constructor · intro heq rw [← hHval, heq] · intro heq apply UScalar.eq_of_val_eq omega have hmodIff : G64 &&& 3#u64 = 1#u64 ↔ G % 4 = 1 := by as_aux_lemma => constructor · intro heq have := congrArg UScalar.val heq rw [andThree, hGval] at this norm_num at this exact this · intro heq apply UScalar.eq_of_val_eq rw [andThree, hGval] norm_num exact heq let tieLow : Prop := x = (G : Real) * (2 : Real) ^ (p.val - 1076) ∧ G % 4 = 1 let codeTie : Prop := lo ≤ 1#u64 ∧ q ≥ (-4)#i64 ∧ q ≤ 23#i64 ∧ G64 &&& 3#u64 = 1#u64 ∧ H = hi have hcodeTie : codeTie ↔ tieLow := by as_aux_lemma => dsimp only [codeTie, tieLow] constructor · intro hc apply htie.mpr refine ⟨?_, ?_, hmodIff.mp hc.2.2.2.1, ?_, hshiftIff.mp hc.2.2.2.2⟩ · simpa only [IScalar.le_equiv] using hc.2.1 · simpa only [IScalar.le_equiv] using hc.2.2.1 · simpa only [UScalar.le_equiv] using hc.1 · intro ht have hc := htie.mp ht refine ⟨?_, ?_, ?_, hmodIff.mpr hc.2.2.1, hshiftIff.mpr hc.2.2.2.2⟩ · simpa only [UScalar.le_equiv] using hc.2.2.2.1 · simpa only [IScalar.le_equiv] using hc.1 · simpa only [IScalar.le_equiv] using hc.2.1 rw [hcastNeg4, hcast23, hi11] at hGc simp only [bind_tc_ok] at hGc rw [hi12] at hGc simp only [bind_tc_ok] at hGc rw [hH] at hGc simp only [bind_tc_ok] at hGc have hGcEq : Gc = if codeTie then G64 &&& ~~~1#u64 else G64 := by as_aux_lemma => dsimp only [codeTie] by_cases c1 : lo ≤ 1#u64 <;> by_cases c2 : q ≥ (-4)#i64 <;> by_cases c3 : q ≤ 23#i64 <;> by_cases c4 : G64 &&& 3#u64 = 1#u64 <;> by_cases c5 : H = hi <;> simp only [c1, c2, c3, c4, c5, if_true, if_false, and_true, and_false] at hGc ⊢ <;> exact (Result.ok.inj hGc).symm have hGcval : Gc.val = if tieLow then G - 1 else G := by as_aux_lemma => by_cases ht : tieLow · have hc := hcodeTie.mpr ht rw [if_pos ht, hGcEq, if_pos hc] rw [clearLow, hGval] · dsimp only [tieLow] at ht omega · have hc : ¬ codeTie := fun h => ht (hcodeTie.mp h) rw [if_neg ht, hGcEq, if_neg hc, hGval] have hGaVal : Ga.val = Gc.val + Gc.val % 2 := by have hs := UScalar.add_equiv Gc (Gc &&& 1#u64) rw [hGa] at hs rw [andOne] at hs exact hs.2.1 have hSspec := UScalar.ShiftRight_IScalar_spec Ga (1#i32) (by norm_num) (by norm_num) rw [hS64] at hSspec have hSval : S64.val = (Gc.val + Gc.val % 2) / 2 := by have hv := hSspec.1 rw [hGaVal] at hv simpa [Nat.shiftRight_eq_div_pow] using hv let lowSig : Nat := G / 2 let chooseHigh : Prop := G % 2 = 1 ∧ ¬ tieLow have hSchoice : S64.val = if chooseHigh then lowSig + 1 else lowSig := by as_aux_lemma => dsimp only [chooseHigh, lowSig] by_cases ht : tieLow · rw [if_pos ht] at hGcval have htmod : G % 4 = 1 := by simpa only [tieLow] using ht.2 have hmod2 : G % 2 = 1 := by omega rw [if_neg (by simp [ht])] rw [hSval, hGcval] omega · rw [if_neg ht] at hGcval by_cases hodd : G % 2 = 1 · rw [if_pos (by exact ⟨hodd, ht⟩)] rw [hSval, hGcval] omega · rw [if_neg (by simp [hodd])] rw [hSval, hGcval] have hm := Nat.mod_lt G (by omega : 0 < 2) omega have hLowSigLower : 2 ^ 52 ≤ lowSig := by dsimp only [lowSig] apply (Nat.le_div_iff_mul_le (by omega : 0 < 2)).2 norm_num at hGLower ⊢ exact hGLower have hLowSigUpper : lowSig < 2 ^ 53 := by dsimp only [lowSig] apply (Nat.div_lt_iff_lt_mul (by omega : 0 < 2)).2 norm_num at hGUpper ⊢ exact hGUpper have hSBounds : 2 ^ 52 ≤ S64.val ∧ S64.val ≤ 2 ^ 53 := by rw [hSchoice] split <;> omega have htwoUSpec := UScalar.ShiftLeft_spec (2#u64) (52#u32) (2 ^ 64) (by norm_num) (by norm_num [U64.size_eq]) rw [htwoU] at htwoUSpec have htwoUval : twoU.val = 2 ^ 53 := by norm_num [WP.spec_ok, Nat.shiftLeft_eq] at htwoUSpec ⊢ exact htwoUSpec.1 have honeUSpec := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64) (by norm_num) (by norm_num [U64.size_eq]) rw [honeU] at honeUSpec have honeUval : oneU.val = 2 ^ 52 := by norm_num [WP.spec_ok, Nat.shiftLeft_eq] at honeUSpec ⊢ exact honeUSpec.1 have hcarryIff : S64 ≥ twoU ↔ S64.val = 2 ^ 53 := by change twoU.val ≤ S64.val ↔ S64.val = 2 ^ 53 rw [htwoUval] omega have hSfPf : (S64.val = 2 ^ 53 ∧ Sf = oneU ∧ pf.val = p.val + 1) ∨ (S64.val < 2 ^ 53 ∧ Sf = S64 ∧ pf = p) := by as_aux_lemma => by_cases hc : S64 ≥ twoU · have hceq := hcarryIff.mp hc simp only [hc, if_true, honeU, bind_tc_ok] at hcarry obtain ⟨pnext, hpnext, hpair⟩ := bind_ok_inv _ _ _ hcarry have hpnextVal : pnext.val = p.val + 1 := by have hs := IScalar.add_equiv p 1#i32 rw [hpnext] at hs exact hs.2.1 have heq := Result.ok.inj hpair cases heq exact Or.inl ⟨hceq, rfl, hpnextVal⟩ · simp only [hc, if_false] at hcarry have heq := Result.ok.inj hcarry have hclt : S64.val < 2 ^ 53 := by have hc' : ¬ twoU.val ≤ S64.val := by simpa only [UScalar.le_equiv] using hc rw [htwoUval] at hc' omega exact Or.inr ⟨hclt, (congrArg Prod.fst heq).symm, (congrArg Prod.snd heq).symm⟩ have hpPos : 0 < p.val := by have hn : ¬ p.val ≤ 0 := by simpa only [IScalar.le_equiv] using hp0 omega let pNat : Nat := p.val.toNat have hpNatCast : (pNat : Int) = p.val := by dsimp only [pNat] exact Int.toNat_of_nonneg hpPos.le let deltaR : Real := (2 : Real) ^ (p.val - 1076) let ulpR : Real := (2 : Real) ^ (p.val - 1075) have hdeltaPos : 0 < deltaR := by dsimp only [deltaR] exact zpow_pos (by norm_num) _ have hulpEq : ulpR = 2 * deltaR := by dsimp only [ulpR, deltaR] rw [show p.val - 1075 = (p.val - 1076) + 1 by omega, zpow_add₀ (by norm_num : (2 : Real) ≠ 0)] norm_num ring have hGdecomp : G = 2 * lowSig + G % 2 := by dsimp only [lowSig] have hd := Nat.mod_add_div G 2 omega have hrem2 : G % 2 ≤ 1 := Nat.le_pred_of_lt (Nat.mod_lt _ (by omega)) have hxcellLo' : (G : Real) * deltaR ≤ x := by simpa only [deltaR] using hxcellLo have hxcellHi' : x < ((G + 1 : Nat) : Real) * deltaR := by simpa only [deltaR] using hxcellHi have hLowX : (lowSig : Real) * ulpR ≤ x := by have hc : ((2 * lowSig : Nat) : Real) * deltaR ≤ (G : Real) * deltaR := by apply mul_le_mul_of_nonneg_right _ hdeltaPos.le exact_mod_cast (by omega : 2 * lowSig ≤ G) rw [hulpEq] push_cast at hc ⊢ linarith only [hc, hxcellLo'] have hXHigh : x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := by have hc : ((G + 1 : Nat) : Real) * deltaR ≤ ((2 * (lowSig + 1) : Nat) : Real) * deltaR := by apply mul_le_mul_of_nonneg_right _ hdeltaPos.le exact_mod_cast (by omega : G + 1 ≤ 2 * (lowSig + 1)) calc x ≤ ((G + 1 : Nat) : Real) * deltaR := hxcellHi'.le _ ≤ ((2 * (lowSig + 1) : Nat) : Real) * deltaR := hc _ = ((lowSig + 1 : Nat) : Real) * ulpR := by rw [hulpEq] push_cast ring have hLowNear (hc : ¬ chooseHigh) : abs (x - (lowSig : Real) * ulpR) ≤ abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) := by as_aux_lemma => rw [abs_of_nonneg (sub_nonneg.mpr hLowX), abs_of_nonpos (sub_nonpos.mpr hXHigh)] dsimp only [chooseHigh] at hc by_cases ht : tieLow · have hteq := ht.1 dsimp only [tieLow] at ht rw [hteq] rw [hulpEq] push_cast have hg : (G : Real) = 2 * (lowSig : Real) + 1 := by exact_mod_cast (show G = 2 * lowSig + 1 by omega) nlinarith only [hdeltaPos, hg] · have heven : G % 2 = 0 := by have hnotOdd : G % 2 ≠ 1 := by intro hodd exact hc ⟨hodd, ht⟩ have hm : G % 2 < 2 := Nat.mod_lt G (by omega) have hmle : G % 2 ≤ 1 := Nat.lt_succ_iff.mp hm apply Nat.eq_zero_of_not_pos intro hpos exact hnotOdd (Nat.le_antisymm hmle hpos) have hmid : x < ((2 * lowSig + 1 : Nat) : Real) * deltaR := by have hnext : G + 1 = 2 * lowSig + 1 := by omega simpa only [hnext] using hxcellHi' rw [hulpEq] push_cast at hmid ⊢ nlinarith only [hmid, hdeltaPos] have hHighNear (hc : chooseHigh) : abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) ≤ abs (x - (lowSig : Real) * ulpR) := by as_aux_lemma => rw [abs_of_nonpos (sub_nonpos.mpr hXHigh), abs_of_nonneg (sub_nonneg.mpr hLowX)] have hodd : G % 2 = 1 := hc.1 have hmid : ((2 * lowSig + 1 : Nat) : Real) * deltaR ≤ x := by have hg : G = 2 * lowSig + 1 := by omega simpa only [hg] using hxcellLo' rw [hulpEq] push_cast at hmid ⊢ nlinarith only [hmid, hdeltaPos] let lowBits : Nat := pNat * unit + (lowSig - unit) let selectedBits : Nat := if chooseHigh then lowBits + 1 else lowBits have hlowParity : lowBits % 2 = lowSig % 2 := by dsimp only [lowBits, unit] omega have hTieLowEven (hc : ¬ chooseHigh) (heq : abs (x - (lowSig : Real) * ulpR) = abs (x - ((lowSig + 1 : Nat) : Real) * ulpR)) : lowBits % 2 = 0 := by as_aux_lemma => rw [abs_of_nonneg (sub_nonneg.mpr hLowX), abs_of_nonpos (sub_nonpos.mpr hXHigh)] at heq have hmid : x = ((2 * lowSig + 1 : Nat) : Real) * deltaR := by rw [hulpEq] at heq push_cast at heq ⊢ nlinarith only [heq, hdeltaPos] have hgOdd : G % 2 = 1 := by by_contra hn have hm := Nat.mod_lt G (by omega : 0 < 2) have hge : G + 1 = 2 * lowSig + 1 := by omega have := hxcellHi' rw [hge, hmid] at this exact (lt_irrefl _ this) have ht : tieLow := by dsimp only [tieLow] have hG : G = 2 * lowSig + 1 := by omega refine ⟨?_, ?_⟩ · simpa only [hG] using hmid · dsimp only [chooseHigh] at hc have hnot : ¬ (G % 2 = 1 ∧ ¬tieLow) := hc by_contra hm4 have hm4lt := Nat.mod_lt G (by omega : 0 < 4) have hnTie : ¬ tieLow := by intro hyes exact hm4 hyes.2 exact hnot ⟨hgOdd, hnTie⟩ have hm4 := ht.2 rw [hlowParity] omega have hTieHighEven (hc : chooseHigh) (heq : abs (x - ((lowSig + 1 : Nat) : Real) * ulpR) = abs (x - (lowSig : Real) * ulpR)) : (lowBits + 1) % 2 = 0 := by as_aux_lemma => rw [abs_of_nonpos (sub_nonpos.mpr hXHigh), abs_of_nonneg (sub_nonneg.mpr hLowX)] at heq have hmid : x = ((2 * lowSig + 1 : Nat) : Real) * deltaR := by rw [hulpEq] at heq push_cast at heq ⊢ nlinarith only [heq, hdeltaPos] have hodd := hc.1 have hG : G = 2 * lowSig + 1 := by omega have hnotTie := hc.2 have hnotMod : G % 4 ≠ 1 := by intro hm apply hnotTie dsimp only [tieLow] exact ⟨by simpa only [hG] using hmid, hm⟩ have hm4lt := Nat.mod_lt G (by omega : 0 < 4) rw [Nat.add_mod, hlowParity] omega have hfpResult : (pf ≥ 2047#i32 → fp = { m := 0#u64, p_biased := 2047#i32 }) ∧ (¬ pf ≥ 2047#i32 → fp = { m := Sf &&& ~~~oneU, p_biased := pf }) := by as_aux_lemma => constructor · intro hover have hr := hfinal simp only [hover, if_true] at hr exact (Result.ok.inj hr).symm · intro hover have hr := hfinal simp only [hover, if_false] at hr exact (Result.ok.inj hr).symm have hfinish : DecimalGoalVocabularyDefault.roundsMagnitude x target := by as_aux_lemma => by_cases hover : pf ≥ 2047#i32 · as_aux_lemma => have hfpEq : fp = { m := 0#u64, p_biased := 2047#i32 } := hfpResult.1 hover have htargetTop : target.val = top := by subst fp dsimp only [DecimalGoalVocabularyDefault] at htargetAssembly dsimp only [top, unit] norm_num [Int.toNat] at htargetAssembly ⊢ exact htargetAssembly have hpfGe : 2047 ≤ pf.val := by simpa only [IScalar.le_equiv] using hover have hthreshold : threshold ≤ x := by rcases hSfPf with hcarryCase | hplainCase · have hpGe : 2046 ≤ p.val := by omega have hchoose : chooseHigh := by by_contra hn rw [hSchoice, if_neg hn] at hcarryCase omega have hlowMax : lowSig = 2 ^ 53 - 1 := by rw [hSchoice, if_pos hchoose] at hcarryCase omega have hGmax : G = 2 ^ 54 - 1 := by have hodd := hchoose.1 omega have hdelta970 : (2 : Real) ^ (970 : Nat) ≤ deltaR := by dsimp only [deltaR] rw [← zpow_natCast] exact zpow_le_zpow_right₀ (by norm_num) (by omega) have hbase : threshold = ((2 ^ 54 - 1 : Nat) : Real) * (2 : Real) ^ (970 : Nat) := by dsimp only [threshold] rw [show (2 : Real) ^ (1024 : Nat) = (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) by rw [← pow_add]] push_cast ring rw [hbase, ← hGmax] exact (mul_le_mul_of_nonneg_left hdelta970 (by positivity)).trans hxcellLo' · have hpGe : 2047 ≤ p.val := by rcases hplainCase with ⟨_, _, heq⟩ have := congrArg IScalar.val heq omega have hdelta971 : (2 : Real) ^ (971 : Nat) ≤ deltaR := by dsimp only [deltaR] rw [← zpow_natCast] exact zpow_le_zpow_right₀ (by norm_num) (by omega) calc threshold ≤ (2 : Real) ^ (1024 : Nat) := by dsimp only [threshold] exact sub_le_self _ (by positivity) _ = (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (971 : Nat) := by rw [← pow_add] _ ≤ (G : Real) * deltaR := by exact mul_le_mul (by exact_mod_cast hGLower) hdelta971 (by positivity) (by positivity) _ ≤ x := hxcellLo' dsimp only [DecimalGoalVocabularyDefault] change 0 ≤ x ∧ target.val ≤ top ∧ (if target.val = top then threshold ≤ x else _) rw [htargetTop, if_pos rfl] exact ⟨hxpos.le, le_rfl, hthreshold⟩ · have hfiniteData : target.val = pf.val.toNat * unit + (Sf.val - unit) ∧ target.val = selectedBits ∧ Sf.val < 2 ^ 53 ∧ pf.val.toNat ≤ 2046 ∧ pNat ≤ 2046 ∧ lowBits + 1 ≤ top := by as_aux_lemma => have hfpEq : fp = { m := Sf &&& ~~~oneU, p_biased := pf } := hfpResult.2 hover have hpfLt : pf.val < 2047 := by have hn : ¬2047 ≤ pf.val := by simpa only [IScalar.le_equiv] using hover omega have hpfPos : 0 < pf.val := by rcases hSfPf with hcarryCase | hplainCase · omega · have heq := congrArg IScalar.val hplainCase.2.2 omega have hSfRange : 2 ^ 52 ≤ Sf.val ∧ Sf.val < 2 ^ 53 := by as_aux_lemma => rcases hSfPf with hcarryCase | hplainCase · rw [hcarryCase.2.1, honeUval] norm_num · rw [hplainCase.2.1] exact ⟨hSBounds.1, hplainCase.1⟩ have hmVal : (Sf &&& ~~~oneU).val = Sf.val - 2 ^ 52 := clearHidden Sf oneU honeUval hSfRange.1 hSfRange.2 have hpfEmod : pf.val.emod (2 ^ 64) = pf.val := by apply Int.emod_eq_of_lt hpfPos.le omega have hpfNatCast : (pf.val.toNat : Int) = pf.val := Int.toNat_of_nonneg hpfPos.le have hpfNatUpper : pf.val.toNat ≤ 2046 := by rw [Int.toNat_le] omega have htargetFormula : target.val = pf.val.toNat * unit + (Sf.val - unit) := by have ha := htargetAssembly dsimp only [DecimalGoalVocabularyDefault] at ha rw [hfpEq, hpfEmod] at ha have hmBound : Sf.val - unit < 2 ^ 52 := by dsimp only [unit] omega have heBound : pf.val.toNat * 2 ^ 52 < 2 ^ 64 := by omega have hor := fieldOr (Sf.val - unit) pf.val.toNat hmBound heBound dsimp only [unit] at ha hor ⊢ rw [Nat.mod_eq_of_lt heBound, hmVal] at ha exact ha.trans (hor.trans (Nat.add_comm _ _)) have htargetSelected : target.val = selectedBits := by as_aux_lemma => rw [htargetFormula] rcases hSfPf with hcarryCase | hplainCase · have hchoose : chooseHigh := by by_contra hn rw [hSchoice, if_neg hn] at hcarryCase omega have hlowMax : lowSig = 2 ^ 53 - 1 := by rw [hSchoice, if_pos hchoose] at hcarryCase omega rw [hcarryCase.2.1, honeUval] have hpfEq : pf.val.toNat = pNat + 1 := by apply Nat.cast_injective (R := Int) rw [hpfNatCast, Nat.cast_add, Nat.cast_one, hpNatCast] push_cast omega rw [hpfEq] dsimp only [selectedBits, lowBits, unit] rw [if_pos hchoose] omega · have hpfEq : pf.val.toNat = pNat := by apply Nat.cast_injective (R := Int) rw [hpfNatCast, hpNatCast] have heq := congrArg IScalar.val hplainCase.2.2 omega rw [hplainCase.2.1, hSchoice, hpfEq] dsimp only [selectedBits, lowBits, unit] split <;> omega have hpNatUpper : pNat ≤ 2046 := by as_aux_lemma => rcases hSfPf with hcarryCase | hplainCase · have hi : (pNat : Int) ≤ 2046 := by rw [hpNatCast] omega exact_mod_cast hi · have heq := congrArg IScalar.val hplainCase.2.2 have hi : (pNat : Int) ≤ 2046 := by rw [hpNatCast] omega exact_mod_cast hi have hlowSuccLe : lowBits + 1 ≤ top := by dsimp only [lowBits, top, unit] omega exact ⟨htargetFormula, htargetSelected, hSfRange.2, hpfNatUpper, hpNatUpper, hlowSuccLe⟩ rcases hfiniteData with ⟨htargetFormula, htargetSelected, hSfUpper, hpfNatUpper, hpNatUpper, hlowSuccLe⟩ by_cases hboundary : lowBits + 1 = top · as_aux_lemma => have hpEq : pNat = 2046 := by dsimp only [lowBits, top, unit] at hboundary omega have hlowMax : lowSig = 2 ^ 53 - 1 := by dsimp only [lowBits, top, unit] at hboundary omega have htargetLt : target.val < top := by rw [htargetFormula] dsimp only [top, unit] omega have hselectedLt : selectedBits < top := by omega have hnotChoose : ¬chooseHigh := by intro hc dsimp only [selectedBits] at hselectedLt rw [if_pos hc, hboundary] at hselectedLt omega have htargetLow : target.val = lowBits := by rw [htargetSelected] dsimp only [selectedBits] rw [if_neg hnotChoose] have hGremZero : G % 2 = 0 := by by_contra hnzero have hm := Nat.mod_lt G (by omega : 0 < 2) have hodd : G % 2 = 1 := by omega have ht : tieLow := by by_contra hnt exact hnotChoose ⟨hodd, hnt⟩ have hG : G = 2 * lowSig + 1 := by omega dsimp only [tieLow] at ht rw [hG, hlowMax] at ht norm_num at ht have hGEq : G = 2 * lowSig := by omega have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by dsimp only [ulpR] rw [← hpNatCast, hpEq] norm_num exact zpow_natCast (2 : Real) 971 have hrem : lowSig - unit < unit := by dsimp only [unit] omega have hlowDiv : lowBits / unit = pNat := by dsimp only [lowBits] rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by dsimp only [unit] positivity), Nat.div_eq_of_lt hrem, Nat.add_zero] have hlowMod : lowBits % unit = lowSig - unit := by dsimp only [lowBits] rw [Nat.mul_comm pNat unit, Nat.mul_add_mod, Nat.mod_eq_of_lt hrem] have hvalueLowEq : value lowBits = (lowSig : Real) * ulpR := by dsimp only [value] rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega), hulp2046] have hsum : unit + (lowSig - unit) = lowSig := by dsimp only [unit] omega rw [hsum, hpEq] have hvalueLow : value lowBits ≤ x := by rw [hvalueLowEq] exact hLowX have hdelta2046 : deltaR = (2 : Real) ^ (970 : Nat) := by dsimp only [deltaR] rw [← hpNatCast, hpEq] norm_num exact zpow_natCast (2 : Real) 970 have hbelow : x < threshold := by have htopCell : ((G + 1 : Nat) : Real) * deltaR = threshold := by rw [hGEq, hlowMax, hdelta2046] dsimp only [threshold] have heq : (2 : Real) ^ (1024 : Nat) = (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by rw [← pow_add] rw [heq] push_cast ring exact hxcellHi'.trans_eq htopCell dsimp only [DecimalGoalVocabularyDefault] change 0 ≤ x ∧ target.val ≤ top ∧ (if target.val = top then threshold ≤ x else x < threshold ∧ ∀ v : F64, v.val < top → v ≠ target → abs (x - value target.val) ≤ abs (x - value v.val) ∧ (abs (x - value target.val) = abs (x - value v.val) → target.val % 2 = 0)) refine ⟨hxpos.le, htargetLt.le, ?_⟩ rw [if_neg (by omega)] refine ⟨hbelow, ?_⟩ intro v hv hne have hvne : v.val ≠ lowBits := by intro heq apply hne apply UScalar.eq_of_val_eq omega have hvlt : v.val < lowBits := by dsimp only [top, unit, lowBits] at hv hboundary ⊢ omega have hvValue : value v.val < value lowBits := hvalueMono _ _ hvlt (by omega) have hvx : value v.val < x := hvValue.trans_le hvalueLow have hstrict : abs (x - value lowBits) < abs (x - value v.val) := by rw [abs_of_nonneg (sub_nonneg.mpr hvalueLow), abs_of_nonneg (sub_nonneg.mpr hvx.le)] linarith rw [htargetLow] exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩ · as_aux_lemma => have hlowTop : lowBits + 1 < top := by omega have hAdjacent : value lowBits = (lowSig : Real) * ulpR ∧ value (lowBits + 1) = ((lowSig + 1 : Nat) : Real) * ulpR := by by_cases hpSmall : pNat ≤ 2045 · simpa only [unit, lowBits, value, ulpR, hpNatCast] using (Binary64AdjacentNormalValues pNat lowSig (by dsimp only [pNat]; omega) hpSmall hLowSigLower hLowSigUpper) · have hpEq : pNat = 2046 := by omega have hsigSucc : lowSig + 1 < 2 ^ 53 := by dsimp only [lowBits, top, unit] at hlowTop omega have hrem : lowSig - unit < unit := by dsimp only [unit] omega have hremSucc : lowSig + 1 - unit < unit := by refine (Nat.sub_lt_iff_lt_add ?_).2 ?_ · exact (by simpa only [unit] using hLowSigLower.trans (Nat.le_add_right lowSig 1)) · norm_num [unit] at hsigSucc ⊢ exact hsigSucc have hlowDiv : lowBits / unit = pNat := by dsimp only [lowBits] rw [Nat.mul_comm pNat unit, Nat.mul_add_div (by dsimp only [unit] positivity)] rw [Nat.div_eq_of_lt hrem, Nat.add_zero] have hlowMod : lowBits % unit = lowSig - unit := by dsimp only [lowBits] rw [Nat.mul_comm pNat unit, Nat.mul_add_mod, Nat.mod_eq_of_lt hrem] have hhighEq : lowBits + 1 = pNat * unit + (lowSig + 1 - unit) := by dsimp only [lowBits, unit] omega have hhighDiv : (lowBits + 1) / unit = pNat := by rw [hhighEq, Nat.mul_comm pNat unit, Nat.mul_add_div (by dsimp only [unit]; positivity), Nat.div_eq_of_lt hremSucc, Nat.add_zero] have hhighMod : (lowBits + 1) % unit = lowSig + 1 - unit := by rw [hhighEq, Nat.mul_comm pNat unit, Nat.mul_add_mod, Nat.mod_eq_of_lt hremSucc] have hulp2046 : ulpR = (2 : Real) ^ (971 : Nat) := by dsimp only [ulpR] rw [← hpNatCast, hpEq] norm_num exact zpow_natCast (2 : Real) 971 constructor · dsimp only [value] rw [hlowDiv, hlowMod, if_neg (by omega), if_pos (by omega), hulp2046] have hsum : unit + (lowSig - unit) = lowSig := by dsimp only [unit] omega rw [hsum] rw [hpEq] · dsimp only [value] rw [hhighDiv, hhighMod, if_neg (by omega), if_pos (by omega), hulp2046] have hsum : unit + (lowSig + 1 - unit) = lowSig + 1 := by dsimp only [unit] omega rw [hsum] rw [hpEq] have hvalueLow : value lowBits ≤ x := by rw [hAdjacent.1] exact hLowX have hvalueHigh : x ≤ value (lowBits + 1) := by rw [hAdjacent.2] exact hXHigh have hchoice : selectedBits = lowBits ∨ selectedBits = lowBits + 1 := by dsimp only [selectedBits] split <;> simp have hbelow : x < threshold := by by_cases hpSmall : pNat ≤ 2045 · have hulpBound : ulpR ≤ (2 : Real) ^ (970 : Nat) := by dsimp only [ulpR] rw [← zpow_natCast] apply zpow_le_zpow_right₀ (by norm_num) rw [← hpNatCast] have hi : (pNat : Int) ≤ 2045 := by exact_mod_cast hpSmall omega have hpow1023 : (2 : Real) ^ (1023 : Nat) < threshold := by have hp : (2 : Real) ^ (970 : Nat) < (2 : Real) ^ (1023 : Nat) := pow_lt_pow_right₀ (by norm_num) (by omega) have heq : (2 : Real) ^ (1024 : Nat) = 2 * (2 : Real) ^ (1023 : Nat) := by rw [show 1024 = 1023 + 1 by omega, pow_succ] ring dsimp only [threshold] rw [heq] rw [show 2 * (2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat) = (2 : Real) ^ (1023 : Nat) + ((2 : Real) ^ (1023 : Nat) - (2 : Real) ^ (970 : Nat)) by ring] exact lt_add_of_pos_right _ (sub_pos.mpr hp) calc x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh _ ≤ (2 : Real) ^ (53 : Nat) * (2 : Real) ^ (970 : Nat) := by apply mul_le_mul · exact_mod_cast (show lowSig + 1 ≤ 2 ^ 53 by omega) · exact hulpBound · dsimp only [ulpR] exact (zpow_pos (by norm_num) _).le · positivity _ = (2 : Real) ^ (1023 : Nat) := by rw [← pow_add] _ < threshold := hpow1023 · have hpEq : pNat = 2046 := by omega have hulpEq2046 : ulpR = (2 : Real) ^ (971 : Nat) := by dsimp only [ulpR] rw [← hpNatCast, hpEq] norm_num exact zpow_natCast (2 : Real) 971 have hsigBound : lowSig + 1 ≤ 2 ^ 53 - 1 := by dsimp only [lowBits, top, unit] at hlowTop omega have hmaxBelow : ((2 ^ 53 - 1 : Nat) : Real) * (2 : Real) ^ (971 : Nat) < threshold := by have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity have heq1024 : (2 : Real) ^ (1024 : Nat) = (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by rw [← pow_add] have heq971 : (2 : Real) ^ (971 : Nat) = 2 * (2 : Real) ^ (970 : Nat) := by rw [show 971 = 970 + 1 by omega, pow_succ] ring dsimp only [threshold] rw [heq1024, heq971] push_cast have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by norm_num rw [h54] calc (9007199254740991 : Real) * (2 * (2 : Real) ^ (970 : Nat)) = 18014398509481982 * (2 : Real) ^ (970 : Nat) := by ring _ < 18014398509481983 * (2 : Real) ^ (970 : Nat) := mul_lt_mul_of_pos_right (by norm_num) hp _ = 18014398509481984 * (2 : Real) ^ (970 : Nat) - (2 : Real) ^ (970 : Nat) := by ring calc x ≤ ((lowSig + 1 : Nat) : Real) * ulpR := hXHigh _ ≤ ((2 ^ 53 - 1 : Nat) : Real) * (2 : Real) ^ (971 : Nat) := by rw [hulpEq2046] exact mul_le_mul_of_nonneg_right (by exact_mod_cast hsigBound) (by positivity) _ < threshold := hmaxBelow apply cellRounds target lowBits selectedBits htargetSelected hlowTop hvalueLow hvalueHigh hchoice · intro hsel rw [hAdjacent.1, hAdjacent.2] exact hLowNear (by dsimp only [selectedBits] at hsel by_contra hc rw [if_pos hc] at hsel omega) · intro hsel rw [hAdjacent.1, hAdjacent.2] exact hHighNear (by dsimp only [selectedBits] at hsel by_contra hc rw [if_neg hc] at hsel omega) · intro hsel heq rw [hAdjacent.1, hAdjacent.2] at heq have hc : ¬chooseHigh := by dsimp only [selectedBits] at hsel by_contra hc rw [if_pos hc] at hsel omega rw [hsel] exact hTieLowEven hc heq · intro hsel heq rw [hAdjacent.1, hAdjacent.2] at heq have hc : chooseHigh := by dsimp only [selectedBits] at hsel by_contra hc rw [if_neg hc] at hsel omega rw [hsel] exact hTieHighEven hc heq · exact hbelow exact hfinish end dec2flt_model