How to detect differences in floating point behaviour across platforms

亡梦爱人 提交于 2019-12-11 02:53:35

问题


What checks can I perform to identify what differences they are in the floating point behaviour of two hardware platforms?

Verifying IEE-754 compliance or checking for known bugs may be sufficient (to explain a difference in output that I've observed).

I have looked at the CPU flags via /proc/cpu and both claim to support SSE2 I looked at:

  • https://www.vinc17.net/research/fptest.en.html
  • http://www.jhauser.us/arithmetic/TestFloat.html

but they look challenging to use. I've built TestFloat but I'm not sure what to do with it. The home page says:

"Unfortunately, TestFloat’s output is not easily interpreted. Detailed knowledge of the IEEE Standard is required to use TestFloat responsibly."

Ideally I just want one or two programs or some simple configure style checks I can run and compare the output between two platforms.

Ideally I would then convert this into configure checks to ensure that the an attempt to compile the non-portable code on a platform that behaves abnormally its detected at configure time rather than run time.

Background

I have found a difference in behaviour for a C++ application on two different platforms:

  • Intel(R) Xeon(R) CPU E5504
  • Intel(R) Core(TM) i5-3470 CPU

Code compiled natively on either machine runs on the other but for one test the behaviour depends on which machine the code is run on.

Clarification The executable compiled on machine A behaves like the executable compiled on machine B when copied to run on machine B and visa versa.

It could an uninitialised variable (though nothing showed up in valgrind) or many other things but I suspected that the cause could be non-portable use of floating point. Perhaps one machine is interpreting the float point assembly differently from the other? The implementers have confirmed they know about this. Its not my code and I have no desire to completely rewrite it to test this. Recompiling is fine though. I want to test my hypothesis.

In the related question I am looking at how to enable software floating point. This question is tackling the problem from the other side.

Update

I've gone down the configure check road tried the following based on @chux's hints.

#include <iostream>
#include <cfloat>

int main(int /*argc*/, const char* /*argv*/[])
{
   std::cout << "FLT_EVAL_METHOD=" << FLT_EVAL_METHOD << "\n";
   std::cout << "FLT_ROUNDS=" << FLT_ROUNDS << "\n";
#ifdef __STDC_IEC_559__
   std::cout << "__STDC_IEC_559__ is defined\n";
#endif
#ifdef __GCC_IEC_559__
   std::cout << "__GCC_IEC_559__ is defined\n";
#endif
   std::cout << "FLT_MIN=" << FLT_MIN << "\n";
   std::cout << "FLT_MAX=" << FLT_MAX << "\n";
   std::cout << "FLT_EPSILON=" << FLT_EPSILON << "\n";
   std::cout << "FLT_RADIX=" << FLT_RADIX << "\n";
   return 0;
}

Giving identical output on both platforms:

./floattest 
FLT_EVAL_METHOD=0
FLT_ROUNDS=1
__STDC_IEC_559__ is defined
FLT_MIN=1.17549e-38
FLT_MAX=3.40282e+38
FLT_EPSILON=1.19209e-07
FLT_RADIX=2

I'm still looking for something that might be different.


回答1:


OP has 2 goals that conflict a bit.

  1. How to detect differences in floating point behaviour across platforms (?)

  2. I just want one or two programs or some simple configure style checks I can run and compare the output between two platforms.

Yes some differences are easy to detect, but some differences can be exceedingly subtle.
Sample Can the floating-point status flag FE_UNDERFLOW set when the result is not sub-normal?

There are no simple tests for the general problem.

Recommend either:

  1. Revamp the coding goal to allow for nominal differences.

  2. See if _STDC_IEC_559__ is defined and hope that is sufficient for you application. Given various other factors like FLT_EVAL_METHOD and FLT_ROUNDS and optimization levels, code can still be compliant yet provide different results, yet the degree will be more manageable.

  3. If super high consistency is needed, do not use floating point.




回答2:


I found a program called esparanoia that does some checks of floating point behaviour. This is based on William Kahan's original paranoid program found the infamous Pentium division bug.

While it did not detect any problems with my test systems (and thus is not sufficient to answer the question) it might be of interest to someone else.



来源:https://stackoverflow.com/questions/55300378/how-to-detect-differences-in-floating-point-behaviour-across-platforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!