Skip to content

Commit 5a5d62a

Browse files
committed
Optimize ptr.is_aligned_to()
Apparently LLVM is unable to understand that if count_ones() == 1 then self != 0. Adding `assume(align != 0)` helps generating better asm: https://rust.godbolt.org/z/ja18YKq91
1 parent 6c1ebff commit 5a5d62a

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

library/core/src/ptr/const_ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,9 @@ impl<T: ?Sized> *const T {
13201320
panic!("is_aligned_to: align is not a power-of-two");
13211321
}
13221322

1323+
// SAFETY: `is_power_of_two()` will return `false` for zero.
1324+
unsafe { core::intrinsics::assume(align != 0) };
1325+
13231326
// Cast is needed for `T: !Sized`
13241327
self.cast::<u8>().addr() % align == 0
13251328
}

library/core/src/ptr/mut_ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,9 @@ impl<T: ?Sized> *mut T {
15891589
panic!("is_aligned_to: align is not a power-of-two");
15901590
}
15911591

1592+
// SAFETY: `is_power_of_two()` will return `false` for zero.
1593+
unsafe { core::intrinsics::assume(align != 0) };
1594+
15921595
// Cast is needed for `T: !Sized`
15931596
self.cast::<u8>().addr() % align == 0
15941597
}

0 commit comments

Comments
 (0)