Saturate negative division

This commit is contained in:
Michael Watzko 2021-08-19 09:59:05 +02:00
parent f136eea97c
commit 8049230852

View File

@ -266,19 +266,6 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> }
#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Div for Saturating<$t> {
type Output = Saturating<$t>;
#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
// saturating div is the default behavior?
Saturating(self.0.div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl DivAssign for Saturating<$t> {
#[inline]
@ -864,6 +851,40 @@ macro_rules! saturating_int_impl_signed {
}
forward_ref_unop! { impl Neg, neg for Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_int_impl)]
/// use std::num::Saturating;
///
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN + 1), Saturating(", stringify!($t), "::MAX) / Saturating(-1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MIN) / Saturating(-1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / Saturating(1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / Saturating(1));")]
/// ```
#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Div for Saturating<$t> {
type Output = Saturating<$t>;
#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
let expected_signum = self.0.signum() * other.0.signum();
let (result, overflowed) = self.0.overflowing_div(other.0);
if !overflowed {
Saturating(result)
} else if expected_signum < 0 {
Saturating(<$t>::MIN)
} else {
Saturating(<$t>::MAX)
}
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
)*)
}
@ -912,6 +933,18 @@ macro_rules! saturating_int_impl_unsigned {
}
}
#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Div for Saturating<$t> {
type Output = Saturating<$t>;
#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
Saturating(self.0.div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
)*)
}