Skip to content

Suboptimal Assembly Output from a Modulo on a Power of Two #129795

Closed
@Altanis

Description

@Altanis

I tried this code:

#[no_mangle]
pub fn index(key: usize, buckets: usize) -> usize {
    assert!(buckets.is_power_of_two());
    key % buckets
}

Since assert!(buckets.is_power_of_two()) is run (and .is_power_of_two() is an inbuilt function), the latter modulus operation should reduce to key & (buckets - 1).

Instead, the outputted assembly here shows that this reduction does not take place.

Meta

The compiler in the GodBolt link is version 1.80.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions