Debian Codegraph @

src/Util/ZUtil/Log2.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.ZUtil.Hints.
4 Require Import Crypto.Util.ZUtil.Hints.Core.
5 Require Import Crypto.Util.ZUtil.Hints.ZArith.
6 Require Import Crypto.Util.ZUtil.Pow.
7 Require Import Crypto.Util.ZUtil.ZSimplify.Core.
8 Require Import Crypto.Util.ZUtil.ZSimplify.Simple.
9 Local Open Scope Z_scope.
10
11 Module Z.
12 Lemma log2_nonneg' n a : n <= 0 -> n <= Z.log2 a.
13 Proof.
14 intros; transitivity 0; auto with zarith.
15 Qed.
16 #[global]
17 Hint Resolve log2_nonneg' : zarith.
18
19 Lemma le_lt_to_log2 x y z : 0 <= z -> 0 < y -> 2^x <= y < 2^z -> x <= Z.log2 y < z.
20 Proof.
21 destruct (Z_le_gt_dec 0 x); auto with concl_log2 lia.
22 Qed.
23
24 Lemma log2_pred_pow2_full a : Z.log2 (Z.pred (2^a)) = Z.max 0 (Z.pred a).
25 Proof.
26 destruct (Z_dec 0 a) as [ [?|?] | ?].
27 { rewrite Z.log2_pred_pow2 by assumption; lia. }
28 { autorewrite with zsimplify; simpl.
29 apply Z.max_case_strong; try lia.
30
31 }
32 { subst; compute; reflexivity. }
33 Qed.
34 #[global]
35 Hint Rewrite log2_pred_pow2_full : zsimplify.
36
37 Lemma log2_up_le_full a : a <= 2^Z.log2_up a.
38 Proof.
39 destruct (Z_dec 1 a) as [ [ ? | ? ] | ? ];
40 first [ apply Z.log2_up_spec; assumption
41 | rewrite Z.log2_up_eqn0 by lia; simpl; lia ].
42 Qed.
43
44 Lemma log2_up_le_pow2_full : forall a b : Z, (0 <= b)%Z -> (a <= 2 ^ b)%Z <-> (Z.log2_up a <= b)%Z.
45 Proof.
46 intros a b H.
47 destruct (Z_lt_le_dec 0 a); [ apply Z.log2_up_le_pow2; assumption | ].
48 split; transitivity 0%Z; try lia; auto with zarith.
49 rewrite Z.log2_up_eqn0 by lia.
50 reflexivity.
51 Qed.
52
53 Lemma log2_lt_pow2_alt a b : 0 < b -> (a < 2^b <-> Z.log2 a < b).
54 Proof.
55 destruct (Z_lt_le_dec 0 a); auto using Z.log2_lt_pow2; [].
56 rewrite Z.log2_nonpos by lia.
57 split; auto;[].
58 intro; eapply Z.le_lt_trans; [ eassumption | ].
59 auto with zarith.
60 Qed.
61
62 Lemma max_log2_up x y : Z.max (Z.log2_up x) (Z.log2_up y) = Z.log2_up (Z.max x y).
63 Proof. apply Z.max_monotone; intros ??; apply Z.log2_up_le_mono. Qed.
64 #[global]
65 Hint Rewrite max_log2_up : push_Zmax.
66 #[global]
67 Hint Rewrite <- max_log2_up : pull_Zmax.
68
69 Lemma log2_up_le_full_max a : Z.max a 1 <= 2^Z.log2_up a.
70 Proof.
71 apply Z.max_case_strong; auto using Z.log2_up_le_full.
72 intros; rewrite Z.log2_up_eqn0 by assumption; reflexivity.
73 Qed.
74 Lemma log2_up_le_1 a : Z.log2_up a <= 1 <-> a <= 2.
75 Proof.
76 pose proof (Z.log2_nonneg (Z.pred a)).
77 destruct (Z_dec a 2) as [ [ ? | ? ] | ? ].
78 { rewrite (proj2 (Z.log2_up_null a)) by lia; split; lia. }
79 { rewrite Z.log2_up_eqn by lia.
80 split; try lia; intro.
81 assert (Z.log2 (Z.pred a) = 0) by lia.
82 assert (Z.pred a <= 1) by (apply Z.log2_null; lia).
83 lia. }
84 { subst; cbv -[Z.le]; split; lia. }
85 Qed.
86 Lemma log2_up_1_le a : 1 <= Z.log2_up a <-> 2 <= a.
87 Proof.
88 pose proof (Z.log2_nonneg (Z.pred a)).
89 destruct (Z_dec a 2) as [ [ ? | ? ] | ? ].
90 { rewrite (proj2 (Z.log2_up_null a)) by lia; split; lia. }
91 { rewrite Z.log2_up_eqn by lia; lia. }
92 { subst; cbv -[Z.le]; split; lia. }
93 Qed.
94 End Z.