Skip to content

Testing bit next to signbit does not constant fold when squaring signed integers #152061

@ZERICO2005

Description

@ZERICO2005

https://godbolt.org/z/7bqs6W8an

bool func_B(int8_t x) {
    return (x * x) & (1 << 30);
}

Assuming that int is 32 bits, then squaring int8_t should clear bits [15, 31] as the result will be [0, 16384]. I noticed that the code is not constant folded to return false when testing bit 30 (the bit next to the signbit). It also generates different llvm compared to testing bits 16-29* (icmp samesign ugt versus icmp ne 0).

Transforming return (x * x) & (1 << 30) to return false appears to be correct:
https://alive2.llvm.org/ce/z/Q6w5gy

*testing bit 15 does not constant fold for different reasons.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions