Re: Compiler optimization problem in MSVC 2003 Release mode

Thanks. I always get stumped with what the compiler does
automatically. Sometimes it helps and sometimes it just screws up. By
the way, is there some place i can read up more on this, or is there a
heading to search in google.

On Jul 19, 5:00 pm, "Igor Tandetnik" <itandet...@xxxxxxxx> wrote:
"Shirsoft" <shirs...@xxxxxxxxx> wrote in message


I have written a small function to check whether a single precision
variable is sufficient to hold a double precision value. The code is

#include <stdio.h>

int cmpFloat_to_Double(float x, double y) { return x == y;}
#define IsSinglePrecisionSufficient(a) cmpFloat_to_Double((float)a, a)

void main( void )
double b = 0.7;

It works fine in debug mode giving output of 0 but in release it gives

Try making it

int cmpFloat_to_Double(volatile float x, double y)

That should hopefully suppress optimizations. Alternatively, you can
compile with /Op flag. Without this flag, the optimizer is allowed to
perform floating point calculations at a precision higher than that
dictated by the variable's type (e.g., delaying conversion from double
to float until the value has to be spilled from FPU register to memory).
Which, of course, completely defeats your exercise.

Also if i change
int cmpFloat_to_Double(float x, double y) { return x == y;}
int cmpFloat_to_Double(volatile float x, volatile double y) { return x
== y;}
it works fine in release mode too.

So you have your solution.
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Relevant Pages

  • Re: C to Java Byte Code
    ... The actual topic is about creating a Java equivalent for a C program. ... as well have argued that a byte, int, long, float, double can all be ... The values of the compiler depend on the underlying hardware. ...
  • Re: OT: C++ overloading operators
    ... way before complex numbers were part of the STL. ... I used the INLINE directive for these trivial functions. ... A compiler can not do inlining without access to the source code. ...
  • Re: Newbie Questions: variable, operators
    ... Its type is float. ... you initialize a float variable with a double value (which ... There is an upper level of what compiler writers build into their ... I would not expect any compiler to actually recognize this optimization. ...
  • Re: About volatile qualifier
    ... In an article about volatile qualifier there is a small code by which the ... void Wakeup{flag = true;} ... compiler optimizes access to that variable by caching it in a register. ... Thread-1 will never get the actual value of the flag and loop forever." ...
  • Re: float pointers
    ... >>float yds,ft,ins; ... It's perfectly legal to declare one function ... > Your compiler must have complained about this line.... ... note that pointers are not subject to promotion, ...