aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/IntegerExtra.v
blob: ad0101573a98cbb2e553957bcd115adbd15b6af2 (plain)
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
Require Import BinInt.
Require Import Lia.
Require Import ZBinary.

From bbv Require Import ZLib.
From compcert Require Import Integers Coqlib.

Require Import Coquplib.

Local Open Scope Z_scope.

Module PtrofsExtra.

  Lemma mul_divs :
    forall x y,
      0 <= Ptrofs.signed y ->
      0 < Ptrofs.signed x ->
      Ptrofs.signed y mod Ptrofs.signed x = 0 ->
      (Integers.Ptrofs.mul x (Integers.Ptrofs.divs y x)) = y.
  Proof.
    intros.

    pose proof (Ptrofs.mods_divs_Euclid y x).
    pose proof (Zquot.Zrem_Zmod_zero (Ptrofs.signed y) (Ptrofs.signed x)).
    apply <- H3 in H1; try lia; clear H3.
    unfold Ptrofs.mods in H2.
    rewrite H1 in H2.
    replace (Ptrofs.repr 0) with (Ptrofs.zero) in H2 by reflexivity.
    rewrite Ptrofs.add_zero in H2.
    rewrite Ptrofs.mul_commut.
    congruence.
  Qed.

  Lemma Z_div_distr_add :
    forall x y z,
      x mod z = 0 ->
      y mod z = 0 ->
      z <> 0 ->
      x / z + y / z = (x + y) / z.
  Proof.
    intros.

    assert ((x + y) mod z = 0).
    { rewrite <- Z.add_mod_idemp_l; try assumption.
      rewrite H. assumption. }

    rewrite <- Z.mul_cancel_r with (p := z); try assumption.
    rewrite Z.mul_add_distr_r.
    repeat rewrite ZLib.div_mul_undo; lia.
  Qed.

  Lemma mul_unsigned :
    forall x y,
      Ptrofs.mul x y =
      Ptrofs.repr (Ptrofs.unsigned x * Ptrofs.unsigned y).
  Proof.
    intros; unfold Ptrofs.mul.
    apply Ptrofs.eqm_samerepr.
    apply Ptrofs.eqm_mult; exists 0; lia.
  Qed.

  Lemma mul_repr :
    forall x y,
      Ptrofs.min_signed <= y <= Ptrofs.max_signed ->
      Ptrofs.min_signed <= x <= Ptrofs.max_signed ->
      Ptrofs.mul (Ptrofs.repr y) (Ptrofs.repr x) = Ptrofs.repr (x * y).
  Proof.
    intros; unfold Ptrofs.mul.
    destruct (Z_ge_lt_dec x 0); destruct (Z_ge_lt_dec y 0).

    - f_equal.
      repeat rewrite Ptrofs.unsigned_repr_eq.
      repeat rewrite Z.mod_small; simplify; lia.

    - assert (Ptrofs.lt (Ptrofs.repr y) Ptrofs.zero = true).
      {
        unfold Ptrofs.lt.
        rewrite Ptrofs.signed_repr; auto.
        rewrite Ptrofs.signed_zero.
        destruct (zlt y 0); try lia; auto.
      }

      rewrite Ptrofs.unsigned_signed with (n := Ptrofs.repr y).
      rewrite H1.
      rewrite Ptrofs.signed_repr; auto.
      rewrite Ptrofs.unsigned_repr_eq.
      rewrite Z.mod_small; simplify; try lia.
      rewrite Z.mul_add_distr_r.
      apply Ptrofs.eqm_samerepr.
      exists x. simplify. lia.

    - assert (Ptrofs.lt (Ptrofs.repr x) Ptrofs.zero = true).
      {
        unfold Ptrofs.lt.
        rewrite Ptrofs.signed_repr; auto.
        rewrite Ptrofs.signed_zero.
        destruct (zlt x 0); try lia; auto.
      }

      rewrite Ptrofs.unsigned_signed with (n := Ptrofs.repr x).
      rewrite H1.
      rewrite Ptrofs.signed_repr; auto.
      rewrite Ptrofs.unsigned_repr_eq.
      rewrite Z.mod_small; simplify; try lia.
      rewrite Z.mul_add_distr_l.
      apply Ptrofs.eqm_samerepr.
      exists y. simplify. lia.

    - assert (Ptrofs.lt (Ptrofs.repr x) Ptrofs.zero = true).
      {
        unfold Ptrofs.lt.
        rewrite Ptrofs.signed_repr; auto.
        rewrite Ptrofs.signed_zero.
        destruct (zlt x 0); try lia; auto.
      }
      assert (Ptrofs.lt (Ptrofs.repr y) Ptrofs.zero = true).
      {
        unfold Ptrofs.lt.
        rewrite Ptrofs.signed_repr; auto.
        rewrite Ptrofs.signed_zero.
        destruct (zlt y 0); try lia; auto.
      }
      rewrite Ptrofs.unsigned_signed with (n := Ptrofs.repr x).
      rewrite Ptrofs.unsigned_signed with (n := Ptrofs.repr y).
      rewrite H2.
      rewrite H1.
      repeat rewrite Ptrofs.signed_repr; auto.
      replace ((y + Ptrofs.modulus) * (x + Ptrofs.modulus)) with
          (x * y + (x + y + Ptrofs.modulus) * Ptrofs.modulus) by lia.
      apply Ptrofs.eqm_samerepr.
      exists (x + y + Ptrofs.modulus). lia.
  Qed.
