Skip to content

Improved assumptions relating to isqrt#155265

Open
Apersoma wants to merge 1 commit intorust-lang:mainfrom
Apersoma:isqrt-smarter
Open

Improved assumptions relating to isqrt#155265
Apersoma wants to merge 1 commit intorust-lang:mainfrom
Apersoma:isqrt-smarter

Conversation

@Apersoma
Copy link
Copy Markdown

@Apersoma Apersoma commented Apr 13, 2026

Improved various assumptions relating to values yielded by isqrt.
Does not solve but does improve #132763.

Re-openeing of #154115

Added assumptions are:

  • if x is nonzero then x.isqrt() is nonzero
  • x.isqrt() <= x
  • x.isqrt() * x.isqrt() <= x

@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 Apr 13, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, scottmcm

@tgross35
Copy link
Copy Markdown
Contributor

r? tgross35

@rustbot rustbot assigned tgross35 and unassigned jhpratt Apr 14, 2026
@tgross35
Copy link
Copy Markdown
Contributor

Could you include a godbolt link or before+after asm outputs indicating what all improves here?

// Since these are integers, the first implies the second
// (though the compiler is too stupid to realize this).
unsafe {
crate::hint::assert_unchecked(result * result <= $n);
Copy link
Copy Markdown
Contributor

@Rudxain Rudxain Apr 14, 2026

Choose a reason for hiding this comment

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

View changes since the review

It might be a good idea to use unchecked_mul instead of *

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

View changes since the review

It might be a good idea to use unchecked_mul instead of *

It doesn't change anything in my testing, but I'll do that since small changes to the compiler might end up affecting that. It also shouldn't have any effects if the compiler is smart enough (because of the result <= isqrt(MAX)), but so should the one right below it and that one does anyway.

@Apersoma
Copy link
Copy Markdown
Author

Apersoma commented Apr 14, 2026

Could you include a godbolt link or before+after asm outputs indicating what all improves here?

examples

Without overflow checks the difference isn't nearly as large but it is an improvement.

@tgross35

Copy link
Copy Markdown
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Accidentally reviewed at the wrong PR, copied the comments over from #154115 (comment).

View changes since this review

Comment on lines +318 to +323
/// Takes the isqrt of `$n` assuming its non-negative.
///
/// # Safety:
///
/// `$n` must be nonnegative
macro_rules! isqrt_of_nonnegative {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't this all live in the uint implementations, then signed integers cast to unsigned (after the negative check) and call that? To avoid the extra macro here.

MAX_RESULT is different but that can just be an additional (narrower) assertion for signed integers. And you can do the >=0 assertion there since it's not meaningful for unsigned.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, I'll change it

Comment on lines +349 to +351
if $n > 0 {
crate::hint::assert_unchecked(result > 0);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is part of the macro's precondition, no?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

the macro's precondition is $n >= 0.

Comment on lines +363 to +364
crate::hint::assert_unchecked(result * result <= $n);
crate::hint::assert_unchecked(result <= $n);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Aren't these two redundant? If not, could you show an asm case indicating that?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 14, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

5 participants