Skip to content

Commit 03c8beb

Browse files
committed
Fix panic not being optimized out.
I don't know why it isn't being optimized out though, which worries me.
1 parent 6c1aded commit 03c8beb

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(compiler_builtins)]
77
#![feature(core_ffi_c)]
88
#![feature(core_intrinsics)]
9+
#![feature(inline_const)]
910
#![feature(lang_items)]
1011
#![feature(linkage)]
1112
#![feature(naked_functions)]

src/mem/x86_64.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,20 @@ pub unsafe fn compare_bytes(a: *const u8, b: *const u8, n: usize) -> i32 {
110110
U: Clone + Copy + Eq,
111111
F: FnOnce(*const U, *const U, usize) -> i32,
112112
{
113-
for _ in 0..n / mem::size_of::<T>() {
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
116+
// This should be equivalent to division with power-of-two sizes, except the former
117+
// somehow still leaves a call to panic because ??
118+
for _ in 0..n >> mem::size_of::<T>().trailing_zeros() {
114119
if a.read_unaligned() != b.read_unaligned() {
115120
return f(a.cast(), b.cast(), mem::size_of::<T>());
116121
}
117122
a = a.add(1);
118123
b = b.add(1);
119124
}
120-
f(a.cast(), b.cast(), n % mem::size_of::<T>())
125+
// Ditto
126+
f(a.cast(), b.cast(), n & (mem::size_of::<T>() - 1))
121127
}
122128
let c1 = |mut a: *const u8, mut b: *const u8, n| {
123129
for _ in 0..n {

0 commit comments

Comments
 (0)