Skip to content

Commit 82a79b9

Browse files
committed
Add codegen test for modulo with power-of-two divisor
1 parent 52daa7d commit 82a79b9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: tests/codegen/issues/issue-129795.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ compile-flags: -Copt-level=3
2+
//@ min-llvm-version: 20
3+
#![crate_type = "lib"]
4+
5+
// Ensure that a modulo operation with an operand that is known to be a power-of-two is properly optimized.
6+
7+
// CHECK-LABEL: @modulo_with_power_of_two_divisor
8+
// CHECK: add i64 %divisor, -1
9+
// CHECK-NEXT: and i64
10+
// CHECK-NEXT: ret i64
11+
#[no_mangle]
12+
pub fn modulo_with_power_of_two_divisor(dividend: u64, divisor: u64) -> u64 {
13+
assert!(divisor.is_power_of_two());
14+
// should be optimized to (dividend & (divisor - 1))
15+
dividend % divisor
16+
}

0 commit comments

Comments
 (0)