Optimize pointer::is_aligned_to#100169
Conversation
|
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
|
(rust-highfive has picked a reviewer for you, use r? to override) |
workingjubilee
left a comment
There was a problem hiding this comment.
Sanity checked these are numerically equivalent in the Playground:
use std::iter::successors;
fn main() {
for addr in successors(Some(1u64), |x| x.checked_mul(2)) {
let mut i = successors(Some(1u64), |x| x.checked_mul(2));
while let Some(align) = i.next() && align <= addr {
for addr in successors(Some(addr), |x| x.checked_add(addr)).take(256) {
assert!(addr % align == addr & align - 1);
assert!(addr & align -1 == 0);
}
}
}
}|
@bors r+ rollup |
…iaskrgr Rollup of 5 pull requests Successful merges: - rust-lang#100071 (deps: dedupe `annotate-snippets` crate versions) - rust-lang#100127 (Remove Windows function preloading) - rust-lang#100130 (Avoid pointing out `return` span if it has nothing to do with type error) - rust-lang#100169 (Optimize `pointer::as_aligned_to`) - rust-lang#100175 (ascii -> ASCII in code comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
|
|
||
| // Cast is needed for `T: !Sized` | ||
| self.cast::<u8>().addr() % align == 0 | ||
| self.cast::<u8>().addr() & align - 1 == 0 |
There was a problem hiding this comment.
self.cast::<u8>().addr() & (align - 1) == 0 are easier to understand
pointer::as_aligned_topointer::is_aligned_to
This PR replaces
addr % alignwithaddr & align - 1, which is correct due toalignbeing a power of two.Here is a proof that this makes things better: [godbolt].
This PR also removes
assume(align != 0), with the new impl it does not improve anything anymore ([godbolt], [original concern]).