@@ -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;
0 commit comments