https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128135
Bug ID: 128135
Summary: wrong code at -O0 and above: pow (1e-310, -1.0)
overflows but errno is not set to ERANGE
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kyuwoncho18 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
#include <errno.h>
#include <math.h>
int main(void)
{
errno = 0;
double r = pow(1e-310, -1.0);
if (r != HUGE_VAL)
__builtin_abort();
if ((math_errhandling & MATH_ERRNO) && errno != ERANGE)
__builtin_abort();
}
godbolt: https://godbolt.org/z/j1MhM7obb
1e-310 is subnormal, so pow (1e-310, -1.0) = 1e310 overflows, and C17 7.12.1p5
and POSIX require pow to return HUGE_VAL and set errno to ERANGE when
math_errhandling & MATH_ERRNO is nonzero, as it is with the default
-fmath-errno. With -O0, -O1, -O2, -O3 or -Os no call to pow is emitted, errno
stays 0 and the second check aborts; with -fno-builtin the program exits
normally. With 0.0 instead of 1e-310, trunk keeps the call and the program
exits normally (PR124715; the overflow case was not covered there or in
PR123826 comment 11).