| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
From Coq Require Import Morphisms. |
| 4 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 5 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 6 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 7 |
Require Import Crypto.Util.ZUtil.Testbit. |
| 8 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 9 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 10 |
|
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
Notation stabilizes_after x l := (exists b, forall n, l < n -> Z.testbit x n = b). |
| 14 |
|
| 15 |
Lemma stabilizes_after_Proper x |
| 16 |
: Proper (Z.le ==> Basics.impl) (fun l => stabilizes_after x l). |
| 17 |
Proof. |
| 18 |
intros ?? H [b H']; exists b. |
| 19 |
intros n H''; apply (H' n); lia. |
| 20 |
Qed. |
| 21 |
|
| 22 |
Module Z. |
| 23 |
Notation stabilization_time x := (Z.max (Z.log2 (Z.pred (- x))) (Z.log2 x)). |
| 24 |
Notation stabilization_time_weaker x := (Z.log2_up (Z.abs x)). |
| 25 |
|
| 26 |
Lemma stabilization_time_nonneg x : 0 <= stabilization_time x. |
| 27 |
Proof. rewrite Z.max_le_iff; constructor; apply Z.log2_nonneg. Qed. |
| 28 |
#[global] |
| 29 |
Hint Resolve Z.stabilization_time_nonneg : zarith. |
| 30 |
|
| 31 |
Lemma stabilization_time_weaker_nonneg x : 0 <= stabilization_time_weaker x. |
| 32 |
Proof. apply Z.log2_up_nonneg. Qed. |
| 33 |
#[global] |
| 34 |
Hint Resolve Z.stabilization_time_weaker_nonneg : zarith. |
| 35 |
End Z. |
| 36 |
Global Hint Resolve Z.stabilization_time_nonneg Z.stabilization_time_weaker_nonneg : zarith. |
| 37 |
|
| 38 |
Lemma stabilization_time (x:Z) : stabilizes_after x (Z.stabilization_time x). |
| 39 |
Proof. |
| 40 |
destruct (Z_lt_le_dec x 0); eexists; intros; |
| 41 |
[ eapply Z.bits_above_log2_neg | eapply Z.bits_above_log2]; lia. |
| 42 |
Qed. |
| 43 |
|
| 44 |
Lemma stabilization_time_weaker (x:Z) : stabilizes_after x (Z.stabilization_time_weaker x). |
| 45 |
Proof. |
| 46 |
eapply stabilizes_after_Proper; try apply stabilization_time. |
| 47 |
repeat match goal with |
| 48 |
| [ |- context[Z.abs _ ] ] => apply Zabs_ind; intro |
| 49 |
| [ |- context[Z.log2 ?x] ] |
| 50 |
=> rewrite (Z.log2_nonpos x) by lia |
| 51 |
| [ |- context[Z.log2_up ?x] ] |
| 52 |
=> rewrite (Z.log2_up_nonpos x) by lia |
| 53 |
| _ => rewrite Z.max_r by auto with zarith |
| 54 |
| _ => rewrite Z.max_l by auto with zarith |
| 55 |
| _ => etransitivity; [ apply Z.le_log2_log2_up | lia ] |
| 56 |
| _ => progress Z.replace_all_neg_with_pos |
| 57 |
| [ H : 0 <= ?x |- _ ] |
| 58 |
=> assert (x = 0 \/ x = 1 \/ 1 < x) by lia; clear H; destruct_head' or; subst |
| 59 |
| _ => lia |
| 60 |
| _ => simpl; lia |
| 61 |
| _ => rewrite Z.log2_up_eqn by assumption |
| 62 |
| _ => progress change (Z.log2_up 1) with 0 |
| 63 |
end. |
| 64 |
Qed. |
| 65 |
|
| 66 |
Lemma land_stabilizes (a b la lb:Z) (Ha:stabilizes_after a la) (Hb:stabilizes_after b lb) : stabilizes_after (Z.land a b) (Z.max la lb). |
| 67 |
Proof. |
| 68 |
destruct Ha as [ba Hba]. destruct Hb as [bb Hbb]. |
| 69 |
exists (andb ba bb); intros n Hn. |
| 70 |
rewrite Z.land_spec, Hba, Hbb; trivial; lia. |
| 71 |
Qed. |
| 72 |
|
| 73 |
Lemma lor_stabilizes (a b la lb:Z) (Ha:stabilizes_after a la) (Hb:stabilizes_after b lb) : stabilizes_after (Z.lor a b) (Z.max la lb). |
| 74 |
Proof. |
| 75 |
destruct Ha as [ba Hba]. destruct Hb as [bb Hbb]. |
| 76 |
exists (orb ba bb); intros n Hn. |
| 77 |
rewrite Z.lor_spec, Hba, Hbb; trivial; lia. |
| 78 |
Qed. |
| 79 |
|
| 80 |
Local Arguments Z.pow !_ !_. |
| 81 |
Local Arguments Z.log2_up !_. |
| 82 |
Local Arguments Z.add !_ !_. |
| 83 |
Lemma testbit_nonneg_iff x |
| 84 |
: (exists l, 0 <= l /\ forall n : Z, l < n -> Z.testbit x n = false) <-> 0 <= x. |
| 85 |
Proof. |
| 86 |
split; intro H. |
| 87 |
{ destruct H as [l [Hl H]]. |
| 88 |
edestruct Z_lt_le_dec; [ | eassumption ]. |
| 89 |
pose proof (fun pf n => Z.bits_above_log2_neg x n pf) as H'. |
| 90 |
specialize_by (lia || assumption). |
| 91 |
specialize (H (1 + Z.max l (Z.log2 (Z.pred (- x))))). |
| 92 |
specialize (H' (1 + Z.max l (Z.log2 (Z.pred (- x))))). |
| 93 |
specialize_by (apply Z.max_case_strong; lia). |
| 94 |
congruence. } |
| 95 |
{ pose proof (fun n => Z.bits_above_log2 x n H) as Hf. |
| 96 |
eexists; split; [ | eapply Hf ]; auto with zarith. } |
| 97 |
Qed. |
| 98 |
|
| 99 |
Lemma stabilizes_bounded_pos (x l:Z) (H:stabilizes_after x l) (Hl : 0 <= l) (Hx : 0 < x) |
| 100 |
: x <= 2^(l + 1) - 1. |
| 101 |
Proof. |
| 102 |
assert (Hlt : forall l n, l < n <-> l + 1 <= n) by (intros; lia). |
| 103 |
destruct H as [b H]. |
| 104 |
destruct (proj2 (testbit_nonneg_iff x)) as [l' [H0' H1']]; [ lia | ]. |
| 105 |
pose proof (Z.testbit_false_bound x (l' + 1)) as Hf. |
| 106 |
pose proof (Z.testbit_false_bound x (l + 1)) as Hf'. |
| 107 |
pose proof (fun pf n => Z.bits_above_log2 x n pf) as Hf''. |
| 108 |
pose proof (fun pf n => Z.log2_lt_pow2 x n pf) as Hlg. |
| 109 |
specialize_by lia. |
| 110 |
setoid_rewrite <- Z.le_ngt in Hf. |
| 111 |
setoid_rewrite <- Z.le_ngt in Hf'. |
| 112 |
setoid_rewrite <- Hlt in Hf; setoid_rewrite <- Hlt in Hf'; clear Hlt. |
| 113 |
setoid_rewrite <- Hlg in Hf''; clear Hlg. |
| 114 |
destruct b; specialize_by (lia || assumption); [ | lia ]. |
| 115 |
specialize (H (1 + Z.max l l')). |
| 116 |
specialize (H1' (1 + Z.max l l')). |
| 117 |
specialize_by (apply Z.max_case_strong; lia). |
| 118 |
congruence. |
| 119 |
Qed. |
| 120 |
|
| 121 |
Lemma stabilizes_bounded (x l:Z) (H:stabilizes_after x l) (Hl : 0 <= l) : Z.abs x <= 2^(1 + l). |
| 122 |
Proof. |
| 123 |
assert (Hlt : forall l n, l < n <-> l + 1 <= n) by (intros; lia). |
| 124 |
rewrite Z.add_comm. |
| 125 |
destruct (Z_zerop x); subst; simpl. |
| 126 |
{ cut (0 < 2^(l + 1)); auto with zarith. } |
| 127 |
apply Zabs_ind; intro. |
| 128 |
{ etransitivity; [ apply stabilizes_bounded_pos; eauto | ]; lia. } |
| 129 |
{ Z.replace_all_neg_with_pos. |
| 130 |
destruct (Z.eq_dec x 1); subst. |
| 131 |
{ assert (1 < 2^(l+1)) by auto with zarith. |
| 132 |
lia. } |
| 133 |
{ assert (H' : stabilizes_after (Z.pred x) l). |
| 134 |
{ destruct H as [b H]; exists (negb b). |
| 135 |
do 2 let x := fresh in intro x; specialize (H x). |
| 136 |
rewrite Z.bits_opp in H by lia. |
| 137 |
destruct b; rewrite ?Bool.negb_true_iff, ?Bool.negb_false_iff in H; assumption. } |
| 138 |
clear H. |
| 139 |
apply stabilizes_bounded_pos in H'; auto; lia. } } |
| 140 |
Qed. |