@@ -2327,23 +2327,33 @@ macro_rules! uint_impl {
2327
2327
( self . wrapping_sub( 1 ) ) & self == 0 && !( self == 0 )
2328
2328
}
2329
2329
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
2330
2335
fn round_up_to_one_less_than_a_power_of_two( self ) -> Self {
2331
2336
let bits = size_of:: <Self >( ) as u32 * 8 ;
2332
2337
let z = self . leading_zeros( ) ;
2333
2338
( if z == bits { 0 as Self } else { !0 } ) . wrapping_shr( z)
2334
2339
}
2335
2340
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
2336
2346
fn one_less_than_next_power_of_two( self ) -> Self {
2337
2347
self . wrapping_sub( 1 )
2338
2348
. round_up_to_one_less_than_a_power_of_two( )
2339
2349
. wrapping_add( if self == 0 { 1 } else { 0 } )
2340
2350
}
2341
2351
2342
2352
/// 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.
2345
2353
///
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).
2347
2357
///
2348
2358
/// # Examples
2349
2359
///
@@ -2353,8 +2363,6 @@ macro_rules! uint_impl {
2353
2363
/// assert_eq!(2u8.next_power_of_two(), 2);
2354
2364
/// assert_eq!(3u8.next_power_of_two(), 4);
2355
2365
/// ```
2356
- ///
2357
- /// [RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
2358
2366
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2359
2367
#[ inline]
2360
2368
pub fn next_power_of_two( self ) -> Self {
0 commit comments