Skip to content

Commit 2159c5d

Browse files
authored
Rollup merge of #88582 - jhpratt:int_roundings, r=joshtriplett
Implement #88581 See #88581 for details. This API was discussed on Zulip. `@rustbot` label: +T-libs-api +S-waiting-on-review r? `@joshtriplett`
2 parents 73162aa + 727a4fc commit 2159c5d

File tree

5 files changed

+346
-0
lines changed

5 files changed

+346
-0
lines changed

library/core/src/num/int_macros.rs

+167
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,173 @@ macro_rules! int_impl {
18341834
}
18351835
}
18361836

1837+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1838+
///
1839+
/// # Panics
1840+
///
1841+
/// This function will panic if `rhs` is 0 or the division results in overflow.
1842+
///
1843+
/// # Examples
1844+
///
1845+
/// Basic usage:
1846+
///
1847+
/// ```
1848+
/// #![feature(int_roundings)]
1849+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
1850+
/// let b = 3;
1851+
///
1852+
/// assert_eq!(a.div_floor(b), 2);
1853+
/// assert_eq!(a.div_floor(-b), -3);
1854+
/// assert_eq!((-a).div_floor(b), -3);
1855+
/// assert_eq!((-a).div_floor(-b), 2);
1856+
/// ```
1857+
#[unstable(feature = "int_roundings", issue = "88581")]
1858+
#[must_use = "this returns the result of the operation, \
1859+
without modifying the original"]
1860+
#[inline]
1861+
#[rustc_inherit_overflow_checks]
1862+
pub const fn div_floor(self, rhs: Self) -> Self {
1863+
let d = self / rhs;
1864+
let r = self % rhs;
1865+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1866+
d - 1
1867+
} else {
1868+
d
1869+
}
1870+
}
1871+
1872+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1873+
///
1874+
/// # Panics
1875+
///
1876+
/// This function will panic if `rhs` is 0 or the division results in overflow.
1877+
///
1878+
/// # Examples
1879+
///
1880+
/// Basic usage:
1881+
///
1882+
/// ```
1883+
/// #![feature(int_roundings)]
1884+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
1885+
/// let b = 3;
1886+
///
1887+
/// assert_eq!(a.div_ceil(b), 3);
1888+
/// assert_eq!(a.div_ceil(-b), -2);
1889+
/// assert_eq!((-a).div_ceil(b), -2);
1890+
/// assert_eq!((-a).div_ceil(-b), 3);
1891+
/// ```
1892+
#[unstable(feature = "int_roundings", issue = "88581")]
1893+
#[must_use = "this returns the result of the operation, \
1894+
without modifying the original"]
1895+
#[inline]
1896+
#[rustc_inherit_overflow_checks]
1897+
pub const fn div_ceil(self, rhs: Self) -> Self {
1898+
let d = self / rhs;
1899+
let r = self % rhs;
1900+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
1901+
d + 1
1902+
} else {
1903+
d
1904+
}
1905+
}
1906+
1907+
/// If `rhs` is positive, calculates the smallest value greater than or
1908+
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1909+
/// calculates the largest value less than or equal to `self` that is a
1910+
/// multiple of `rhs`.
1911+
///
1912+
/// # Panics
1913+
///
1914+
/// This function will panic if `rhs` is 0 or the operation results in overflow.
1915+
///
1916+
/// # Examples
1917+
///
1918+
/// Basic usage:
1919+
///
1920+
/// ```
1921+
/// #![feature(int_roundings)]
1922+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
1923+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
1924+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
1925+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
1926+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
1927+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
1928+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
1929+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
1930+
/// ```
1931+
#[unstable(feature = "int_roundings", issue = "88581")]
1932+
#[must_use = "this returns the result of the operation, \
1933+
without modifying the original"]
1934+
#[inline]
1935+
#[rustc_inherit_overflow_checks]
1936+
pub const fn next_multiple_of(self, rhs: Self) -> Self {
1937+
// This would otherwise fail when calculating `r` when self == T::MIN.
1938+
if rhs == -1 {
1939+
return self;
1940+
}
1941+
1942+
let r = self % rhs;
1943+
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1944+
r + rhs
1945+
} else {
1946+
r
1947+
};
1948+
1949+
if m == 0 {
1950+
self
1951+
} else {
1952+
self + (rhs - m)
1953+
}
1954+
}
1955+
1956+
/// If `rhs` is positive, calculates the smallest value greater than or
1957+
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1958+
/// calculates the largest value less than or equal to `self` that is a
1959+
/// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
1960+
/// would result in overflow.
1961+
///
1962+
/// # Examples
1963+
///
1964+
/// Basic usage:
1965+
///
1966+
/// ```
1967+
/// #![feature(int_roundings)]
1968+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
1969+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
1970+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
1971+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
1972+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
1973+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
1974+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
1975+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
1976+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
1977+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
1978+
/// ```
1979+
#[unstable(feature = "int_roundings", issue = "88581")]
1980+
#[must_use = "this returns the result of the operation, \
1981+
without modifying the original"]
1982+
#[inline]
1983+
#[rustc_inherit_overflow_checks]
1984+
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1985+
// This would otherwise fail when calculating `r` when self == T::MIN.
1986+
if rhs == -1 {
1987+
return Some(self);
1988+
}
1989+
1990+
let r = try_opt!(self.checked_rem(rhs));
1991+
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1992+
try_opt!(r.checked_add(rhs))
1993+
} else {
1994+
r
1995+
};
1996+
1997+
if m == 0 {
1998+
Some(self)
1999+
} else {
2000+
self.checked_add(try_opt!(rhs.checked_sub(m)))
2001+
}
2002+
}
2003+
18372004
/// Returns the logarithm of the number with respect to an arbitrary base.
18382005
///
18392006
/// This method might not be optimized owing to implementation details;

