Hi,
This is a full patch for the roundeven variants along with
documentation and additional testcases. The following code also
conforms to GNU's coding standards.

Thanks,
Tejas

2019-08-22  Tejas Joshi  <tejasjoshi9...@gmail.com>
            Martin Jambor  <mjam...@suse.cz>

        * builtins.c (mathfn_built_in_2): Added CASE_MATHFN for ROUNDEVEN.
        * builtins.def: Added function definitions for roundeven function
        variants.
        * fold-const-call.c (fold_const_call_ss): Added case for roundeven
        function call.
        * fold-const.c (negate_mathfn_p): Added case for roundeven function.
        (tree_call_nonnegative_warnv_p): Added case for roundeven function.
        (integer_valued_real_call_p): Added case for roundeven function.
        * real.c (is_even): New function. Returns true if real number is even,
        otherwise returns false.
        (is_halfway_below): New function. Returns true if real number is
        halfway between two integers, else return false.
        (real_roundeven): New function. Round real number to nearest integer,
        rounding halfway cases towards even.
        * real.h (real_value): Added descriptive comments.  Added function
        declaration for roundeven function.
        * doc/extend.texi (Other Builtins): List roundeven variants among
        functions which can be handled as builtins.

gcc/testsuite/ChangeLog:

2019-08-21  Tejas Joshi  <tejasjoshi9...@gmail.com>

        * gcc.dg/torture/builtin-round-roundeven.c: New test.
        * gcc.dg/torture/builtin-round-roundevenf128.c: New test.

On Thu, 22 Aug 2019 at 20:03, Joseph Myers <jos...@codesourcery.com> wrote:
>
> On Thu, 22 Aug 2019, Martin Jambor wrote:
>
> > +/* Round X to nearest integer, rounding halfway cases towards even.  */
> > +
> > +void
> > +real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
> > +             const REAL_VALUE_TYPE *x)
> > +{
> > +  if (is_halfway_below (x))
> > +  {
> > +    do_add (r, x, &dconsthalf, x->sign);
> > +    if (!is_even (r))
> > +      do_add (r, r, &dconstm1, x->sign);
>
> I'm concerned that this would produce +0.0 for an argument of -0.5 (via
> -0.5 - 0.5 - -1.0 producing +0.0) when it needs to produce -0.0.
>
> Note that testcases for the sign of zero results need to check e.g.
> !!__builtin_signbit on the result, or the result of calling
> __builtin_copysign* to extract the sign of the result, since 0.0 == -0.0
> so checking with ==, while necessary, is not sufficient in that case.
>
> --
> Joseph S. Myers
> jos...@codesourcery.com
diff --git a/gcc/builtins.c b/gcc/builtins.c
index 9a766e4ad63..5149d901a96 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -2056,6 +2056,7 @@ mathfn_built_in_2 (tree type, combined_fn fn)
     CASE_MATHFN (REMQUO)
     CASE_MATHFN_FLOATN (RINT)
     CASE_MATHFN_FLOATN (ROUND)
+    CASE_MATHFN (ROUNDEVEN)
     CASE_MATHFN (SCALB)
     CASE_MATHFN (SCALBLN)
     CASE_MATHFN (SCALBN)
diff --git a/gcc/builtins.def b/gcc/builtins.def
index 6d41bdb4f44..8bb7027aac7 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -542,12 +542,18 @@ DEF_C99_BUILTIN        (BUILT_IN_RINTL, "rintl", BT_FN_LONGDOUBLE_LONGDOUBLE, AT
 #define RINT_TYPE(F) BT_FN_##F##_##F
 DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_RINT, "rint", RINT_TYPE, ATTR_CONST_NOTHROW_LEAF_LIST)
 #undef RINT_TYPE
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ROUNDEVEN, "roundeven", BT_FN_DOUBLE_DOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ROUNDEVENF, "roundevenf", BT_FN_FLOAT_FLOAT, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_ROUNDEVENL, "roundevenl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_C99_BUILTIN        (BUILT_IN_ROUND, "round", BT_FN_DOUBLE_DOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_C99_BUILTIN        (BUILT_IN_ROUNDF, "roundf", BT_FN_FLOAT_FLOAT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_C99_BUILTIN        (BUILT_IN_ROUNDL, "roundl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
 #define ROUND_TYPE(F) BT_FN_##F##_##F
 DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ROUND, "round", ROUND_TYPE, ATTR_CONST_NOTHROW_LEAF_LIST)
 #undef ROUND_TYPE
