@@ -1043,7 +1043,8 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
1043
1043
/// Any questions go to @nagisa.
1044
1044
#[ lang = "align_offset" ]
1045
1045
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`.
1047
1048
///
1048
1049
/// This implementation is tailored for align_offset and has following preconditions:
1049
1050
///
@@ -1052,7 +1053,7 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
1052
1053
///
1053
1054
/// Implementation of this function shall not panic. Ever.
1054
1055
#[ 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 {
1056
1057
/// Multiplicative modular inverse table modulo 2^4 = 16.
1057
1058
///
1058
1059
/// 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 {
1064
1065
const INV_TABLE_MOD : usize = 1 << INV_TABLE_MOD_POW ;
1065
1066
1066
1067
let table_inverse = INV_TABLE_MOD_16 [ ( x & ( INV_TABLE_MOD - 1 ) ) >> 1 ] as usize ;
1067
- let mask = ( 1usize << mpow) - 1 ;
1068
1068
1069
1069
if mpow <= INV_TABLE_MOD_POW {
1070
1070
table_inverse & mask
@@ -1147,7 +1147,8 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
1147
1147
let a2minus1 = a2. wrapping_sub ( 1 ) ;
1148
1148
let s2 = smoda >> gcdpow;
1149
1149
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;
1151
1152
}
1152
1153
1153
1154
// Cannot be aligned at all.
0 commit comments