5

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:

  1. Using an environment variable:
#include <fenv.h>
// ...
fesetenv(FE_DFL_DISABLE_SSE_DENORMS_ENV);
  1. Using one of the XMM registers' headers:
#include <xmmintrin.h>
// ...
_mm_setcsr( _mm_getcsr() | 0x0040 | 0x8000 );
  1. 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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    As those options make your code not compliant with standard C++, you should not expect a standard way of doing that making the question somewhat pointless as you already list multiple ways of doing it. – Phil1970 Dec 31 '19 at 21:57
  • @Phil1970: Does the standard require no denormal-to-zero? Also, I explained why I'm looking for something else. Think about something like [Boost.StackTrace](https://www.boost.org/doc/libs/1_71_0/doc/html/stacktrace.html) for example - that's all platform-specific, not-in-the-standard work, and yet it's a nice cross-platform library. – einpoklum Dec 31 '19 at 22:03
  • Two multiplies should do the equivalent: `(x * 2^-53) * 2*53` (The `53` may be off by 1.) – Rick James Jan 10 '20 at 05:17
  • @DavidFong: I was hoping there might be some library that does this already. Anyway, by now, I'm not working on that anymore... – einpoklum Aug 24 '22 at 18:06

0 Answers0