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

            Bug ID: 93013
           Summary: PPC: optimization leads around modulo leads to
                    incorrect result
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jens.seifert at de dot ibm.com
  Target Milestone: ---

Input:
int mod(int x, int y, int &z)
{
        z = x % y;
        if (y == 0)
        {
                // division by zero
                return 1;
        }
        else if (y == -1)
        {
                // gcc removes this branch, which leads to wrong results for
-2^31 % -1
                z=0;
        }
        return 0;
}

gcc -maix64 -O2 modulo.C -save-temps

Output:
._Z3modiiRi:
LFB..0:
        divw 9,3,4
        mr 10,3
        cntlzw 3,4
        srwi 3,3,5
        mullw 9,9,4
        subf 9,9,10
        stw 9,0(5)
        blr

For input x=-2^31 y=-1, the result is expected to be 0.
As modulo is emulated using x-(x/y)*y and x/y for x=-2^31 y=-1 is undefined on
PowerPC, the result is incorrect if the branch gets optimized away.

Reply via email to