Skip to content

Commit fe1ac25

Browse files
committed
Optimize core::ptr::align_offset
- Pass mask as arg to mod_pow_2_inv instead of calculating it again.
1 parent f974aff commit fe1ac25

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libcore/ptr/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
10431043
/// Any questions go to @nagisa.
10441044
#[lang = "align_offset"]
10451045
pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
1046-
/// Calculate multiplicative modular inverse of `x` modulo `m = 2^mpow`.
1046+
/// Calculate multiplicative modular inverse of `x` modulo `m`, where
1047+
/// `m = 2^mpow` and `mask = m - 1`.
10471048
///
10481049
/// This implementation is tailored for align_offset and has following preconditions:
10491050
///
@@ -1052,7 +1053,7 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
10521053
///
10531054
/// Implementation of this function shall not panic. Ever.
10541055
#[inline]
1055-
fn mod_pow_2_inv(x: usize, mpow: usize) -> usize {
1056+
fn mod_pow_2_inv(x: usize, mpow: usize, mask: usize) -> usize {
10561057
/// Multiplicative modular inverse table modulo 2^4 = 16.
10571058
///
10581059
/// Note, that this table does not contain values where inverse does not exist (i.e., for
@@ -1064,7 +1065,6 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
10641065
const INV_TABLE_MOD: usize = 1 << INV_TABLE_MOD_POW;
10651066

10661067
let table_inverse = INV_TABLE_MOD_16[(x & (INV_TABLE_MOD - 1)) >> 1] as usize;
1067-
let mask = (1usize << mpow) - 1;
10681068

10691069
if mpow <= INV_TABLE_MOD_POW {
10701070
table_inverse & mask
@@ -1147,7 +1147,8 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
11471147
let a2minus1 = a2.wrapping_sub(1);
11481148
let s2 = smoda >> gcdpow;
11491149
let minusp2 = a2.wrapping_sub(pmoda >> gcdpow);
1150-
return (minusp2.wrapping_mul(mod_pow_2_inv(s2, apow.wrapping_sub(gcdpow)))) & a2minus1;
1150+
return (minusp2.wrapping_mul(mod_pow_2_inv(s2, apow.wrapping_sub(gcdpow), a2minus1)))
1151+
& a2minus1;
11511152
}
11521153

11531154
// Cannot be aligned at all.

0 commit comments

Comments
 (0)