Skip to content

Commit df0bcfe

Browse files
author
Lukas Markeffsky
committed
address more review comments
* `cfg` only the body of `align_offset` * put explicit panics back * explain why `ptr.align_offset(align) == 0` is slow
1 parent 093c02e commit df0bcfe

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,21 +1323,6 @@ impl<T: ?Sized> *const T {
13231323
#[must_use]
13241324
#[stable(feature = "align_offset", since = "1.36.0")]
13251325
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
1326-
#[cfg(not(bootstrap))]
1327-
pub const fn align_offset(self, align: usize) -> usize
1328-
where
1329-
T: Sized,
1330-
{
1331-
assert!(align.is_power_of_two(), "align_offset: align is not a power-of-two");
1332-
1333-
// SAFETY: `align` has been checked to be a power of 2 above
1334-
unsafe { align_offset(self, align) }
1335-
}
1336-
1337-
#[stable(feature = "align_offset", since = "1.36.0")]
1338-
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
1339-
#[allow(missing_docs)]
1340-
#[cfg(bootstrap)]
13411326
pub const fn align_offset(self, align: usize) -> usize
13421327
where
13431328
T: Sized,
@@ -1346,21 +1331,30 @@ impl<T: ?Sized> *const T {
13461331
panic!("align_offset: align is not a power-of-two");
13471332
}
13481333

1349-
fn rt_impl<T>(p: *const T, align: usize) -> usize {
1350-
// SAFETY: `align` has been checked to be a power of 2 above
1351-
unsafe { align_offset(p, align) }
1352-
}
1334+
#[cfg(bootstrap)]
1335+
{
1336+
fn rt_impl<T>(p: *const T, align: usize) -> usize {
1337+
// SAFETY: `align` has been checked to be a power of 2 above
1338+
unsafe { align_offset(p, align) }
1339+
}
13531340

1354-
const fn ctfe_impl<T>(_: *const T, _: usize) -> usize {
1355-
usize::MAX
1341+
const fn ctfe_impl<T>(_: *const T, _: usize) -> usize {
1342+
usize::MAX
1343+
}
1344+
1345+
// SAFETY:
1346+
// It is permissible for `align_offset` to always return `usize::MAX`,
1347+
// algorithm correctness can not depend on `align_offset` returning non-max values.
1348+
//
1349+
// As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1350+
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
13561351
}
13571352

1358-
// SAFETY:
1359-
// It is permissible for `align_offset` to always return `usize::MAX`,
1360-
// algorithm correctness can not depend on `align_offset` returning non-max values.
1361-
//
1362-
// As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1363-
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
1353+
#[cfg(not(bootstrap))]
1354+
{
1355+
// SAFETY: `align` has been checked to be a power of 2 above
1356+
unsafe { align_offset(self, align) }
1357+
}
13641358
}
13651359

13661360
/// Returns whether the pointer is properly aligned for `T`.
@@ -1522,13 +1516,17 @@ impl<T: ?Sized> *const T {
15221516
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
15231517
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
15241518
pub const fn is_aligned_to(self, align: usize) -> bool {
1525-
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
1519+
if !align.is_power_of_two() {
1520+
panic!("is_aligned_to: align is not a power-of-two")
1521+
}
15261522

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

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

library/core/src/ptr/mut_ptr.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,21 +1591,6 @@ impl<T: ?Sized> *mut T {
15911591
#[must_use]
15921592
#[stable(feature = "align_offset", since = "1.36.0")]
15931593
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
1594-
#[cfg(not(bootstrap))]
1595-
pub const fn align_offset(self, align: usize) -> usize
1596-
where
1597-
T: Sized,
1598-
{
1599-
assert!(align.is_power_of_two(), "align_offset: align is not a power-of-two");
1600-
1601-
// SAFETY: `align` has been checked to be a power of 2 above
1602-
unsafe { align_offset(self, align) }
1603-
}
1604-
1605-
#[stable(feature = "align_offset", since = "1.36.0")]
1606-
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
1607-
#[allow(missing_docs)]
1608-
#[cfg(bootstrap)]
16091594
pub const fn align_offset(self, align: usize) -> usize
16101595
where
16111596
T: Sized,
@@ -1614,21 +1599,30 @@ impl<T: ?Sized> *mut T {
16141599
panic!("align_offset: align is not a power-of-two");
16151600
}
16161601

1617-
fn rt_impl<T>(p: *mut T, align: usize) -> usize {
1618-
// SAFETY: `align` has been checked to be a power of 2 above
1619-
unsafe { align_offset(p, align) }
1602+
#[cfg(bootstrap)]
1603+
{
1604+
fn rt_impl<T>(p: *mut T, align: usize) -> usize {
1605+
// SAFETY: `align` has been checked to be a power of 2 above
1606+
unsafe { align_offset(p, align) }
1607+
}
1608+
1609+
const fn ctfe_impl<T>(_: *mut T, _: usize) -> usize {
1610+
usize::MAX
1611+
}
1612+
1613+
// SAFETY:
1614+
// It is permissible for `align_offset` to always return `usize::MAX`,
1615+
// algorithm correctness can not depend on `align_offset` returning non-max values.
1616+
//
1617+
// As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1618+
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
16201619
}
16211620

1622-
const fn ctfe_impl<T>(_: *mut T, _: usize) -> usize {
1623-
usize::MAX
1621+
#[cfg(not(bootstrap))]
1622+
{
1623+
// SAFETY: `align` has been checked to be a power of 2 above
1624+
unsafe { align_offset(self, align) }
16241625
}
1625-
1626-
// SAFETY:
1627-
// It is permissible for `align_offset` to always return `usize::MAX`,
1628-
// algorithm correctness can not depend on `align_offset` returning non-max values.
1629-
//
1630-
// As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1631-
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
16321626
}
16331627

16341628
/// Returns whether the pointer is properly aligned for `T`.
@@ -1790,13 +1784,17 @@ impl<T: ?Sized> *mut T {
17901784
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
17911785
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
17921786
pub const fn is_aligned_to(self, align: usize) -> bool {
1793-
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
1787+
if !align.is_power_of_two() {
1788+
panic!("is_aligned_to: align is not a power-of-two")
1789+
}
17941790

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

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

0 commit comments

Comments
 (0)