-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathIntegers.v
4998 lines (4399 loc) · 156 KB
/
Integers.v
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Lesser General Public License as *)
(* published by the Free Software Foundation, either version 2.1 of *)
(* the License, or (at your option) any later version. *)
(* This file is also distributed under the terms of the *)
(* INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Formalizations of machine integers modulo $2^N$ #2<sup>N</sup>#. *)
Require Import Eqdep_dec Zquot Zwf.
Require Import Coqlib Zbits.
Require Archi.
(** Backwards compatibility for Hint Rewrite locality attributes. *)
Set Warnings "-unsupported-attributes".
(** * Comparisons *)
Inductive comparison : Type :=
| Ceq : comparison (**r same *)
| Cne : comparison (**r different *)
| Clt : comparison (**r less than *)
| Cle : comparison (**r less than or equal *)
| Cgt : comparison (**r greater than *)
| Cge : comparison. (**r greater than or equal *)
Definition negate_comparison (c: comparison): comparison :=
match c with
| Ceq => Cne
| Cne => Ceq
| Clt => Cge
| Cle => Cgt
| Cgt => Cle
| Cge => Clt
end.
Definition swap_comparison (c: comparison): comparison :=
match c with
| Ceq => Ceq
| Cne => Cne
| Clt => Cgt
| Cle => Cge
| Cgt => Clt
| Cge => Cle
end.
(** * Parameterization by the word size, in bits. *)
Module Type WORDSIZE.
Parameter wordsize: nat.
Axiom wordsize_not_zero: wordsize <> 0%nat.
End WORDSIZE.
(* To avoid useless definitions of inductors in extracted code. *)
Local Unset Elimination Schemes.
Local Unset Case Analysis Schemes.
Module Make(WS: WORDSIZE).
Definition wordsize: nat := WS.wordsize.
Definition zwordsize: Z := Z.of_nat wordsize.
Definition modulus : Z := two_power_nat wordsize.
Definition half_modulus : Z := modulus / 2.
Definition max_unsigned : Z := modulus - 1.
Definition max_signed : Z := half_modulus - 1.
Definition min_signed : Z := - half_modulus.
Remark wordsize_pos: zwordsize > 0.
Proof.
unfold zwordsize, wordsize. generalize WS.wordsize_not_zero. lia.
Qed.
Remark modulus_power: modulus = two_p zwordsize.
Proof.
unfold modulus. apply two_power_nat_two_p.
Qed.
Remark modulus_gt_one: modulus > 1.
Proof.
rewrite modulus_power. apply Z.lt_gt. apply (two_p_monotone_strict 0).
generalize wordsize_pos; lia.
Qed.
Remark modulus_pos: modulus > 0.
Proof.
generalize modulus_gt_one; lia.
Qed.
Global Hint Resolve modulus_pos: ints.
(** * Representation of machine integers *)
(** A machine integer (type [int]) is represented as a Coq arbitrary-precision
integer (type [Z]) plus a proof that it is in the range 0 (included) to
[modulus] (excluded). *)
Record int: Type := mkint { intval: Z; intrange: -1 < intval < modulus }.
(** Fast normalization modulo [2^wordsize] *)
Definition Z_mod_modulus (x: Z) : Z :=
match x with
| Z0 => 0
| Zpos p => P_mod_two_p p wordsize
| Zneg p => let r := P_mod_two_p p wordsize in if zeq r 0 then 0 else modulus - r
end.
Lemma Z_mod_modulus_range:
forall x, 0 <= Z_mod_modulus x < modulus.
Proof (Z_mod_two_p_range wordsize).
Lemma Z_mod_modulus_range':
forall x, -1 < Z_mod_modulus x < modulus.
Proof.
intros. generalize (Z_mod_modulus_range x); intuition auto with zarith.
Qed.
Lemma Z_mod_modulus_eq:
forall x, Z_mod_modulus x = x mod modulus.
Proof (Z_mod_two_p_eq wordsize).
(** The [unsigned] and [signed] functions return the Coq integer corresponding
to the given machine integer, interpreted as unsigned or signed
respectively. *)
Definition unsigned (n: int) : Z := intval n.
Definition signed (n: int) : Z :=
let x := unsigned n in
if zlt x half_modulus then x else x - modulus.
(** Conversely, [repr] takes a Coq integer and returns the corresponding
machine integer. The argument is treated modulo [modulus]. *)
Definition repr (x: Z) : int :=
mkint (Z_mod_modulus x) (Z_mod_modulus_range' x).
Definition zero := repr 0.
Definition one := repr 1.
Definition mone := repr (-1).
Definition iwordsize := repr zwordsize.
Lemma mkint_eq:
forall x y Px Py, x = y -> mkint x Px = mkint y Py.
Proof.
intros. subst y.
assert (forall (n m: Z) (P1 P2: n < m), P1 = P2).
{
unfold Z.lt; intros.
apply eq_proofs_unicity.
intros c1 c2. destruct c1; destruct c2; (left; reflexivity) || (right; congruence).
}
destruct Px as [Px1 Px2]. destruct Py as [Py1 Py2].
rewrite (H _ _ Px1 Py1).
rewrite (H _ _ Px2 Py2).
reflexivity.
Qed.
Lemma eq_dec: forall (x y: int), {x = y} + {x <> y}.
Proof.
intros. destruct x; destruct y. destruct (zeq intval0 intval1).
left. apply mkint_eq. auto.
right. red; intro. injection H. exact n.
Defined.
(** * Arithmetic and logical operations over machine integers *)
Definition eq (x y: int) : bool :=
if zeq (unsigned x) (unsigned y) then true else false.
Definition lt (x y: int) : bool :=
if zlt (signed x) (signed y) then true else false.
Definition ltu (x y: int) : bool :=
if zlt (unsigned x) (unsigned y) then true else false.
Definition neg (x: int) : int := repr (- unsigned x).
Definition add (x y: int) : int :=
repr (unsigned x + unsigned y).
Definition sub (x y: int) : int :=
repr (unsigned x - unsigned y).
Definition mul (x y: int) : int :=
repr (unsigned x * unsigned y).
Definition divs (x y: int) : int :=
repr (Z.quot (signed x) (signed y)).
Definition mods (x y: int) : int :=
repr (Z.rem (signed x) (signed y)).
Definition divu (x y: int) : int :=
repr (unsigned x / unsigned y).
Definition modu (x y: int) : int :=
repr ((unsigned x) mod (unsigned y)).
(** Bitwise boolean operations. *)
Definition and (x y: int): int := repr (Z.land (unsigned x) (unsigned y)).
Definition or (x y: int): int := repr (Z.lor (unsigned x) (unsigned y)).
Definition xor (x y: int) : int := repr (Z.lxor (unsigned x) (unsigned y)).
Definition not (x: int) : int := xor x mone.
(** Shifts and rotates. *)
Definition shl (x y: int): int := repr (Z.shiftl (unsigned x) (unsigned y)).
Definition shru (x y: int): int := repr (Z.shiftr (unsigned x) (unsigned y)).
Definition shr (x y: int): int := repr (Z.shiftr (signed x) (unsigned y)).
Definition rol (x y: int) : int :=
let n := (unsigned y) mod zwordsize in
repr (Z.lor (Z.shiftl (unsigned x) n) (Z.shiftr (unsigned x) (zwordsize - n))).
Definition ror (x y: int) : int :=
let n := (unsigned y) mod zwordsize in
repr (Z.lor (Z.shiftr (unsigned x) n) (Z.shiftl (unsigned x) (zwordsize - n))).
Definition rolm (x a m: int): int := and (rol x a) m.
(** Viewed as signed divisions by powers of two, [shrx] rounds towards
zero, while [shr] rounds towards minus infinity. *)
Definition shrx (x y: int): int :=
divs x (shl one y).
(** High half of full multiply. *)
Definition mulhu (x y: int): int := repr ((unsigned x * unsigned y) / modulus).
Definition mulhs (x y: int): int := repr ((signed x * signed y) / modulus).
(** Condition flags *)
Definition negative (x: int): int :=
if lt x zero then one else zero.
Definition add_carry (x y cin: int): int :=
if zlt (unsigned x + unsigned y + unsigned cin) modulus then zero else one.
Definition add_overflow (x y cin: int): int :=
let s := signed x + signed y + signed cin in
if zle min_signed s && zle s max_signed then zero else one.
Definition sub_borrow (x y bin: int): int :=
if zlt (unsigned x - unsigned y - unsigned bin) 0 then one else zero.
Definition sub_overflow (x y bin: int): int :=
let s := signed x - signed y - signed bin in
if zle min_signed s && zle s max_signed then zero else one.
(** [shr_carry x y] is 1 if [x] is negative and at least one 1 bit is shifted away. *)
Definition shr_carry (x y: int) : int :=
if lt x zero && negb (eq (and x (sub (shl one y) one)) zero)
then one else zero.
(** Zero and sign extensions *)
Definition zero_ext (n: Z) (x: int) : int := repr (Zzero_ext n (unsigned x)).
Definition sign_ext (n: Z) (x: int) : int := repr (Zsign_ext n (unsigned x)).
(** Decomposition of a number as a sum of powers of two. *)
Definition one_bits (x: int) : list int :=
List.map repr (Z_one_bits wordsize (unsigned x) 0).
(** Recognition of powers of two. *)
Definition is_power2 (x: int) : option int :=
match Z_is_power2 (unsigned x) with
| Some i => Some (repr i)
| None => None
end.
(** Comparisons. *)
Definition cmp (c: comparison) (x y: int) : bool :=
match c with
| Ceq => eq x y
| Cne => negb (eq x y)
| Clt => lt x y
| Cle => negb (lt y x)
| Cgt => lt y x
| Cge => negb (lt x y)
end.
Definition cmpu (c: comparison) (x y: int) : bool :=
match c with
| Ceq => eq x y
| Cne => negb (eq x y)
| Clt => ltu x y
| Cle => negb (ltu y x)
| Cgt => ltu y x
| Cge => negb (ltu x y)
end.
Definition is_false (x: int) : Prop := x = zero.
Definition is_true (x: int) : Prop := x <> zero.
Definition notbool (x: int) : int := if eq x zero then one else zero.
(** x86-style extended division and modulus *)
Definition divmodu2 (nhi nlo: int) (d: int) : option (int * int) :=
if eq_dec d zero then None else
(let (q, r) := Z.div_eucl (unsigned nhi * modulus + unsigned nlo) (unsigned d) in
if zle q max_unsigned then Some(repr q, repr r) else None).
Definition divmods2 (nhi nlo: int) (d: int) : option (int * int) :=
if eq_dec d zero then None else
(let (q, r) := Z.quotrem (signed nhi * modulus + unsigned nlo) (signed d) in
if zle min_signed q && zle q max_signed then Some(repr q, repr r) else None).
(** * Properties of integers and integer arithmetic *)
(** ** Properties of [modulus], [max_unsigned], etc. *)
Remark half_modulus_power:
half_modulus = two_p (zwordsize - 1).
Proof.
unfold half_modulus. rewrite modulus_power.
set (ws1 := zwordsize - 1).
replace (zwordsize) with (Z.succ ws1).
rewrite two_p_S. rewrite Z.mul_comm. apply Z_div_mult. lia.
unfold ws1. generalize wordsize_pos; lia.
unfold ws1. lia.
Qed.
Remark half_modulus_modulus: modulus = 2 * half_modulus.
Proof.
rewrite half_modulus_power. rewrite modulus_power.
rewrite <- two_p_S. apply f_equal. lia.
generalize wordsize_pos; lia.
Qed.
(** Relative positions, from greatest to smallest:
<<
max_unsigned
max_signed
2*wordsize-1
wordsize
0
min_signed
>>
*)
Remark half_modulus_pos: half_modulus > 0.
Proof.
rewrite half_modulus_power. apply two_p_gt_ZERO. generalize wordsize_pos; lia.
Qed.
Remark min_signed_neg: min_signed < 0.
Proof.
unfold min_signed. generalize half_modulus_pos. lia.
Qed.
Remark max_signed_pos: max_signed >= 0.
Proof.
unfold max_signed. generalize half_modulus_pos. lia.
Qed.
Remark wordsize_max_unsigned: zwordsize <= max_unsigned.
Proof.
assert (zwordsize < modulus).
rewrite modulus_power. apply two_p_strict.
generalize wordsize_pos. lia.
unfold max_unsigned. lia.
Qed.
Remark two_wordsize_max_unsigned: 2 * zwordsize - 1 <= max_unsigned.
Proof.
assert (2 * zwordsize - 1 < modulus).
rewrite modulus_power. apply two_p_strict_2. generalize wordsize_pos; lia.
unfold max_unsigned; lia.
Qed.
Remark max_signed_unsigned: max_signed < max_unsigned.
Proof.
unfold max_signed, max_unsigned. rewrite half_modulus_modulus.
generalize half_modulus_pos. lia.
Qed.
Lemma unsigned_repr_eq:
forall x, unsigned (repr x) = Z.modulo x modulus.
Proof.
intros. simpl. apply Z_mod_modulus_eq.
Qed.
Lemma signed_repr_eq:
forall x, signed (repr x) = if zlt (Z.modulo x modulus) half_modulus then Z.modulo x modulus else Z.modulo x modulus - modulus.
Proof.
intros. unfold signed. rewrite unsigned_repr_eq. auto.
Qed.
(** ** Modulo arithmetic *)
(** [eqm] is equality modulo $2^{wordsize}$ #2<sup>wordsize</sup>#. *)
Definition eqm := eqmod modulus.
Lemma eqm_refl: forall x, eqm x x.
Proof (eqmod_refl modulus).
Global Hint Resolve eqm_refl: ints.
Lemma eqm_refl2:
forall x y, x = y -> eqm x y.
Proof (eqmod_refl2 modulus).
Global Hint Resolve eqm_refl2: ints.
Lemma eqm_sym: forall x y, eqm x y -> eqm y x.
Proof (eqmod_sym modulus).
Global Hint Resolve eqm_sym: ints.
Lemma eqm_trans: forall x y z, eqm x y -> eqm y z -> eqm x z.
Proof (eqmod_trans modulus).
Global Hint Resolve eqm_trans: ints.
Lemma eqm_small_eq:
forall x y, eqm x y -> 0 <= x < modulus -> 0 <= y < modulus -> x = y.
Proof (eqmod_small_eq modulus).
Global Hint Resolve eqm_small_eq: ints.
Lemma eqm_add:
forall a b c d, eqm a b -> eqm c d -> eqm (a + c) (b + d).
Proof (eqmod_add modulus).
Global Hint Resolve eqm_add: ints.
Lemma eqm_neg:
forall x y, eqm x y -> eqm (-x) (-y).
Proof (eqmod_neg modulus).
Global Hint Resolve eqm_neg: ints.
Lemma eqm_sub:
forall a b c d, eqm a b -> eqm c d -> eqm (a - c) (b - d).
Proof (eqmod_sub modulus).
Global Hint Resolve eqm_sub: ints.
Lemma eqm_mult:
forall a b c d, eqm a c -> eqm b d -> eqm (a * b) (c * d).
Proof (eqmod_mult modulus).
Global Hint Resolve eqm_mult: ints.
Lemma eqm_same_bits:
forall x y,
(forall i, 0 <= i < zwordsize -> Z.testbit x i = Z.testbit y i) ->
eqm x y.
Proof (eqmod_same_bits wordsize).
Lemma same_bits_eqm:
forall x y i,
eqm x y ->
0 <= i < zwordsize ->
Z.testbit x i = Z.testbit y i.
Proof (same_bits_eqmod wordsize).
(** ** Properties of the coercions between [Z] and [int] *)
Lemma eqm_samerepr: forall x y, eqm x y -> repr x = repr y.
Proof.
intros. unfold repr. apply mkint_eq.
rewrite !Z_mod_modulus_eq. apply eqmod_mod_eq. auto with ints. exact H.
Qed.
Lemma eqm_unsigned_repr:
forall z, eqm z (unsigned (repr z)).
Proof.
unfold eqm; intros. rewrite unsigned_repr_eq. apply eqmod_mod. auto with ints.
Qed.
Global Hint Resolve eqm_unsigned_repr: ints.
Lemma eqm_unsigned_repr_l:
forall a b, eqm a b -> eqm (unsigned (repr a)) b.
Proof.
intros. apply eqm_trans with a.
apply eqm_sym. apply eqm_unsigned_repr. auto.
Qed.
Global Hint Resolve eqm_unsigned_repr_l: ints.
Lemma eqm_unsigned_repr_r:
forall a b, eqm a b -> eqm a (unsigned (repr b)).
Proof.
intros. apply eqm_trans with b. auto.
apply eqm_unsigned_repr.
Qed.
Global Hint Resolve eqm_unsigned_repr_r: ints.
Lemma eqm_signed_unsigned:
forall x, eqm (signed x) (unsigned x).
Proof.
intros; red. unfold signed. set (y := unsigned x).
case (zlt y half_modulus); intro.
apply eqmod_refl. red; exists (-1); ring.
Qed.
Theorem unsigned_range:
forall i, 0 <= unsigned i < modulus.
Proof.
destruct i. simpl. lia.
Qed.
Global Hint Resolve unsigned_range: ints.
Theorem unsigned_range_2:
forall i, 0 <= unsigned i <= max_unsigned.
Proof.
intro; unfold max_unsigned.
generalize (unsigned_range i). lia.
Qed.
Global Hint Resolve unsigned_range_2: ints.
Theorem signed_range:
forall i, min_signed <= signed i <= max_signed.
Proof.
intros. unfold signed.
generalize (unsigned_range i). set (n := unsigned i). intros.
case (zlt n half_modulus); intro.
unfold max_signed. generalize min_signed_neg. lia.
unfold min_signed, max_signed.
rewrite half_modulus_modulus in *. lia.
Qed.
Theorem repr_unsigned:
forall i, repr (unsigned i) = i.
Proof.
destruct i; simpl. unfold repr. apply mkint_eq.
rewrite Z_mod_modulus_eq. apply Z.mod_small; lia.
Qed.
Global Hint Resolve repr_unsigned: ints.
Lemma repr_signed:
forall i, repr (signed i) = i.
Proof.
intros. transitivity (repr (unsigned i)).
apply eqm_samerepr. apply eqm_signed_unsigned. auto with ints.
Qed.
Global Hint Resolve repr_signed: ints.
Opaque repr.
Lemma eqm_repr_eq: forall x y, eqm x (unsigned y) -> repr x = y.
Proof.
intros. rewrite <- (repr_unsigned y). apply eqm_samerepr; auto.
Qed.
Theorem unsigned_repr:
forall z, 0 <= z <= max_unsigned -> unsigned (repr z) = z.
Proof.
intros. rewrite unsigned_repr_eq.
apply Z.mod_small. unfold max_unsigned in H. lia.
Qed.
Global Hint Resolve unsigned_repr: ints.
Theorem signed_repr:
forall z, min_signed <= z <= max_signed -> signed (repr z) = z.
Proof.
intros. unfold signed. destruct (zle 0 z).
replace (unsigned (repr z)) with z.
rewrite zlt_true. auto. unfold max_signed in H. lia.
symmetry. apply unsigned_repr. generalize max_signed_unsigned. lia.
pose (z' := z + modulus).
replace (repr z) with (repr z').
replace (unsigned (repr z')) with z'.
rewrite zlt_false. unfold z'. lia.
unfold z'. unfold min_signed in H.
rewrite half_modulus_modulus. lia.
symmetry. apply unsigned_repr.
unfold z', max_unsigned. unfold min_signed, max_signed in H.
rewrite half_modulus_modulus. lia.
apply eqm_samerepr. unfold z'; red. exists 1. lia.
Qed.
Theorem signed_eq_unsigned:
forall x, unsigned x <= max_signed -> signed x = unsigned x.
Proof.
intros. unfold signed. destruct (zlt (unsigned x) half_modulus).
auto. unfold max_signed in H. extlia.
Qed.
Theorem signed_positive:
forall x, signed x >= 0 <-> unsigned x <= max_signed.
Proof.
intros. unfold signed, max_signed.
generalize (unsigned_range x) half_modulus_modulus half_modulus_pos; intros.
destruct (zlt (unsigned x) half_modulus); lia.
Qed.
(** ** Properties of zero, one, minus one *)
Theorem unsigned_zero: unsigned zero = 0.
Proof.
unfold zero; rewrite unsigned_repr_eq. apply Zmod_0_l.
Qed.
Theorem unsigned_one: unsigned one = 1.
Proof.
unfold one; rewrite unsigned_repr_eq. apply Z.mod_small. split. lia.
unfold modulus. replace wordsize with (S(Init.Nat.pred wordsize)).
rewrite two_power_nat_S. generalize (two_power_nat_pos (Init.Nat.pred wordsize)).
lia.
generalize wordsize_pos. unfold zwordsize. lia.
Qed.
Theorem unsigned_mone: unsigned mone = modulus - 1.
Proof.
unfold mone; rewrite unsigned_repr_eq.
replace (-1) with ((modulus - 1) + (-1) * modulus).
rewrite Z_mod_plus_full. apply Z.mod_small.
generalize modulus_pos. lia. lia.
Qed.
Theorem signed_zero: signed zero = 0.
Proof.
unfold signed. rewrite unsigned_zero. apply zlt_true. generalize half_modulus_pos; lia.
Qed.
Theorem signed_one: zwordsize > 1 -> signed one = 1.
Proof.
intros. unfold signed. rewrite unsigned_one. apply zlt_true.
change 1 with (two_p 0). rewrite half_modulus_power. apply two_p_monotone_strict. lia.
Qed.
Theorem signed_mone: signed mone = -1.
Proof.
unfold signed. rewrite unsigned_mone.
rewrite zlt_false. lia.
rewrite half_modulus_modulus. generalize half_modulus_pos. lia.
Qed.
Theorem one_not_zero: one <> zero.
Proof.
assert (unsigned one <> unsigned zero).
rewrite unsigned_one; rewrite unsigned_zero; congruence.
congruence.
Qed.
Theorem unsigned_repr_wordsize:
unsigned iwordsize = zwordsize.
Proof.
unfold iwordsize; rewrite unsigned_repr_eq. apply Z.mod_small.
generalize wordsize_pos wordsize_max_unsigned; unfold max_unsigned; lia.
Qed.
(** ** Properties of equality *)
Theorem eq_sym:
forall x y, eq x y = eq y x.
Proof.
intros; unfold eq. case (zeq (unsigned x) (unsigned y)); intro.
rewrite e. rewrite zeq_true. auto.
rewrite zeq_false. auto. auto.
Qed.
Theorem eq_spec: forall (x y: int), if eq x y then x = y else x <> y.
Proof.
intros; unfold eq. case (eq_dec x y); intro.
subst y. rewrite zeq_true. auto.
rewrite zeq_false. auto.
destruct x; destruct y.
simpl. red; intro. elim n. apply mkint_eq. auto.
Qed.
Theorem eq_true: forall x, eq x x = true.
Proof.
intros. generalize (eq_spec x x); case (eq x x); intros; congruence.
Qed.
Theorem eq_false: forall x y, x <> y -> eq x y = false.
Proof.
intros. generalize (eq_spec x y); case (eq x y); intros; congruence.
Qed.
Theorem same_if_eq: forall x y, eq x y = true -> x = y.
Proof.
intros. generalize (eq_spec x y); rewrite H; auto.
Qed.
Theorem eq_signed:
forall x y, eq x y = if zeq (signed x) (signed y) then true else false.
Proof.
intros. predSpec eq eq_spec x y.
subst x. rewrite zeq_true; auto.
destruct (zeq (signed x) (signed y)); auto.
elim H. rewrite <- (repr_signed x). rewrite <- (repr_signed y). congruence.
Qed.
(** ** Properties of addition *)
Theorem add_unsigned: forall x y, add x y = repr (unsigned x + unsigned y).
Proof. intros; reflexivity.
Qed.
Theorem add_signed: forall x y, add x y = repr (signed x + signed y).
Proof.
intros. rewrite add_unsigned. apply eqm_samerepr.
apply eqm_add; apply eqm_sym; apply eqm_signed_unsigned.
Qed.
Theorem add_commut: forall x y, add x y = add y x.
Proof. intros; unfold add. decEq. lia. Qed.
Theorem add_zero: forall x, add x zero = x.
Proof.
intros. unfold add. rewrite unsigned_zero.
rewrite Z.add_0_r. apply repr_unsigned.
Qed.
Theorem add_zero_l: forall x, add zero x = x.
Proof.
intros. rewrite add_commut. apply add_zero.
Qed.
Theorem add_assoc: forall x y z, add (add x y) z = add x (add y z).
Proof.
intros; unfold add.
set (x' := unsigned x).
set (y' := unsigned y).
set (z' := unsigned z).
apply eqm_samerepr.
apply eqm_trans with ((x' + y') + z').
auto with ints.
rewrite <- Z.add_assoc. auto with ints.
Qed.
Theorem add_permut: forall x y z, add x (add y z) = add y (add x z).
Proof.
intros. rewrite (add_commut y z). rewrite <- add_assoc. apply add_commut.
Qed.
Theorem add_neg_zero: forall x, add x (neg x) = zero.
Proof.
intros; unfold add, neg, zero. apply eqm_samerepr.
replace 0 with (unsigned x + (- (unsigned x))).
auto with ints. lia.
Qed.
Theorem unsigned_add_carry:
forall x y,
unsigned (add x y) = unsigned x + unsigned y - unsigned (add_carry x y zero) * modulus.
Proof.
intros.
unfold add, add_carry. rewrite unsigned_zero. rewrite Z.add_0_r.
rewrite unsigned_repr_eq.
generalize (unsigned_range x) (unsigned_range y). intros.
destruct (zlt (unsigned x + unsigned y) modulus).
rewrite unsigned_zero. apply Zmod_unique with 0. lia. lia.
rewrite unsigned_one. apply Zmod_unique with 1. lia. lia.
Qed.
Corollary unsigned_add_either:
forall x y,
unsigned (add x y) = unsigned x + unsigned y
\/ unsigned (add x y) = unsigned x + unsigned y - modulus.
Proof.
intros. rewrite unsigned_add_carry. unfold add_carry.
rewrite unsigned_zero. rewrite Z.add_0_r.
destruct (zlt (unsigned x + unsigned y) modulus).
rewrite unsigned_zero. left; lia.
rewrite unsigned_one. right; lia.
Qed.
(** ** Properties of negation *)
Theorem neg_repr: forall z, neg (repr z) = repr (-z).
Proof.
intros; unfold neg. apply eqm_samerepr. auto with ints.
Qed.
Theorem neg_zero: neg zero = zero.
Proof.
unfold neg. rewrite unsigned_zero. auto.
Qed.
Theorem neg_involutive: forall x, neg (neg x) = x.
Proof.
intros; unfold neg.
apply eqm_repr_eq. eapply eqm_trans. apply eqm_neg.
apply eqm_unsigned_repr_l. apply eqm_refl. apply eqm_refl2. lia.
Qed.
Theorem neg_add_distr: forall x y, neg(add x y) = add (neg x) (neg y).
Proof.
intros; unfold neg, add. apply eqm_samerepr.
apply eqm_trans with (- (unsigned x + unsigned y)).
auto with ints.
replace (- (unsigned x + unsigned y))
with ((- unsigned x) + (- unsigned y)).
auto with ints. lia.
Qed.
(** ** Properties of subtraction *)
Theorem sub_zero_l: forall x, sub x zero = x.
Proof.
intros; unfold sub. rewrite unsigned_zero.
replace (unsigned x - 0) with (unsigned x) by lia. apply repr_unsigned.
Qed.
Theorem sub_zero_r: forall x, sub zero x = neg x.
Proof.
intros; unfold sub, neg. rewrite unsigned_zero. auto.
Qed.
Theorem sub_add_opp: forall x y, sub x y = add x (neg y).
Proof.
intros; unfold sub, add, neg. apply eqm_samerepr.
apply eqm_add; auto with ints.
Qed.
Theorem sub_idem: forall x, sub x x = zero.
Proof.
intros; unfold sub. unfold zero. decEq. lia.
Qed.
Theorem sub_add_l: forall x y z, sub (add x y) z = add (sub x z) y.
Proof.
intros. repeat rewrite sub_add_opp.
repeat rewrite add_assoc. decEq. apply add_commut.
Qed.
Theorem sub_add_r: forall x y z, sub x (add y z) = add (sub x z) (neg y).
Proof.
intros. repeat rewrite sub_add_opp.
rewrite neg_add_distr. rewrite add_permut. apply add_commut.
Qed.
Theorem sub_shifted:
forall x y z,
sub (add x z) (add y z) = sub x y.
Proof.
intros. rewrite sub_add_opp. rewrite neg_add_distr.
rewrite add_assoc.
rewrite (add_commut (neg y) (neg z)).
rewrite <- (add_assoc z). rewrite add_neg_zero.
rewrite (add_commut zero). rewrite add_zero.
symmetry. apply sub_add_opp.
Qed.
Theorem sub_signed:
forall x y, sub x y = repr (signed x - signed y).
Proof.
intros. unfold sub. apply eqm_samerepr.
apply eqm_sub; apply eqm_sym; apply eqm_signed_unsigned.
Qed.
Theorem unsigned_sub_borrow:
forall x y,
unsigned (sub x y) = unsigned x - unsigned y + unsigned (sub_borrow x y zero) * modulus.
Proof.
intros.
unfold sub, sub_borrow. rewrite unsigned_zero. rewrite Z.sub_0_r.
rewrite unsigned_repr_eq.
generalize (unsigned_range x) (unsigned_range y). intros.
destruct (zlt (unsigned x - unsigned y) 0).
rewrite unsigned_one. apply Zmod_unique with (-1). lia. lia.
rewrite unsigned_zero. apply Zmod_unique with 0. lia. lia.
Qed.
(** ** Properties of multiplication *)
Theorem mul_commut: forall x y, mul x y = mul y x.
Proof.
intros; unfold mul. decEq. ring.
Qed.
Theorem mul_zero: forall x, mul x zero = zero.
Proof.
intros; unfold mul. rewrite unsigned_zero.
unfold zero. decEq. ring.
Qed.
Theorem mul_one: forall x, mul x one = x.
Proof.
intros; unfold mul. rewrite unsigned_one.
transitivity (repr (unsigned x)). decEq. ring.
apply repr_unsigned.
Qed.
Theorem mul_mone: forall x, mul x mone = neg x.
Proof.
intros; unfold mul, neg. rewrite unsigned_mone.
apply eqm_samerepr.
replace (-unsigned x) with (0 - unsigned x) by lia.
replace (unsigned x * (modulus - 1)) with (unsigned x * modulus - unsigned x) by ring.
apply eqm_sub. exists (unsigned x). lia. apply eqm_refl.
Qed.
Theorem mul_assoc: forall x y z, mul (mul x y) z = mul x (mul y z).
Proof.
intros; unfold mul.
set (x' := unsigned x).
set (y' := unsigned y).
set (z' := unsigned z).
apply eqm_samerepr. apply eqm_trans with ((x' * y') * z').
auto with ints.
rewrite <- Z.mul_assoc. auto with ints.
Qed.
Theorem mul_add_distr_l:
forall x y z, mul (add x y) z = add (mul x z) (mul y z).
Proof.
intros; unfold mul, add.
apply eqm_samerepr.
set (x' := unsigned x).
set (y' := unsigned y).
set (z' := unsigned z).
apply eqm_trans with ((x' + y') * z').
auto with ints.
replace ((x' + y') * z') with (x' * z' + y' * z').
auto with ints.
ring.
Qed.
Theorem mul_add_distr_r:
forall x y z, mul x (add y z) = add (mul x y) (mul x z).
Proof.
intros. rewrite mul_commut. rewrite mul_add_distr_l.
decEq; apply mul_commut.
Qed.
Theorem neg_mul_distr_l:
forall x y, neg(mul x y) = mul (neg x) y.
Proof.
intros. unfold mul, neg.
set (x' := unsigned x). set (y' := unsigned y).
apply eqm_samerepr. apply eqm_trans with (- (x' * y')).
auto with ints.
replace (- (x' * y')) with ((-x') * y') by ring.
auto with ints.
Qed.
Theorem neg_mul_distr_r:
forall x y, neg(mul x y) = mul x (neg y).
Proof.
intros. rewrite (mul_commut x y). rewrite (mul_commut x (neg y)).
apply neg_mul_distr_l.
Qed.
Theorem mul_signed:
forall x y, mul x y = repr (signed x * signed y).
Proof.
intros; unfold mul. apply eqm_samerepr.
apply eqm_mult; apply eqm_sym; apply eqm_signed_unsigned.
Qed.
(** ** Properties of division and modulus *)
Lemma modu_divu_Euclid:
forall x y, y <> zero -> x = add (mul (divu x y) y) (modu x y).
Proof.
intros. unfold add, mul, divu, modu.
transitivity (repr (unsigned x)). auto with ints.
apply eqm_samerepr.
set (x' := unsigned x). set (y' := unsigned y).
apply eqm_trans with ((x' / y') * y' + x' mod y').
apply eqm_refl2. rewrite Z.mul_comm. apply Z_div_mod_eq.
generalize (unsigned_range y); intro.
assert (unsigned y <> 0). red; intro.
elim H. rewrite <- (repr_unsigned y). unfold zero. congruence.
unfold y'. lia.
auto with ints.
Qed.
Theorem modu_divu:
forall x y, y <> zero -> modu x y = sub x (mul (divu x y) y).
Proof.
intros.
assert (forall a b c, a = add b c -> c = sub a b).
intros. subst a. rewrite sub_add_l. rewrite sub_idem.
rewrite add_commut. rewrite add_zero. auto.
apply H0. apply modu_divu_Euclid. auto.
Qed.
Lemma mods_divs_Euclid:
forall x y, x = add (mul (divs x y) y) (mods x y).
Proof.
intros. unfold add, mul, divs, mods.
transitivity (repr (signed x)). auto with ints.
apply eqm_samerepr.
set (x' := signed x). set (y' := signed y).
apply eqm_trans with ((Z.quot x' y') * y' + Z.rem x' y').
apply eqm_refl2. rewrite Z.mul_comm. apply Z.quot_rem'.
apply eqm_add; auto with ints.
apply eqm_unsigned_repr_r. apply eqm_mult; auto with ints.
unfold y'. apply eqm_signed_unsigned.
Qed.
Theorem mods_divs:
forall x y, mods x y = sub x (mul (divs x y) y).
Proof.
intros.
assert (forall a b c, a = add b c -> c = sub a b).
intros. subst a. rewrite sub_add_l. rewrite sub_idem.
rewrite add_commut. rewrite add_zero. auto.
apply H. apply mods_divs_Euclid.
Qed.
Theorem divu_one: