Debian Codegraph @

src/Util/ZUtil/ArithmeticShiftr.v

raw
1 From Coq Require Import Bool.
2 From Coq Require Import ZArith.
3 From Coq Require Import Lia.
4
5 Require Import Crypto.Util.ZUtil.Definitions.
6 Require Import Crypto.Util.ZUtil.Notations.
7 Require Import Crypto.Util.ZUtil.Land.
8 Require Import Crypto.Util.ZUtil.Lor.
9 Require Import Crypto.Util.ZUtil.Testbit.
10 Require Import Crypto.Util.ZUtil.Div.
11 Require Import Crypto.Util.ZUtil.LandLorShiftBounds.
12 Require Import Crypto.Util.ZUtil.Zselect.
13 Require Import Crypto.Util.ZUtil.Ones.
14 Require Import Crypto.Util.ZUtil.OnesFrom.
15 Require Import Crypto.Util.ZUtil.Pow.
16 Require Import Crypto.Util.ZUtil.TruncatingShiftl.
17 Require Import Crypto.Util.ZUtil.SignBit.
18 Require Import Crypto.Util.ZUtil.TwosComplement.
19
20 Require Import Crypto.Util.Decidable.
21 Require Import Crypto.Util.LetIn.
22 Require Import Crypto.Util.ListUtil.
23 Require Import Crypto.Util.ZUtil.Tactics.SolveRange.
24 Require Import Crypto.Util.ZUtil.Tactics.SolveTestbit.
25
26 Local Open Scope Z_scope.
27
28 Module Z.
29 Lemma arithmetic_shiftr1_testbit_spec_full m a i
30 (Hm : 0 < m)
31 (Ha : 0 <= a < 2 ^ m) :
32 Z.testbit (Z.arithmetic_shiftr1 m a) i =
33 if (i <? 0) then false else if i =? (m - 1) then Z.testbit a (m - 1) else Z.testbit a (i + 1).
34 Proof. unfold Z.arithmetic_shiftr1; Z.solve_testbit. Qed.
35
36 #[global]
37 Hint Rewrite arithmetic_shiftr1_testbit_spec_full : testbit_rewrite.
38
39 Lemma arithmetic_shiftr1_bound m a (Ha : 0 <= a < 2 ^ m) :
40 0 <= Z.arithmetic_shiftr1 m a < 2 ^ m.
41 Proof. unfold Z.arithmetic_shiftr1; Z.solve_range. Qed.
42
43 #[global]
44 Hint Resolve arithmetic_shiftr1_bound : zarith.
45
46 Lemma arithmetic_shiftr1_spec m a
47 (Hm : 0 < m)
48 (Ha : 0 <= a < 2 ^ m) :
49 Z.twos_complement m (Z.arithmetic_shiftr1 m a) = (Z.twos_complement m a) / 2.
50 Proof. Z.solve_using_testbit. Qed.
51
52 Lemma arithmetic_shiftr_bound m a k
53 (Hm : 0 <= m)
54 (Ha : 0 <= a < 2 ^ m)
55 (Hk : 0 <= k) :
56 0 <= Z.arithmetic_shiftr m a k < 2 ^ m.
57 Proof.
58 unfold Z.arithmetic_shiftr; rewrite unfold_Let_In, Zselect.Z.zselect_correct.
59 destruct (dec (Z.sign_bit m a = 0)); [Z.solve_range|].
60 destruct (dec (m - k <= 0)); Z.solve_range. Qed.
61
62 #[global]
63 Hint Resolve arithmetic_shiftr_bound : zarith.
64
65 Lemma arithmetic_shiftr_testbit_spec m a k i
66 (Hm : 0 < m)
67 (Hk : 0 <= k)
68 (Ha : 0 <= a < 2 ^ m) :
69 Z.testbit (Z.arithmetic_shiftr m a k) i =
70 if (i <? 0) then false else if (m - k <=? i) && ((i <? m) || (k <? 0)) then Z.testbit a (m - 1) else Z.testbit a (i + k).
71 Proof.
72 unfold Z.arithmetic_shiftr; rewrite unfold_Let_In, Zselect.Z.zselect_correct.
73 rewrite (Z.testbit_b2z a), Z.sign_bit_testbit by lia.
74 destruct (Z.testbit a (m - 1)); Z.solve_testbit. Qed.
75
76 #[global]
77 Hint Rewrite arithmetic_shiftr_testbit_spec : testbit_rewrite.
78
79 Lemma arithmetic_shiftr_1 m a
80 (Hm : 0 < m)
81 (Ha : 0 <= a < 2 ^ m) :
82 Z.arithmetic_shiftr m a 1 = Z.arithmetic_shiftr1 m a.
83 Proof. Z.solve_using_testbit. Qed.
84
85 Lemma arithmetic_shiftr_0 m a
86 (Hm : 0 < m)
87 (Ha : 0 <= a < 2 ^ m) :
88 Z.arithmetic_shiftr m a 0 = a.
89 Proof. Z.solve_using_testbit. Qed.
90
91 Lemma ones_lor_shift n m k
92 (Hn : 0 <= n)
93 (Hm : 0 <= m)
94 (Hk : 0 <= k) :
95 Z.ones n << k |' Z.ones m << (k - m) = Z.ones (n + m) << (k - m).
96 Proof. Z.solve_using_testbit. Qed.
97
98 Lemma arithmetic_shiftr_sign_bit m a k
99 (Hm : 0 < m)
100 (Ha : 0 <= a < 2 ^ m)
101 (Hk : 0 <= k) :
102 Z.sign_bit m (Z.arithmetic_shiftr m a k) = Z.sign_bit m a.
103 Proof.
104 rewrite !Z.sign_bit_testbit
105 by (try apply arithmetic_shiftr_bound; lia). apply f_equal; Z.solve_testbit. Qed.
106
107 Lemma arithmetic_shiftr_arithmetic_shiftr m a p q
108 (Hm : 0 < m)
109 (Ha : 0 <= a < 2 ^ m)
110 (Hp : 0 <= p)
111 (Hq : 0 <= q) :
112 Z.arithmetic_shiftr m (Z.arithmetic_shiftr m a p) q =
113 Z.arithmetic_shiftr m a (p + q).
114 Proof. Z.solve_using_testbit. Qed.
115
116 Lemma arithmetic_shiftr_arithmetic_shiftr1 m a k
117 (Hm : 0 < m)
118 (Ha : 0 <= a < 2 ^ m)
119 (Hk : 0 <= k) :
120 Z.arithmetic_shiftr m (Z.arithmetic_shiftr1 m a) k =
121 Z.arithmetic_shiftr m a (k + 1).
122 Proof. Z.solve_using_testbit. Qed.
123
124 Lemma arithmetic_shiftr_spec m a k
125 (Hm : 0 < m)
126 (Ha : 0 <= a < 2 ^ m)
127 (Hk : 0 <= k) :
128 Z.twos_complement m (Z.arithmetic_shiftr m a k) = (Z.twos_complement m a) / 2 ^ k.
129 Proof. Z.solve_using_testbit. Qed.
130 End Z.