From 709a6c913eb44f361b264fb98cdd419dc8ae6912 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 10 Aug 2021 19:22:06 +0200 Subject: [PATCH 01/20] Add Saturating type (based on Wrapping type) --- library/core/src/num/mod.rs | 4 + library/core/src/num/saturating.rs | 814 +++++++++++++++++++++++++++++ library/std/src/lib.rs | 1 + library/std/src/num.rs | 2 + 4 files changed, 821 insertions(+) create mode 100644 library/core/src/num/saturating.rs diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 9788404dd05bf..09b7418bec0d9 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -43,8 +43,12 @@ mod uint_macros; // import uint_impl! mod error; mod int_log10; mod nonzero; +#[unstable(feature = "saturating_int_impl", issue = "87920")] +mod saturating; mod wrapping; +#[unstable(feature = "saturating_int_impl", issue = "87920")] +pub use saturating::Saturating; #[stable(feature = "rust1", since = "1.0.0")] pub use wrapping::Wrapping; diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs new file mode 100644 index 0000000000000..4f1937c68c7e2 --- /dev/null +++ b/library/core/src/num/saturating.rs @@ -0,0 +1,814 @@ +//! Definitions of `Saturating`. + +use crate::fmt; +use crate::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}; +use crate::ops::{BitXor, BitXorAssign, Div, DivAssign}; +use crate::ops::{Mul, MulAssign, Neg, Not}; +use crate::ops::{Sub, SubAssign}; + +/// Provides intentionally-wrapped arithmetic on `T`. +/// +/// Operations like `+` on `u32` values are intended to never overflow, +/// and in some debug configurations overflow is detected and results +/// in a panic. While most arithmetic falls into this category, some +/// code explicitly expects and relies upon modular arithmetic (e.g., +/// hashing). +/// +/// Saturating arithmetic can be achieved either through methods like +/// `saturating_add`, or through the `Saturating` type, which says that +/// all standard arithmetic operations on the underlying value are +/// intended to have saturating semantics. +/// +/// The underlying value can be retrieved through the `.0` index of the +/// `Saturating` tuple. +/// +/// # Examples +/// +/// ``` +/// #![feature(saturating_int_impl)] +/// use std::num::Saturating; +/// +/// let max = Saturating(u32::MAX); +/// let one = Saturating(1u32); +/// +/// assert_eq!(u32::MAX, (max + one).0); +/// ``` +#[unstable(feature = "saturating_int_impl", issue = "87920")] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)] +#[repr(transparent)] +pub struct Saturating(#[stable(feature = "rust1", since = "1.0.0")] pub T); + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Debug for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[stable(feature = "saturating_display", since = "1.10.0")] +impl fmt::Display for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[stable(feature = "saturating_fmt", since = "1.11.0")] +impl fmt::Binary for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[stable(feature = "saturating_fmt", since = "1.11.0")] +impl fmt::Octal for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[stable(feature = "saturating_fmt", since = "1.11.0")] +impl fmt::LowerHex for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[stable(feature = "saturating_fmt", since = "1.11.0")] +impl fmt::UpperHex for Saturating { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[allow(unused_macros)] +macro_rules! sh_impl_signed { + ($t:ident, $f:ident) => { + + }; +} + +macro_rules! sh_impl_unsigned { + ($t:ident, $f:ident) => { + + }; +} + +// FIXME (#23545): uncomment the remaining impls +macro_rules! sh_impl_all { + ($($t:ident)*) => ($( + //sh_impl_unsigned! { $t, u8 } + //sh_impl_unsigned! { $t, u16 } + //sh_impl_unsigned! { $t, u32 } + //sh_impl_unsigned! { $t, u64 } + //sh_impl_unsigned! { $t, u128 } + sh_impl_unsigned! { $t, usize } + + //sh_impl_signed! { $t, i8 } + //sh_impl_signed! { $t, i16 } + //sh_impl_signed! { $t, i32 } + //sh_impl_signed! { $t, i64 } + //sh_impl_signed! { $t, i128 } + //sh_impl_signed! { $t, isize } + )*) +} + +sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } + +// FIXME(30524): impl Op for Saturating, impl OpAssign for Saturating +macro_rules! saturating_impl { + ($($t:ty)*) => ($( + #[stable(feature = "rust1", since = "1.0.0")] + impl Add for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn add(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0.saturating_add(other.0)) + } + } + forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl AddAssign for Saturating<$t> { + #[inline] + fn add_assign(&mut self, other: Saturating<$t>) { + *self = *self + other; + } + } + forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "rust1", since = "1.0.0")] + impl Sub for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn sub(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0.saturating_sub(other.0)) + } + } + forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl SubAssign for Saturating<$t> { + #[inline] + fn sub_assign(&mut self, other: Saturating<$t>) { + *self = *self - other; + } + } + forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "rust1", since = "1.0.0")] + impl Mul for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn mul(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0.saturating_mul(other.0)) + } + } + forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl MulAssign for Saturating<$t> { + #[inline] + fn mul_assign(&mut self, other: Saturating<$t>) { + *self = *self * other; + } + } + forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "saturating_div", since = "1.3.0")] + 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>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl DivAssign for Saturating<$t> { + #[inline] + fn div_assign(&mut self, other: Saturating<$t>) { + *self = *self / other; + } + } + forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "rust1", since = "1.0.0")] + impl Not for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn not(self) -> Saturating<$t> { + Saturating(!self.0) + } + } + forward_ref_unop! { impl Not, not for Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "rust1", since = "1.0.0")] + impl BitXor for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn bitxor(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0 ^ other.0) + } + } + forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl BitXorAssign for Saturating<$t> { + #[inline] + fn bitxor_assign(&mut self, other: Saturating<$t>) { + *self = *self ^ other; + } + } + forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "rust1", since = "1.0.0")] + impl BitOr for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn bitor(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0 | other.0) + } + } + forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl BitOrAssign for Saturating<$t> { + #[inline] + fn bitor_assign(&mut self, other: Saturating<$t>) { + *self = *self | other; + } + } + forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "rust1", since = "1.0.0")] + impl BitAnd for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn bitand(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0 & other.0) + } + } + forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + #[stable(feature = "op_assign_traits", since = "1.8.0")] + impl BitAndAssign for Saturating<$t> { + #[inline] + fn bitand_assign(&mut self, other: Saturating<$t>) { + *self = *self & other; + } + } + forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t> } + + #[stable(feature = "saturating_neg", since = "1.45.0")] + impl Neg for Saturating<$t> { + type Output = Self; + #[inline] + fn neg(self) -> Self { + Saturating(0) - self + } + } + forward_ref_unop! { impl Neg, neg for Saturating<$t>, + #[stable(feature = "saturating_ref", since = "1.14.0")] } + + )*) +} + +saturating_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } + +macro_rules! saturating_int_impl { + ($($t:ty)*) => ($( + impl Saturating<$t> { + /// Returns the smallest value that can be represented by this integer type. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(>::MIN, Saturating(", stringify!($t), "::MIN));")] + /// ``` + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const MIN: Self = Self(<$t>::MIN); + + /// Returns the largest value that can be represented by this integer type. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(>::MAX, Saturating(", stringify!($t), "::MAX));")] + /// ``` + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const MAX: Self = Self(<$t>::MAX); + + /// Returns the size of this integer type in bits. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(>::BITS, ", stringify!($t), "::BITS);")] + /// ``` + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const BITS: u32 = <$t>::BITS; + + /// Returns the number of ones in the binary representation of `self`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0b01001100", stringify!($t), ");")] + /// + /// assert_eq!(n.count_ones(), 3); + /// ``` + #[inline] + #[doc(alias = "popcount")] + #[doc(alias = "popcnt")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn count_ones(self) -> u32 { + self.0.count_ones() + } + + /// Returns the number of zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(Saturating(!0", stringify!($t), ").count_zeros(), 0);")] + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn count_zeros(self) -> u32 { + self.0.count_zeros() + } + + /// Returns the number of trailing zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0b0101000", stringify!($t), ");")] + /// + /// assert_eq!(n.trailing_zeros(), 3); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn trailing_zeros(self) -> u32 { + self.0.trailing_zeros() + } + + /// Shifts the bits to the left by a specified amount, `n`, + /// saturating the truncated bits to the end of the resulting + /// integer. + /// + /// Please note this isn't the same operation as the `<<` shifting + /// operator! + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + /// let n: Saturating = Saturating(0x0123456789ABCDEF); + /// let m: Saturating = Saturating(-0x76543210FEDCBA99); + /// + /// assert_eq!(n.rotate_left(32), m); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn rotate_left(self, n: u32) -> Self { + Saturating(self.0.rotate_left(n)) + } + + /// Shifts the bits to the right by a specified amount, `n`, + /// saturating the truncated bits to the beginning of the resulting + /// integer. + /// + /// Please note this isn't the same operation as the `>>` shifting + /// operator! + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + /// let n: Saturating = Saturating(0x0123456789ABCDEF); + /// let m: Saturating = Saturating(-0xFEDCBA987654322); + /// + /// assert_eq!(n.rotate_right(4), m); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn rotate_right(self, n: u32) -> Self { + Saturating(self.0.rotate_right(n)) + } + + /// Reverses the byte order of the integer. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + /// let n: Saturating = Saturating(0b0000000_01010101); + /// assert_eq!(n, Saturating(85)); + /// + /// let m = n.swap_bytes(); + /// + /// assert_eq!(m, Saturating(0b01010101_00000000)); + /// assert_eq!(m, Saturating(21760)); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn swap_bytes(self) -> Self { + Saturating(self.0.swap_bytes()) + } + + /// Reverses the bit pattern of the integer. + /// + /// # Examples + /// + /// Please note that this example is shared between integer types. + /// Which explains why `i16` is used here. + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + /// let n = Saturating(0b0000000_01010101i16); + /// assert_eq!(n, Saturating(85)); + /// + /// let m = n.reverse_bits(); + /// + /// assert_eq!(m.0 as u16, 0b10101010_00000000); + /// assert_eq!(m, Saturating(-22016)); + /// ``` + #[stable(feature = "reverse_bits", since = "1.37.0")] + #[rustc_const_stable(feature = "const_reverse_bits", since = "1.37.0")] + #[inline] + #[must_use] + pub const fn reverse_bits(self) -> Self { + Saturating(self.0.reverse_bits()) + } + + /// Converts an integer from big endian to the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0x1A", stringify!($t), ");")] + /// + /// if cfg!(target_endian = "big") { + #[doc = concat!(" assert_eq!(>::from_be(n), n)")] + /// } else { + #[doc = concat!(" assert_eq!(>::from_be(n), n.swap_bytes())")] + /// } + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn from_be(x: Self) -> Self { + Saturating(<$t>::from_be(x.0)) + } + + /// Converts an integer from little endian to the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0x1A", stringify!($t), ");")] + /// + /// if cfg!(target_endian = "little") { + #[doc = concat!(" assert_eq!(>::from_le(n), n)")] + /// } else { + #[doc = concat!(" assert_eq!(>::from_le(n), n.swap_bytes())")] + /// } + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn from_le(x: Self) -> Self { + Saturating(<$t>::from_le(x.0)) + } + + /// Converts `self` to big endian from the target's endianness. + /// + /// On big endian this is a no-op. On little endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0x1A", stringify!($t), ");")] + /// + /// if cfg!(target_endian = "big") { + /// assert_eq!(n.to_be(), n) + /// } else { + /// assert_eq!(n.to_be(), n.swap_bytes()) + /// } + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn to_be(self) -> Self { + Saturating(self.0.to_be()) + } + + /// Converts `self` to little endian from the target's endianness. + /// + /// On little endian this is a no-op. On big endian the bytes are + /// swapped. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(0x1A", stringify!($t), ");")] + /// + /// if cfg!(target_endian = "little") { + /// assert_eq!(n.to_le(), n) + /// } else { + /// assert_eq!(n.to_le(), n.swap_bytes()) + /// } + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn to_le(self) -> Self { + Saturating(self.0.to_le()) + } + + /// Raises self to the power of `exp`, using exponentiation by squaring. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(Saturating(3", stringify!($t), ").pow(4), Saturating(81));")] + /// ``` + /// + /// Results that are too large are wrapped: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + /// assert_eq!(Saturating(3i8).pow(5), Saturating(127)); + /// assert_eq!(Saturating(3i8).pow(6), Saturating(127)); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub fn pow(self, exp: u32) -> Self { + Saturating(self.0.saturating_pow(exp)) + } + } + )*) +} + +saturating_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } + +macro_rules! saturating_int_impl_signed { + ($($t:ty)*) => ($( + impl Saturating<$t> { + /// Returns the number of leading zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / 4;")] + /// + /// assert_eq!(n.leading_zeros(), 3); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn leading_zeros(self) -> u32 { + self.0.leading_zeros() + } + + /// Computes the absolute value of `self`, saturating around at + /// the boundary of the type. + /// + /// The only case where such saturating can occur is when one takes the absolute value of the negative + /// minimal value for the type this is a positive value that is too large to represent in the type. In + /// such a case, this function returns `MIN` itself. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(Saturating(100", stringify!($t), ").abs(), Saturating(100));")] + #[doc = concat!("assert_eq!(Saturating(-100", stringify!($t), ").abs(), Saturating(100));")] + #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating(", stringify!($t), "::MIN));")] + /// assert_eq!(Saturating(-128i8).abs().0 as u8, 128u8); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub fn abs(self) -> Saturating<$t> { + Saturating(self.0.saturating_abs()) + } + + /// Returns a number representing sign of `self`. + /// + /// - `0` if the number is zero + /// - `1` if the number is positive + /// - `-1` if the number is negative + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(Saturating(10", stringify!($t), ").signum(), Saturating(1));")] + #[doc = concat!("assert_eq!(Saturating(0", stringify!($t), ").signum(), Saturating(0));")] + #[doc = concat!("assert_eq!(Saturating(-10", stringify!($t), ").signum(), Saturating(-1));")] + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub fn signum(self) -> Saturating<$t> { + Saturating(self.0.signum()) + } + + /// Returns `true` if `self` is positive and `false` if the number is zero or + /// negative. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert!(Saturating(10", stringify!($t), ").is_positive());")] + #[doc = concat!("assert!(!Saturating(-10", stringify!($t), ").is_positive());")] + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn is_positive(self) -> bool { + self.0.is_positive() + } + + /// Returns `true` if `self` is negative and `false` if the number is zero or + /// positive. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert!(Saturating(-10", stringify!($t), ").is_negative());")] + #[doc = concat!("assert!(!Saturating(10", stringify!($t), ").is_negative());")] + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn is_negative(self) -> bool { + self.0.is_negative() + } + } + )*) +} + +saturating_int_impl_signed! { isize i8 i16 i32 i64 i128 } + +macro_rules! saturating_int_impl_unsigned { + ($($t:ty)*) => ($( + impl Saturating<$t> { + /// Returns the number of leading zeros in the binary representation of `self`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / 4;")] + /// + /// assert_eq!(n.leading_zeros(), 2); + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub const fn leading_zeros(self) -> u32 { + self.0.leading_zeros() + } + + /// Returns `true` if and only if `self == 2^k` for some `k`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert!(Saturating(16", stringify!($t), ").is_power_of_two());")] + #[doc = concat!("assert!(!Saturating(10", stringify!($t), ").is_power_of_two());")] + /// ``` + #[inline] + #[unstable(feature = "saturating_int_impl", issue = "87920")] + pub fn is_power_of_two(self) -> bool { + self.0.is_power_of_two() + } + + } + )*) +} + +saturating_int_impl_unsigned! { usize u8 u16 u32 u64 u128 } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 861a6fc193cc8..818ca7df3e37a 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -308,6 +308,7 @@ #![feature(ptr_internals)] #![feature(rustc_attrs)] #![feature(rustc_private)] +#![feature(saturating_int_impl)] #![feature(slice_concat_ext)] #![feature(slice_internals)] #![feature(slice_ptr_get)] diff --git a/library/std/src/num.rs b/library/std/src/num.rs index e7051f0ce95b2..46064bd283770 100644 --- a/library/std/src/num.rs +++ b/library/std/src/num.rs @@ -12,6 +12,8 @@ mod tests; #[cfg(test)] mod benches; +#[unstable(feature = "saturating_int_impl", issue = "87920")] +pub use core::num::Saturating; #[stable(feature = "rust1", since = "1.0.0")] pub use core::num::Wrapping; #[stable(feature = "rust1", since = "1.0.0")] From 8841155ce5bf0f814243566aeb21e2ec355026c6 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 10 Aug 2021 19:51:49 +0200 Subject: [PATCH 02/20] Remove unused macros --- library/core/src/num/saturating.rs | 33 ------------------------------ 1 file changed, 33 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 4f1937c68c7e2..5a775dd99c986 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -80,39 +80,6 @@ impl fmt::UpperHex for Saturating { } } -#[allow(unused_macros)] -macro_rules! sh_impl_signed { - ($t:ident, $f:ident) => { - - }; -} - -macro_rules! sh_impl_unsigned { - ($t:ident, $f:ident) => { - - }; -} - -// FIXME (#23545): uncomment the remaining impls -macro_rules! sh_impl_all { - ($($t:ident)*) => ($( - //sh_impl_unsigned! { $t, u8 } - //sh_impl_unsigned! { $t, u16 } - //sh_impl_unsigned! { $t, u32 } - //sh_impl_unsigned! { $t, u64 } - //sh_impl_unsigned! { $t, u128 } - sh_impl_unsigned! { $t, usize } - - //sh_impl_signed! { $t, i8 } - //sh_impl_signed! { $t, i16 } - //sh_impl_signed! { $t, i32 } - //sh_impl_signed! { $t, i64 } - //sh_impl_signed! { $t, i128 } - //sh_impl_signed! { $t, isize } - )*) -} - -sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } // FIXME(30524): impl Op for Saturating, impl OpAssign for Saturating macro_rules! saturating_impl { From cdc90f9281251889156098f99ad9a42fc612fd77 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 10 Aug 2021 19:53:42 +0200 Subject: [PATCH 03/20] Rustfmt --- library/core/src/num/saturating.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 5a775dd99c986..9199dc6dece3a 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -80,7 +80,6 @@ impl fmt::UpperHex for Saturating { } } - // FIXME(30524): impl Op for Saturating, impl OpAssign for Saturating macro_rules! saturating_impl { ($($t:ty)*) => ($( From d4c9f76fd287698e4341c0e1137b1fda0d339743 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 10 Aug 2021 20:34:08 +0200 Subject: [PATCH 04/20] Fix missed tests --- library/core/src/num/saturating.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 9199dc6dece3a..6aaa5bf335a48 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -626,7 +626,7 @@ macro_rules! saturating_int_impl_signed { /// #![feature(saturating_int_impl)] /// use std::num::Saturating; /// - #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / 4;")] + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / Saturating(4", stringify!($t), ");")] /// /// assert_eq!(n.leading_zeros(), 3); /// ``` @@ -636,12 +636,8 @@ macro_rules! saturating_int_impl_signed { self.0.leading_zeros() } - /// Computes the absolute value of `self`, saturating around at - /// the boundary of the type. - /// - /// The only case where such saturating can occur is when one takes the absolute value of the negative - /// minimal value for the type this is a positive value that is too large to represent in the type. In - /// such a case, this function returns `MIN` itself. + /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self == MIN` + /// instead of overflowing. /// /// # Examples /// @@ -653,8 +649,9 @@ macro_rules! saturating_int_impl_signed { /// #[doc = concat!("assert_eq!(Saturating(100", stringify!($t), ").abs(), Saturating(100));")] #[doc = concat!("assert_eq!(Saturating(-100", stringify!($t), ").abs(), Saturating(100));")] - #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating(", stringify!($t), "::MIN));")] - /// assert_eq!(Saturating(-128i8).abs().0 as u8, 128u8); + #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating((", stringify!($t), "::MIN + 1).abs()));")] + #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating(", stringify!($t), "::MIN.saturating_abs()));")] + /// assert_eq!(Saturating(-128i8).abs().0 as u8, i8::MAX as u8); /// ``` #[inline] #[unstable(feature = "saturating_int_impl", issue = "87920")] @@ -744,7 +741,7 @@ macro_rules! saturating_int_impl_unsigned { /// #![feature(saturating_int_impl)] /// use std::num::Saturating; /// - #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / 4;")] + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / Saturating(4", stringify!($t), ");")] /// /// assert_eq!(n.leading_zeros(), 2); /// ``` From 631766c0555acad0571f761f948bc4b144620534 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 09:06:43 +0200 Subject: [PATCH 05/20] Make all the impls for Staturating unstable saturating_int_impl --- library/core/src/num/saturating.rs | 66 +++++++++++++++--------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 6aaa5bf335a48..4616656b1acc6 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -36,44 +36,44 @@ use crate::ops::{Sub, SubAssign}; #[unstable(feature = "saturating_int_impl", issue = "87920")] #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)] #[repr(transparent)] -pub struct Saturating(#[stable(feature = "rust1", since = "1.0.0")] pub T); +pub struct Saturating(#[unstable(feature = "saturating_int_impl", issue = "87920")] pub T); -#[stable(feature = "rust1", since = "1.0.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::Debug for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -#[stable(feature = "saturating_display", since = "1.10.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::Display for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -#[stable(feature = "saturating_fmt", since = "1.11.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::Binary for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -#[stable(feature = "saturating_fmt", since = "1.11.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::Octal for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -#[stable(feature = "saturating_fmt", since = "1.11.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::LowerHex for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } -#[stable(feature = "saturating_fmt", since = "1.11.0")] +#[unstable(feature = "saturating_int_impl", issue = "87920")] impl fmt::UpperHex for Saturating { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) @@ -83,7 +83,7 @@ impl fmt::UpperHex for Saturating { // FIXME(30524): impl Op for Saturating, impl OpAssign for Saturating macro_rules! saturating_impl { ($($t:ty)*) => ($( - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Add for Saturating<$t> { type Output = Saturating<$t>; @@ -93,9 +93,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl AddAssign for Saturating<$t> { #[inline] fn add_assign(&mut self, other: Saturating<$t>) { @@ -104,7 +104,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl AddAssign, add_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Sub for Saturating<$t> { type Output = Saturating<$t>; @@ -114,9 +114,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Sub, sub for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl SubAssign for Saturating<$t> { #[inline] fn sub_assign(&mut self, other: Saturating<$t>) { @@ -125,7 +125,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl SubAssign, sub_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Mul for Saturating<$t> { type Output = Saturating<$t>; @@ -135,9 +135,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Mul, mul for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl MulAssign for Saturating<$t> { #[inline] fn mul_assign(&mut self, other: Saturating<$t>) { @@ -146,7 +146,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "saturating_div", since = "1.3.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Div for Saturating<$t> { type Output = Saturating<$t>; @@ -157,9 +157,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl DivAssign for Saturating<$t> { #[inline] fn div_assign(&mut self, other: Saturating<$t>) { @@ -168,7 +168,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Not for Saturating<$t> { type Output = Saturating<$t>; @@ -178,9 +178,9 @@ macro_rules! saturating_impl { } } forward_ref_unop! { impl Not, not for Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitXor for Saturating<$t> { type Output = Saturating<$t>; @@ -190,9 +190,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitXor, bitxor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitXorAssign for Saturating<$t> { #[inline] fn bitxor_assign(&mut self, other: Saturating<$t>) { @@ -201,7 +201,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitOr for Saturating<$t> { type Output = Saturating<$t>; @@ -211,9 +211,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitOr, bitor for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitOrAssign for Saturating<$t> { #[inline] fn bitor_assign(&mut self, other: Saturating<$t>) { @@ -222,7 +222,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitAnd for Saturating<$t> { type Output = Saturating<$t>; @@ -232,9 +232,9 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl BitAnd, bitand for Saturating<$t>, Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } - #[stable(feature = "op_assign_traits", since = "1.8.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl BitAndAssign for Saturating<$t> { #[inline] fn bitand_assign(&mut self, other: Saturating<$t>) { @@ -243,7 +243,7 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t> } - #[stable(feature = "saturating_neg", since = "1.45.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Neg for Saturating<$t> { type Output = Self; #[inline] @@ -252,7 +252,7 @@ macro_rules! saturating_impl { } } forward_ref_unop! { impl Neg, neg for Saturating<$t>, - #[stable(feature = "saturating_ref", since = "1.14.0")] } + #[unstable(feature = "saturating_int_impl", issue = "87920")] } )*) } @@ -464,7 +464,7 @@ macro_rules! saturating_int_impl { /// assert_eq!(m.0 as u16, 0b10101010_00000000); /// assert_eq!(m, Saturating(-22016)); /// ``` - #[stable(feature = "reverse_bits", since = "1.37.0")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] #[rustc_const_stable(feature = "const_reverse_bits", since = "1.37.0")] #[inline] #[must_use] From 6cf4dd975b841a75388cbc4b53effc8d80faeb34 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 09:32:44 +0200 Subject: [PATCH 06/20] Remove mentioning of modular arithmetic --- library/core/src/num/saturating.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 4616656b1acc6..55c6a51f581a9 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -11,8 +11,7 @@ use crate::ops::{Sub, SubAssign}; /// Operations like `+` on `u32` values are intended to never overflow, /// and in some debug configurations overflow is detected and results /// in a panic. While most arithmetic falls into this category, some -/// code explicitly expects and relies upon modular arithmetic (e.g., -/// hashing). +/// code explicitly expects and relies upon saturating arithmetic. /// /// Saturating arithmetic can be achieved either through methods like /// `saturating_add`, or through the `Saturating` type, which says that @@ -93,7 +92,7 @@ macro_rules! saturating_impl { } } forward_ref_binop! { impl Add, add for Saturating<$t>, Saturating<$t>, - #[unstable(feature = "saturating_int_impl", issue = "87920")] + #[unstable(feature = "saturating_int_impl", issue = "87920")] } #[unstable(feature = "saturating_int_impl", issue = "87920")] impl AddAssign for Saturating<$t> { From 57dacfe4d14b7e26f627a94e675c936c8a981f24 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 11:25:03 +0200 Subject: [PATCH 07/20] Like in Wrapping use shift in doctest --- library/core/src/num/saturating.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 55c6a51f581a9..283fcaed9427d 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -625,7 +625,7 @@ macro_rules! saturating_int_impl_signed { /// #![feature(saturating_int_impl)] /// use std::num::Saturating; /// - #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / Saturating(4", stringify!($t), ");")] + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX >> 2);")] /// /// assert_eq!(n.leading_zeros(), 3); /// ``` @@ -740,7 +740,7 @@ macro_rules! saturating_int_impl_unsigned { /// #![feature(saturating_int_impl)] /// use std::num::Saturating; /// - #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX) / Saturating(4", stringify!($t), ");")] + #[doc = concat!("let n = Saturating(", stringify!($t), "::MAX >> 2);")] /// /// assert_eq!(n.leading_zeros(), 2); /// ``` From e240853dfc9c9e448bf32e8f2b059186e3cbd907 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 12:08:30 +0200 Subject: [PATCH 08/20] Replace doc test with doc macro call --- library/core/src/num/saturating.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 283fcaed9427d..9f013b71350ad 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -650,7 +650,7 @@ macro_rules! saturating_int_impl_signed { #[doc = concat!("assert_eq!(Saturating(-100", stringify!($t), ").abs(), Saturating(100));")] #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating((", stringify!($t), "::MIN + 1).abs()));")] #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating(", stringify!($t), "::MIN.saturating_abs()));")] - /// assert_eq!(Saturating(-128i8).abs().0 as u8, i8::MAX as u8); + #[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN).abs(), Saturating(", stringify!($t), "::MAX));")] /// ``` #[inline] #[unstable(feature = "saturating_int_impl", issue = "87920")] From 3f7d2ce28f8cf4dec56bf65fa2e6da0cf329ec55 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 12:26:10 +0200 Subject: [PATCH 09/20] Add naive shift implementation to Saturating --- library/core/src/num/saturating.rs | 159 ++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 9f013b71350ad..9f4d7104fa5ad 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -4,7 +4,7 @@ use crate::fmt; use crate::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}; use crate::ops::{BitXor, BitXorAssign, Div, DivAssign}; use crate::ops::{Mul, MulAssign, Neg, Not}; -use crate::ops::{Sub, SubAssign}; +use crate::ops::{Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign}; /// Provides intentionally-wrapped arithmetic on `T`. /// @@ -78,6 +78,127 @@ impl fmt::UpperHex for Saturating { self.0.fmt(f) } } +#[allow(unused_macros)] +macro_rules! sh_impl_signed { + ($t:ident, $f:ident) => { + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Shl<$f> for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn shl(self, other: $f) -> Saturating<$t> { + if other < 0 { + Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32)) + } else { + Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) + } + } + } + forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl ShlAssign<$f> for Saturating<$t> { + #[inline] + fn shl_assign(&mut self, other: $f) { + *self = *self << other; + } + } + forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Shr<$f> for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn shr(self, other: $f) -> Saturating<$t> { + if other < 0 { + Saturating(self.0.shl((-other & self::shift_max::$t as $f) as u32)) + } else { + Saturating(self.0.shr((other & self::shift_max::$t as $f) as u32)) + } + } + } + forward_ref_binop! { impl Shr, shr for Saturating<$t>, $f, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl ShrAssign<$f> for Saturating<$t> { + #[inline] + fn shr_assign(&mut self, other: $f) { + *self = *self >> other; + } + } + forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f } + }; +} + +macro_rules! sh_impl_unsigned { + ($t:ident, $f:ident) => { + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Shl<$f> for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn shl(self, other: $f) -> Saturating<$t> { + Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) + } + } + forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl ShlAssign<$f> for Saturating<$t> { + #[inline] + fn shl_assign(&mut self, other: $f) { + *self = *self << other; + } + } + forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Shr<$f> for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn shr(self, other: $f) -> Saturating<$t> { + Saturating(self.0.shr((other & self::shift_max::$t as $f) as u32)) + } + } + forward_ref_binop! { impl Shr, shr for Saturating<$t>, $f, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl ShrAssign<$f> for Saturating<$t> { + #[inline] + fn shr_assign(&mut self, other: $f) { + *self = *self >> other; + } + } + forward_ref_op_assign! { impl ShrAssign, shr_assign for Saturating<$t>, $f } + }; +} + +// FIXME (#23545): uncomment the remaining impls +macro_rules! sh_impl_all { + ($($t:ident)*) => ($( + //sh_impl_unsigned! { $t, u8 } + //sh_impl_unsigned! { $t, u16 } + //sh_impl_unsigned! { $t, u32 } + //sh_impl_unsigned! { $t, u64 } + //sh_impl_unsigned! { $t, u128 } + sh_impl_unsigned! { $t, usize } + + //sh_impl_signed! { $t, i8 } + //sh_impl_signed! { $t, i16 } + //sh_impl_signed! { $t, i32 } + //sh_impl_signed! { $t, i64 } + //sh_impl_signed! { $t, i128 } + //sh_impl_signed! { $t, isize } + )*) +} + +sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize } // FIXME(30524): impl Op for Saturating, impl OpAssign for Saturating macro_rules! saturating_impl { @@ -774,3 +895,39 @@ macro_rules! saturating_int_impl_unsigned { } saturating_int_impl_unsigned! { usize u8 u16 u32 u64 u128 } + +mod shift_max { + #![allow(non_upper_case_globals)] + + #[cfg(target_pointer_width = "16")] + mod platform { + pub const usize: u32 = super::u16; + pub const isize: u32 = super::i16; + } + + #[cfg(target_pointer_width = "32")] + mod platform { + pub const usize: u32 = super::u32; + pub const isize: u32 = super::i32; + } + + #[cfg(target_pointer_width = "64")] + mod platform { + pub const usize: u32 = super::u64; + pub const isize: u32 = super::i64; + } + + pub const i8: u32 = (1 << 3) - 1; + pub const i16: u32 = (1 << 4) - 1; + pub const i32: u32 = (1 << 5) - 1; + pub const i64: u32 = (1 << 6) - 1; + pub const i128: u32 = (1 << 7) - 1; + pub use self::platform::isize; + + pub const u8: u32 = i8; + pub const u16: u32 = i16; + pub const u32: u32 = i32; + pub const u64: u32 = i64; + pub const u128: u32 = i128; + pub use self::platform::usize; +} From 7861121ae441f94a03b2de8ac1e61ec4fbf1e4cf Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 12:28:30 +0200 Subject: [PATCH 10/20] Add naive remainder impl to Saturating --- library/core/src/num/saturating.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 9f4d7104fa5ad..b0a172c72d0bd 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -3,7 +3,7 @@ use crate::fmt; use crate::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}; use crate::ops::{BitXor, BitXorAssign, Div, DivAssign}; -use crate::ops::{Mul, MulAssign, Neg, Not}; +use crate::ops::{Mul, MulAssign, Neg, Not, Rem, RemAssign}; use crate::ops::{Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign}; /// Provides intentionally-wrapped arithmetic on `T`. @@ -288,6 +288,27 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl DivAssign, div_assign for Saturating<$t>, Saturating<$t> } + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Rem for Saturating<$t> { + type Output = Saturating<$t>; + + #[inline] + fn rem(self, other: Saturating<$t>) -> Saturating<$t> { + Saturating(self.0.rem(other.0)) + } + } + forward_ref_binop! { impl Rem, rem for Saturating<$t>, Saturating<$t>, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl RemAssign for Saturating<$t> { + #[inline] + fn rem_assign(&mut self, other: Saturating<$t>) { + *self = *self % other; + } + } + forward_ref_op_assign! { impl RemAssign, rem_assign for Saturating<$t>, Saturating<$t> } + #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Not for Saturating<$t> { type Output = Saturating<$t>; From f136eea97c8a1aff9814c4ec2779884bd870feed Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 12 Aug 2021 13:43:20 +0200 Subject: [PATCH 11/20] Implement Neg only for signed Saturating types --- library/core/src/num/saturating.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index b0a172c72d0bd..3bb668d9340b4 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -384,17 +384,6 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Saturating<$t>, Saturating<$t> } - #[unstable(feature = "saturating_int_impl", issue = "87920")] - impl Neg for Saturating<$t> { - type Output = Self; - #[inline] - fn neg(self) -> Self { - Saturating(0) - self - } - } - forward_ref_unop! { impl Neg, neg for Saturating<$t>, - #[unstable(feature = "saturating_int_impl", issue = "87920")] } - )*) } @@ -864,6 +853,17 @@ macro_rules! saturating_int_impl_signed { self.0.is_negative() } } + + #[unstable(feature = "saturating_int_impl", issue = "87920")] + impl Neg for Saturating<$t> { + type Output = Self; + #[inline] + fn neg(self) -> Self { + Saturating(self.0.saturating_neg()) + } + } + forward_ref_unop! { impl Neg, neg for Saturating<$t>, + #[unstable(feature = "saturating_int_impl", issue = "87920")] } )*) } From 8049230852676cda16fe4fb443b4e2b99be87cf8 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 09:59:05 +0200 Subject: [PATCH 12/20] Saturate negative division --- library/core/src/num/saturating.rs | 59 +++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 3bb668d9340b4..b06ed36689cda 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -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")] } )*) } From 742d450783cb9c9e7faef0f2dc3f09c72f7ff217 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 10:08:58 +0200 Subject: [PATCH 13/20] Add and use saturating_div instead of impl inside Saturating --- library/core/src/num/int_macros.rs | 26 ++++++++++++++++++++++++++ library/core/src/num/saturating.rs | 11 +---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 0bc646995c7c7..f5966a53ed26e 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -918,6 +918,32 @@ macro_rules! int_impl { } } + /// Saturating integer division. Computes `self / rhs`, saturating at the + /// numeric bounds instead of overflowing. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// + /// ``` + #[unstable(feature = "saturating_int_impl", issue = "87920")] + #[rustc_const_unstable(feature = "saturating_int_impl", issue = "87920")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub const fn saturating_div(self, rhs: Self) -> Self { + match self.checked_div(rhs) { + Some(x) => x, + None => if (self < 0) == (rhs < 0) { + Self::MAX + } else { + Self::MIN + } + } + } + /// Saturating integer exponentiation. Computes `self.pow(exp)`, /// saturating at the numeric bounds instead of overflowing. /// diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index b06ed36689cda..4b346409ebf0b 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -871,16 +871,7 @@ macro_rules! saturating_int_impl_signed { #[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) - } + Saturating(self.0.saturating_div(other.0)) } } forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>, From 6bb3acab74f9cf4bf3d3b81f0805416cd7b3ee20 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 11:06:08 +0200 Subject: [PATCH 14/20] Add doctests to and fix saturating_div for signed integer types --- library/core/src/num/int_macros.rs | 31 +++++++++++++++++++++--------- library/std/src/lib.rs | 1 + 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index f5966a53ed26e..7c36f4cdd203f 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -926,21 +926,34 @@ macro_rules! int_impl { /// Basic usage: /// /// ``` + /// #![feature(saturating_div)] + /// + #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")] /// /// ``` - #[unstable(feature = "saturating_int_impl", issue = "87920")] - #[rustc_const_unstable(feature = "saturating_int_impl", issue = "87920")] + /// + /// ```should_panic + /// #![feature(saturating_div)] + /// + #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")] + /// + /// ``` + #[unstable(feature = "saturating_div", issue = "87920")] + #[rustc_const_unstable(feature = "saturating_div", issue = "87920")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] pub const fn saturating_div(self, rhs: Self) -> Self { - match self.checked_div(rhs) { - Some(x) => x, - None => if (self < 0) == (rhs < 0) { - Self::MAX - } else { - Self::MIN - } + let (result, overflowed) = self.overflowing_div(rhs); + + if !overflowed { + result + } else if (self < 0) == (rhs < 0) { + Self::MAX + } else { + Self::MIN } } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 818ca7df3e37a..676ca5dea5d26 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -308,6 +308,7 @@ #![feature(ptr_internals)] #![feature(rustc_attrs)] #![feature(rustc_private)] +#![feature(saturating_div)] #![feature(saturating_int_impl)] #![feature(slice_concat_ext)] #![feature(slice_internals)] From a0e61e278057ffa3a6ca1d39fe04bf1569d3cf40 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 11:07:34 +0200 Subject: [PATCH 15/20] Add saturating_div to unsigned integer types --- library/core/src/num/uint_macros.rs | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index ae113a47e95d6..d6bd3115a0254 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1041,6 +1041,36 @@ macro_rules! uint_impl { } } + /// Saturating integer division. Computes `self / rhs`, saturating at the + /// numeric bounds instead of overflowing. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_div)] + /// + #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")] + /// + /// ``` + /// + /// ```should_panic + /// #![feature(saturating_div)] + /// + #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")] + /// + /// ``` + #[unstable(feature = "saturating_div", issue = "87920")] + #[rustc_const_unstable(feature = "saturating_div", issue = "87920")] + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[inline] + pub const fn saturating_div(self, rhs: Self) -> Self { + // on unsigned types, there is no overflow in integer division + self.wrapping_div(rhs) + } + /// Saturating integer exponentiation. Computes `self.pow(exp)`, /// saturating at the numeric bounds instead of overflowing. /// From 5ca6993307c926f1eb1e536bf957620685b31b00 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 11:07:53 +0200 Subject: [PATCH 16/20] Simplify Div impl for Saturating by using saturating_div --- library/core/src/num/saturating.rs | 68 ++++++++++++++---------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 4b346409ebf0b..2e84f0657131d 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -266,6 +266,37 @@ macro_rules! saturating_impl { } forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> } + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(saturating_int_impl, saturating_div)] + /// use std::num::Saturating; + /// + #[doc = concat!("assert_eq!(Saturating(2", stringify!($t), "), Saturating(5", stringify!($t), ") / Saturating(2));")] + #[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));")] + /// ``` + /// + /// ```should_panic + /// #![feature(saturating_int_impl, saturating_div)] + /// use std::num::Saturating; + /// + #[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")] + /// ``` + #[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.saturating_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] @@ -851,31 +882,6 @@ 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> { - Saturating(self.0.saturating_div(other.0)) - } - } - forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>, - #[unstable(feature = "saturating_int_impl", issue = "87920")] } )*) } @@ -924,18 +930,6 @@ 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")] } )*) } From 2b5970f993a091ad51624cdbfefee7dd97f65682 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Thu, 19 Aug 2021 11:28:33 +0200 Subject: [PATCH 17/20] Simplify saturating_div --- library/core/src/num/int_macros.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 7c36f4cdd203f..2c866812937e9 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -946,14 +946,9 @@ macro_rules! int_impl { without modifying the original"] #[inline] pub const fn saturating_div(self, rhs: Self) -> Self { - let (result, overflowed) = self.overflowing_div(rhs); - - if !overflowed { - result - } else if (self < 0) == (rhs < 0) { - Self::MAX - } else { - Self::MIN + match self.overflowing_div(rhs) { + (result, false) => result, + (_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow } } From acf0a0c3940e3b1589e93f868c4708f4f8c4f4eb Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Sat, 28 Aug 2021 13:28:35 +0200 Subject: [PATCH 18/20] Use wrapping shift for unsigned types --- library/core/src/num/saturating.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 2e84f0657131d..b2705ca0cc47b 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -141,7 +141,7 @@ macro_rules! sh_impl_unsigned { #[inline] fn shl(self, other: $f) -> Saturating<$t> { - Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) + Saturating(self.0.wrapping_shl(other as u32)) } } forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f, @@ -162,7 +162,7 @@ macro_rules! sh_impl_unsigned { #[inline] fn shr(self, other: $f) -> Saturating<$t> { - Saturating(self.0.shr((other & self::shift_max::$t as $f) as u32)) + Saturating(self.0.wrapping_shr(other as u32)) } } forward_ref_binop! { impl Shr, shr for Saturating<$t>, $f, From 977ae5ac2c5f0a53c8b0e979bd1b2450d66e1030 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Sat, 28 Aug 2021 13:29:19 +0200 Subject: [PATCH 19/20] Fix mentions of wrapping operations --- library/core/src/num/saturating.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index b2705ca0cc47b..75d7f24307e06 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -6,7 +6,7 @@ use crate::ops::{BitXor, BitXorAssign, Div, DivAssign}; use crate::ops::{Mul, MulAssign, Neg, Not, Rem, RemAssign}; use crate::ops::{Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign}; -/// Provides intentionally-wrapped arithmetic on `T`. +/// Provides intentionally-saturating arithmetic on `T`. /// /// Operations like `+` on `u32` values are intended to never overflow, /// and in some debug configurations overflow is detected and results @@ -741,7 +741,7 @@ macro_rules! saturating_int_impl { #[doc = concat!("assert_eq!(Saturating(3", stringify!($t), ").pow(4), Saturating(81));")] /// ``` /// - /// Results that are too large are wrapped: + /// Results that are too large are saturated: /// /// ``` /// #![feature(saturating_int_impl)] From ce636f25e55493f42e7339cb533cb523879b62a5 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Sat, 28 Aug 2021 13:39:09 +0200 Subject: [PATCH 20/20] Unimpl Shl{Assign} for signed Saturating types until the correct impl is clear --- library/core/src/num/saturating.rs | 122 +++++++++++++++-------------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/library/core/src/num/saturating.rs b/library/core/src/num/saturating.rs index 75d7f24307e06..f6dd3603c4914 100644 --- a/library/core/src/num/saturating.rs +++ b/library/core/src/num/saturating.rs @@ -81,30 +81,32 @@ impl fmt::UpperHex for Saturating { #[allow(unused_macros)] macro_rules! sh_impl_signed { ($t:ident, $f:ident) => { - #[unstable(feature = "saturating_int_impl", issue = "87920")] - impl Shl<$f> for Saturating<$t> { - type Output = Saturating<$t>; - - #[inline] - fn shl(self, other: $f) -> Saturating<$t> { - if other < 0 { - Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32)) - } else { - Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) - } - } - } - forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f, - #[unstable(feature = "saturating_int_impl", issue = "87920")] } - - #[unstable(feature = "saturating_int_impl", issue = "87920")] - impl ShlAssign<$f> for Saturating<$t> { - #[inline] - fn shl_assign(&mut self, other: $f) { - *self = *self << other; - } - } - forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } + // FIXME what is the correct implementation here? see discussion https://github.com/rust-lang/rust/pull/87921#discussion_r695870065 + // + // #[unstable(feature = "saturating_int_impl", issue = "87920")] + // impl Shl<$f> for Saturating<$t> { + // type Output = Saturating<$t>; + // + // #[inline] + // fn shl(self, other: $f) -> Saturating<$t> { + // if other < 0 { + // Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32)) + // } else { + // Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32)) + // } + // } + // } + // forward_ref_binop! { impl Shl, shl for Saturating<$t>, $f, + // #[unstable(feature = "saturating_int_impl", issue = "87920")] } + // + // #[unstable(feature = "saturating_int_impl", issue = "87920")] + // impl ShlAssign<$f> for Saturating<$t> { + // #[inline] + // fn shl_assign(&mut self, other: $f) { + // *self = *self << other; + // } + // } + // forward_ref_op_assign! { impl ShlAssign, shl_assign for Saturating<$t>, $f } #[unstable(feature = "saturating_int_impl", issue = "87920")] impl Shr<$f> for Saturating<$t> { @@ -935,38 +937,40 @@ macro_rules! saturating_int_impl_unsigned { saturating_int_impl_unsigned! { usize u8 u16 u32 u64 u128 } -mod shift_max { - #![allow(non_upper_case_globals)] - - #[cfg(target_pointer_width = "16")] - mod platform { - pub const usize: u32 = super::u16; - pub const isize: u32 = super::i16; - } - - #[cfg(target_pointer_width = "32")] - mod platform { - pub const usize: u32 = super::u32; - pub const isize: u32 = super::i32; - } - - #[cfg(target_pointer_width = "64")] - mod platform { - pub const usize: u32 = super::u64; - pub const isize: u32 = super::i64; - } - - pub const i8: u32 = (1 << 3) - 1; - pub const i16: u32 = (1 << 4) - 1; - pub const i32: u32 = (1 << 5) - 1; - pub const i64: u32 = (1 << 6) - 1; - pub const i128: u32 = (1 << 7) - 1; - pub use self::platform::isize; - - pub const u8: u32 = i8; - pub const u16: u32 = i16; - pub const u32: u32 = i32; - pub const u64: u32 = i64; - pub const u128: u32 = i128; - pub use self::platform::usize; -} +// Related to potential Shl and ShlAssign implementation +// +// mod shift_max { +// #![allow(non_upper_case_globals)] +// +// #[cfg(target_pointer_width = "16")] +// mod platform { +// pub const usize: u32 = super::u16; +// pub const isize: u32 = super::i16; +// } +// +// #[cfg(target_pointer_width = "32")] +// mod platform { +// pub const usize: u32 = super::u32; +// pub const isize: u32 = super::i32; +// } +// +// #[cfg(target_pointer_width = "64")] +// mod platform { +// pub const usize: u32 = super::u64; +// pub const isize: u32 = super::i64; +// } +// +// pub const i8: u32 = (1 << 3) - 1; +// pub const i16: u32 = (1 << 4) - 1; +// pub const i32: u32 = (1 << 5) - 1; +// pub const i64: u32 = (1 << 6) - 1; +// pub const i128: u32 = (1 << 7) - 1; +// pub use self::platform::isize; +// +// pub const u8: u32 = i8; +// pub const u16: u32 = i16; +// pub const u32: u32 = i32; +// pub const u64: u32 = i64; +// pub const u128: u32 = i128; +// pub use self::platform::usize; +// }