Skip to content

Commit e835d0d

Browse files
committed
Optimize core::ptr::align_offset
- Stopping condition inside mod_inv can be >= instead of > - Remove intrinsics::unchecked_rem, we are working modulu powers-of-2 so we can simply mask
1 parent f43c34a commit e835d0d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/libcore/ptr/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
10831083
// anyway.
10841084
inverse = inverse.wrapping_mul(2usize.wrapping_sub(x.wrapping_mul(inverse)))
10851085
& (going_mod - 1);
1086-
if going_mod > m {
1086+
if going_mod >= m {
10871087
return inverse & (m - 1);
10881088
}
10891089
going_mod = going_mod.wrapping_mul(going_mod);
@@ -1134,7 +1134,7 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
11341134
// to take the result $o mod lcm(s, a)$. We can replace $lcm(s, a)$ with just a $a / g$.
11351135
let j = a.wrapping_sub(pmoda) >> gcdpow;
11361136
let k = smoda >> gcdpow;
1137-
return intrinsics::unchecked_rem(j.wrapping_mul(mod_inv(k, a)), a >> gcdpow);
1137+
return (j.wrapping_mul(mod_inv(k, a))) & ((a >> gcdpow).wrapping_sub(1));
11381138
}
11391139

11401140
// Cannot be aligned at all.

0 commit comments

Comments
 (0)