Bug 27257 - Error integer compare in g++ 4.1.0
Summary: Error integer compare in g++ 4.1.0
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-22 04:56 UTC by johnzhang
Modified: 2007-01-15 23:05 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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. ***