Microsoft Visual C++ Floating-Point Optimization

As you might already know, there's such an article from Eric Fleegal on MSDN:

Dave, a peer of mine, asked a related question to the C++ team and I thought the answer would be valuable to others. From Marko Radmilac:

We haven’t provided details in the article on when we would be allowed to perform a single precision operation instead of double. Here’s the correct example:


float f = (float) sqrt( (float) value);


Because casts ensure that value going into sqrt and value coming out are rounded to single precision, we can change sqrt to sqrtf, thus only changing the place of rounding. If the value coming into sqrt was double value and we still performed this transformation, the results would be disastrous (up to half of precision bits could be wrong). In short, /fp:fast does whatever VS2003 was doing without /Op switch and perhaps a few other, relatively safe optimizations. For their purposes /fp:fast should be sufficient and they shouldn’t shy away from it.




P.S. The example for the other downcast is as follows:


float f = (float) ((double) f1 + (double) f2);


Since f1 and f2 are floats to begin with and we store into f, then we change this into:


float f = f1 + f2;


This is especially beneficial on x64 platform and x86 SSE2.

Comments (0)

Skip to main content