This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Builtin function roundeven folding implementation


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

Would the following overhaul be acceptable as the condition is
specialized for -0.5 and +0.5 only. This seems to solve the problem. I
did test the roundeven tests and it passes the tests.

void
real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
        const REAL_VALUE_TYPE *x)
{
  if (is_halfway_below (x))
  {
    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);
}

tests:

/* { 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__);
  return 0;
}

Thanks,
Tejas



On Thu, 22 Aug 2019 at 20:03, Joseph Myers <joseph@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
> joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]