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

            Bug ID: 78115
           Summary: Missed optimization for "int modulo 2^31"
           Product: gcc
           Version: 6.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoeppe at google dot com
  Target Milestone: ---

Consider the operation of mapping an int to the unique modular representative
in [0, 2^31).


Readable code:

#include <climits>

int mod31(int num) {
  if (num < 0) { num = num + 1 + INT_MAX; }
  return num;
}


Paranoid bit-shifter's code:

int mod31shift(int num) {
  return static_cast<unsigned int>(num) % (1U << 31);
}

Clang generates the same machine code for both, but GCC does not:
https://godbolt.org/g/2BjNqA

Reply via email to