[Bug tree-optimization/82853] New: Optimize x % 3 == 0 without modulo
andi-gcc at firstfloor dot org
gcc-bugzilla@gcc.gnu.org
Sun Nov 5 19:57:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82853
Bug ID: 82853
Summary: Optimize x % 3 == 0 without modulo
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: andi-gcc at firstfloor dot org
Target Milestone: ---
Ralph Levien pointed out as part of FizzBuzz optimization:
Turns out you can compute x%3 == 0 with even fewer steps, it's (x*0xaaaaaaaab)
< 0x55555556 (assuming wrapping unsigned 32 bit arithmetic).
gcc currently generates the full modulo and then checks.
Could be done in match.pd I suppose.
Test case
unsigned mod3(unsigned a) { return 0==(a%3); }
More information about the Gcc-bugs
mailing list