import Tablet.DecimalGoalVocabularyDefault import Tablet.P861_T1_IntegerSubnormalRounds import Tablet.P873_T1_IntegerNormalRounds 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 let unit : Nat := 2 ^ 52 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 · simp only [hp0, if_true] at hrun exact P861_T1_IntegerSubnormalRounds q w G64 p G fp target hw hqlo hpLower hp0 hGval hGUpper hxcellLo hxcellHi htargetAssembly hrun · 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 hxpos : 0 < x := by dsimp only [x] exact mul_pos (by exact_mod_cast hw) (zpow_pos (by norm_num) _) exact P873_T1_IntegerNormalRounds x G p S64 Sf oneU pf fp target hxpos hpPos hGLower hGUpper honeUval hSfPf htargetAssembly hfinal hSchoice hxcellLo' hxcellHi' hLowX hXHigh hLowNear hHighNear hTieLowEven hTieHighEven end dec2flt_model