public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Waterman <andrew@sifive.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Jeff Law <jeffreyalaw@gmail.com>, Andreas Schwab <schwab@suse.de>,
	 Jonathan Wakely <jwakely@redhat.com>,
	libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH][risc-v] libstdc++: Preserve signbit of nan when converting float to double [PR113578]
Date: Wed, 8 May 2024 03:32:56 -0700	[thread overview]
Message-ID: <CA++6G0DnvO9q=p81J=_CA7DChW8VaN9zgN_ZzZs6cpAMPHOshA@mail.gmail.com> (raw)
In-Reply-To: <CAH6eHdSm=tfUKyrpX1KOOoAFk4kFp1sbi51FEr7GwJvS4AtBcQ@mail.gmail.com>

On Tue, May 7, 2024 at 9:46 AM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> On Tue, 7 May 2024 at 17:39, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> >
> > On Tue, 7 May 2024 at 17:33, Jeff Law wrote:
> > >
> > >
> > >
> > > On 5/7/24 9:36 AM, Andreas Schwab wrote:
> > > > On Mai 07 2024, Jonathan Wakely wrote:
> > > >
> > > >> +#ifdef __riscv
> > > >> +    return _M_insert(__builtin_copysign((double)__f,
> > > >> +                                        (double)-__builtin_signbit(__f));
> > > >
> > > > Should this use static_cast<double>?
> >
> > Meh. It wouldn't fit in 80 columns any more with static_cast, and it
> > means exactly the same thing.
> >
> > > And it's missing a close paren.
> >
> > Now that's more important! Thanks.
>
> Also, I've just realised that signbit might return a negative value if
> the signbit is set. The spec only says it returns non-zero if the
> signbit is set.
>
> So maybe we want:
>
> #ifdef __riscv
>         const int __neg = __builtin_signbit(__f) ? -1 : 0;
>         return _M_insert(__builtin_copysign(static_cast<double>(__f),
>                                               static_cast<double>(__neg)));
> #else
>         return _M_insert(static_cast<double>(__f));
> #endif

We can avoid the signbit call altogether by taking advantage of the
fact that type-punning the float to an int, then converting that int
to a double, will produce a double with the sign of the original
value, with no exceptions raised in the process.  (I don't know
whether we're allowed to use std::bit_cast in this context, but a
type-punning memcpy would have the same effect.)

  int __i = std::bit_cast<int, float>(__f);
  return _M_insert(__builtin_copysign(static_cast<double>(__f),
static_cast<double>(__i)));

Empirically, this saves 3 instructions on RV64 or 1 instruction on
RV32 (as measured on GCC 13.2.0).  Note, I'm not trying to drag-race
on performance here.  Rather, I'm trying to minimize the extent to
which this RISC-V idiosyncrasy results in static code-size bloat.

BTW, I agree with Palmer that adding a __builtin with these semantics
seems advisable if this pattern turns out to recur frequently.

  reply	other threads:[~2024-05-08 10:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-07 13:49 Jonathan Wakely
2024-05-07 13:57 ` Jeff Law
2024-05-07 14:06   ` Jonathan Wakely
2024-05-07 14:11     ` Jonathan Wakely
2024-05-07 16:24       ` Palmer Dabbelt
2024-05-10 10:58       ` Jonathan Wakely
2024-05-07 15:25     ` Jeff Law
2024-05-07 15:36 ` Andreas Schwab
2024-05-07 16:31   ` Jeff Law
2024-05-07 16:39     ` Jonathan Wakely
2024-05-07 16:45       ` Jonathan Wakely
2024-05-08 10:32         ` Andrew Waterman [this message]
2024-05-08 10:40           ` Jonathan Wakely

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='CA++6G0DnvO9q=p81J=_CA7DChW8VaN9zgN_ZzZs6cpAMPHOshA@mail.gmail.com' \
    --to=andrew@sifive.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=schwab@suse.de \
    /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).