https://gcc.gnu.org/g:2abc365bf623d42d6f50fe532117012890b30602

commit r17-6014-g2abc365bf623d42d6f50fe532117012890b30602
Author: Roger Sayle <[email protected]>
Date:   Sat Nov 7 12:06:50 2026 +0000

    middle-end: PR 128135/PR 128138: Corrections to pow folding.
    
    This patch resolves both PR tree-optimization/128135 and
    PR middle-end/128138, both issues with folding of pow.
    PR 128138 is a regression caused by silly mistakes/typos in
    my recent patch to improve folding of pow(-Inf,x).  PR 128135
    is a much older math-errno issue that seemed reasonable to
    fix at the same time.
    
    2026-11-07  Roger Sayle  <[email protected]>
    
    gcc/ChangeLog
            PR tree-optimization/128135
            PR middle-end/12838
            * match.pd (pow(x,-1) -> 1/x); Only transform when !flag_errno_math
            and let fold_const_pow handle cases where x is constant, including
            appropriate checks for cases where errno may be updated.
            * real.cc (real_isoddinteger): Fix mistakes.  Confirm bits after
            the decimal point are zero, i.e. test for isoddinteger not isodd.
    
    gcc/testsuite/ChangeLog
            PR tree-optimization/128135
            PR middle-end/12838
            * gcc.dg/errno-5.c: New test case.
            * gcc.dg/pr128138.c: Likewise.

Diff:
---
 gcc/match.pd                    |  4 ++--
 gcc/real.cc                     |  8 ++++++--
 gcc/testsuite/gcc.dg/errno-5.c  | 31 +++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr128138.c | 23 +++++++++++++++++++++++
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index c9f5abd9f9a3..82fe9d6824a5 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -10647,9 +10647,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     /* pow(x,1) -> x.  */
     (if (real_equal (value, &dconst1))
      @0)
-    /* pow(x,-1) -> 1/x.  */
+    /* pow(x,-1) -> 1/x.  Let fold_const_pow handle errno checking.  */
     (if (real_equal (value, &dconstm1)
-        && (!flag_errno_math || !real_maybe_zerop (@0)))
+        && !flag_errno_math)
      (rdiv { build_real (type, dconst1); } @0))
     /* pow(x,0.5) -> sqrt(x).  */
     (if (flag_unsafe_math_optimizations
diff --git a/gcc/real.cc b/gcc/real.cc
index d7a6556822aa..3fabad1e1189 100644
--- a/gcc/real.cc
+++ b/gcc/real.cc
@@ -5441,13 +5441,17 @@ real_isoddinteger (const REAL_VALUE_TYPE *c, 
format_helper fmt)
     }
 
   int e = REAL_EXP (c);
-  if (e <= 0 && e > SIGNIFICAND_BITS)
+  if (e <= 0 || e > SIGNIFICAND_BITS)
     return false;
   unsigned int n = SIGNIFICAND_BITS - e;
   int w = n / HOST_BITS_PER_LONG;
+  int i;
+  for (i = 0; i < w; ++i)
+    if (c->sig[i] != 0)
+      return false;
   unsigned int t = n % HOST_BITS_PER_LONG;
   unsigned long mask = (unsigned long)1 << t;
-  return (c->sig[w] & mask) != 0;
+  return (c->sig[w] & (mask|(mask-1))) == mask;
 }
 
 /* Calculate nextafter (X, Y) or nexttoward (X, Y).  Return true if
diff --git a/gcc/testsuite/gcc.dg/errno-5.c b/gcc/testsuite/gcc.dg/errno-5.c
new file mode 100644
index 000000000000..7f0ebba1ad83
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/errno-5.c
@@ -0,0 +1,31 @@
+/* PR tree-optimization/128135 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#ifdef __NO_MATH_ERRNO__
+int main() { return 0; }
+#else
+#include <errno.h>
+#include <math.h>
+
+int main(void)
+{
+#ifdef math_errhandling
+#ifdef MATH_ERRNO
+  if ((math_errhandling & MATH_ERRNO) == 0)
+    return 0;
+#else
+  if ((math_errhandling & 1) == 0)
+    return 0;
+#endif
+#endif
+
+  errno = 0;
+  double r = pow(1e-310, -1.0);
+  if (r != HUGE_VAL)
+    __builtin_abort();
+  if ((math_errhandling & MATH_ERRNO) && errno != ERANGE)
+    __builtin_abort();
+  return 0;
+}
+#endif
diff --git a/gcc/testsuite/gcc.dg/pr128138.c b/gcc/testsuite/gcc.dg/pr128138.c
new file mode 100644
index 000000000000..b451cc4a3d7c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128138.c
@@ -0,0 +1,23 @@
+/* { dg-do link } */
+/* { dg-options "-O2 -fno-math-errno" } */
+
+void link_error ();
+
+double foo(void)
+{
+  return __builtin_pow(-__builtin_inf(), 3.5);
+}
+
+double bar(void)
+{
+  return __builtin_pow(-__builtin_inf(), 1e300);
+}
+
+int main()
+{
+  if (foo() != __builtin_inf())
+    link_error ();
+  if (bar() != __builtin_inf())
+    link_error ();
+  return 0;
+}

Reply via email to