如何检测跨平台的浮点行为的差异 [英] How to detect differences in floating point behaviour across platforms

查看:112
本文介绍了如何检测跨平台的浮点行为的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以执行哪些检查来确定它们在两个硬件平台的浮点行为上有什么区别?

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

验证IEE-754的合规性或检查已知的错误可能就足够了(以解释我观察到的输出差异).

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

我已经通过/proc/cpu查看了CPU标志,并且都声称支持SSE2 我看着:

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

,但使用起来似乎有些挑战. 我已经构建了TestFloat,但是不确定如何处理.主页显示:

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

"不幸的是,TestFloat的输出不容易解释.详细 要使用TestFloat,必须具备IEEE标准的知识 负责任地."

"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.

我发现在两个不同平台上C ++应用程序的行为有所不同:

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

  • 英特尔®至强®CPU E5504
  • 英特尔(R)酷睿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.

说明 在计算机A上编译的可执行文件在复制以在计算机B上运行时,其行为类似于在计算机B上编译的可执行文件,反之亦然.

它可能是未初始化的变量(尽管valgrind中未显示任何变量)或许多其他东西,但是 我怀疑原因可能是非便携式使用浮点数. 也许一台机器对浮点组件的解释与另一台机器有所不同? 实施者已经确认他们知道这一点. 它不是我的代码,我也不想完全重写它来测试它.重新编译是可以的. 我想检验一下我的假设.

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.

我已经沿着@chux的提示尝试了以下配置检查.

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.

推荐答案

我找到了名为 esparanoia 会检查浮点行为.这是基于威廉·卡汉的原始偏执狂程序发现的臭名昭著的奔腾分裂漏洞.

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.

这篇关于如何检测跨平台的浮点行为的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