https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101050
Bug ID: 101050 Summary: Range comparisons with more significant bits constant are not optimised into masked equality tests Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: felix.von.s at posteo dot de Target Milestone: --- Target: x86_64-linux-gnu Created attachment 50995 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50995&action=edit Sample functions f and g Attached sample functions f and g have identical behaviour, but GCC optimises them differently at -O3. The former is optimised into an arithmetic offset and unsigned comparison, while the latter compiles into bit masking and an equality test, as written. I presume it would be advantageous to reduce the former to the latter whenever possible (that is, to simplify (C <= x && x < C + (1 << N)) where (C & ((1 << N) - 1)) == 0 into (x & ~(1 << N)) == C), since the operations involved are slightly weaker. The Other Compiler does this. And yes, the values in the example are not accidental. This comes up in Unicode processing, so I think it’s worth the trouble.