Correctness of instruction selection for integer division
Require Import Coqlib.
Require Import Zquot.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Memory.
Require Import Globalenvs.
Require Import Events.
Require Import Cminor.
Require Import Op.
Require Import CminorSel.
Require Import SelectOp.
Require Import SelectOpproof.
Require Import SelectDiv.
Open Local Scope cminorsel_scope.
Main approximation theorems
Section Z_DIV_MUL.
Variable N:
Z.
(* number of relevant bits *)
Hypothesis N_pos:
N >= 0.
Variable d:
Z.
(* divisor *)
Hypothesis d_pos:
d > 0.
This is theorem 4.2 from Granlund and Montgomery, PLDI 1994.
Lemma Zdiv_mul_pos:
forall m l,
l >= 0 ->
two_p (
N+
l) <=
m *
d <=
two_p (
N+
l) +
two_p l ->
forall n,
0 <=
n <
two_p N ->
Zdiv n d =
Zdiv (
m *
n) (
two_p (
N +
l)).
Proof.
Lemma Zdiv_unique_2:
forall x y q,
y > 0 -> 0 <
y *
q -
x <=
y ->
Zdiv x y =
q - 1.
Proof.
intros.
apply Zdiv_unique with (
x - (
q - 1) *
y).
ring.
replace ((
q - 1) *
y)
with (
y *
q -
y)
by ring.
omega.
Qed.
Lemma Zdiv_mul_opp:
forall m l,
l >= 0 ->
two_p (
N+
l) <
m *
d <=
two_p (
N+
l) +
two_p l ->
forall n,
0 <
n <=
two_p N ->
Zdiv n d = -
Zdiv (
m * (-
n)) (
two_p (
N +
l)) - 1.
Proof.
This is theorem 5.1 from Granlund and Montgomery, PLDI 1994.
Lemma Zquot_mul:
forall m l,
l >= 0 ->
two_p (
N+
l) <
m *
d <=
two_p (
N+
l) +
two_p l ->
forall n,
-
two_p N <=
n <
two_p N ->
Z.quot n d =
Zdiv (
m *
n) (
two_p (
N +
l)) + (
if zlt n 0
then 1
else 0).
Proof.
End Z_DIV_MUL.
Correctness of the division parameters
Lemma divs_mul_params_sound:
forall d m p,
divs_mul_params d =
Some(
p,
m) ->
0 <=
m <
Int.modulus /\ 0 <=
p < 32 /\
forall n,
Int.min_signed <=
n <=
Int.max_signed ->
Z.quot n d =
Zdiv (
m *
n) (
two_p (32 +
p)) + (
if zlt n 0
then 1
else 0).
Proof with
Lemma divu_mul_params_sound:
forall d m p,
divu_mul_params d =
Some(
p,
m) ->
0 <=
m <
Int.modulus /\ 0 <=
p < 32 /\
forall n,
0 <=
n <
Int.modulus ->
Zdiv n d =
Zdiv (
m *
n) (
two_p (32 +
p)).
Proof with
Lemma divs_mul_shift_gen:
forall x y m p,
divs_mul_params (
Int.signed y) =
Some(
p,
m) ->
0 <=
m <
Int.modulus /\ 0 <=
p < 32 /\
Int.divs x y =
Int.add (
Int.shr (
Int.repr ((
Int.signed x *
m) /
Int.modulus)) (
Int.repr p))
(
Int.shru x (
Int.repr 31)).
Proof.
Theorem divs_mul_shift_1:
forall x y m p,
divs_mul_params (
Int.signed y) =
Some(
p,
m) ->
m <
Int.half_modulus ->
0 <=
p < 32 /\
Int.divs x y =
Int.add (
Int.shr (
Int.mulhs x (
Int.repr m)) (
Int.repr p))
(
Int.shru x (
Int.repr 31)).
Proof.
Theorem divs_mul_shift_2:
forall x y m p,
divs_mul_params (
Int.signed y) =
Some(
p,
m) ->
m >=
Int.half_modulus ->
0 <=
p < 32 /\
Int.divs x y =
Int.add (
Int.shr (
Int.add (
Int.mulhs x (
Int.repr m))
x) (
Int.repr p))
(
Int.shru x (
Int.repr 31)).
Proof.
Theorem divu_mul_shift:
forall x y m p,
divu_mul_params (
Int.unsigned y) =
Some(
p,
m) ->
0 <=
p < 32 /\
Int.divu x y =
Int.shru (
Int.mulhu x (
Int.repr m)) (
Int.repr p).
Proof.
Correctness of the smart constructors for division and modulus
Section CMCONSTRS.
Variable ge:
genv.
Variable sp:
val.
Variable e:
env.
Variable m:
mem.
Lemma eval_divu_mul:
forall le x y p M,
divu_mul_params (
Int.unsigned y) =
Some(
p,
M) ->
nth_error le O =
Some (
Vint x) ->
eval_expr ge sp e m le (
divu_mul p M) (
Vint (
Int.divu x y)).
Proof.
Theorem eval_divuimm:
forall le e1 x n2 z,
eval_expr ge sp e m le e1 x ->
Val.divu x (
Vint n2) =
Some z ->
exists v,
eval_expr ge sp e m le (
divuimm e1 n2)
v /\
Val.lessdef z v.
Proof.
Theorem eval_divu:
forall le a b x y z,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
Val.divu x y =
Some z ->
exists v,
eval_expr ge sp e m le (
divu a b)
v /\
Val.lessdef z v.
Proof.
Lemma eval_mod_from_div:
forall le a n x y,
eval_expr ge sp e m le a (
Vint y) ->
nth_error le O =
Some (
Vint x) ->
eval_expr ge sp e m le (
mod_from_div a n) (
Vint (
Int.sub x (
Int.mul y n))).
Proof.
unfold mod_from_div;
intros.
exploit eval_mulimm;
eauto.
instantiate (1 :=
n).
intros [
v [
A B]].
simpl in B.
inv B.
EvalOp.
Qed.
Theorem eval_moduimm:
forall le e1 x n2 z,
eval_expr ge sp e m le e1 x ->
Val.modu x (
Vint n2) =
Some z ->
exists v,
eval_expr ge sp e m le (
moduimm e1 n2)
v /\
Val.lessdef z v.
Proof.
Theorem eval_modu:
forall le a b x y z,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
Val.modu x y =
Some z ->
exists v,
eval_expr ge sp e m le (
modu a b)
v /\
Val.lessdef z v.
Proof.
Lemma eval_divs_mul:
forall le x y p M,
divs_mul_params (
Int.signed y) =
Some(
p,
M) ->
nth_error le O =
Some (
Vint x) ->
eval_expr ge sp e m le (
divs_mul p M) (
Vint (
Int.divs x y)).
Proof.
Theorem eval_divsimm:
forall le e1 x n2 z,
eval_expr ge sp e m le e1 x ->
Val.divs x (
Vint n2) =
Some z ->
exists v,
eval_expr ge sp e m le (
divsimm e1 n2)
v /\
Val.lessdef z v.
Proof.
Theorem eval_divs:
forall le a b x y z,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
Val.divs x y =
Some z ->
exists v,
eval_expr ge sp e m le (
divs a b)
v /\
Val.lessdef z v.
Proof.
Theorem eval_modsimm:
forall le e1 x n2 z,
eval_expr ge sp e m le e1 x ->
Val.mods x (
Vint n2) =
Some z ->
exists v,
eval_expr ge sp e m le (
modsimm e1 n2)
v /\
Val.lessdef z v.
Proof.
Theorem eval_mods:
forall le a b x y z,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
Val.mods x y =
Some z ->
exists v,
eval_expr ge sp e m le (
mods a b)
v /\
Val.lessdef z v.
Proof.
Floating-point division
Theorem eval_divf:
forall le a b x y,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
exists v,
eval_expr ge sp e m le (
divf a b)
v /\
Val.lessdef (
Val.divf x y)
v.
Proof.
intros until y.
unfold divf.
destruct (
divf_match b);
intros.
-
unfold divfimm.
destruct (
Float.exact_inverse n2)
as [
n2' | ]
eqn:
EINV.
+
inv H0.
inv H4.
simpl in H6.
inv H6.
econstructor;
split.
EvalOp.
constructor.
eauto.
constructor.
EvalOp.
simpl;
eauto.
constructor.
simpl;
eauto.
destruct x;
simpl;
auto.
erewrite Float.div_mul_inverse;
eauto.
+
TrivialExists.
-
TrivialExists.
Qed.
Theorem eval_divfs:
forall le a b x y,
eval_expr ge sp e m le a x ->
eval_expr ge sp e m le b y ->
exists v,
eval_expr ge sp e m le (
divfs a b)
v /\
Val.lessdef (
Val.divfs x y)
v.
Proof.
intros until y.
unfold divfs.
destruct (
divfs_match b);
intros.
-
unfold divfsimm.
destruct (
Float32.exact_inverse n2)
as [
n2' | ]
eqn:
EINV.
+
inv H0.
inv H4.
simpl in H6.
inv H6.
econstructor;
split.
EvalOp.
constructor.
eauto.
constructor.
EvalOp.
simpl;
eauto.
constructor.
simpl;
eauto.
destruct x;
simpl;
auto.
erewrite Float32.div_mul_inverse;
eauto.
+
TrivialExists.
-
TrivialExists.
Qed.
End CMCONSTRS.