bitfield.v 11.5 KB
Newer Older
Fengmin Zhu's avatar
Fengmin Zhu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
From refinedc.lang Require Import base int_type builtins_specs.
From refinedc.lithium Require Import simpl_classes tactics_extend infrastructure Z_bitblast.

(* raw bit vector constructors *)

Definition bf_nil : Z := 0.

Definition bf_cons (a k x : Z) (l : Z) :=
  Z.lor (x  a) l.

Definition bf_mask_cons (a k : Z) (l : Z) : Z :=
  Z.lor (Z.ones k  a) l.

Definition bf_slice (a k : Z) (bv : Z) : Z :=
  Z.land (bv  a) (Z.ones k).

Definition bf_update (a k x : Z) (bv : Z) : Z :=
  Z.lor (Z.land bv (Z.lnot (Z.ones k  a))) (x  a).

(* singleton list *)

Lemma bf_shr_singleton a k x :
  0  a  (bf_cons a k x bf_nil)  a = x.
Proof.
  rewrite /bf_cons /bf_nil => ?.
  bitblast.
Qed.

Lemma bf_cons_bool_singleton_false_iff a b :
  0  a 
  bf_cons a 1 (Z_of_bool b) bf_nil = 0  b = false.
Proof.
  intros. rewrite /bf_cons /bf_nil Z.lor_0_r.
  split.
  - destruct b; last done.
    simplify_eq/=. rewrite Z.shiftl_1_l.
    have ? : 2 ^ a  0 by apply Z.pow_nonzero.
    contradiction.
  - move => ->. simplify_eq/=. by rewrite Z.shiftl_0_l.
Qed.

Lemma bf_cons_bool_singleton_true_iff a b :
  0  a 
  bf_cons a 1 (Z_of_bool b) bf_nil  0  b = true.
Proof.
  intros. rewrite /not bf_cons_bool_singleton_false_iff //.
  destruct b; naive_solver.
Qed.

Lemma bf_mask_cons_singleton a k :
  bf_mask_cons a k bf_nil = (Z.ones k)  a.
Proof.
  rewrite /bf_mask_cons /bf_nil.
  bitblast.
Qed.

Lemma bf_mask_cons_singleton_nonempty a k :
  0  a  0 < k  bf_mask_cons a k bf_nil > 0.
Proof.
  intros.
  rewrite bf_mask_cons_singleton Z.ones_equiv Z.shiftl_mul_pow2; last lia.
  apply Zmult_gt_0_compat.
  - suff : 2 ^ 0 < 2 ^ k by lia.
    apply Z.pow_lt_mono_r. all: lia.
  - suff : 0 < 2 ^ a by lia.
    by apply Z.pow_pos_nonneg.
Qed.

(* Rewriting rules *)

Lemma bf_land_nil bv :
  Z.land bv bf_nil = bf_nil.
Proof.
  by rewrite Z.land_0_r.
Qed.

Lemma bf_land_mask_cons bv a k l :
  0  a  0  k 
  Z.land bv (bf_mask_cons a k l) = bf_cons a k (bf_slice a k bv) (Z.land bv l).
Proof.
  rewrite /bf_cons /bf_mask_cons /bf_slice => ??.
  bitblast.
Qed.

Lemma bf_land_mask_flip bv a k N :
  0  a  0  k  a + k  N 
  0  bv < 2 ^ N 
  Z.land bv (Z_lunot N (bf_mask_cons a k bf_nil)) = bf_update a k 0 bv.
Proof.
  rewrite /bf_mask_cons /bf_update /bf_nil /Z_lunot => ????.
  rewrite -Z.land_ones; last lia.
  bitblast.
  symmetry. apply (Z_bounded_iff_bits_nonneg N bv); lia.
Qed.

Lemma bf_lor_nil_l bv :
  Z.lor bf_nil bv = bv.
Proof. done. Qed.

Lemma bf_lor_nil_r bv :
  Z.lor bv bf_nil = bv.
Proof.
  by rewrite Z.lor_0_r.
Qed.

