https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103304
Bug ID: 103304
Summary: tgmath returns incorrect results for float fma (fmaf)
Product: gcc
Version: 11.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: oscar.smith at juliacomputing dot com
Target Milestone: ---
In the following program, g++ incorrectly believes that the fma call is a
double precision fma rather than a templated single precision fma, leading to
an incorrect answer due to double rounding. This program produces
-4167095.500000
-4167095.750000
instead of the correct result which would be
-4167095.750000
-4167095.750000
#include <ctgmath>
#include <cstdio>
int main() {
float a = -1.9369631e13f, b = 2.1513551e-7f, c = -1.7354427e-24f;
float f1 = fma(a, b, c);
float f2 = fmaf(a, b, c);
printf("%f\n", f1);
printf("%f\n", f2);
return 0;
}
For comparison, the identical C program
#include <tgmath.h>
#include <stdio.h>
int main() {
float a = -1.9369631e13f, b = 2.1513551e-7f, c = -1.7354427e-24f;
float f1 = fma(a, b, c);
float f2 = fmaf(a, b, c);
printf("%f\n", f1);
printf("%f\n", f2);
return 0;
}
produces
-4167095.750000
-4167095.750000
as expected.
These were compiled with g++ -o testoutcpp test.cpp -march=haswell -Wall
-Wextra -std=c++11
and gcc -o testoutc test.c -march=haswell -Wall -Wextra -lm -std=c11
respectively (although I get the same results for all combinations of flags
that I've tried.