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]

g++ optimization issue??


Hello,

I'm sorry if this has come up before and/or is well known or obvious. If so I would appreciated to be pointed in the right direction.

When compiled with "-O6" on
g++ -v
Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.5/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
Thread model: posix
gcc version 3.3.5 (Debian 1:3.3.5-3)


the program below produces the following output:
203744051  0.440158
203744051  0.633306
203744051  0.633306
203744051  0.440158


I've added some comments on what I think went wrong. Please have a look.

Colin

#include <iostream>

void case1()
{
    // first sinf call
    // 0x080489f2 <case2+82>:  fildl  (%esp)            // load+convert 203744051
    //                                                  // (val on fp-stack: 0x401ac24e333000000000 == 203744051)
    // 0x080489f5 <case2+85>:  add    $0x4,%esp
    // 0x080489f8 <case2+88>:  fsts   0xfffffff4(%ebp)  // save value for the second call  --> LOSS OF PRECISION!!
    //                                                  // val at ebp-12: 0x4d424e33 == 203744048
    // 0x080489fb <case2+91>:  fdivs  0x8048c38
    // 0x08048a01 <case2+97>:  fstps  (%esp)
    // 0x08048a04 <case2+100>: call   0x80486ac <_init+72>

    // seconds sinf call
    // 0x08048a62 <case2+194>: flds   0xfffffff4(%ebp)  // recall value saved earlier 0x4d424e33 == 203744048
    // 0x08048a65 <case2+197>: mov    %eax,%ebx
    // 0x08048a67 <case2+199>: fdivs  0x8048c38         // /10
    // 0x08048a6d <case2+205>: fstps  (%esp)
    // 0x08048a70 <case2+208>: call   0x80486ac <_init+72>  // arg on stack: 0x4b9b71c2 == 20374404


    int r(203744051);
    for(;r<203744052; ++r) {
	std::cout << r << "  " << sinf(static_cast<float>(r)/10.0f) << std::endl;
	std::cout << r << "  " << sinf(static_cast<float>(r)/10.0f) << std::endl;
    }
}

void case2()
{
    // first sinf call
    // 0x080488bb <case1+75>:  mov    $0x4b9b71c2,%edx  // pre-calced val==20374404.0
    // 0x080488c0 <case1+80>:  mov    %eax,%ebx
    // 0x080488c2 <case1+82>:  mov    %edx,(%esp)
    // 0x080488c5 <case1+85>:  call   0x80486ac <_init+72> // arg on stack 0x4b9b71c2 == 20374404.0

    // second sinf call
    // 0x0804892f <case1+191>: fildl  (%esp)                // yields different val than what the pre-calc was based on
    // 0x08048932 <case1+194>: add    $0x4,%esp
    // 0x08048935 <case1+197>: fdivs  0x8048c38            // /10
    // 0x0804893b <case1+203>: fstps  (%esp)
    // 0x0804893e <case1+206>: call   0x80486ac <_init+72> // arg on stack 0x4b9b71c3 == 20374406.0


    int r(203744051);
    std::cout << r << "  " << sinf(static_cast<float>(r)/10.0f) << std::endl;
    std::cout << r << "  " << sinf(static_cast<float>(r)/10.0f) << std::endl;    
}


int main(int argc, char** argv)
{
    case1();
    case2();
    return 0;
}






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