@@ -1262,6 +1262,7 @@ macro_rules! uint_impl {
1262
1262
( $SelfT: ty, $ActualT: ty, $BITS: expr,
1263
1263
$ctpop: path,
1264
1264
$ctlz: path,
1265
+ $ctlz_nonzero: path,
1265
1266
$cttz: path,
1266
1267
$bswap: path,
1267
1268
$add_with_overflow: path,
@@ -2184,6 +2185,7 @@ macro_rules! uint_impl {
2184
2185
// This method cannot overflow, as in the `next_power_of_two`
2185
2186
// overflow cases it instead ends up returning the maximum value
2186
2187
// of the type, and can return 0 for 0.
2188
+ #[ inline]
2187
2189
fn one_less_than_next_power_of_two( self ) -> Self {
2188
2190
if self <= 1 { return 0 ; }
2189
2191
@@ -2192,7 +2194,7 @@ macro_rules! uint_impl {
2192
2194
// (such as intel pre-haswell) have more efficient ctlz
2193
2195
// intrinsics when the argument is non-zero.
2194
2196
let p = self - 1 ;
2195
- let z = p . leading_zeros ( ) ;
2197
+ let z = unsafe { $ctlz_nonzero ( p ) } ;
2196
2198
<$SelfT>:: max_value( ) >> z
2197
2199
}
2198
2200
@@ -2236,11 +2238,17 @@ macro_rules! uint_impl {
2236
2238
}
2237
2239
}
2238
2240
2241
+ #[ cfg( stage0) ]
2242
+ unsafe fn ctlz_nonzero < T > ( x : T ) -> T { intrinsics:: ctlz ( x) }
2243
+ #[ cfg( not( stage0) ) ]
2244
+ unsafe fn ctlz_nonzero < T > ( x : T ) -> T { intrinsics:: ctlz_nonzero ( x) }
2245
+
2239
2246
#[ lang = "u8" ]
2240
2247
impl u8 {
2241
2248
uint_impl ! { u8 , u8 , 8 ,
2242
2249
intrinsics:: ctpop,
2243
2250
intrinsics:: ctlz,
2251
+ ctlz_nonzero,
2244
2252
intrinsics:: cttz,
2245
2253
intrinsics:: bswap,
2246
2254
intrinsics:: add_with_overflow,
@@ -2253,6 +2261,7 @@ impl u16 {
2253
2261
uint_impl ! { u16 , u16 , 16 ,
2254
2262
intrinsics:: ctpop,
2255
2263
intrinsics:: ctlz,
2264
+ ctlz_nonzero,
2256
2265
intrinsics:: cttz,
2257
2266
intrinsics:: bswap,
2258
2267
intrinsics:: add_with_overflow,
@@ -2265,6 +2274,7 @@ impl u32 {
2265
2274
uint_impl ! { u32 , u32 , 32 ,
2266
2275
intrinsics:: ctpop,
2267
2276
intrinsics:: ctlz,
2277
+ ctlz_nonzero,
2268
2278
intrinsics:: cttz,
2269
2279
intrinsics:: bswap,
2270
2280
intrinsics:: add_with_overflow,
@@ -2277,6 +2287,7 @@ impl u64 {
2277
2287
uint_impl ! { u64 , u64 , 64 ,
2278
2288
intrinsics:: ctpop,
2279
2289
intrinsics:: ctlz,
2290
+ ctlz_nonzero,
2280
2291
intrinsics:: cttz,
2281
2292
intrinsics:: bswap,
2282
2293
intrinsics:: add_with_overflow,
@@ -2289,6 +2300,7 @@ impl u128 {
2289
2300
uint_impl ! { u128 , u128 , 128 ,
2290
2301
intrinsics:: ctpop,
2291
2302
intrinsics:: ctlz,
2303
+ ctlz_nonzero,
2292
2304
intrinsics:: cttz,
2293
2305
intrinsics:: bswap,
2294
2306
intrinsics:: add_with_overflow,
@@ -2302,6 +2314,7 @@ impl usize {
2302
2314
uint_impl ! { usize , u16 , 16 ,
2303
2315
intrinsics:: ctpop,
2304
2316
intrinsics:: ctlz,
2317
+ ctlz_nonzero,
2305
2318
intrinsics:: cttz,
2306
2319
intrinsics:: bswap,
2307
2320
intrinsics:: add_with_overflow,
@@ -2314,6 +2327,7 @@ impl usize {
2314
2327
uint_impl ! { usize , u32 , 32 ,
2315
2328
intrinsics:: ctpop,
2316
2329
intrinsics:: ctlz,
2330
+ ctlz_nonzero,
2317
2331
intrinsics:: cttz,
2318
2332
intrinsics:: bswap,
2319
2333
intrinsics:: add_with_overflow,
@@ -2327,6 +2341,7 @@ impl usize {
2327
2341
uint_impl ! { usize , u64 , 64 ,
2328
2342
intrinsics:: ctpop,
2329
2343
intrinsics:: ctlz,
2344
+ ctlz_nonzero,
2330
2345
intrinsics:: cttz,
2331
2346
intrinsics:: bswap,
2332
2347
intrinsics:: add_with_overflow,
0 commit comments