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 NormaliseSpec.
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.
Require Import Values_symbolictype.
Require Import Values_symbolic.
Require Import IntFacts.
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:
expr_sym.
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 (
Eval (
Vint x)) ->
exists v,
eval_expr ge sp e m le (
divu_mul p M)
v /\
same_eval v (
Eval (
Vint (
Int.divu x y))).
Proof.
Theorem eval_divuimm:
forall n2,
unary_constructor_sound ge sp e m (
fun x =>
divuimm x n2) (
fun x =>
Val.divu x (
Eval (
Vint n2))).
Proof.
Theorem eval_divu:
binary_constructor_sound ge sp e m divu Val.divu.
Proof.
Theorem eval_moduimm:
forall n2,
unary_constructor_sound ge sp e m (
fun x =>
moduimm x n2) (
fun x =>
Val.modu x (
Eval (
Vint n2))).
Proof.
Theorem eval_modu:
binary_constructor_sound ge sp e m modu Val.modu.
Proof.
Theorem eval_divsimm:
forall n2,
unary_constructor_sound ge sp e m (
fun x =>
divsimm x n2) (
fun x =>
Val.divs x (
Eval (
Vint n2))).
Proof.
Theorem eval_divs:
binary_constructor_sound ge sp e m divs Val.divs.
Proof.
Theorem eval_modsimm:
forall n2,
unary_constructor_sound ge sp e m (
fun x =>
modsimm x n2) (
fun x =>
Val.mods x (
Eval (
Vint n2))).
Proof.
Theorem eval_mods:
binary_constructor_sound ge sp e m mods Val.mods.
Proof.
Floating-point division
Ltac TrivialExists :=
eexists;
split; [
repeat (
econstructor;
simpl;
eauto)|
eauto;
try solve[
constructor;
simpl;
auto]].
Theorem eval_divf:
binary_constructor_sound ge sp e m divf Val.divf.
Proof.
Theorem eval_divfs:
binary_constructor_sound ge sp e m divfs Val.divfs.
Proof.
End CMCONSTRS.