A questionable but faster MSVC compiler switch

/fp:fast

The related article :
1) https://blogs.msdn.microsoft.com/vcblog/2015/10/19/do-you-prefer-fast-or-precise/
2) https://blogs.msdn.microsoft.com/vcblog/2016/05/04/new-code-optimizer/

MSVC2012+ will generate more faster code using -fp:fast, especially on some intrinsic functions and Auto-Vectorization (/arch:SSE2, /arch:AVX?), but maybe this will produce wrong results because of reducing the accuracy.
So if we want to use the compiler switch, you should add some code to avoid the precision decrease.

#ifdef _MSC_VER
#pragma float_control(precise, on, push)
#endif

// Your higher precision code

#ifdef _MSC_VER
#pragma float_control(pop)
#endif

The code section will overwrite /fp:fast to /fp:precise, and very useful to apply for Mozilla code, because Mozilla use unified build to make the projects, it’s more trouble to change it in one source file.

Take Mozilla code for example, we can use -fp:fast on GFX code, and take the patch below.

diff --git a/gfx/2d/BasePoint4D.h b/gfx/2d/BasePoint4D.h
--- a/gfx/2d/BasePoint4D.h
+++ b/gfx/2d/BasePoint4D.h
@@ -6,16 +6,20 @@
#ifndef MOZILLA_BASEPOINT4D_H_
#define MOZILLA_BASEPOINT4D_H_

#include "mozilla/Assertions.h"

namespace mozilla {
namespace gfx {

+#ifdef _MSC_VER
+#pragma float_control(precise, on, push)
+#endif
+
/**
* Do not use this class directly. Subclass it, pass that subclass as the
* Sub parameter, and only use that subclass. This allows methods to safely
* cast 'this' to 'Sub*'.
*/
template
struct BasePoint4D {
union {
@@ -120,12 +124,16 @@ struct BasePoint4D {

void Normalize() {
*this /= Length();
}

bool HasPositiveWCoord() { return w; }
};

+#ifdef _MSC_VER
+#pragma float_control(pop)
+#endif
+
} // namespace gfx
} // namespace mozilla

#endif /* MOZILLA_BASEPOINT4D_H_ */

And in the same time, we also can use the code section below to overwrite /fp:precise to /fp:fast

#ifdef _MSC_VER
#pragma float_control(precise, off, push)
#endif

// Your lower precision code

#ifdef _MSC_VER
#pragma float_control(pop)
#endif
更高