End PtrofsExtra.

Module IntExtra.
  Lemma mul_unsigned :
    forall x y,
      Int.mul x y =
      Int.repr (Int.unsigned x * Int.unsigned y).
  Proof.
    intros; unfold Int.mul.
    apply Int.eqm_samerepr.
    apply Int.eqm_mult; exists 0; lia.
  Qed.

  Lemma mul_repr :
    forall x y,
      Int.min_signed <= y <= Int.max_signed ->
      Int.min_signed <= x <= Int.max_signed ->
      Int.mul (Int.repr y) (Int.repr x) = Int.repr (x * y).
  Proof.
    intros; unfold Int.mul.
    destruct (Z_ge_lt_dec x 0); destruct (Z_ge_lt_dec y 0).

    - f_equal.
      repeat rewrite Int.unsigned_repr_eq.
      repeat rewrite Z.mod_small; simplify; lia.

    - assert (Int.lt (Int.repr y) Int.zero = true).
      {
        unfold Int.lt.
        rewrite Int.signed_repr; auto.
        rewrite Int.signed_zero.
        destruct (zlt y 0); try lia; auto.
      }

      rewrite Int.unsigned_signed with (n := Int.repr y).
      rewrite H1.
      rewrite Int.signed_repr; auto.
      rewrite Int.unsigned_repr_eq.
      rewrite Z.mod_small; simplify; try lia.
      rewrite Z.mul_add_distr_r.
      apply Int.eqm_samerepr.
      exists x. simplify. lia.

    - assert (Int.lt (Int.repr x) Int.zero = true).
      {
        unfold Int.lt.
        rewrite Int.signed_repr; auto.
        rewrite Int.signed_zero.
        destruct (zlt x 0); try lia; auto.
      }

      rewrite Int.unsigned_signed with (n := Int.repr x).
      rewrite H1.
      rewrite Int.signed_repr; auto.
      rewrite Int.unsigned_repr_eq.
      rewrite Z.mod_small; simplify; try lia.
      rewrite Z.mul_add_distr_l.
      apply Int.eqm_samerepr.
      exists y. simplify. lia.

    - assert (Int.lt (Int.repr x) Int.zero = true).
      {
        unfold Int.lt.
        rewrite Int.signed_repr; auto.
        rewrite Int.signed_zero.
        destruct (zlt x 0); try lia; auto.
      }
      assert (Int.lt (Int.repr y) Int.zero = true).
      {
        unfold Int.lt.
        rewrite Int.signed_repr; auto.
        rewrite Int.signed_zero.
        destruct (zlt y 0); try lia; auto.
      }
      rewrite Int.unsigned_signed with (n := Int.repr x).
      rewrite Int.unsigned_signed with (n := Int.repr y).
      rewrite H2.
      rewrite H1.
      repeat rewrite Int.signed_repr; auto.
      replace ((y + Int.modulus) * (x + Int.modulus)) with
          (x * y + (x + y + Int.modulus) * Int.modulus) by lia.
      apply Int.eqm_samerepr.
      exists (x + y + Int.modulus). lia.
  Qed.
End IntExtra.

Lemma mul_of_int :
  forall x y,
    0 <= x < Integers.Ptrofs.modulus ->
    Integers.Ptrofs.mul (Integers.Ptrofs.repr x) (Integers.Ptrofs.of_int y) =
    Integers.Ptrofs.of_int (Integers.Int.mul (Integers.Int.repr x) y).
Proof.
  intros.
  pose proof (Integers.Ptrofs.agree32_of_int eq_refl y) as A.
  pose proof (Integers.Ptrofs.agree32_to_int eq_refl (Integers.Ptrofs.repr x)) as B.
  exploit Integers.Ptrofs.agree32_mul; [> reflexivity | exact B | exact A | intro C].
  unfold Integers.Ptrofs.to_int in C.
  unfold Integers.Ptrofs.of_int in C.
  rewrite Integers.Ptrofs.unsigned_repr_eq in C.
  rewrite Z.mod_small in C; auto.
  symmetry.
  apply Integers.Ptrofs.agree32_of_int_eq; auto.
Qed.