Skip to content

Commit 22c06e4

Browse files
committed
Use unchecked_div/rem
1 parent f15f99f commit 22c06e4

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/mem/x86_64.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// feature is present at compile-time. We don't bother detecting other features.
1717
// Note that ERMSB does not enhance the backwards (DF=1) "rep movsb".
1818

19+
use core::intrinsics;
1920
use core::mem;
2021

2122
#[inline(always)]
@@ -110,22 +111,22 @@ pub unsafe fn compare_bytes(a: *const u8, b: *const u8, n: usize) -> i32 {
110111
U: Clone + Copy + Eq,
111112
F: FnOnce(*const U, *const U, usize) -> i32,
112113
{
113-
// Just to be sure we're actually working with powers of two...
114-
let _ = const { 1 - mem::size_of::<T>().count_ones() }; // <= 1
115-
let _ = const { mem::size_of::<T>().count_ones() - 1 }; // >= 1
114+
// Ensure T is not a ZST.
115+
const { assert!(mem::size_of::<T>() != 0) };
116116

117-
// This should be equivalent to division with power-of-two sizes, except the former
118-
// somehow still leaves a call to panic because ??
119-
let end = a.add(n >> mem::size_of::<T>().trailing_zeros());
117+
let end = a.add(intrinsics::unchecked_div(n, mem::size_of::<T>()));
120118
while a != end {
121119
if a.read_unaligned() != b.read_unaligned() {
122120
return f(a.cast(), b.cast(), mem::size_of::<T>());
123121
}
124122
a = a.add(1);
125123
b = b.add(1);
126124
}
127-
// Ditto
128-
f(a.cast(), b.cast(), n & (mem::size_of::<T>() - 1))
125+
f(
126+
a.cast(),
127+
b.cast(),
128+
intrinsics::unchecked_rem(n, mem::size_of::<T>()),
129+
)
129130
}
130131
let c1 = |mut a: *const u8, mut b: *const u8, n| {
131132
for _ in 0..n {

0 commit comments

Comments
 (0)