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

news:1184841798.657510.295610@xxxxxxxxxxxxxxxxxxxxxxxxxxxx



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

#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;
printf("%d",IsSinglePrecisionSufficient(b));
}

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

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;}
to
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


.