GCC: program doesn't work with compilation option -O3

后端 未结 16 1520
感动是毒
感动是毒 2020-12-16 20:12

I\'m writing a C++ program that doesn\'t work (I get a segmentation fault) when I compile it with optimizations (options -O1, -O2, -O3, etc.), but it works just fine when I

相关标签:
16条回答
  • 2020-12-16 21:02

    Now that you posted the code fragment and a working workaround was found (@Windows programmer's answer), I can say that perhaps what you are looking for is -ffloat-store.

    -ffloat-store

    Do not store floating point variables in registers, and inhibit other options that might change whether a floating point value is taken from a register or memory.

    This option prevents undesirable excess precision on machines such as the 68000 where the floating registers (of the 68881) keep more precision than a double is supposed to have. Similarly for the x86 architecture. For most programs, the excess precision does only good, but a few programs rely on the precise definition of IEEE floating point. Use -ffloat-store for such programs, after modifying them to store all pertinent intermediate computations into variables.

    Source: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html

    0 讨论(0)
  • 2020-12-16 21:02

    I would assume your code is wrong first.
    Though it is hard to tell.

    Does your code compile with 0 warnings?

     g++ -Wall -Wextra -pedantic -ansi
    
    0 讨论(0)
  • 2020-12-16 21:06

    As an experiment, try to see if this will force the compiler to round everything consistently.

    volatile float d1=distance(point,p1) ;
    volatile float d2=distance(point,p2) ;
    return d1 < d2 ;
    
    0 讨论(0)
  • 2020-12-16 21:06

    as other have pointed out, probably strict aliasing. turn it of in o3 and try again. My guess is that you are doing some pointer tricks in your functor (fast float as int compare? object type in lower 2 bits?) that fail across inlining template functions. warnings do not help to catch this case. "if the compiler could detect all strict aliasing problems it could just as well avoid them" just changing an unrelated line of code may make the problem appear or go away as it changes register allocation.

    0 讨论(0)
提交回复
热议问题