Skip to content

Commit 93219a2

Browse files
committed
Add comments to explain helper functions
1 parent ed24829 commit 93219a2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/libcore/num/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,23 +2327,33 @@ macro_rules! uint_impl {
23272327
(self.wrapping_sub(1)) & self == 0 && !(self == 0)
23282328
}
23292329

2330+
// Returns one less than next greater power of two.
2331+
// (For 8u8 next greater power of two is 16u8 and for 6u8 it is 8u8)
2332+
//
2333+
// 8u8.round_up_to_one_less_than_a_power_of_two() == 15
2334+
// 6u8.round_up_to_one_less_than_a_power_of_two() == 7
23302335
fn round_up_to_one_less_than_a_power_of_two(self) -> Self {
23312336
let bits = size_of::<Self>() as u32 * 8;
23322337
let z = self.leading_zeros();
23332338
(if z == bits { 0 as Self } else { !0 }).wrapping_shr(z)
23342339
}
23352340

2341+
// Returns one less than next power of two.
2342+
// (For 8u8 next power of two is 8u8 and for 6u8 it is 8u8)
2343+
//
2344+
// 8u8.one_less_than_next_power_of_two() == 7
2345+
// 6u8.one_less_than_next_power_of_two() == 7
23362346
fn one_less_than_next_power_of_two(self) -> Self {
23372347
self.wrapping_sub(1)
23382348
.round_up_to_one_less_than_a_power_of_two()
23392349
.wrapping_add(if self == 0 { 1 } else { 0 })
23402350
}
23412351

23422352
/// Returns the smallest power of two greater than or equal to `self`.
2343-
/// When return value overflows, it panics in debug mode and return
2344-
/// value is wrapped in release mode.
23452353
///
2346-
/// More details about overflow behavior can be found in [RFC 560].
2354+
/// When return value overflows (i.e. `self > (1 << (N-1))` for type
2355+
/// `uN`), it panics in debug mode and return value is wrapped to 0 in
2356+
/// release mode (the only situation in which method can return 0).
23472357
///
23482358
/// # Examples
23492359
///
@@ -2353,8 +2363,6 @@ macro_rules! uint_impl {
23532363
/// assert_eq!(2u8.next_power_of_two(), 2);
23542364
/// assert_eq!(3u8.next_power_of_two(), 4);
23552365
/// ```
2356-
///
2357-
/// [RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
23582366
#[stable(feature = "rust1", since = "1.0.0")]
23592367
#[inline]
23602368
pub fn next_power_of_two(self) -> Self {

0 commit comments

Comments
 (0)