Skip to content

Improved assumptions relating to isqrt#155265

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Apersoma:isqrt-smarter
Apr 23, 2026
Merged

Improved assumptions relating to isqrt#155265
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Apersoma:isqrt-smarter

Conversation

@Apersoma
Copy link
Copy Markdown
Contributor

@Apersoma Apersoma commented Apr 13, 2026

View all comments

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?

Comment thread library/core/src/num/imp/int_sqrt.rs Outdated
@Apersoma
Copy link
Copy Markdown
Contributor 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 thread library/core/src/num/imp/int_sqrt.rs Outdated
Comment thread library/core/src/num/imp/int_sqrt.rs Outdated
Comment thread library/core/src/num/imp/int_sqrt.rs Outdated
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fn foo(x: u32) -> (u32, u32) {
    let sqrt = smart_sqrt(x);
    let a = x - sqrt;
    let b = x - sqrt*sqrt;
    (a, b)
}

Overflow checks will be emitted when computing a regardless of crate::hint::assert_unchecked(result * result <= $n) and overflow checks will be emitted when computing b regardless of crate::hint::assert_unchecked(result <= $n) so both are necessary.

@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.

@Apersoma
Copy link
Copy Markdown
Contributor Author

@rustbot ready

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

This comment has been minimized.

@Apersoma
Copy link
Copy Markdown
Contributor Author

i have no idea how my computer missed the missing e but the tidy should pass now

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 21, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 21, 2026
Improved assumptions relating to isqrt
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 21, 2026

☀️ Try build successful (CI)
Build commit: c201a78 (c201a78525f5ec530e7598347c60d8a87df77f88, parent: 84c11900724736a2b9afac4822031d78a9ed8e86)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c201a78): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.6% [2.4%, 2.8%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.3%, secondary -3.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.3% [2.0%, 2.5%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.5% [-4.0%, -2.3%] 7
All ❌✅ (primary) 2.3% [2.0%, 2.5%] 2

Binary size

Results (primary -0.1%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 55
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 24
All ❌✅ (primary) -0.1% [-0.1%, -0.0%] 55

Bootstrap: 490.276s -> 490.941s (0.14%)
Artifact size: 394.36 MiB -> 394.31 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 21, 2026
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 21, 2026

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 21, 2026

📌 Commit 7b59818 has been approved by jhpratt

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 21, 2026
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.

Sorry sorry, I had some thoughts that I meant to post. Thank you Jacob for taking a look!

Also, please squash!

View changes since this review

Comment thread library/core/src/num/uint_macros.rs Outdated
Comment thread library/core/src/num/uint_macros.rs
Comment thread library/core/src/num/uint_macros.rs Outdated
Comment thread library/core/src/num/uint_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs
Comment thread library/core/src/num/int_macros.rs Outdated
Comment thread library/core/src/num/int_macros.rs
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 21, 2026
@tgross35
Copy link
Copy Markdown
Contributor

@bors r-

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 22, 2026

⚠️ Warning ⚠️

@rust-log-analyzer

This comment has been minimized.

@Apersoma
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 22, 2026
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.

Looks great, thank you!

@bors r=jhpratt,tgross35

View changes since this review

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 23, 2026

📌 Commit e722ba5 has been approved by jhpratt,tgross35

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2026
@tgross35
Copy link
Copy Markdown
Contributor

Since perf didn't seem to report anything,
@bors rollup=maybe

rust-bors Bot pushed a commit that referenced this pull request Apr 23, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #146544 (mir-opt: Remove the workaround in UnreachableEnumBranching)
 - #154819 (Fix ICE for inherent associated type mismatches)
 - #155265 (Improved assumptions relating to isqrt)
 - #152576 (c-variadic: use `emit_ptr_va_arg` for  mips)
 - #154481 (Mark a function only used in nightly as nightly only)
 - #155614 (c-variadic: rename `VaList::arg` to `VaList::next_arg`)
 - #155630 (Make `//@ skip-filecheck` a normal compiletest directive)
 - #155641 (Remove non-working code for "running" mir-opt tests)
 - #155652 (Expand `Path::is_empty` docs)
 - #155656 (rustc_llvm: update opt-level handling for LLVM 23)
@rust-bors rust-bors Bot merged commit 04bcb39 into rust-lang:main Apr 23, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 23, 2026
rust-timer added a commit that referenced this pull request Apr 23, 2026
Rollup merge of #155265 - Apersoma:isqrt-smarter, r=jhpratt,tgross35

Improved assumptions relating to isqrt

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`
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Apr 24, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#146544 (mir-opt: Remove the workaround in UnreachableEnumBranching)
 - rust-lang/rust#154819 (Fix ICE for inherent associated type mismatches)
 - rust-lang/rust#155265 (Improved assumptions relating to isqrt)
 - rust-lang/rust#152576 (c-variadic: use `emit_ptr_va_arg` for  mips)
 - rust-lang/rust#154481 (Mark a function only used in nightly as nightly only)
 - rust-lang/rust#155614 (c-variadic: rename `VaList::arg` to `VaList::next_arg`)
 - rust-lang/rust#155630 (Make `//@ skip-filecheck` a normal compiletest directive)
 - rust-lang/rust#155641 (Remove non-working code for "running" mir-opt tests)
 - rust-lang/rust#155652 (Expand `Path::is_empty` docs)
 - rust-lang/rust#155656 (rustc_llvm: update opt-level handling for LLVM 23)
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 24, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#146544 (mir-opt: Remove the workaround in UnreachableEnumBranching)
 - rust-lang/rust#154819 (Fix ICE for inherent associated type mismatches)
 - rust-lang/rust#155265 (Improved assumptions relating to isqrt)
 - rust-lang/rust#152576 (c-variadic: use `emit_ptr_va_arg` for  mips)
 - rust-lang/rust#154481 (Mark a function only used in nightly as nightly only)
 - rust-lang/rust#155614 (c-variadic: rename `VaList::arg` to `VaList::next_arg`)
 - rust-lang/rust#155630 (Make `//@ skip-filecheck` a normal compiletest directive)
 - rust-lang/rust#155641 (Remove non-working code for "running" mir-opt tests)
 - rust-lang/rust#155652 (Expand `Path::is_empty` docs)
 - rust-lang/rust#155656 (rustc_llvm: update opt-level handling for LLVM 23)
@Apersoma Apersoma deleted the isqrt-smarter branch April 24, 2026 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants