Skip to content

Commit 91bec3a

Browse files
committed
NonZero<u*>::isqrt now calls u*::isqrt; add comments about crashing tests
1 parent 9f685e1 commit 91bec3a

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

library/core/src/num/int_macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,9 @@ macro_rules! int_impl {
15871587
};
15881588

15891589
// SAFETY: Inform the optimizer what the range of outputs is.
1590+
// If testing `core` crashes with no panic message and a
1591+
// `num::int_sqrt::i*` test failed, it's because your edits
1592+
// caused these assertions to become false.
15901593
//
15911594
// Integer square root is a monotonically nondecreasing
15921595
// function, which means that increasing the input will never

library/core/src/num/int_sqrt.rs

-25
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,6 @@ pub const fn u8(n: u8) -> u8 {
5050
U8_ISQRT_WITH_REMAINDER[n as usize].0
5151
}
5252

53-
/// Returns the [integer square root][1] of any [`usize`](prim@usize) input.
54-
///
55-
/// [1]: <https://en.wikipedia.org/wiki/Integer_square_root>
56-
/// "Wikipedia contributors. Integer square root. Wikipedia, The Free
57-
/// Encyclopedia."
58-
#[must_use = "this returns the result of the operation, \
59-
without modifying the original"]
60-
#[inline(always)]
61-
pub const fn usize(n: usize) -> usize {
62-
#[cfg(target_pointer_width = "16")]
63-
{
64-
u16(n as u16) as usize
65-
}
66-
67-
#[cfg(target_pointer_width = "32")]
68-
{
69-
u32(n as u32) as usize
70-
}
71-
72-
#[cfg(target_pointer_width = "64")]
73-
{
74-
u64(n as u64) as usize
75-
}
76-
}
77-
7853
/// Generates an `i*` function that returns the [integer square root][1] of any
7954
/// **nonnegative** input of a specific signed integer type.
8055
///

library/core/src/num/nonzero.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1544,27 +1544,26 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
15441544
#[must_use = "this returns the result of the operation, \
15451545
without modifying the original"]
15461546
pub const fn isqrt(self) -> Self {
1547-
let result = super::int_sqrt::$Int(self.get());
1547+
let result = self.get().isqrt();
15481548

15491549
// SAFETY: Inform the optimizer what the range of outputs is.
1550+
// If testing `core` crashes with no panic message and a
1551+
// `num::int_sqrt::u*` test failed, it's because your edits
1552+
// caused these assertions or the assertions in `fn isqrt` of
1553+
// `uint_macros.rs` to become false.
15501554
//
15511555
// Integer square root is a monotonically nondecreasing
15521556
// function, which means that increasing the input will never
15531557
// cause the output to decrease.
15541558
//
1555-
// The minimum input is 1. The maximum input is `<$Int>::MAX`.
1559+
// The minimum input is 1. When n is 1, sqrt(n) is 1. If n
1560+
// increases above 1, sqrt(n) can't decrease below 1, so sqrt(n)
1561+
// can't decrease below 1 no matter what n is.
15561562
//
1557-
// When n is 1, sqrt(n) is 1. If n increases above 1, sqrt(n)
1558-
// can't decrease below 1, so sqrt(n) can't decrease below 1 no
1559-
// matter what n is.
1560-
//
1561-
// When n is below `<$Int>::MAX`, sqrt(n) can't decrease at all
1562-
// when you increase n to `<$Int>::MAX`, so sqrt(n) can't be above
1563-
// sqrt(`<$Int>::MAX`) no matter what n is.
1563+
// The maximum possible output already has an assertion in
1564+
// `fn isqrt` of `uint_macros.rs`.
15641565
unsafe {
15651566
hint::assert_unchecked(result > 0);
1566-
const MAX_RESULT: $Int = super::int_sqrt::$Int(<$Int>::MAX);
1567-
hint::assert_unchecked(result <= MAX_RESULT);
15681567
}
15691568

15701569
// SAFETY: As explained above, the minimum integer square root is

library/core/src/num/uint_macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,10 @@ macro_rules! uint_impl {
26802680
let result = crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT;
26812681

26822682
// SAFETY: Inform the optimizer what the range of outputs is.
2683+
// If testing `core` crashes with no panic message and a
2684+
// `num::int_sqrt::u*` test failed, it's because your edits
2685+
// caused these assertions or the assertions in `fn isqrt` of
2686+
// `nonzero.rs` to become false.
26832687
//
26842688
// Integer square root is a monotonically nondecreasing
26852689
// function, which means that increasing the input will never

0 commit comments

Comments
 (0)