GCC Bugzilla has been upgraded from version 4.4.9 to 5.0rc3. If you see any problem, please report it to bug 64968.

Bug 27257

Summary: Error integer compare in g++ 4.1.0
Product: gcc Reporter: johnzhang <johnzhang>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: normal CC: felix-gcc, gcc-bugs, gcc-bugs, pluto
Priority: P3    
Version: 4.1.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description johnzhang 2006-04-22 04:56:00 UTC
0X80000000 - 10 < 0 and 10 - 0X80000000 < 0
gcc version 3.3.4 gets a right result of comparing between 10 and 0X80000000, but gcc version 4.1.0 fails.

the test code followed:

#include <stdio.h>
int main()
{
        int b = 0X80000000;
        if ((10 -  b) < 0)
        {
                printf("(10 - b) = %d < 0 is True\n", 10 - b);
        }
        else
        {
                printf("(10 - b) = %d < 0 is False\n", 10 - b);
        }
        if ((b - 10) < 0)
        {
                printf("(b - 10) = %d < 0 is True\n", b - 10);
        }
        else
        {
                printf("(b - 10) = %d < 0 is False\n", b - 10);
        }

        return 0;
}

I had tested the code and got the result:
bingo@sttest:~/tmp> ../bin/g++ t.cpp
bingo@sttest:~/tmp> a.out
(10 - b) = -2147483638 < 0 is True
(b - 10) = 2147483638 < 0 is True
Comment 1 Pawel Sikora 2006-04-22 08:23:53 UTC
(In reply to comment #0)

>         int b = 0X80000000;

>         if ((b - 10) < 0)

b - 10 is 0xff(...)7ffffff6 and this is an integer overflow.

[ cite: c++ standard / $5.5 ]

if during the evaluation of an expression the result isn't mathematically
defined nor in the range of representable values for its type the behaviuor
is undefined, unless such an expression is a constant expression, in which
case the program is ill-formed. (...)

[ /cite ]

so, use -fwrapv (man g++) to get not quite correct behaviour.
Comment 2 Andrew Pinski 2006-04-22 15:35:35 UTC
This is signed int overflow being undefined, in fact I should I know because I was the one who write the patch to optimize this.
Comment 3 johnzhang 2006-04-28 06:36:24 UTC
Ok, you are right. it would be nice if g++ 4.1.0 acts as what g++ 3.3.4 does.
Comment 4 Andrew Pinski 2006-04-28 16:29:08 UTC
(In reply to comment #3)
> Ok, you are right. it would be nice if g++ 4.1.0 acts as what g++ 3.3.4 does.

Use -fwrapv if you want defined wrapping.
Comment 5 Andrew Pinski 2007-01-15 20:04:37 UTC
*** Bug 30475 has been marked as a duplicate of this bug. ***