Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting denormal float operations on Linux for x86

Tags:

c++

linux

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.

like image 256
Pierre Andersson Avatar asked Nov 13 '22 02:11

Pierre Andersson


1 Answers

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

like image 173
Saran Tunyasuvunakool Avatar answered Nov 14 '22 23:11

Saran Tunyasuvunakool