首页 > 解决方案 > AIX 上的 C C++ (gcc 4.2.0) 和 Linux (gcc 4.8.5) 在 gcc 中的浮点选项差异

问题描述

我的问题是:在 gcc Linux 上是否有一个选项类似于 gcc AIX 上的快速数学(在这种特殊情况下)?

我不得不从 AIX 迁移到 Linux,有几百个 C 和 C++ 程序。在 AIX 上,我们使用 xlc 进行编译。在 Linux 上,我们将使用 gcc。在某些程序中,我们有浮点运算,我知道浮点可以在不同的平台上给出不同的结果,但我很好奇 gcc 给我的结果与 xlc 不同的情况(这是正常的),当我添加 - -ffast-math 选项它给了我想要的结果,但在 Linux 上,相同的选项对结果没有任何影响。这是代码和我所做的:

#include <stdio.h>

int main () {
double importe, franja, base, porcentaje, irpf;

printf("/*-----------------*/\n");

importe = 10562.5;
franja = 3.0;
base = 2226.0;
porcentaje = 10.0;
irpf = franja * base * ( porcentaje/ 100 );
printf("Redondeo1 [%.16f]\n", irpf );

importe -= franja * base;
franja = 5;
porcentaje = 15.0;
irpf += importe * ( porcentaje / 100 );
printf("Redondeo2 [%.16f]\n", irpf );

return 0;
}

编译

AIX> /usr/vac/bin/xlc -o redondo.xlc redondo.c

并执行

AIX> redondo.xlc
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4750000000001364]

然后我尝试了第一步,在 AIX 上用 gcc 编译

AIX> gcc -o redondo.gcc redondo.c

执行它,在第二个结果上有所不同

AIX> redondo.gcc
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4749999999999091]

所以我尝试了-ffast-math

AIX> gcc  -ffast-math -o redondo.gcc-ffast-math  redondo.c

并执行

AIX> redondo.gcc-ffast-math
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4750000000001364]

解决了这种情况....在 AIX 上。当我尝试在 Linux 上复制该解决方案时,它总是给出相同的结果:

Linux> gcc -o redondo.gcc  redondo.c
Linux> redondo.gcc
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4749999999999091]
Linux> gcc -ffast-math -o redondo.gcc-ffast-math  redondo.c
Linux> redondo.gcc-ffast-math
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4749999999999091]

我知道浮点是不确定的,不是每个数字都是可表示的,结果取决于系统架构以及编译器和库等......但也许 gcc Linux 上有一些选项表现得像 fast- gcc AIX 上的数学。我尝试了很多 gcc 选项(frounding-math、mfpmath、contract、store、excess、m387、unsafe、reciprocal、finite、ffast-math),甚至重新排序操作,但没有成功。


AIX 信息:

AIX> oslevel -s
7100-04-05-1720

AIX> prtconf
System Model: IBM,9009-42A
Processor Type: PowerPC_POWER9
Processor Implementation Mode: POWER 8
Processor Version: PV_8_Compat
Number Of Processors: 2
Processor Clock Speed: 2750 MHz
CPU Type: 64-bit
Kernel Type: 64-bit
LPAR Info: 24 dc4_bpdesa6
Memory Size: 8192 MB
Good Memory Size: 8192 MB
Platform Firmware level: VL910_135
Firmware Version: IBM,VL910_135
Console Login: enable
Auto Restart: true
Full Core: false

AIX> gcc --version
gcc (GCC) 4.2.0
Copyright (C) 2007 Free Software Foundation, Inc.

Linux信息:

Linux> cat /etc/os-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.5 (Maipo)"
ID="rhel"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="7.5"
PRETTY_NAME="Red Hat Enterprise Linux"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:7.5:GA:server"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
REDHAT_BUGZILLA_PRODUCT_VERSION=7.5
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="7.5"

Linux> gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.

标签: clinuxgccfloating-pointaix

解决方案


推荐阅读