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

Added unchecked_div and unchecked_rem to signed and unsigned numerical types #137598

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

madhav-madhusoodanan
Copy link
Contributor

Context

Adds unchecked_div and unchecked_rem to signed and unsigned numerical types in core.
This is an unstable addition.

Related Issue

@rustbot
Copy link
Collaborator

rustbot commented Feb 25, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 25, 2025
@madhav-madhusoodanan madhav-madhusoodanan changed the title [WIP] Added unchecked_div and unchecked_rem to signed and unsigned numerical types Added unchecked_div and unchecked_rem to signed and unsigned numerical types Feb 25, 2025
@rust-log-analyzer

This comment has been minimized.

);

// SAFETY: this is guaranteed to be safe by the caller.
unsafe { intrinsics::unchecked_div(self, rhs) }
Copy link
Contributor Author

@madhav-madhusoodanan madhav-madhusoodanan Feb 25, 2025

Choose a reason for hiding this comment

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

I am tempted to perform a safety check here.
Is it a good idea to do such a check for the other unchecked_div and unchecked_rem implementations too?

Suggested change
unsafe { intrinsics::unchecked_div(self, rhs) }
if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
self
} else {
// SAFETY: div by zero and by INT_MIN have been checked above
unsafe { intrinsics::unchecked_div(self, rhs) }
}

@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants