|
| 1 | +//@ compile-flags: -Copt-level=3 |
| 2 | +// Test that `checked_ilog` can use a faster implementation when `base` is a |
| 3 | +// known power of two |
| 4 | + |
| 5 | +#![crate_type = "lib"] |
| 6 | + |
| 7 | +// CHECK-LABEL: @checked_ilog2 |
| 8 | +#[no_mangle] |
| 9 | +pub fn checked_ilog2(val: u32) -> Option<u32> { |
| 10 | + // CHECK: %[[ICMP:.+]] = icmp ne i32 %val, 0 |
| 11 | + // CHECK: %[[CTZ:.+]] = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %val, i1 true) |
| 12 | + // CHECK: %[[LOG2:.+]] = xor i32 %[[CTZ]], 31 |
| 13 | + val.checked_ilog(2) |
| 14 | +} |
| 15 | + |
| 16 | +// log(4, x) == log(2, x) / 2 |
| 17 | +// CHECK-LABEL: @checked_ilog4 |
| 18 | +#[no_mangle] |
| 19 | +pub fn checked_ilog4(val: u32) -> Option<u32> { |
| 20 | + // CHECK: %[[ICMP:.+]] = icmp ne i32 %val, 0 |
| 21 | + // CHECK: %[[CTZ:.+]] = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %val, i1 true) |
| 22 | + // CHECK: %[[DIV2:.+]] = lshr i32 %[[CTZ]], 1 |
| 23 | + // CHECK: %[[LOG4:.+]] = xor i32 %[[DIV2]], 15 |
| 24 | + val.checked_ilog(4) |
| 25 | +} |
| 26 | + |
| 27 | +// log(16, x) == log(2, x) / 4 |
| 28 | +// CHECK-LABEL: @checked_ilog16 |
| 29 | +#[no_mangle] |
| 30 | +pub fn checked_ilog16(val: u32) -> Option<u32> { |
| 31 | + // CHECK: %[[ICMP:.+]] = icmp ne i32 %val, 0 |
| 32 | + // CHECK: %[[CTZ:.+]] = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %val, i1 true) |
| 33 | + // CHECK: %[[DIV4:.+]] = lshr i32 %[[CTZ]], 2 |
| 34 | + // CHECK: %[[LOG16:.+]] = xor i32 %[[DIV2]], 7 |
| 35 | + val.checked_ilog(16) |
| 36 | +} |
0 commit comments