I'm working with some code which uses inline Intel X86_64 assembly to set the processor's SSE flags to include flush-denormal-to-zero and treat-denormals-as-zero: stmxcsr
and then ldmxcsr
.
I don't particularly like that approach (which we shall denote as approach 1). The Wikipedia page on denormals suggests a few other options:
- Using an environment variable:
#include <fenv.h>
// ...
fesetenv(FE_DFL_DISABLE_SSE_DENORMS_ENV);
- Using one of the XMM registers' headers:
#include <xmmintrin.h>
// ...
_mm_setcsr( _mm_getcsr() | 0x0040 | 0x8000 );
- Using more headers and some macros:
#include <pmmintrin.h>
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
#include <xmmintrin.h>
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
... but I don't like any of these too much either. They're not in any common/standard library; and, of course, they're Intel-specific. What if I'm on ARM? I might still want to do this. I'm also not sure which of these will work with which compiler (Wikipedia says about some of these that they "may work").
So, how should I best tell my processor(s) to flush-denormals-to-zero?
Note: My question hasn't really distinguished between C and C++. I'm interested in a C++ idiom, but if there's only a "C'ish" idiom, I can live with that.