library/core/src/num/uint_macros.rs

+104
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,110 @@ macro_rules! uint_impl {
18481848
self % rhs
18491849
}
18501850

1851+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1852+
///
1853+
/// This is the same as performing `self / rhs` for all unsigned integers.
1854+
///
1855+
/// # Panics
1856+
///
1857+
/// This function will panic if `rhs` is 0.
1858+
///
1859+
/// # Examples
1860+
///
1861+
/// Basic usage:
1862+
///
1863+
/// ```
1864+
/// #![feature(int_roundings)]
1865+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".div_floor(4), 1);")]
1866+
/// ```
1867+
#[unstable(feature = "int_roundings", issue = "88581")]
1868+
#[inline(always)]
1869+
#[rustc_inherit_overflow_checks]
1870+
pub const fn div_floor(self, rhs: Self) -> Self {
1871+
self / rhs
1872+
}
1873+
1874+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1875+
///
1876+
/// # Panics
1877+
///
1878+
/// This function will panic if `rhs` is 0.
1879+
///
1880+
/// # Examples
1881+
///
1882+
/// Basic usage:
1883+
///
1884+
/// ```
1885+
/// #![feature(int_roundings)]
1886+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".div_ceil(4), 2);")]
1887+
/// ```
1888+
#[unstable(feature = "int_roundings", issue = "88581")]
1889+
#[inline]
1890+
#[rustc_inherit_overflow_checks]
1891+
pub const fn div_ceil(self, rhs: Self) -> Self {
1892+
let d = self / rhs;
1893+
let r = self % rhs;
1894+
if r > 0 && rhs > 0 {
1895+
d + 1
1896+
} else {
1897+
d
1898+
}
1899+
}
1900+
1901+
/// Calculates the smallest value greater than or equal to `self` that
1902+
/// is a multiple of `rhs`.
1903+
///
1904+
/// # Panics
1905+
///
1906+
/// This function will panic if `rhs` is 0 or the operation results in overflow.
1907+
///
1908+
/// # Examples
1909+
///
1910+
/// Basic usage:
1911+
///
1912+
/// ```
1913+
/// #![feature(int_roundings)]
1914+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
1915+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
1916+
/// ```
1917+
#[unstable(feature = "int_roundings", issue = "88581")]
1918+
#[must_use = "this returns the result of the operation, \
1919+
without modifying the original"]
1920+
#[inline]
1921+
#[rustc_inherit_overflow_checks]
1922+
pub const fn next_multiple_of(self, rhs: Self) -> Self {
1923+
match self % rhs {
1924+
0 => self,
1925+
r => self + (rhs - r)
1926+
}
1927+
}
1928+
1929+
/// Calculates the smallest value greater than or equal to `self` that
1930+
/// is a multiple of `rhs`. If `rhs` is negative,
1931+
///
1932+
/// # Examples
1933+
///
1934+
/// Basic usage:
1935+
///
1936+
/// ```
1937+
/// #![feature(int_roundings)]
1938+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
1939+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
1940+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
1941+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
1942+
/// ```
1943+
#[unstable(feature = "int_roundings", issue = "88581")]
1944+
#[must_use = "this returns the result of the operation, \
1945+
without modifying the original"]
1946+
#[inline]
1947+
#[rustc_inherit_overflow_checks]
1948+
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1949+
match try_opt!(self.checked_rem(rhs)) {
1950+
0 => Some(self),
1951+
r => self.checked_add(try_opt!(rhs.checked_sub(r)))
1952+
}
1953+
}
1954+
18511955
/// Returns `true` if and only if `self == 2^k` for some `k`.
18521956
///
18531957
/// # Examples

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#![feature(unsized_tuple_coercion)]
6565
#![feature(const_option)]
6666
#![feature(integer_atomics)]
67+
#![feature(int_roundings)]
6768
#![feature(slice_group_by)]
6869
#![feature(trusted_random_access)]
6970
#![feature(unsize)]

