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


.



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. ...
    (comp.programming)
  • 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. ...
    (comp.dsp)
  • 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. ...
    (alt.comp.lang.learn.c-cpp)
  • 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." ...
    (microsoft.public.vc.language)
  • Re: stdarg definitions
    ... they are on most implementations) is promoted to int when it gets passed ... (fixed in later versions of the macros and compiler). ... void vfoo(int nargs, float firstfloat, va_list ap) { ... variable-argument function never has a type that undergoes promotion. ...
    (comp.lang.c)