Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement div_ceil for NonZero<unsigned> #132665

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,35 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
*self = *self % other;
}
}

impl NonZero<$Int> {
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
///
/// The result is guaranteed to be non-zero.
///
/// # Examples
///
/// ```
/// # #![feature(unsigned_nonzero_div_ceil)]
/// # use std::num::NonZero;
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
/// assert_eq!(one.div_ceil(max), one);
///
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
/// assert_eq!(three.div_ceil(two), two);
/// ```
#[unstable(feature = "unsigned_nonzero_div_ceil", issue = "none")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
let v = self.get().div_ceil(rhs.get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you inline div_ceil here? That will lead to unchecked operations being used for the division and remainder which might allow some more optimizations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like the following?

pub const fn div_ceil(self, rhs: Self) -> Self {
  let a = self.get();
  let b = rhs.get();
  let d = a / b;
  let r = a % b;
  let v = if r > 0 {
    d + 1
  } else {
    d
  };
  // SAFETY: ceiled division of two positive integers can never be zero.
  unsafe { Self::new_unchecked(v) }
}

Why would this allow more optimizations? They seem to produce the same ASM: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=735568e0474e49fa0a1b86d773a486d0

// SAFETY: ceiled division of two positive integers can never be zero.
unsafe { Self::new_unchecked(v) }
}
}
};
// Impls for signed nonzero types only.
(signed $Int:ty) => {
Expand Down
Loading