Skip to content

Commit d1d0e9f

Browse files
author
Lukas Markeffsky
committed
always use align_offset in is_aligned_to + add assembly test
1 parent 7c9d575 commit d1d0e9f

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

core/src/ptr/const_ptr.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ impl<T: ?Sized> *const T {
13211321
/// # }
13221322
/// ```
13231323
#[must_use]
1324+
#[inline]
13241325
#[stable(feature = "align_offset", since = "1.36.0")]
13251326
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
13261327
pub const fn align_offset(self, align: usize) -> usize
@@ -1562,19 +1563,11 @@ impl<T: ?Sized> *const T {
15621563
panic!("is_aligned_to: align is not a power-of-two")
15631564
}
15641565

1565-
#[inline]
1566-
fn runtime(ptr: *const u8, align: usize) -> bool {
1567-
ptr.addr() & (align - 1) == 0
1568-
}
1569-
1570-
// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
1571-
// slower than `ptr & (align - 1) == 0`
1572-
const fn comptime(ptr: *const u8, align: usize) -> bool {
1573-
ptr.align_offset(align) == 0
1574-
}
1575-
1576-
// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
1577-
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
1566+
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
1567+
// The cast to `()` is used to
1568+
// 1. deal with fat pointers; and
1569+
// 2. ensure that `align_offset` doesn't actually try to compute an offset.
1570+
self.cast::<()>().align_offset(align) == 0
15781571
}
15791572
}
15801573

core/src/ptr/mut_ptr.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ impl<T: ?Sized> *mut T {
15891589
/// # }
15901590
/// ```
15911591
#[must_use]
1592+
#[inline]
15921593
#[stable(feature = "align_offset", since = "1.36.0")]
15931594
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
15941595
pub const fn align_offset(self, align: usize) -> usize
@@ -1830,19 +1831,11 @@ impl<T: ?Sized> *mut T {
18301831
panic!("is_aligned_to: align is not a power-of-two")
18311832
}
18321833

1833-
#[inline]
1834-
fn runtime(ptr: *mut u8, align: usize) -> bool {
1835-
ptr.addr() & (align - 1) == 0
1836-
}
1837-
1838-
// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
1839-
// slower than `ptr & (align - 1) == 0`
1840-
const fn comptime(ptr: *mut u8, align: usize) -> bool {
1841-
ptr.align_offset(align) == 0
1842-
}
1843-
1844-
// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
1845-
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
1834+
// We can't use the address of `self` in a `const fn`, so we use `align_offset` instead.
1835+
// The cast to `()` is used to
1836+
// 1. deal with fat pointers; and
1837+
// 2. ensure that `align_offset` doesn't actually try to compute an offset.
1838+
self.cast::<()>().align_offset(align) == 0
18461839
}
18471840
}
18481841

0 commit comments

Comments
 (0)