From 1ba421505037fe8c53a8f13967abf96a7e5f02ec Mon Sep 17 00:00:00 2001 From: Lokathor Date: Tue, 5 Sep 2023 14:08:36 -0600 Subject: [PATCH] Create `align_offset` feature so that we can continue to work on 1.34 --- Cargo.toml | 15 +++++++++++---- src/internal.rs | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a82e8b..e6d77a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,19 @@ extern_crate_alloc = [] extern_crate_std = ["extern_crate_alloc"] zeroable_maybe_uninit = [] zeroable_atomics = [] -min_const_generics = [] -wasm_simd = [] # Until >= 1.54.0 is MSRV this is an off-by-default feature. -must_cast = [] # Until >= 1.57.0 is MSRV this is an off-by-default feature. + +# Causes MSRV 1.36, use `align_offset` method instead of casting to `usize` to +# check alignment of pointers, this *may* improve codegen in some cases (but it +# has never been formally benchmarked!) +align_offset = [] + +min_const_generics = [] # Causes MSRV 1.51 + +wasm_simd = [] # Until >= 1.54.0 is MSRV this is an off-by-default feature. +must_cast = [] # Until >= 1.57.0 is MSRV this is an off-by-default feature. aarch64_simd = [] # Until >= 1.59.0 is MSRV this is an off-by-default feature. -# Do not use if you can avoid it, because this is unsound. +# Do not use if you can avoid it, because this is **unsound**!!!! unsound_ptr_pod_impl = [] # NOT SEMVER SUPPORTED! TEMPORARY ONLY! diff --git a/src/internal.rs b/src/internal.rs index be8c309..3ede50f 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -136,11 +136,18 @@ pub(crate) unsafe fn pod_read_unaligned(bytes: &[u8]) -> T { /// * If `align` is not a power of two. This includes when `align` is zero. #[inline] pub(crate) fn is_aligned_to(ptr: *const (), align: usize) -> bool { - // This is in a way better than `ptr as usize % align == 0`, - // because casting a pointer to an integer has the side effect that it exposes - // the pointer's provenance, which may theoretically inhibit some compiler - // optimizations. - ptr.align_offset(align) == 0 + #[cfg(feature = "align_offset")] + { + // This is in a way better than `ptr as usize % align == 0`, + // because casting a pointer to an integer has the side effect that it + // exposes the pointer's provenance, which may theoretically inhibit + // some compiler optimizations. + ptr.align_offset(align) == 0 + } + #[cfg(not(feature = "align_offset"))] + { + ((ptr as usize) % align) == 0 + } } /// Re-interprets `&[u8]` as `&T`.