Skip to content

Commit

Permalink
always use align_offset in is_aligned_to + add assembly test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Nov 19, 2022
1 parent 4696e89 commit daccb8c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 26 deletions.
19 changes: 6 additions & 13 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ impl<T: ?Sized> *const T {
/// # }
/// ```
#[must_use]
#[inline]
#[stable(feature = "align_offset", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
pub const fn align_offset(self, align: usize) -> usize
Expand Down Expand Up @@ -1562,19 +1563,11 @@ impl<T: ?Sized> *const T {
panic!("is_aligned_to: align is not a power-of-two")
}

#[inline]
fn runtime(ptr: *const u8, align: usize) -> bool {
ptr.addr() & (align - 1) == 0
}

// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
// slower than `ptr & (align - 1) == 0`
const fn comptime(ptr: *const u8, align: usize) -> bool {
ptr.align_offset(align) == 0
}

// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
// The cast to `()` is used to
// 1. deal with fat pointers; and
// 2. ensure that `align_offset` doesn't actually try to compute an offset.
self.cast::<()>().align_offset(align) == 0
}
}

Expand Down
19 changes: 6 additions & 13 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ impl<T: ?Sized> *mut T {
/// # }
/// ```
#[must_use]
#[inline]
#[stable(feature = "align_offset", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
pub const fn align_offset(self, align: usize) -> usize
Expand Down Expand Up @@ -1830,19 +1831,11 @@ impl<T: ?Sized> *mut T {
panic!("is_aligned_to: align is not a power-of-two")
}

#[inline]
fn runtime(ptr: *mut u8, align: usize) -> bool {
ptr.addr() & (align - 1) == 0
}

// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
// slower than `ptr & (align - 1) == 0`
const fn comptime(ptr: *mut u8, align: usize) -> bool {
ptr.align_offset(align) == 0
}

// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
// The cast to `()` is used to
// 1. deal with fat pointers; and
// 2. ensure that `align_offset` doesn't actually try to compute an offset.
self.cast::<()>().align_offset(align) == 0
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/test/assembly/is_aligned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// assembly-output: emit-asm
// min-llvm-version: 14.0
// only-x86_64
// revisions: opt-speed opt-size
// [opt-speed] compile-flags: -Copt-level=1
// [opt-size] compile-flags: -Copt-level=s
#![crate_type="rlib"]

#![feature(core_intrinsics)]
#![feature(pointer_is_aligned)]

// CHECK-LABEL: is_aligned_to_unchecked
// CHECK: decq %rsi
// CHECK-NEXT: testq %rdi, %rsi
// CHECK-NEXT: sete %al
// CHECK-NEXT: retq
#[no_mangle]
pub unsafe fn is_aligned_to_unchecked(ptr: *const u8, align: usize) -> bool {
unsafe {
std::intrinsics::assume(align.is_power_of_two())
}
ptr.is_aligned_to(align)
}

// CHECK-LABEL: is_aligned_1
// CHECK: movb $1, %al
// CHECK-NEXT: retq
#[no_mangle]
pub fn is_aligned_1(ptr: *const u8) -> bool {
ptr.is_aligned()
}

// CHECK-LABEL: is_aligned_2
// CHECK: testb $1, %dil
// CHECK-NEXT: sete %al
// CHECK-NEXT: retq
#[no_mangle]
pub fn is_aligned_2(ptr: *const u16) -> bool {
ptr.is_aligned()
}

// CHECK-LABEL: is_aligned_4
// CHECK: testb $3, %dil
// CHECK-NEXT: sete %al
// CHECK-NEXT: retq
#[no_mangle]
pub fn is_aligned_4(ptr: *const u32) -> bool {
ptr.is_aligned()
}

// CHECK-LABEL: is_aligned_8
// CHECK: testb $7, %dil
// CHECK-NEXT: sete %al
// CHECK-NEXT: retq
#[no_mangle]
pub fn is_aligned_8(ptr: *const u64) -> bool {
ptr.is_aligned()
}

0 comments on commit daccb8c

Please sign in to comment.