https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87650

            Bug ID: 87650
           Summary: suboptimal codegen for testing low bit
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hoganmeier at gmail dot com
  Target Milestone: ---

int pow(int x, unsigned int n)
{
    int y = 1;
    while (n > 1)
    {
        auto m = n%2;
        n = n/2;
        if (m)
            y *= x;
        x = x*x;
    }
    return x*y;
}

produces
mov edx, esi
and edx, 1
test edx, edx

instead of just
test sil, 1

while clang chooses a branchless version:
https://godbolt.org/z/L6VUZ1

Interestingly gcc does use test sil,1 if you get rid of m:
godbolt.org/z/9oL1oc

Assembly analysis:
https://stackoverflow.com/a/52877279/594456

Reply via email to