library/core/tests/num/int_macros.rs

+49
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,55 @@ macro_rules! int_module {
289289
assert_eq!(r.saturating_pow(3), -8 as $T);
290290
assert_eq!(r.saturating_pow(0), 1 as $T);
291291
}
292+
293+
#[test]
294+
fn test_div_floor() {
295+
let a: $T = 8;
296+
let b = 3;
297+
assert_eq!(a.div_floor(b), 2);
298+
assert_eq!(a.div_floor(-b), -3);
299+
assert_eq!((-a).div_floor(b), -3);
300+
assert_eq!((-a).div_floor(-b), 2);
301+
}
302+
303+
#[test]
304+
fn test_div_ceil() {
305+
let a: $T = 8;
306+
let b = 3;
307+
assert_eq!(a.div_ceil(b), 3);
308+
assert_eq!(a.div_ceil(-b), -2);
309+
assert_eq!((-a).div_ceil(b), -2);
310+
assert_eq!((-a).div_ceil(-b), 3);
311+
}
312+
313+
#[test]
314+
fn test_next_multiple_of() {
315+
assert_eq!((16 as $T).next_multiple_of(8), 16);
316+
assert_eq!((23 as $T).next_multiple_of(8), 24);
317+
assert_eq!((16 as $T).next_multiple_of(-8), 16);
318+
assert_eq!((23 as $T).next_multiple_of(-8), 16);
319+
assert_eq!((-16 as $T).next_multiple_of(8), -16);
320+
assert_eq!((-23 as $T).next_multiple_of(8), -16);
321+
assert_eq!((-16 as $T).next_multiple_of(-8), -16);
322+
assert_eq!((-23 as $T).next_multiple_of(-8), -24);
323+
assert_eq!(MIN.next_multiple_of(-1), MIN);
324+
}
325+
326+
#[test]
327+
fn test_checked_next_multiple_of() {
328+
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
329+
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
330+
assert_eq!((16 as $T).checked_next_multiple_of(-8), Some(16));
331+
assert_eq!((23 as $T).checked_next_multiple_of(-8), Some(16));
332+
assert_eq!((-16 as $T).checked_next_multiple_of(8), Some(-16));
333+
assert_eq!((-23 as $T).checked_next_multiple_of(8), Some(-16));
334+
assert_eq!((-16 as $T).checked_next_multiple_of(-8), Some(-16));
335+
assert_eq!((-23 as $T).checked_next_multiple_of(-8), Some(-24));
336+
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
337+
assert_eq!(MAX.checked_next_multiple_of(2), None);
338+
assert_eq!(MIN.checked_next_multiple_of(-3), None);
339+
assert_eq!(MIN.checked_next_multiple_of(-1), Some(MIN));
340+
}
292341
}
293342
};
294343
}

library/core/tests/num/uint_macros.rs

+25
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,31 @@ macro_rules! uint_module {
205205
assert_eq!(r.overflowing_pow(2), (1 as $T, true));
206206
assert_eq!(r.saturating_pow(2), MAX);
207207
}
208+
209+
#[test]
210+
fn test_div_floor() {
211+
assert_eq!((8 as $T).div_floor(3), 2);
212+
}
213+
214+
#[test]
215+
fn test_div_ceil() {
216+
assert_eq!((8 as $T).div_ceil(3), 3);
217+
}
218+
219+
#[test]
220+
fn test_next_multiple_of() {
221+
assert_eq!((16 as $T).next_multiple_of(8), 16);
222+
assert_eq!((23 as $T).next_multiple_of(8), 24);
223+
assert_eq!(MAX.next_multiple_of(1), MAX);
224+
}
225+
226+
#[test]
227+
fn test_checked_next_multiple_of() {
228+
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
229+
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
230+
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
231+
assert_eq!(MAX.checked_next_multiple_of(2), None);
232+
}
208233
}
209234
};
210235
}

0 commit comments

Comments
 (0)