public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] Builtin function roundeven folding implementation
       [not found]               ` <alpine.DEB.2.21.1908221426570.27000@digraph.polyomino.org.uk>
@ 2019-08-22 16:28                 ` Tejas Joshi
  2019-08-22 16:35                   ` Joseph Myers
       [not found]                 ` <CACMrGjA3UyUYcShruM-tiT=rqBiSLX9cmSVmpX0G6SfOq_r4EQ@mail.gmail.com>
  1 sibling, 1 reply; 6+ messages in thread
From: Tejas Joshi @ 2019-08-22 16:28 UTC (permalink / raw)
  To: gcc; +Cc: Martin Jambor, hubicka, joseph

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Builtin function roundeven folding implementation
  2019-08-22 16:28                 ` [PATCH] Builtin function roundeven folding implementation Tejas Joshi
@ 2019-08-22 16:35                   ` Joseph Myers
  2019-08-22 16:56                     ` Tejas Joshi
  0 siblings, 1 reply; 6+ messages in thread
From: Joseph Myers @ 2019-08-22 16:35 UTC (permalink / raw)
  To: Tejas Joshi; +Cc: gcc, Martin Jambor, hubicka

On Thu, 22 Aug 2019, Tejas Joshi wrote:

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

I think that would be reasonable with a comment added to explain that it's 
ensuring the correct sign of a zero result.

>   if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1)
>     link_error (__LINE__);

I think you should have at least four tests of sign of zero result 
(arguments -0.5, -0.0, 0.0 and 0.5).  Probably also tests of values 
between +/- 0.5 and 0, e.g. test -0.25 and 0.25 as well.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Builtin function roundeven folding implementation
  2019-08-22 16:35                   ` Joseph Myers
@ 2019-08-22 16:56                     ` Tejas Joshi
  2019-08-22 17:00                       ` Joseph Myers
  0 siblings, 1 reply; 6+ messages in thread
From: Tejas Joshi @ 2019-08-22 16:56 UTC (permalink / raw)
  To: gcc; +Cc: Martin Jambor, hubicka, joseph

> I think you should have at least four tests of sign of zero result
> (arguments -0.5, -0.0, 0.0 and 0.5).  Probably also tests of values
> between +/- 0.5 and 0, e.g. test -0.25 and 0.25 as well.

Okay, I have made the following changes and again, the tests pass for roundeven.

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);
}

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__);
  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;
}

Thanks,
Tejas


On Thu, 22 Aug 2019 at 22:05, Joseph Myers <joseph@codesourcery.com> wrote:
>
> On Thu, 22 Aug 2019, Tejas Joshi wrote:
>
> > > 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.
>
> I think that would be reasonable with a comment added to explain that it's
> ensuring the correct sign of a zero result.
>
> >   if (__builtin_copysign (1, __builtin_roundeven (-0.5)) != -1)
> >     link_error (__LINE__);
>
> I think you should have at least four tests of sign of zero result
> (arguments -0.5, -0.0, 0.0 and 0.5).  Probably also tests of values
> between +/- 0.5 and 0, e.g. test -0.25 and 0.25 as well.
>
> --
> Joseph S. Myers
> joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Builtin function roundeven folding implementation
  2019-08-22 16:56                     ` Tejas Joshi
@ 2019-08-22 17:00                       ` Joseph Myers
  0 siblings, 0 replies; 6+ messages in thread
From: Joseph Myers @ 2019-08-22 17:00 UTC (permalink / raw)
  To: Tejas Joshi; +Cc: gcc, Martin Jambor, hubicka

On Thu, 22 Aug 2019, Tejas Joshi wrote:

> > I think you should have at least four tests of sign of zero result
> > (arguments -0.5, -0.0, 0.0 and 0.5).  Probably also tests of values
> > between +/- 0.5 and 0, e.g. test -0.25 and 0.25 as well.
> 
> Okay, I have made the following changes and again, the tests pass for roundeven.

