Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float values behaving differently across the release and debug builds

My application is generating different floating point values when I compile it in release mode and in debug mode. The only reason that I found out is I save a binary trace log and the one from the release build is ever so slightly off from the debug build, it looks like the bottom two bits of the 32 bit float values are different about 1/2 of the cases.

Would you consider this "difference" to be a bug or would this type of difference be expected. Would this be a compiler bug or an internal library bug.

For example:

LEFTPOS and SPACING are defined floating point values.
float def_x;
int xpos;

def_x = LEFTPOS + (xpos * (SPACING / 2));

The issue is in regards to the X360 compiler.

like image 355
KPexEA Avatar asked Sep 26 '08 20:09

KPexEA


3 Answers

Release mode may have a different FP strategy set. There are different floating point arithmetic modes depending on the level of optimization you'd like. MSVC, for example, has strict, fast, and precise modes.

like image 198
Nick Avatar answered Sep 30 '22 13:09

Nick


I know that on PC, floating point registers are 80 bits wide. So if a calculation is done entirely within the FPU, you get the benefit of 80 bits of precision. On the other hand, if an intermediate result is moved out into a normal register and back, it gets truncated to 32 bits, which gives different results.

Now consider that a release build will have optimisations which keep intermediate results in FPU registers, whereas a debug build will probably naively copy intermediate results back and forward between memory and registers - and there you have your difference in behaviour.

I don't know whether this happens on X360 too or not.

like image 35
Luke Halliwell Avatar answered Sep 30 '22 13:09

Luke Halliwell


It's not a bug. Any floating point uperation has a certain imprecision. In Release mode, optimization will change the order of the operations and you'll get a slightly different result. The difference should be small, though. If it's big you might have other problems.

like image 38
Branan Avatar answered Sep 30 '22 13:09

Branan