(** Conversion from an integer.  Round to nearest. *)

Definition BofZ (n: Z) : binary_float :=
  binary_normalize prec emax prec_gt_0_ Hmax mode_NE n 0 false.

(* Specifications: *)
Theorem BofZ_correct: forall n, if Rlt_bool (Rabs (round radix2 fexp (round_mode mode_NE) (Z2R n))) (bpow radix2 emax) then B2R prec emax (BofZ n) = round radix2 fexp (round_mode mode_NE) (Z2R n) /\ is_finite _ _ (BofZ n) = true /\ Bsign prec emax (BofZ n) = Z.ltb n 0 else B2FF prec emax (BofZ n) = binary_overflow prec emax mode_NE (Z.ltb n 0).
Theorem BofZ_finite: forall n, Z.abs n <= 2^emax - 2^(emax-prec) -> B2R _ _ (BofZ n) = round radix2 fexp (round_mode mode_NE) (Z2R n) /\ is_finite _ _ (BofZ n) = true /\ Bsign _ _ (BofZ n) = Z.ltb n 0%Z.
Theorem BofZ_exact: forall n, -2^prec <= n <= 2^prec -> B2R _ _ (BofZ n) = Z2R n /\ is_finite _ _ (BofZ n) = true /\ Bsign _ _ (BofZ n) = Z.ltb n 0%Z.
Lemma BofZ_finite_pos0: forall n, Z.abs n <= 2^emax - 2^(emax-prec) -> is_finite_pos0 (BofZ n) = true.
Lemma BofZ_finite_equal: forall x y, Z.abs x <= 2^emax - 2^(emax-prec) -> Z.abs y <= 2^emax - 2^(emax-prec) -> B2R _ _ (BofZ x) = B2R _ _ (BofZ y) -> BofZ x = BofZ y.
Theorem BofZ_plus: forall nan p q, integer_representable p -> integer_representable q -> Bplus _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p + q).
Theorem BofZ_minus: forall nan p q, integer_representable p -> integer_representable q -> Bminus _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p - q).
Theorem BofZ_mult: forall nan p q, integer_representable p -> integer_representable q -> 0 < q -> Bmult _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p * q).
Theorem BofZ_mult_2p: forall nan x p, Z.abs x <= 2^emax - 2^(emax-prec) -> 2^prec <= Z.abs x -> 0 <= p <= emax - 1 -> Bmult _ _ _ Hmax nan mode_NE (BofZ x) (BofZ (2^p)) = BofZ (x * 2^p).
Lemma round_odd_flt: forall prec' emin' x choice, prec > 1 -> prec' > 1 -> prec' >= prec + 2 -> emin' <= emin - 2 -> round radix2 fexp (Znearest choice) (round radix2 (FLT_exp emin' prec') Zrnd_odd x) = round radix2 fexp (Znearest choice) x.
Theorem BofZ_correct: forall n, if Rlt_bool (Rabs (round radix2 fexp (round_mode mode_NE) (Z2R n))) (bpow radix2 emax) then B2R prec emax (BofZ n) = round radix2 fexp (round_mode mode_NE) (Z2R n) /\ is_finite _ _ (BofZ n) = true /\ Bsign prec emax (BofZ n) = Z.ltb n 0 else B2FF prec emax (BofZ n) = binary_overflow prec emax mode_NE (Z.ltb n 0).
Theorem BofZ_finite: forall n, Z.abs n <= 2^emax - 2^(emax-prec) -> B2R _ _ (BofZ n) = round radix2 fexp (round_mode mode_NE) (Z2R n) /\ is_finite _ _ (BofZ n) = true /\ Bsign _ _ (BofZ n) = Z.ltb n 0%Z.
Theorem BofZ_exact: forall n, -2^prec <= n <= 2^prec -> B2R _ _ (BofZ n) = Z2R n /\ is_finite _ _ (BofZ n) = true /\ Bsign _ _ (BofZ n) = Z.ltb n 0%Z.
Lemma BofZ_finite_pos0: forall n, Z.abs n <= 2^emax - 2^(emax-prec) -> is_finite_pos0 (BofZ n) = true.
Lemma BofZ_finite_equal: forall x y, Z.abs x <= 2^emax - 2^(emax-prec) -> Z.abs y <= 2^emax - 2^(emax-prec) -> B2R _ _ (BofZ x) = B2R _ _ (BofZ y) -> BofZ x = BofZ y.
Theorem BofZ_plus: forall nan p q, integer_representable p -> integer_representable q -> Bplus _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p + q).
Theorem BofZ_minus: forall nan p q, integer_representable p -> integer_representable q -> Bminus _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p - q).
Theorem BofZ_mult: forall nan p q, integer_representable p -> integer_representable q -> 0 < q -> Bmult _ _ _ Hmax nan mode_NE (BofZ p) (BofZ q) = BofZ (p * q).
Theorem BofZ_mult_2p: forall nan x p, Z.abs x <= 2^emax - 2^(emax-prec) -> 2^prec <= Z.abs x -> 0 <= p <= emax - 1 -> Bmult _ _ _ Hmax nan mode_NE (BofZ x) (BofZ (2^p)) = BofZ (x * 2^p).
Lemma round_odd_flt: forall prec' emin' x choice, prec > 1 -> prec' > 1 -> prec' >= prec + 2 -> emin' <= emin - 2 -> round radix2 fexp (Znearest choice) (round radix2 (FLT_exp emin' prec') Zrnd_odd x) = round radix2 fexp (Znearest choice) x.