Hi,

When i run with the options g++ prog.c -o prog and run the exectuable
it gives me the correct output
but when i do g++ prog.c -o prog -O2 i get the wrong output


The  inputs are below

[EMAIL PROTECTED]:/home/junk/prog# g++ bug_gccopt.cpp -O2
[EMAIL PROTECTED]:/home/junk/prog# ./a.out
1
10000000000000333
3135439247023686131
[EMAIL PROTECTED]:/home/junk/prog# g++ bug_gccopt.cpp
[EMAIL PROTECTED]:/home/junk/prog# ./a.out
1
10000000000000333
4511787964595
[EMAIL PROTECTED]:/home/junk/prog#

the file is attached.

i  think i know where the bug is because i did it the wrong way and
then later corrected the overflow.

ull is unsigned long long.

    for (it = pm.begin (); it != pm.end (); it++)
        {
          ull a = it->first, b = it->second;
/* These lines are getting optimized and overflow is occuring */
##        ull num= (ull) (pow (( double) a, (double) b + 1) - 1);
##        ull den=a-1;
##        gg=gcd(num,den);
##        num/=gg;
##        den/=gg;
          numprod *=num;
          denprod *=den;
          gg = gcd (numprod, denprod);
          numprod /= gg;
          denprod /= gg;
        }

i got the wrong result when it were (the optimized code is giving the
answer when the actual code was below
The result i got from the below code is the same that i get from -O2
of the above code which is resulting in overflow )

    for (it = pm.begin (); it != pm.end (); it++)
        {
          ull a = it->first, b = it->second;
           nprod*= (ull) (pow (( double) a, (double) b + 1) - 1);
           denprod*=a-1;
          gg = gcd (numprod, denprod);
          numprod /= gg;
          denprod /= gg;
        }

Am i doing anything wrong above ? is the typecast of ull and double wrong.




[EMAIL PROTECTED]:~/prog$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--enable-mpfr --enable-targets=all --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
[EMAIL PROTECTED]:~/prog$ g++-4.1 -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1
--enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug
--enable-mpfr --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.3 20080308 (prerelease) (Ubuntu 4.1.2-21ubuntu1)
[EMAIL PROTECTED]:~/prog$

Attachment: bug_gccopt.cpp
Description: Binary data

Reply via email to