From fbcafb85c587cf20bd6550cfb6510c3b20c4c61a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 5 Aug 2022 17:11:47 +0400 Subject: [PATCH] Optimize `pointer::as_aligned_to` --- core/src/ptr/const_ptr.rs | 5 +---- core/src/ptr/mut_ptr.rs | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/core/src/ptr/const_ptr.rs b/core/src/ptr/const_ptr.rs index e0655d68d..2cd6e23c7 100644 --- a/core/src/ptr/const_ptr.rs +++ b/core/src/ptr/const_ptr.rs @@ -1336,11 +1336,8 @@ impl *const T { panic!("is_aligned_to: align is not a power-of-two"); } - // SAFETY: `is_power_of_two()` will return `false` for zero. - unsafe { core::intrinsics::assume(align != 0) }; - // Cast is needed for `T: !Sized` - self.cast::().addr() % align == 0 + self.cast::().addr() & align - 1 == 0 } } diff --git a/core/src/ptr/mut_ptr.rs b/core/src/ptr/mut_ptr.rs index fc3dd2a9b..56ad5f765 100644 --- a/core/src/ptr/mut_ptr.rs +++ b/core/src/ptr/mut_ptr.rs @@ -1614,11 +1614,8 @@ impl *mut T { panic!("is_aligned_to: align is not a power-of-two"); } - // SAFETY: `is_power_of_two()` will return `false` for zero. - unsafe { core::intrinsics::assume(align != 0) }; - // Cast is needed for `T: !Sized` - self.cast::().addr() % align == 0 + self.cast::().addr() & align - 1 == 0 } }