Lemma bf_lor_update a k x x' dl1 dl2 :
  0  a  0  k 
  x = 0 
  Z.lor (bf_cons a k x dl1) (bf_cons a k x' dl2) = bf_cons a k x' (Z.lor dl1 dl2).
Proof.
  move => ? ? ->.
  rewrite /bf_cons.
  bitblast.
Qed.

Lemma bf_lor_update_ne a1 k1 x1 a2 k2 x2 dl1 dl2 :
  0  a1  0  k1  0  a2  0  k2 
  (a1 + k1  a2  a2 + k2  a1) 
  Z.lor (bf_cons a1 k1 x1 dl1) (bf_cons a2 k2 x2 dl2) = bf_cons a1 k1 x1 (Z.lor dl1 (bf_cons a2 k2 x2 dl2)).
Proof.
  rewrite /bf_cons => ?????.
  bitblast.
Qed.

Lemma bf_lor_mask_cons_l a k x dl1 dl2 :
  0  a  0  k 
  x = 0 
  Z.lor (bf_mask_cons a k dl1) (bf_cons a k x dl2) = bf_cons a k (Z.ones k) (Z.lor dl1 dl2).
Proof.
  move => ? ? ->.
  rewrite /bf_cons /bf_mask_cons.
  bitblast.
Qed.

Lemma bf_lor_mask_cons_ne_l a1 k1 x1 a2 k2 dl1 ml :
  0  a1  0  k1  0  a2  0  k2 
  (a1 + k1  a2  a2 + k2  a1) 
  Z.lor (bf_mask_cons a1 k1 ml) (bf_cons a2 k2 x1 dl1) = bf_cons a2 k2 x1 (Z.lor dl1 (bf_mask_cons a1 k1 ml)).
Proof.
  rewrite /bf_cons /bf_mask_cons => ?????.
  bitblast.
Qed.

Lemma bf_lor_mask_cons_r a k x dl1 dl2 :
  0  a  0  k 
  x = 0 
  Z.lor (bf_cons a k x dl1) (bf_mask_cons a k dl2) = bf_cons a k (Z.ones k) (Z.lor dl1 dl2).
Proof.
  move => ? ? ->.
  rewrite /bf_cons /bf_mask_cons.
  bitblast.
Qed.

Lemma bf_lor_mask_cons a1 k1 dl1 a2 k2 dl2 :
  0  a1  0  k1  0  a2  0  k2 
  (a1 + k1  a2  a2 + k2  a1) 
  Z.lor (bf_mask_cons a1 k1 dl1) (bf_mask_cons a2 k2 dl2) = bf_mask_cons a1 k1 (Z.lor dl1 (bf_mask_cons a2 k2 dl2)).
Proof.
  rewrite /bf_cons /bf_mask_cons => ?????.
  bitblast.
Qed.

Lemma bf_lor_mask_cons_ne_r a1 k1 x1 a2 k2 dl1 ml :
  0  a1  0  k1  0  a2  0  k2 
  (a1 + k1  a2  a2 + k2  a1) 
  Z.lor (bf_cons a1 k1 x1 dl1) (bf_mask_cons a2 k2 ml) = bf_cons a1 k1 x1 (Z.lor dl1 (bf_mask_cons a2 k2 ml)).
Proof.
  rewrite /bf_cons /bf_mask_cons => ?????.
  bitblast.
Qed.

Lemma bf_slice_nil a k :
  bf_slice a k bf_nil = 0.
Proof.
  rewrite /bf_slice /bf_nil.
  bitblast.
Qed.

Definition bf_range_empty a k bv : Prop :=
   i, a  i < a + k  Z.testbit bv i = false.

Lemma bf_range_empty_nil a k :
  bf_range_empty a k bf_nil.
Proof.
  rewrite /bf_range_empty /bf_nil => i ?.
  by rewrite Z.bits_0.
Qed.

Lemma bf_range_empty_cons a k x l a' k' :
  0  a  0  k  0  a'  0  k' 
  0  x < 2^k 
  (a + k  a'  a' + k'  a) 
  bf_range_empty a' k' (bf_cons a k x l)  bf_range_empty a' k' l.
Proof.
  rewrite /bf_range_empty /bf_cons => ???? [??] Hor.
  split.
  - move => Hl i [??].
    have : Z.testbit (Z.lor (x  a) l) i = false by apply Hl.
    rewrite Z.lor_spec Z.shiftl_spec ?Hl; [|lia..].
    by apply orb_false_elim.
  - move => Hl i [??].
    rewrite Z.lor_spec Z.shiftl_spec ?Hl; [|lia..].
    simplify_bool_eq. destruct Hor.
    + apply (Z_bounded_iff_bits_nonneg k x); lia.
    + rewrite Z.testbit_neg_r //. lia.
Qed.

Lemma bf_slice_cons a k x l :
  0  a  0  k 
  0  x < 2^k 
  bf_range_empty a k l 
  bf_slice a k (bf_cons a k x l) = x.
Proof.
  rewrite /bf_slice /bf_cons => ??? Hi.
  bitblast.
  rewrite Hi; [by simplify_bool_eq | lia].
  symmetry. apply (Z_bounded_iff_bits_nonneg k x); lia.
Qed.

Lemma bf_slice_cons_ne a k x a' k' l :
  0  a  0  k  0  a'  0  k' 
  0  x < 2^k 
  (a + k  a'  a' + k'  a) 
  bf_slice a' k' (bf_cons a k x l) = bf_slice a' k' l.
Proof.
  rewrite /bf_slice /bf_cons => ????? [?|?].
  all: bitblast.
  have -> : Z.testbit x (i + a' - a) = false
    by apply (Z_bounded_iff_bits_nonneg k x); lia.
  by simplify_bool_eq.
Qed.

Lemma bf_update_nil a k x :
  bf_update a k x bf_nil = bf_cons a k x bf_nil.
Proof.
  rewrite /bf_update /bf_nil /bf_cons.
  bitblast.
Qed.

Lemma bf_update_cons a k x x' dl :
  0  a  0  k 
  0  x < 2^k 
  bf_range_empty a k dl 
  bf_update a k x' (bf_cons a k x dl) = bf_cons a k x' dl.
Proof.
  rewrite /bf_update /bf_nil /bf_cons => ??? Hi.
  bitblast.
  + rewrite Hi; [by simplify_bool_eq | lia].
  + have -> : Z.testbit x (i - a) = false
      by apply (Z_bounded_iff_bits_nonneg k x); lia.
    simplify_bool_eq. by apply orb_comm.
Qed.

Lemma bf_update_cons_ne a k x a' k' x' dl :
  0  a  0  k  0  a'  0  k' 
  0  x < 2^k 
  (a + k  a'  a' + k'  a) 
  bf_update a' k' x' (bf_cons a k x dl) = bf_cons a k x (bf_update a' k' x' dl).
Proof.
  rewrite /bf_update /bf_nil /bf_cons => ????? [?|?].
  all: bitblast.
  have -> : Z.testbit x (i - a) = false
    by apply (Z_bounded_iff_bits_nonneg k x); lia.
  by simplify_bool_eq.
Qed.

(* rewrite & simpl rules *)
Create HintDb bitfield_rewrite discriminated.

Hint Rewrite bf_land_nil : bitfield_rewrite.
Hint Rewrite bf_land_mask_cons using can_solve_tac : bitfield_rewrite.
Hint Rewrite bf_land_mask_flip using can_solve_tac : bitfield_rewrite.

Hint Rewrite bf_lor_nil_l : bitfield_rewrite.
Hint Rewrite bf_lor_nil_r : bitfield_rewrite.
Hint Rewrite bf_lor_update using lia : bitfield_rewrite.
Hint Rewrite bf_lor_update_ne using lia : bitfield_rewrite.
Hint Rewrite bf_lor_mask_cons_l using lia : bitfield_rewrite.
Hint Rewrite bf_lor_mask_cons_ne_l using lia : bitfield_rewrite.
Hint Rewrite bf_lor_mask_cons_r using lia : bitfield_rewrite.
Hint Rewrite bf_lor_mask_cons_ne_r using lia : bitfield_rewrite.
Hint Rewrite bf_lor_mask_cons using lia : bitfield_rewrite.

Hint Rewrite bf_slice_nil : bitfield_rewrite.
Hint Rewrite bf_slice_cons using can_solve_tac : bitfield_rewrite.
Hint Rewrite bf_slice_cons_ne using lia : bitfield_rewrite.

Hint Rewrite bf_update_nil : bitfield_rewrite.
Hint Rewrite bf_update_cons using can_solve_tac : bitfield_rewrite.
Hint Rewrite bf_update_cons_ne using lia : bitfield_rewrite.

Definition normalize_bitfield (bv norm : Z) : Prop := bv = norm.
Typeclasses Opaque normalize_bitfield.

Class NormalizeBitfield (bv norm : Z) : Prop :=
  normalize_bitfield_proof : bv = norm.

Global Instance simpl_and_normalize_bitfield bv norm `{!NormalizeBitfield bv norm'} `{!IsProtected norm} :
  SimplAnd (normalize_bitfield bv norm) (λ T, norm' = norm  T).
Proof. erewrite normalize_bitfield_proof. done. Qed.

Global Hint Extern 10 (NormalizeBitfield _ _) =>
  autorewrite with bitfield_rewrite; exact: eq_refl : typeclass_instances.

(* Side cond: ∀ i ∈ I, Z.testbit bv i = false. *)
Global Instance bf_range_empty_nil_inst a k :
  SimplAnd (bf_range_empty a k bf_nil) (λ T, T).
Proof.
  have ? := bf_range_empty_nil.
  split; naive_solver.
Qed.

Global Instance bf_range_empty_cons_inst a k x l a' k'
  `{!CanSolve (0  a  0  k  0  a'  0  k')}
  `{!CanSolve (0  x < 2^k)}
  `{!CanSolve (a + k  a'  a' + k'  a)} :
  SimplAnd (bf_range_empty a' k' (bf_cons a k x l)) (λ T, bf_range_empty a' k' l  T).
Proof.
  unfold CanSolve in *.
  have ? := bf_range_empty_cons.
  split; naive_solver.
Qed.

(* Linux macros for bits *)

Lemma Z_shl_bound a k x N :
  0  a  0  k  a + k  N 
  0  x < 2 ^ a 
  x  x  k  2 ^ N - 1.
Proof.
  intros. rewrite Z.shiftl_mul_pow2; last lia.
  destruct (decide (x = 0)) as [->|].
  - rewrite Z.mul_0_l.
    have ? : 0 < 2 ^ N. { apply Z.pow_pos_nonneg. all: lia. }
    lia.
  - split.
    + apply Z.le_mul_diag_r; first lia.
      suff : 0 < 2 ^ k by lia.
      by apply Z.pow_pos_nonneg.
    + suff : (x * 2 ^ k) < 2 ^ N by lia.
      apply Z.log2_lt_cancel.
      rewrite Z.log2_mul_pow2 ?Z.log2_pow2; [|lia..].
      have ? : Z.log2 x < a. { apply Z.log2_lt_pow2. all: lia. }
      lia.
Qed.

Lemma Z_shiftl_1_range i N :
  0  i < N  1  1  i  2 ^ N - 1.
Proof.
  intros.
  apply (Z_shl_bound 1 _ _ _). all: lia.
Qed.

Lemma bf_mask_bit i :
  1  i = bf_mask_cons i 1 bf_nil.
Proof.
  rewrite /bf_mask_cons /bf_nil.
  bitblast.
Qed.

Lemma bf_mask_high N k :
  0  k  N  2 ^ N - 1 - 1  k + 1 = bf_mask_cons k (N - k) bf_nil.
Proof.
  intros.
  rewrite bf_mask_cons_singleton Z.ones_equiv !Z.shiftl_mul_pow2; [|lia..].
  rewrite Z.mul_pred_l -Z.pow_add_r ?Z.sub_add; lia.
Qed.

Lemma bf_mask_gen h l N :
  0  l  l < h < N 
  Z.land (2 ^ N - 1 - 1  l + 1) ((2 ^ N - 1)  (N - 1 - h)) = bf_mask_cons l (h - l + 1) bf_nil.
Proof.
  intros.
  rewrite bf_mask_high /bf_mask_cons /bf_nil; last lia.
  have -> : 2 ^ N - 1 = Z.pred (2 ^ N) by lia.
  rewrite -Z.ones_equiv.
  bitblast.
Qed.

Lemma Z_least_significant_one_nonempty_mask (a k : Z) :
  0  a  0 < k 
  Z_least_significant_one (bf_mask_cons a k bf_nil) = a.
Proof.
  intros.
  apply Z_least_significant_one_sound => //.
  rewrite bf_mask_cons_singleton.
  split.
  - rewrite Z.shiftl_spec ?Z.ones_spec_low //. lia.
  - intros. rewrite Z.shiftl_spec ?Z.testbit_neg_r //; lia.
Qed.

Lemma bf_slice_shl a k x :
  0  a  0 < k 
  0  x < 2^k 
  bf_slice a k (x  a) = x.
Proof.
  rewrite /bf_slice => ???.
  bitblast.
  symmetry. apply (Z_bounded_iff_bits_nonneg k x); lia.
Qed.

(* opaque *)
Global Opaque bf_nil bf_cons bf_mask_cons bf_slice bf_update.