简体   繁体   中英

Detecting denormal float operations on Linux for x86

I'm in the process of porting a windows program to linux, and have gotten stumped on piece of msvc-specific code that seems to check for floating point operations that has given a denormal or inexact result. I'm very much unsure on how to implement it in a robust manner. I should add that I'm fairly inexperienced when it comes to both linux-specific programming and very low-level operations like these.

Specifically, the part that gives me trouble is the following:

  if ( _statusfp() & ( _SW_INEXACT | _SW_DENORMAL) )
  {
     ... portable stuff ...
  }

  _clearfp();

While fenv.h seems to give the ability to both clear the status flag and check for the inexact flag, it does not seem to provide any assistance in checking the denormal flag. Furthermore, I have had it suggested to me that gcc might handle floating point operations differently enough that a simple straight port of this piece of code may not be possible. I'd be grateful for any assistance in this.

If it is relevant, this is used in a very heavy number crunching part of the program where performance matter.

Edit: The flag in fenv.h called FE_UNDERFLOW seems to be raised when a denormal result is generated according to http://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions , but have seen several other sources state that it is raised only when the result is too small even for a subnormal. Will run tests to see if it does what I need it too and and answer myself if so.

Is C++11 an option for you? If so, perhaps you could call std::isnormal on the result, see eg http://en.cppreference.com/w/cpp/numeric/math/isnormal .

As said in the question, it seems that fenv.h has a flag FE_UNDERFLOW that on some architectures at least indicate a subnormal/denormal result. My own testing indicates that this seems to be the case on my test x86 architecture, so I will go ahead and use this for now unless a better solution is provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM