#include <stdio.h>
int main() {
    int x = 0x87654321;
    switch (3*x % 3) {
        case 2: puts("SUCCESS"); break;
        default: puts("FAILURE"); break;
    }
    return 0;
}

gcc -fwrapv test.c
./a.out
FAILURE

Expected output: SUCCESS

This would not be a bug, since integer overflow is undefined in C, *except* for
the fact that I passed -fwrapv. I was under the impression that -fwrapv would
enforce integer arithmetic modulo 2**32, and not incorrectly optimize (x*n%x)
as if it were equivalent to 0.

If -fwrapv does not strictly enforce arithmetic mod 2**32, then it could use
some better documentation. If it *is* supposed to enforce arithmetic mod 2**32,
then this is a bug that ought to be fixed.


-- 
           Summary: Even with -fwrapv, (3*x%3) is assumed to always be equal
                    to 0
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: foo at mailinator dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42615

Reply via email to