From Flocq Require Import BinarySingleNaN Core Digits Operations Round Bracket Sterbenz
Binary Round_odd Bits.
Require Archi.
Require Import Coqlib.
Require Import Compopts.
Require Import AST.
Require Import Reals.
Require Import Integers.
Require Import Floats.
Require Import Op.
Require Import CminorSel.
Require Import OpHelpers.
Require Import ExtFloats.
Require Import DecBoolOps.
Require Import Chunks.
Require Import Builtins.
Require Import Values Globalenvs.
Require Compopts.
Require Import Psatz.
Require Import IEEE754_extra.
From Gappa Require Import Gappa_tactic.
Local Open Scope cminorsel_scope.
Ltac rew_spec :=
change (
SpecFloat.fexp 24 128)
with (
FLT_exp (-149) 24)
in *;
change (
SpecFloat.fexp 53 1024)
with (
FLT_exp (-1074) 53)
in *.
Definition approx_inv b :=
let invb_s :=
Eop Oinvfs ((
Eop Osingleofintu ((
Eletvar 0%
nat):::
Enil)):::
Enil)
in
let invb_d :=
Eop Ofloatofsingle (
invb_s :::
Enil)
in
let b_d :=
Eop Ofloatoflongu ((
Eop Ocast32unsigned ((
Eletvar 1%
nat):::
Enil)):::
Enil)
in
let invb_d_var :=
Eletvar (0%
nat)
in
let one :=
Eop (
Ofloatconst ExtFloat.one)
Enil in
let alpha :=
Eop Ofmsubf (
one :::
invb_d_var :::
b_d :::
Enil)
in
let x :=
Eop Ofmaddf (
invb_d_var :::
alpha :::
invb_d_var :::
Enil)
in
Elet b (
Elet invb_d x).
Definition approx_inv_thresh := (1/17179869184)%
R.
Lemma Rabs_relax:
forall b b' (
INEQ : (
b <
b')%
R)
x,
(-
b <=
x <=
b)%
R -> (
Rabs x <
b')%
R.
Proof.
intros.
apply Rabs_lt.
lra.
Qed.
Theorem approx_inv_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_b :
expr) (
b :
int)
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : ((
Int.unsigned b) > 0)%
Z),
exists f :
float,
eval_expr ge sp cmenv memenv le (
approx_inv expr_b) (
Vfloat f) /\
is_finite _ _
f =
true /\ (
Rabs((
B2R _ _
f) - (1 /
IZR (
Int.unsigned b))) <=
approx_inv_thresh)%
R.
Proof.
intros.
unfold approx_inv.
econstructor.
constructor.
{
repeat econstructor.
{
eassumption. }
{
reflexivity. } }
set (
invb_d := (
Float.of_single (
ExtFloat32.inv (
Float32.of_intu b)))).
set (
b' :=
Int.unsigned b)
in *.
pose proof (
Int.unsigned_range b)
as RANGE.
fold b' in RANGE.
change Int.modulus with 4294967296%
Z in RANGE.
assert (0 <=
b' <=
Int64.max_unsigned)%
Z as b'RANGE.
{
change Int64.max_unsigned with 18446744073709551615%
Z.
lia. }
assert (1 <=
IZR b' <= 4294967295)%
R as RANGE'.
{
split.
{
apply IZR_le.
lia. }
apply IZR_le.
lia.
}
cbn.
set (
b_d := (
Float.of_longu (
Int64.repr b')))
in *.
Local Transparent Float.of_longu.
unfold Float.of_longu in b_d.
assert(
SILLY : (- 2 ^ 24 <= 1 <= 2 ^ 24)%
Z)
by lia.
destruct (
BofZ_exact 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1
SILLY)
as (
C0E &
C0F & _).
clear SILLY.
assert(
SILLY : (- 2 ^ 53 <= 1 <= 2 ^ 53)%
Z)
by lia.
destruct (
BofZ_exact 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1
SILLY)
as (
C9E &
C9F & _).
clear SILLY.
pose proof (
BofZ_correct 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
b')
as C1.
rewrite Rlt_bool_true in C1;
cycle 1.
{
clear C1.
cbn.
eapply (
Rabs_relax (
IZR 4294967296)).
lra.
rew_spec.
gappa.
}
rewrite Zlt_bool_false in C1 by lia.
destruct C1 as (
C1E &
C1F & _).
Local Transparent Float32.of_intu Float32.of_int Float32.div.
unfold ExtFloat32.inv,
ExtFloat32.one,
Float32.of_intu,
Float32.of_int,
Float32.div in invb_d.
fold b' in invb_d.
change (
Int.signed (
Int.repr 1%
Z))
with 1%
Z in invb_d.
pose proof (
Bdiv_correct 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
Float32.binop_nan mode_NE
(
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1)
(
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
b'))
as C2.
rewrite Rlt_bool_true in C2;
cycle 1.
{
clear C2.
rewrite C1E.
apply (
Rabs_relax (
bpow radix2 10)).
{
cbn;
lra. }
unfold F2R.
cbn.
unfold F2R.
cbn.
rew_spec.
gappa.
}
assert (
B2R 24 128 (
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
b') <> 0%
R)
as NONZ.
{
clear C2.
rewrite C1E.
cbn.
assert (1 <=
round radix2 (
FLT_exp (-149) 24)
ZnearestE (
IZR b'))%
R by gappa.
rew_spec.
lra.
}
destruct (
C2 NONZ)
as (
C2E &
C2F & _).
clear C2 NONZ.
Local Transparent Float.of_single.
unfold Float.of_single in invb_d.
pose proof (
Bconv_correct 24 128 53 1024 (@
eq_refl Datatypes.comparison Lt)
(@
eq_refl Datatypes.comparison Lt)
Float.of_single_nan mode_NE
(
Bdiv 24 128 (@
eq_refl Datatypes.comparison Lt)
(@
eq_refl Datatypes.comparison Lt)
Float32.binop_nan mode_NE
(
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt)
(@
eq_refl Datatypes.comparison Lt) 1)
(
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt)
(@
eq_refl Datatypes.comparison Lt)
b')))
as C3.
fold invb_d in C3.
rewrite Rlt_bool_true in C3;
cycle 1.
{
clear C3.
rewrite C2E.
rewrite C1E.
rewrite C0E.
apply (
Rabs_relax (
bpow radix2 10)).
{
apply bpow_lt;
lia. }
cbn.
rew_spec.
gappa.
}
change (
is_finite 24 128 (
BofZ 24 128 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1))
with true in C2F.
destruct (
C3 C2F)
as (
C3E &
C3F & _).
clear C3.
unfold Float.fma.
assert (
is_finite _ _ (
Float.neg invb_d) =
true)
as invb_d_F.
{
Local Transparent Float.neg.
unfold Float.neg.
rewrite is_finite_Bopp.
assumption.
}
assert(
SILLY : (- 2 ^ 53 <=
b' <= 2 ^ 53)%
Z)
by lia.
destruct (
BofZ_exact 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
b' SILLY)
as (
C4E &
C4F & _).
clear SILLY.
assert (
is_finite 53 1024
b_d =
true)
as b_d_F.
{
unfold b_d.
rewrite Int64.unsigned_repr by lia.
assumption.
}
assert (
is_finite 53 1024
ExtFloat.one =
true)
as one_F by reflexivity.
pose proof (
Bfma_correct 53 1024 (@
eq_refl Datatypes.comparison Lt)
(@
eq_refl Datatypes.comparison Lt)
Float.fma_nan mode_NE
(
Float.neg invb_d)
b_d ExtFloat.one invb_d_F b_d_F one_F)
as C5.
cbn zeta in C5.
rewrite Rlt_bool_true in C5;
cycle 1.
{
clear C5.
unfold Float.neg.
rewrite B2R_Bopp.
rewrite C3E.
rewrite C2E.
rewrite C0E.
rewrite C1E.
unfold ExtFloat.one.
change (
Float.of_int (
Int.repr 1))
with (
BofZ 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1).
rewrite C9E.
unfold b_d.
rewrite Int64.unsigned_repr by lia.
rewrite C4E.
apply (
Rabs_relax (
bpow radix2 10)).
{
apply bpow_lt;
lia. }
cbn.
rew_spec.
gappa.
}
destruct C5 as (
C5E &
C5F & _).
pose proof (
Bfma_correct 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
Float.fma_nan mode_NE
(
Bfma 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt)
Float.fma_nan mode_NE
(
Float.neg invb_d)
b_d ExtFloat.one)
invb_d invb_d C5F C3F C3F)
as C6.
cbn zeta in C6.
rewrite Rlt_bool_true in C6;
cycle 1.
{
clear C6.
rewrite C3E.
rewrite C2E.
rewrite C1E.
rewrite C0E.
rewrite C5E.
unfold Float.neg.
rewrite B2R_Bopp.
rewrite C3E.
rewrite C2E.
rewrite C0E.
rewrite C1E.
unfold b_d.
rewrite Int64.unsigned_repr by lia.
rewrite C4E.
unfold ExtFloat.one.
change (
Float.of_int (
Int.repr 1))
with (
BofZ 53 1024 (@
eq_refl Datatypes.comparison Lt) (@
eq_refl Datatypes.comparison Lt) 1).
rewrite C9E.
apply (
Rabs_relax (
bpow radix2 10)).
{
apply bpow_lt;
lia. }
cbn.
rew_spec.
gappa.
}
destruct C6 as (
C6E &
C6F & _).
split.
{
exact C6F. }
rewrite C6E.
rewrite C5E.
rewrite C3E.
rewrite C2E.
rewrite C1E.
rewrite C0E.
unfold Float.neg.
rewrite B2R_Bopp.
unfold ExtFloat.one.
Local Transparent Float.of_int.
unfold Float.of_int.
rewrite (
Int.signed_repr 1)
by (
cbn ;
lia).
rewrite C9E.
rewrite C3E.
rewrite C2E.
rewrite C0E.
rewrite C1E.
unfold b_d.
rewrite Int64.unsigned_repr by lia.
rewrite C4E.
cbn.
rew_spec.
set (
rd :=
round radix2 (
FLT_exp (-1074) 53)
ZnearestE)
in *.
set (
rs :=
round radix2 (
FLT_exp (-149) 24)
ZnearestE)
in *.
set (
bi :=
IZR b')
in *.
set (
invb0 :=
rd (
rs (1/
rs bi))%
R)
in *.
set (
alpha := (-
invb0 *
bi + 1)%
R)
in *.
set (
alpha' := ((1/
bi -
rd (
rs (1/
rs bi)))/(1/
bi))%
R)
in *.
assert (
alpha =
alpha')%
R as expand_alpha.
{
unfold alpha,
alpha',
invb0.
field.
lra.
}
assert(-1/2097152 <=
alpha' <= 1/2097152)%
R as alpha_BOUND.
{
unfold alpha',
rd,
rs.
gappa.
}
set (
delta := (
rd (
rd alpha *
invb0 +
invb0) - (
alpha *
invb0 +
invb0))%
R).
assert(-1/1125899906842624 <=
delta <= 1/1125899906842624)%
R as delta_BOUND.
{
unfold delta,
invb0.
rewrite expand_alpha.
unfold rd,
rs.
gappa.
}
replace (
rd (
rd alpha *
invb0 +
invb0) - 1/
bi)%
R with
(
delta + ((
alpha *
invb0 +
invb0)-1/
bi))%
R by (
unfold delta;
ring).
replace (
alpha *
invb0 +
invb0 - 1 /
bi)%
R with (
alpha * (
invb0 - 1/
bi) + (
alpha * (1/
bi) +
invb0 - 1 /
bi))%
R by ring.
replace (
alpha * (1 /
bi) +
invb0 - 1 /
bi)%
R with 0%
R;
cycle 1.
{
unfold alpha.
field.
lra.
}
apply Rabs_le.
unfold invb0,
rd,
rs,
approx_inv_thresh.
rewrite expand_alpha.
gappa.
Qed.
Definition fp_divu32 a b :=
let a_var :=
Eletvar (1%
nat)
in
let b_var :=
Eletvar (0%
nat)
in
let a_d :=
Eop Ofloatoflongu ((
Eop Ocast32unsigned (
a_var :::
Enil)) :::
Enil)
in
let qr :=
Eop Olonguoffloat_ne ((
Eop Omulf (
a_d:::(
approx_inv b_var):::
Enil)):::
Enil)
in
let qr_var :=
Eletvar 0%
nat in
let rem :=
Eop Omsubl ((
Eop Ocast32unsigned ((
Eletvar (2%
nat)):::
Enil)):::
qr_var :::
(
Eop Ocast32unsigned ((
Eletvar (1%
nat)):::
Enil)):::
Enil)
in
let qr_m1 :=
Eop (
Oaddlimm (
Int64.repr (-1)%
Z)) (
qr_var:::
Enil)
in
let cases :=
Eop (
Osel (
Ccompl0 Clt)
Tlong)
(
qr_m1 :::
qr_var :::
rem :::
Enil)
in
Eop Olowlong ((
Elet a (
Elet (
lift b) (
Elet qr cases))) :::
Enil).
Open Scope Z.
Definition div_approx_reals (
a b :
Z) (
x :
R) :=
let q:=
ZnearestE x in
let r:=
a-
q*
b in
if r <? 0
then q-1
else q.
Lemma floor_ball1:
forall x :
R,
forall y :
Z,
(
Rabs (
x -
IZR y) < 1)%
R ->
Zfloor x = (
y-1)%
Z \/
Zfloor x =
y.
Proof.
intros x y BALL.
apply Rabs_lt_inv in BALL.
case (
Rcompare_spec x (
IZR y));
intro CMP.
-
left.
apply Zfloor_imp.
ring_simplify (
y-1+1).
rewrite minus_IZR.
lra.
-
subst.
rewrite Zfloor_IZR.
right.
reflexivity.
-
right.
apply Zfloor_imp.
rewrite plus_IZR.
lra.
Qed.
Theorem div_approx_reals_correct:
forall a b :
Z,
forall x :
R,
b > 0 ->
(
Rabs (
x -
IZR a/
IZR b) < 1/2)%
R ->
div_approx_reals a b x = (
a/
b)%
Z.
Proof.
Opaque approx_inv.
Theorem fp_divu32_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_a expr_b :
expr) (
a b :
int)
(
EVAL_a :
eval_expr ge sp cmenv memenv le expr_a (
Vint a))
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : (
Int.unsigned b > 0)%
Z),
eval_expr ge sp cmenv memenv le (
fp_divu32 expr_a expr_b)
(
Vint (
Int.divu a b)).
Proof.
Definition e_msubl a b c :=
Eop Omsub (
a :::
b :::
c :::
Enil).
Definition fp_modu32 a b :=
Elet a (
Elet (
lift b) (
e_msubl (
Eletvar 1%
nat) (
Eletvar 0%
nat)
(
fp_divu32 (
Eletvar 1%
nat) (
Eletvar 0%
nat)))).
Theorem fp_modu32_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_a expr_b :
expr) (
a b :
int)
(
EVAL_a :
eval_expr ge sp cmenv memenv le expr_a (
Vint a))
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : (
Int.unsigned b > 0)%
Z),
eval_expr ge sp cmenv memenv le (
fp_modu32 expr_a expr_b)
(
Vint (
Int.modu a b)).
Proof.
Definition e_is_neg a :=
Eop (
Ocmp (
Ccompimm Clt Int.zero)) (
a :::
Enil).
Definition e_xorw a b :=
Eop Oxor (
a :::
b :::
Enil).
Definition e_ite ty c vc v1 v2 :=
Eop (
Osel c ty) (
v1 :::
v2 :::
vc :::
Enil).
Definition e_neg a :=
Eop Oneg (
a :::
Enil).
Definition e_abs a :=
Eop (
Oabsdiffimm Int.zero) (
a :::
Enil).
Definition fp_divs32 a b :=
Elet a (
Elet (
lift b)
(
Elet (
fp_divu32 (
e_abs (
Eletvar (1%
nat))) (
e_abs (
Eletvar (0%
nat))))
(
e_ite Tint (
Ccompu0 Cne) (
e_xorw (
e_is_neg (
Eletvar 2%
nat))
(
e_is_neg (
Eletvar 1%
nat)))
(
e_neg (
Eletvar 0%
nat)) (
Eletvar 0%
nat)))).
Lemma nonneg_signed_unsigned:
forall x (
x_NONNEG :
Int.signed x >= 0),
(
Int.signed x) = (
Int.unsigned x).
Proof.
Lemma int_min_signed_unsigned :
(-
Int.min_signed <
Int.max_unsigned)%
Z.
Proof.
reflexivity.
Qed.
Lemma int_divs_divu :
forall a b
(
b_NOT0 :
Int.signed b <> 0),
Int.divs a b =
if xorb (
Int.lt a Int.zero)
(
Int.lt b Int.zero)
then Int.neg (
Int.divu (
ExtValues.int_abs a)
(
ExtValues.int_abs b))
else Int.divu (
ExtValues.int_abs a) (
ExtValues.int_abs b).
Proof.
Lemma nonzero_unsigned_signed :
forall b,
Int.unsigned b > 0 ->
Int.signed b <> 0.
Proof.
Theorem fp_divs32_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_a expr_b :
expr) (
a b :
int)
(
EVAL_a :
eval_expr ge sp cmenv memenv le expr_a (
Vint a))
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : (
Int.unsigned b > 0)%
Z),
eval_expr ge sp cmenv memenv le (
fp_divs32 expr_a expr_b)
(
Vint (
Int.divs a b)).
Proof.
Lemma int_mods_modu :
forall a b
(
b_NOT0 :
Int.signed b <> 0),
Int.mods a b =
if Int.lt a Int.zero
then Int.neg (
Int.modu (
ExtValues.int_abs a)
(
ExtValues.int_abs b))
else Int.modu (
ExtValues.int_abs a) (
ExtValues.int_abs b).
Proof.
Definition fp_mods32z a b :=
Elet a (
Elet (
lift b)
(
Elet (
fp_modu32 (
e_abs (
Eletvar (1%
nat))) (
e_abs (
Eletvar (0%
nat))))
(
e_ite Tint (
Ccomp0 Clt) (
Eletvar 2%
nat)
(
e_neg (
Eletvar 0%
nat)) (
Eletvar 0%
nat)))).
Theorem fp_mods32z_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_a expr_b :
expr) (
a b :
int)
(
EVAL_a :
eval_expr ge sp cmenv memenv le expr_a (
Vint a))
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : (
Int.unsigned b > 0)%
Z),
eval_expr ge sp cmenv memenv le (
fp_mods32z expr_a expr_b)
(
Vint (
Int.mods a b)).
Proof.
Definition e_msub a b c :=
Eop Omsub (
a :::
b :::
c :::
Enil).
Definition fp_mods32 a b :=
Elet a (
Elet (
lift b)
(
Elet (
fp_divs32 (
Eletvar (1%
nat)) (
Eletvar (0%
nat)))
(
e_msub (
Eletvar 2%
nat) (
Eletvar 1%
nat) (
Eletvar 0%
nat)))).
Theorem fp_mods32_correct :
forall (
ge :
genv) (
sp:
val)
cmenv memenv
(
le :
letenv) (
expr_a expr_b :
expr) (
a b :
int)
(
EVAL_a :
eval_expr ge sp cmenv memenv le expr_a (
Vint a))
(
EVAL_b :
eval_expr ge sp cmenv memenv le expr_b (
Vint b))
(
b_nz : (
Int.unsigned b > 0)%
Z),
eval_expr ge sp cmenv memenv le (
fp_mods32 expr_a expr_b)
(
Vint (
Int.mods a b)).
Proof.