Debian Codegraph @

src/Util/ZUtil/Land.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 From Coq Require Import Bool.
4 Require Import Crypto.Util.ZUtil.Notations.
5 Require Import Crypto.Util.ZUtil.Definitions.
6 Require Import Crypto.Util.ZUtil.Hints.Core.
7 Require Import Crypto.Util.ZUtil.Testbit.
8 Local Open Scope bool_scope. Local Open Scope Z_scope.
9
10 Module Z.
11 Lemma land_same_r : forall a b, (a &' b) &' b = a &' b.
12 Proof.
13 intros a b; apply Z.bits_inj'; intros n H.
14 rewrite !Z.land_spec.
15 case_eq (Z.testbit b n); intros;
16 rewrite ?Bool.andb_true_r, ?Bool.andb_false_r; reflexivity.
17 Qed.
18
19 Lemma land_m1'_l a : Z.land (-1) a = a.
20 Proof. apply Z.land_m1_l. Qed.
21 #[global]
22 Hint Rewrite Z.land_m1_l land_m1'_l : zsimplify_const zsimplify zsimplify_fast.
23
24 Lemma land_m1'_r a : Z.land a (-1) = a.
25 Proof. apply Z.land_m1_r. Qed.
26 #[global]
27 Hint Rewrite Z.land_m1_r land_m1'_r : zsimplify_const zsimplify zsimplify_fast.
28
29 Lemma sub_1_lt_le x y : (x - 1 < y) <-> (x <= y).
30 Proof. lia. Qed.
31
32 Lemma land_mod a b :
33 0 <= b ->
34 a &' b = (a mod (2 ^ (Z.log2 b + 1))) &' b.
35 Proof.
36 pose proof (Z.log2_nonneg b).
37 intros. rewrite <-Z.land_ones by lia.
38 rewrite <-Z.land_assoc, (Z.land_comm (Z.ones _)).
39 rewrite Z.land_ones_low; auto with zarith.
40 Qed.
41
42 Lemma land_add_high a b c d :
43 Z.log2 d < c ->
44 0 <= d ->
45 (a + b * 2 ^ c) &' d = a &' d.
46 Proof.
47 pose proof (Z.log2_nonneg d).
48 intros. rewrite land_mod by lia.
49 rewrite Z.add_mod, Z.mul_mod by (apply Z.pow_nonzero; lia).
50 match goal with
51 | |- context [?a ^ ?b mod ?a ^ ?c] =>
52 replace b with ((b - c) + c) by lia;
53 rewrite (Z.pow_add_r a (b - c) c) by lia;
54 rewrite Z.mod_mul by (apply Z.pow_nonzero; lia)
55 end.
56 rewrite Z.mul_0_r, Z.mod_0_l, Z.add_0_r, Z.mod_mod
57 by (apply Z.pow_nonzero; lia).
58 rewrite <-Z.land_ones, <-Z.land_assoc, (Z.land_comm (Z.ones _))
59 by auto with zarith.
60 rewrite Z.land_ones_low; auto with zarith.
61 Qed.
62
63 Lemma land_pow2 x n :
64 0 <= n ->
65 Z.land x (2^n-1) = x mod 2^n.
66 Proof.
67 intros. rewrite Z.sub_1_r, <- Z.ones_equiv.
68 apply Z.land_ones; auto with zarith.
69 Qed.
70
71 Lemma land_pow2_testbit a b :
72 a &' 2^b = if Z.testbit a b then 2^b else 0.
73 Proof.
74 apply Z.bits_inj_iff; red; intros; rewrite Z.land_spec.
75 destruct (Z.testbit a b) eqn:E.
76 - destruct (Z.eqb_spec b n); subst.
77 + now rewrite E, andb_true_l.
78 + now rewrite Z.pow2_bits_false, andb_false_r.
79 - rewrite Z.testbit_0_l; destruct (Z.eqb_spec b n); subst.
80 + now rewrite E, andb_false_l.
81 + now rewrite Z.pow2_bits_false, andb_false_r. Qed.
82
83 Lemma land_pow2_small a b
84 (Ha : 0 <= a < 2^b) :
85 a &' 2^b = 0.
86 Proof. now rewrite land_pow2_testbit, Testbit.Z.bits_above_pow2. Qed.
87
88 Lemma land_pow2_small_neg a b
89 (Ha : - 2^b <= a < 0)
90 (Hb : 0 < b) :
91 a &' 2^b = 2^b.
92 Proof. now rewrite land_pow2_testbit, Testbit.Z.testbit_small_neg. Qed.
93
94 Lemma land_div2 a b (Ha : 0 <= a < 2^(b + 1)) :
95 a / 2 &' 2^b = 0.
96 Proof.
97 destruct (Z.ltb_spec b 0).
98 - now rewrite Pow.Z.base_pow_neg, Z.land_0_r.
99 - rewrite land_pow2_testbit, Z.div2_bits, Testbit.Z.bits_above_pow2;
100 try (replace (Z.succ b) with (b + 1); nia). Qed.
101
102 Lemma land_ones_eq_of_bounded v n
103 (H : (0 <= v < 2 ^ (Z.of_N n))%Z)
104 : Z.land v (Z.ones (Z.of_N n)) = v.
105 Proof.
106 rewrite Z.land_ones by lia.
107 rewrite Z.mod_small by lia.
108 reflexivity.
109 Qed.
110 End Z.