clang gets numerical underflow wrong, please fix.
Steve Kargl
sgk at troutmask.apl.washington.edu
Mon Mar 14 01:53:12 UTC 2016
On Mon, Mar 14, 2016 at 01:02:20AM +0100, Dimitry Andric wrote:
>
> $ gcc -O overflow-iter.c -o overflow-iter-gcc -lm
> $ ./overflow-iter-gcc
> FE_OVERFLOW: x = inf after 1024 iterations
> $ gcc -O2 overflow-iter.c -o overflow-iter-gcc -lm
> $ ./overflow-iter-gcc
> FE_OVERFLOW: x = inf after 16384 iterations
>
Change the program to
#include <fenv.h>
#include <stdio.h>
int
main(void)
{
int i;
float x = 1.f;
i = 0;
feclearexcept(FE_ALL_EXCEPT);
do {
x *= 2;
i++;
printf("%d %e\n", i, x);
} while(!fetestexcept(FE_OVERFLOW));
if (fetestexcept(FE_OVERFLOW)) printf("FE_UNDERFLOW: ");
printf("x = %e after %d iterations\n", x, i);
return 0;
}
You'll get a bunch of invalid output before the OVERFLOW.
% cc -O -o z b.c -lm && ./z | tail
1016 7.022239e+305 <-- not a valid float
1017 1.404448e+306 <-- not a valid float
1018 2.808896e+306 <-- not a valid float
1019 5.617791e+306 <-- not a valid float
1020 1.123558e+307 <-- not a valid float
1021 2.247116e+307 <-- not a valid float
1022 4.494233e+307 <-- not a valid float
1023 8.988466e+307 <-- not a valid float
1024 inf
FE_UNDERFLOW: x = inf after 1024 iterations
Clang is broken with or without #pragma FENV_ACCESS "on".
--
Steve
More information about the freebsd-toolchain
mailing list