]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
real: Fix roundeven on inf/nan [PR93663]
authorJakub Jelinek <jakub@redhat.com>
Wed, 12 Feb 2020 21:14:27 +0000 (22:14 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 12 Feb 2020 21:15:40 +0000 (22:15 +0100)
As can be seen in the testcase, roundeven with inf or nan arguments
ICE because of those asserts where nothing prevents from is_halfway_below
being called with those arguments.

The following patch fixes that by just returning false for rvc_inf/rvc_nan
like it returns for rvc_zero, so that we handle roundeven with all those
values as round.  Inf/NaN are not halfway in between two integers...

2020-02-12  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/93663
* real.c (is_even): Make static.  Function comment fix.
(is_halfway_below): Make static, don't assert R is not inf/nan,
instead return false for those.  Small formatting fixes.

* gcc.dg/torture/builtin-round-roundeven.c (main): Add tests
for DBL_MAX, inf, their negations and nan.

gcc/ChangeLog
gcc/real.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c

index 3332f56527ec8d2942dee7f78df7d25a1f06eba0..b566ca4b59193ebe8bc2d18cced85d64a276c986 100644 (file)
@@ -1,3 +1,10 @@
+2020-02-12  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/93663
+       * real.c (is_even): Make static.  Function comment fix.
+       (is_halfway_below): Make static, don't assert R is not inf/nan,
+       instead return false for those.  Small formatting fixes.
+
 2020-02-12  Martin Sebor  <msebor@redhat.com>
 
        PR middle-end/93646
index 46d8126efe40f869fe80b7461cc4f14140ca1d8f..00b23ceb41e44a8685f24495299e425d2c018e48 100644 (file)
@@ -5150,10 +5150,10 @@ real_round (REAL_VALUE_TYPE *r, format_helper fmt,
     real_convert (r, fmt, r);
 }
 
-/* Return true including 0 if integer part of R is even, else return
+/* Return true (including 0) if integer part of R is even, else return
    false.  The function is not valid for rvc_inf and rvc_nan classes.  */
 
-bool
+static bool
 is_even (REAL_VALUE_TYPE *r)
 {
   gcc_assert (r->cl != rvc_inf);
@@ -5184,16 +5184,12 @@ is_even (REAL_VALUE_TYPE *r)
 }
 
 /* Return true if R is halfway between two integers, else return
-   false.  The function is not valid for rvc_inf and rvc_nan classes.  */
+   false.  */
 
-bool
+static bool
 is_halfway_below (const REAL_VALUE_TYPE *r)
 {
-  gcc_assert (r->cl != rvc_inf);
-  gcc_assert (r->cl != rvc_nan);
-  int i;
-
-  if (r->cl == rvc_zero)
+  if (r->cl != rvc_normal)
     return false;
 
   /* For numbers (-0.5,0) and (0,0.5).  */
@@ -5205,13 +5201,13 @@ is_halfway_below (const REAL_VALUE_TYPE *r)
       unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
       int w = n / HOST_BITS_PER_LONG;
 
-      for (i = 0; i < w; ++i)
+      for (int i = 0; i < w; ++i)
        if (r->sig[i] != 0)
          return false;
 
-      unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
+      unsigned long num = 1UL << (n % HOST_BITS_PER_LONG);
 
-      if (((r->sig[w] & num) != 0) && ((r->sig[w] & (num-1)) == 0))
+      if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0)
        return true;
     }
   return false;
index c87281281d76ecc4908eec1400abe5329f5a21be..2b4a189c1087190ea3d12578ccc92f7308cdf170 100644 (file)
@@ -1,3 +1,9 @@
+2020-02-12  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/93663
+       * gcc.dg/torture/builtin-round-roundeven.c (main): Add tests
+       for DBL_MAX, inf, their negations and nan.
+
 2020-02-12  Martin Sebor  <msebor@redhat.com>
 
        PR middle-end/93646
index a39ab0c8de2d0fec8e0c768d0bcbf3a46590354c..8ce9d3d65ee065af4476429e5e9d77e0d4aaaf3e 100644 (file)
@@ -18,6 +18,13 @@ main (void)
   TEST(roundeven,  -1.5, -2);
   TEST(roundeven,  3.499, 3);
   TEST(roundeven,  3.501, 4);
+  TEST(roundeven,  __DBL_MAX__, __DBL_MAX__);
+  TEST(roundeven,  -__DBL_MAX__, -__DBL_MAX__);
+  TEST(roundeven,  __builtin_inf (), __builtin_inf ());
+  TEST(roundeven,  -__builtin_inf (), -__builtin_inf ());
+
+  if (!__builtin_isnan (__builtin_roundeven (__builtin_nan (""))))
+    link_error (__LINE__);
 
   if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1)
     link_error (__LINE__);
@@ -31,6 +38,5 @@ main (void)
     link_error (__LINE__);
   if (__builtin_copysign (-1, __builtin_roundeven (0.25)) != 1)
     link_error (__LINE__);
- return 0;
 return 0;
 }
-