This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: O2 and Overflow


2010/4/23 Heinz Riener <hriener@student.tugraz.at>:
> Dear all,
>
> I'm using the native GCC version[1] of my GNU/Linux distribution. ?I wonder
> whether GCC's optimization behavior is in the following case correct.
> ?Consider the following two programs:
>
> (1)
> int test(int n) {
> ?if (n > 0)
> ? ?return 1;
> ?return 0;
> }
>
> (2)
> int test(int n) {
> ?if (2*n > 0)
> ? ?return 1;
> ?return 0;
> }
>
> After compiling both with the flags '-c -O2 -pedantic -Wall', they result in
> the same object file. ?I expected the object files to be different. (The
> second program may overflow, the first program does not.) ?Please, point me
> to the right direction.
>
> [1]: gcc (GCC) 4.4.3 20100316 (prerelease)
>
> Thanks,
> Heinz
>

For such overflow checks you have to use the option -fwrapv to enable
overflow checks.

Kai

-- 
|  (\_/) This is Bunny. Copy and paste
| (='.'=) Bunny into your signature to help
| (")_(") him gain world domination


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]