My question is: is there an option on gcc Linux that behaves like fast-math on gcc AIX (in this particular case)?
I had to migrate from AIX to Linux, a couple of hundred C and C++ programs. On AIX we used xlc to compile. On Linux we will use gcc. In some programs, we have floating point operations, I know that floating point could give different results on different platforms, but I'm curious about a situation where gcc gives me a different result than xlc (which is normal), when I add --ffast-math option it gives me the result that I want, but on Linux, the same option doesn't have any impact on the result. Here's the code and what I've done:
#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;
}
Compiled with
AIX> /usr/vac/bin/xlc -o redondo.xlc redondo.c
and executed
AIX> redondo.xlc
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4750000000001364]
Then I tried a first step, compiling with gcc on AIX
AIX> gcc -o redondo.gcc redondo.c
executing it, gives a difference on the second result
AIX> redondo.gcc
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4749999999999091]
so I tried -ffast-math
AIX> gcc -ffast-math -o redondo.gcc-ffast-math redondo.c
and executed
AIX> redondo.gcc-ffast-math
/*-----------------*/
Redondeo1 [667.8000000000000682]
Redondeo2 [1250.4750000000001364]
solves the situation ....on AIX. When I tried to replicate the solution on Linux, it always gives the same result:
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]
I know that floating point is non deterministic, not every number is representable, the results depends on the system architecture and the compiler and libraries, etc. etc. .... but perhaps, there's and option on gcc Linux that behaves like fast-math on gcc AIX. I tried a lot of gcc options (frounding-math, mfpmath, contract, store, excess, m387, unsafe, reciprocal, finite, ffast-math) and even reordering the operations, with no success.
AIX info:
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 info:
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.