Skip to content

Commit

Permalink
core:: -> crate:: and # Safety docs for macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaiTRex committed Aug 19, 2024
1 parent 6af4863 commit d376ac8
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion library/core/src/num/int_sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ macro_rules! unsigned_fn {
}

/// Generates the first stage of the computation after normalization.
///
/// # Safety
///
/// `$n` must be nonzero.
macro_rules! first_stage {
($original_bits:literal, $n:ident) => {{
const N_SHIFT: u32 = $original_bits - 8;
Expand All @@ -169,6 +173,10 @@ macro_rules! first_stage {
}

/// Generates a middle stage of the computation.
///
/// # Safety
///
/// `$s` must be nonzero.
macro_rules! middle_stage {
($original_bits:literal, $ty:ty, $n:ident, $s:ident, $r:ident) => {{
// Inform the optimizer that `$s` is nonzero. This will allow it to
Expand Down Expand Up @@ -219,14 +227,18 @@ macro_rules! middle_stage {
}

/// Generates the last stage of the computation before denormalization.
///
/// # Safety
///
/// `$s` must be nonzero.
macro_rules! last_stage {
($ty:ty, $n:ident, $s:ident, $r:ident) => {{
// Inform the optimizer that `$s` is nonzero. This will allow it to
// avoid generating code to handle division-by-zero panics in the
// division below.
//
// SAFETY: See the proof in the `middle_stage` macro above.
unsafe { core::hint::assert_unchecked($s != 0) };
unsafe { crate::hint::assert_unchecked($s != 0) };

const HALF_BITS: u32 = <$ty>::BITS >> 1;
const QUARTER_BITS: u32 = <$ty>::BITS >> 2;
Expand All @@ -248,6 +260,10 @@ macro_rules! last_stage {

/// Generates the stages of the computation between normalization and
/// denormalization for [`u16`](prim@u16).
///
/// # Safety
///
/// `$n` must be nonzero.
macro_rules! u16_stages {
($n:ident) => {{
let (s, r) = first_stage!(16, $n);
Expand All @@ -257,6 +273,10 @@ macro_rules! u16_stages {

/// Generates the stages of the computation between normalization and
/// denormalization for [`u32`](prim@u32).
///
/// # Safety
///
/// `$n` must be nonzero.
macro_rules! u32_stages {
($n:ident) => {{
let (s, r) = first_stage!(32, $n);
Expand All @@ -267,6 +287,10 @@ macro_rules! u32_stages {

/// Generates the stages of the computation between normalization and
/// denormalization for [`u64`](prim@u64).
///
/// # Safety
///
/// `$n` must be nonzero.
macro_rules! u64_stages {
($n:ident) => {{
let (s, r) = first_stage!(64, $n);
Expand All @@ -278,6 +302,10 @@ macro_rules! u64_stages {

/// Generates the stages of the computation between normalization and
/// denormalization for [`u128`](prim@u128).
///
/// # Safety
///
/// `$n` must be nonzero.
macro_rules! u128_stages {
($n:ident) => {{
let (s, r) = first_stage!(128, $n);
Expand Down

0 comments on commit d376ac8

Please sign in to comment.