public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Joseph Myers <joseph@codesourcery.com>
Cc: Tejas Joshi <tejasjoshi9673@gmail.com>, gcc-patches@gcc.gnu.org
Subject: [PATCH] real: Fix roundeven on inf/nan [PR93663]
Date: Wed, 12 Feb 2020 06:20:00 -0000	[thread overview]
Message-ID: <20200212061901.GV17695@tucnak> (raw)
In-Reply-To: <alpine.DEB.2.21.1908232023080.19272@digraph.polyomino.org.uk>

On Fri, Aug 23, 2019 at 08:38:43PM +0000, Joseph Myers wrote:
> > +/* Return true if R is halfway between two integers, else return
> > +   false.  The function is not valid for rvc_inf and rvc_nan classes.  */
> > +
> > +bool
> > +is_halfway_below (const REAL_VALUE_TYPE *r)
> > +{
> > +  gcc_assert (r->cl != rvc_inf);
> > +  gcc_assert (r->cl != rvc_nan);
> > +  int i;
> 
> Explicitly check for rvc_zero and return false in that case (that seems to 
> be the convention in real.c, rather than relying on code using REAL_EXP to 
> do something sensible for zero, which has REAL_EXP of 0).

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...

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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/real.c.jj	2020-01-23 16:16:16.456601333 +0100
+++ gcc/real.c	2020-02-11 12:40:06.571398420 +0100
@@ -5150,10 +5150,10 @@ real_round (REAL_VALUE_TYPE *r, format_h
     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
       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;
--- gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c.jj	2020-01-12 11:54:37.547396300 +0100
+++ gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c	2020-02-11 12:47:55.346354893 +0100
@@ -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;
 }
-


	Jakub

  parent reply	other threads:[~2020-02-12  6:20 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-28  3:40 [PATCH] Builtin function roundeven folding implementation Tejas Joshi
2019-07-29 16:49 ` Martin Jambor
2019-08-09 21:26 ` Joseph Myers
2019-08-14  7:24   ` Tejas Joshi
2019-08-21 11:52     ` Martin Jambor
2019-08-21 12:48       ` Joseph Myers
2019-08-21 19:10         ` Martin Jambor
2019-08-21 20:50           ` Joseph Myers
2019-08-22 14:40             ` Martin Jambor
2019-08-22 15:52               ` Joseph Myers
2019-08-22 21:39                 ` Tejas Joshi
2019-08-23 12:21                   ` Martin Jambor
2019-08-23 21:24                   ` Joseph Myers
2019-08-25 20:28                     ` Tejas Joshi
2020-02-12  6:20                     ` Jakub Jelinek [this message]
2020-02-12 20:56                       ` [PATCH] real: Fix roundeven on inf/nan [PR93663] Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200212061901.GV17695@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=tejasjoshi9673@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).