GCC在32位机器上以不同的方式编译程序

GCC compiles program differently in 32-bit machine

本文关键字:方式 编译程序 32位 机器 GCC      更新时间:2023-10-16

我遇到了这样一种情况,在32位机器上编译的同一个程序与在64位机器上编辑的结果不同。一段代码就像这个

#include <iostream>
int main(int argc, char *argv[])
{
  int index = 1;
  int total = 21;
  int min = 79;
  int max = 100;
  double coef = index / (double)total;
  int ret1 = coef * (max - min);
  std::cout << ret1 << "n";
  return 0;
} 

我希望结果是1,但在32位Linux上,我得到的结果是0。coeff*(max-min)的结果可能是0.9999999…并且分配给int导致0。两个朋友在64位机器上尝试了相同的代码,结果是1。为什么它在32位上给出结果0?它可能与虚拟机有关吗?我的机器上的测试环境:

  • 32位Linux Mint 17.2在VMWare 6.0.7上运行
  • gcc 4.8.4
  • 用于构建的命令:g++main.cpp-o main

我可以用gcc 4.8.4:在64位Ubuntu 14.04上重现这个问题

$ g++ -m32 main.c -o main && ./main
0
$ g++ -m64 main.c -o main && ./main
1

我相信@void_ptr是对的。这是由于x87内部使用80位数学,而SSE2使用64位。

如果使用-ffloat-store编译32位可执行文件以强制64位浮点精度,就会发生这种情况:

$ g++ -m32 -ffloat-store main.c -o main && ./main 
1

以下是man gcc对这个问题的看法:

   -ffloat-store
       Do not store floating-point variables in registers, and inhibit
       other options that might change whether a floating-point value is
       taken from a register or memory.
       This option prevents undesirable excess precision on machines such
       as the 68000 where the floating registers (of the 68881) keep more
       precision than a "double" is supposed to have.  Similarly for the
       x86 architecture.  For most programs, the excess precision does
       only good, but a few programs rely on the precise definition of
       IEEE floating point.  Use -ffloat-store for such programs, after
       modifying them to store all pertinent intermediate computations
       into variables.

在任何情况下,永远不要依赖浮点在数学上是准确的。