Thanks, I look forward to seeing a full patch with these revisions on 
gcc-patches.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Builtin function roundeven folding implementation
       [not found]                   ` <alpine.DEB.2.21.1908232023080.19272@digraph.polyomino.org.uk>
@ 2019-08-25  8:25                     ` Tejas Joshi
  2019-08-25  9:38                       ` Martin Jambor
  0 siblings, 1 reply; 6+ messages in thread
From: Tejas Joshi @ 2019-08-25  8:25 UTC (permalink / raw)
  To: gcc; +Cc: Martin Jambor, hubicka, joseph

I have made the respective changes and fixed the indentations and it
passes the testing.

> I encourage a followup looking for and fixing further places in the source
> tree that handle round-to-integer function families (ceil / floor / trunc
> / round / rint / nearbyint) and should handle roundeven as well, as that
> would lead to more optimization of roundeven calls.  Such places aren't
> that easy to search for because most of those names are common words used
> in other contexts in the compiler.  But, for example, match.pd has
> patterns

I will follow up to make these optimizations for sure.

Thanks,
Tejas


On Sat, 24 Aug 2019 at 02:08, Joseph Myers <joseph@codesourcery.com> wrote:
>
> On Fri, 23 Aug 2019, Tejas Joshi wrote:
>
> > 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)
>
> This should use CASE_MATHFN_FLOATN, as for the other round-to-integer
> functions.
>
> > +  /* 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;
>
> Fix the indentation here (the braces should be indented two columns from
> the "else", the contents then two columns from the braces).
>
> > +  }
> > +
> > +  else
>
> And remove the stray blank line before "else".
>
> > +/* 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).
>
> > +  else if (REAL_EXP (r) < SIGNIFICAND_BITS)
> > +  {
>
> Another place to fix indentation.
>
> > +void
> > +real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
> > +             const REAL_VALUE_TYPE *x)
> > +{
> > +  if (is_halfway_below (x))
> > +  {
>
> Again, fix indentation throughout this function.
>
> The patch is OK with those fixes, assuming the fixed patch passes testing.
> I encourage a followup looking for and fixing further places in the source
> tree that handle round-to-integer function families (ceil / floor / trunc
> / round / rint / nearbyint) and should handle roundeven as well, as that
> would lead to more optimization of roundeven calls.  Such places aren't
> that easy to search for because most of those names are common words used
> in other contexts in the compiler.  But, for example, match.pd has
> patterns
>
> /* trunc(trunc(x)) -> trunc(x), etc.  */
>
> /* f(x) -> x if x is integer valued and f does nothing for such values.  */
>
>  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
>
>  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
>     if x is a float.  */
>
> which should apply to roundeven as well.
>
> --
> Joseph S. Myers
> joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Builtin function roundeven folding implementation
  2019-08-25  8:25                     ` Tejas Joshi
@ 2019-08-25  9:38                       ` Martin Jambor
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Jambor @ 2019-08-25  9:38 UTC (permalink / raw)
  To: Tejas Joshi, gcc; +Cc: hubicka, joseph

Hi,

On Sun, Aug 25 2019, Tejas Joshi wrote:
> I have made the respective changes and fixed the indentations and it
> passes the testing.

Great, please send the patch (to me and to the mailing list too), so
that I can commit it.

Thanks,

Martin

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-08-25  9:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CACMrGjCu54zN+Smc7+3JF8MBZV76HXwaP-vk1HFTQGhRc=Toww@mail.gmail.com>
     [not found] ` <alpine.DEB.2.21.1908092021090.31221@digraph.polyomino.org.uk>
     [not found]   ` <CACMrGjCPNr+Hjz--CL6ocV5y3GqGp13fpQCXtXJT4qeGQWk86A@mail.gmail.com>
     [not found]     ` <ri636hug2zr.fsf@suse.cz>
     [not found]       ` <alpine.DEB.2.21.1908211156360.6172@digraph.polyomino.org.uk>
     [not found]         ` <ri6pnkye7ll.fsf@suse.cz>
     [not found]           ` <alpine.DEB.2.21.1908211906340.25161@digraph.polyomino.org.uk>
     [not found]             ` <ri6ftlte12y.fsf@suse.cz>
     [not found]               ` <alpine.DEB.2.21.1908221426570.27000@digraph.polyomino.org.uk>
2019-08-22 16:28                 ` [PATCH] Builtin function roundeven folding implementation Tejas Joshi
2019-08-22 16:35                   ` Joseph Myers
2019-08-22 16:56                     ` Tejas Joshi
2019-08-22 17:00                       ` Joseph Myers
     [not found]                 ` <CACMrGjA3UyUYcShruM-tiT=rqBiSLX9cmSVmpX0G6SfOq_r4EQ@mail.gmail.com>
     [not found]                   ` <alpine.DEB.2.21.1908232023080.19272@digraph.polyomino.org.uk>
2019-08-25  8:25                     ` Tejas Joshi
2019-08-25  9:38                       ` Martin Jambor

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