+#define ROUNDEVEN_TYPE(F) BT_FN_##F##_##F
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ROUNDEVEN, "roundeven", ROUNDEVEN_TYPE, ATTR_CONST_NOTHROW_LEAF_LIST)
+#undef ROUNDEVEN_TYPE
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_SCALB, "scalb", BT_FN_DOUBLE_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_SCALBF, "scalbf", BT_FN_FLOAT_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_SCALBL, "scalbl", BT_FN_LONGDOUBLE_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 235be99abcb..4aea4d31761 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -12448,7 +12448,8 @@ Outside strict ISO C mode (@option{-ansi}, @option{-std=c90},
 @code{j1f}, @code{j1l}, @code{j1}, @code{jnf}, @code{jnl}, @code{jn},
 @code{lgammaf_r}, @code{lgammal_r}, @code{lgamma_r}, @code{mempcpy},
 @code{pow10f}, @code{pow10l}, @code{pow10}, @code{printf_unlocked},
-@code{rindex}, @code{scalbf}, @code{scalbl}, @code{scalb},
+@code{rindex}, @code{roundeven}, @code{roundevenf}, @code{roudnevenl},
+@code{scalbf}, @code{scalbl}, @code{scalb},
 @code{signbit}, @code{signbitf}, @code{signbitl}, @code{signbitd32},
 @code{signbitd64}, @code{signbitd128}, @code{significandf},
 @code{significandl}, @code{significand}, @code{sincosf},
diff --git a/gcc/fold-const-call.c b/gcc/fold-const-call.c
index e21d8e11072..3a14d2a41c1 100644
--- a/gcc/fold-const-call.c
+++ b/gcc/fold-const-call.c
@@ -836,7 +836,7 @@ fold_const_call_ss (real_value *result, combined_fn fn,
 
     CASE_CFN_FLOOR:
     CASE_CFN_FLOOR_FN:
-      if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
+      if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
 	{
 	  real_floor (result, format, arg);
 	  return true;
@@ -845,7 +845,7 @@ fold_const_call_ss (real_value *result, combined_fn fn,
 
     CASE_CFN_CEIL:
     CASE_CFN_CEIL_FN:
-      if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
+      if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
 	{
 	  real_ceil (result, format, arg);
 	  return true;
@@ -854,18 +854,31 @@ fold_const_call_ss (real_value *result, combined_fn fn,
 
     CASE_CFN_TRUNC:
     CASE_CFN_TRUNC_FN:
-      real_trunc (result, format, arg);
-      return true;
+      if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
+	{
+	  real_trunc (result, format, arg);
+	  return true;
+	}
+      return false;
 
     CASE_CFN_ROUND:
     CASE_CFN_ROUND_FN:
-      if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
+      if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
 	{
 	  real_round (result, format, arg);
 	  return true;
 	}
       return false;
 
+    CASE_CFN_ROUNDEVEN:
+    CASE_CFN_ROUNDEVEN_FN:
+      if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
+	{
+	  real_roundeven (result, format, arg);
+	  return true;
+	}
+      return false;
+
     CASE_CFN_LOGB:
       return fold_const_logb (result, arg, format);
 
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 8c711aba12a..0376cdb73a4 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -329,6 +329,8 @@ negate_mathfn_p (combined_fn fn)
     CASE_CFN_LLROUND:
     CASE_CFN_LROUND:
     CASE_CFN_ROUND:
+    CASE_CFN_ROUNDEVEN:
+    CASE_CFN_ROUNDEVEN_FN:
     CASE_CFN_SIN:
     CASE_CFN_SINH:
     CASE_CFN_TAN:
@@ -13107,6 +13109,8 @@ tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
     CASE_CFN_RINT_FN:
     CASE_CFN_ROUND:
     CASE_CFN_ROUND_FN:
+    CASE_CFN_ROUNDEVEN:
+    CASE_CFN_ROUNDEVEN_FN:
     CASE_CFN_SCALB:
     CASE_CFN_SCALBLN:
     CASE_CFN_SCALBN:
@@ -13630,6 +13634,8 @@ integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
     CASE_CFN_RINT_FN:
     CASE_CFN_ROUND:
     CASE_CFN_ROUND_FN:
+    CASE_CFN_ROUNDEVEN:
+    CASE_CFN_ROUNDEVEN_FN:
     CASE_CFN_TRUNC:
     CASE_CFN_TRUNC_FN:
       return true;
diff --git a/gcc/real.c b/gcc/real.c
index 0164f097a53..b252609e197 100644
--- a/gcc/real.c
+++ b/gcc/real.c
@@ -5010,6 +5010,99 @@ 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
+   false.  The function is not valid for rvc_inf and rvc_nan classes.  */
+
+bool
+is_even (REAL_VALUE_TYPE *r)
+{
+  gcc_assert (r->cl != rvc_inf);
+  gcc_assert (r->cl != rvc_nan);
+
+  if (r->cl == rvc_zero)
+    return true;
+
+  /* For (-1,1), number is even.  */
+  if (REAL_EXP (r) <= 0)
+    return true;
+
+  /* Check lowest bit, if not set, return true.  */
+  else if (REAL_EXP (r) <= SIGNIFICAND_BITS)
+  {
+    unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r);
+    int w = n / HOST_BITS_PER_LONG;
+
+    unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
+
+    if ((r->sig[w] & num) == 0)
+      return true;
+  }
+
+  else
+    return true;
+
+  return false;
+}
+
+/* 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;
+
+  /* For numbers (-0.5,0) and (0,0.5).  */
+  if (REAL_EXP (r) < 0)
+    return false;
+
+  else if (REAL_EXP (r) < SIGNIFICAND_BITS)
+  {
+    unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
+    int w = n / HOST_BITS_PER_LONG;
+
+    for (i = 0; i < w; ++i)
+      if (r->sig[i] != 0)
+	return false;
+
+    unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
+
+    if (((r->sig[w] & num) != 0) && ((r->sig[w] & (num-1)) == 0))
+      return true;
+  }
+  return false;
+}
+
+/* Round X to nearest integer, rounding halfway cases towards even.  */
+
+void
+real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
+		const REAL_VALUE_TYPE *x)
+{
+  if (is_halfway_below (x))
+  {
+    /* Special case as -0.5 rounds to -0.0 and
+       similarly +0.5 rounds to +0.0.  */
+    if (REAL_EXP (x) == 0)
+    {
+      *r = *x;
+      clear_significand_below (r, SIGNIFICAND_BITS);
+    }
+    else
+    {
+      do_add (r, x, &dconsthalf, x->sign);
+      if (!is_even (r))
+	do_add (r, r, &dconstm1, x->sign);
+    }
+    if (fmt)
+      real_convert (r, fmt, r);
+  }
+  else
+    real_round (r, fmt, x);
+}
+
 /* Set the sign of R to the sign of X.  */
 
 void
diff --git a/gcc/real.h b/gcc/real.h
index 95b9db83d24..2f41834ecfd 100644
--- a/gcc/real.h
+++ b/gcc/real.h
@@ -41,11 +41,18 @@ struct GTY(()) real_value {
      sure they're packed together, otherwise REAL_VALUE_TYPE_SIZE will
      be miscomputed.  */
   unsigned int /* ENUM_BITFIELD (real_value_class) */ cl : 2;
+  /* 1 if number is decimal floating point.  */
   unsigned int decimal : 1;
+  /* 1 if number is negative.  */
   unsigned int sign : 1;
+  /* 1 if number is signalling.  */
   unsigned int signalling : 1;
+  /* 1 if number is canonical
+  All are generally used for handling cases in real.c.  */
   unsigned int canonical : 1;
+  /* unbiased exponent of the number.  */
   unsigned int uexp : EXP_BITS;
+  /* significand of the number.  */
   unsigned long sig[SIGSZ];
 };
 
@@ -500,6 +507,8 @@ extern void real_ceil (REAL_VALUE_TYPE *, format_helper,
 		       const REAL_VALUE_TYPE *);
 extern void real_round (REAL_VALUE_TYPE *, format_helper,
 			const REAL_VALUE_TYPE *);
+extern void real_roundeven (REAL_VALUE_TYPE *, format_helper,
+			    const REAL_VALUE_TYPE *);
 
 /* Set the sign of R to the sign of X.  */
 extern void real_copysign (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c b/gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c
new file mode 100644
index 00000000000..a39ab0c8de2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/builtin-round-roundeven.c
@@ -0,0 +1,36 @@
+/* { dg-do link } */
+
+extern int link_error (int);
+
+#define TEST(FN, VALUE, RESULT) \
+  if (__builtin_##FN (VALUE) != RESULT) link_error (__LINE__);
+
+int
+main (void)
+{
+  TEST(roundeven,  0, 0);
+  TEST(roundeven,  0.5, 0);
+  TEST(roundeven,  -0.5, 0);
+  TEST(roundeven,  6, 6);
+  TEST(roundeven,  -8, -8);
+  TEST(roundeven,  2.5, 2);
+  TEST(roundeven,  3.5, 4);
+  TEST(roundeven,  -1.5, -2);
+  TEST(roundeven,  3.499, 3);
+  TEST(roundeven,  3.501, 4);
+
+  if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1)
+    link_error (__LINE__);
+  if (__builtin_copysign (1, __builtin_roundeven (-0.0)) != -1)
+    link_error (__LINE__);
+  if (__builtin_copysign (-1, __builtin_roundeven (0.5)) != 1)
+    link_error (__LINE__);
+  if (__builtin_copysign (-1, __builtin_roundeven (0.0)) != 1)
+    link_error (__LINE__);
+  if (__builtin_copysign (1, __builtin_roundeven (-0.25)) != -1)
+    link_error (__LINE__);
+  if (__builtin_copysign (-1, __builtin_roundeven (0.25)) != 1)
+    link_error (__LINE__);
+ return 0;
+}
+
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-round-roundevenf128.c b/gcc/testsuite/gcc.dg/torture/builtin-round-roundevenf128.c
new file mode 100644
index 00000000000..85a8cbfd532
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/builtin-round-roundevenf128.c
@@ -0,0 +1,21 @@
+/* { dg-do link } */
+/* { dg-add-options float128 } */
+/* { dg-require-effective-target float128 } */
+
+extern int link_error (int);
+
+#define TEST(FN, VALUE, RESULT) \
+  if (__builtin_##FN##f128 (VALUE) != RESULT) link_error (__LINE__);
+
+int
+main (void)
+{
+  TEST(roundeven,  (0x1p64+0.5f128), (0x1p64f128));
+  TEST(roundeven,  (0x1p63+0.5f128), (0x1p63f128));
+  TEST(roundeven,  (0x1p63-0.5f128), (0x1p63f128));
+  TEST(roundeven,  (0x1p64-0.5f128), (0x1p64f128));
+  TEST(roundeven,  (0x1p64+0.501f128), (0x1p64+1.0f128));
+  TEST(roundeven,  (0x1.C00000000000039A5653p1f128), (0x1p2f128))
+  return 0;
+}
+

Reply via email to