public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Palmer Dabbelt <palmer@dabbelt.com>
To: jwakely@redhat.com, fantasquex@gmail.com, adhemerval.zanella@linaro.org
Cc: jeffreyalaw@gmail.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: Tue, 07 May 2024 09:24:04 -0700 (PDT)	[thread overview]
Message-ID: <mhng-cddb7452-a13e-4858-8f77-5bfe8a90fa0d@palmer-ri-x1c9> (raw)
In-Reply-To: <CACb0b4mQzytAbe_qMV8EBiO_qvJtbNe8uvpkfMtBY99f9y08TA@mail.gmail.com>

[+Adhemerval and Letu, who handled the glibc side of things, in case 
they have any more context.]

On Tue, 07 May 2024 07:11:08 PDT (-0700), jwakely@redhat.com wrote:
> On Tue, 7 May 2024 at 15:06, Jonathan Wakely wrote:
>>
>> On Tue, 7 May 2024 at 14:57, Jeff Law wrote:
>> >
>> >
>> >
>> > On 5/7/24 7:49 AM, Jonathan Wakely wrote:
>> > > Do we want this change for RISC-V, to fix PR113578?
>> > >
>> > > I haven't tested it on RISC-V, only on x86_64-linux (where it doesn't do
>> > > anything).
>> > >
>> > > -- >8 --
>> > >
>> > > libstdc++-v3/ChangeLog:
>> > >
>> > >       PR libstdc++/113578
>> > >       * include/std/ostream (operator<<(basic_ostream&, float)):
>> > >       Restore signbit after converting to double.
>> > No strong opinion.     One could argue that the existence of a
>> > conditional like that inherently implies the generic code is dependent
>> > on specific processor behavior which probably is unwise.  But again, no
>> > strong opinion.
>>
>> Yes, but I'm not aware of any other processors that lose the signbit
>> like this, so in practice it's always worked fine to cast the float to
>> double.
>
> The similar glibc fix for strfrom is specific to RISC-V:
> https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0cc0033ef19bd3378445c2b851e53d7255cb1b1e

I missed the glibc patch, but IIUC the issue here is NaN 
canonicalization losing sign bits.  Presumably it's OK to lose the other 
bits?  Otherwise we'd need some different twiddling.

Either way, I think having the signed-NaN-preserving conversion is 
reasonable as it's what users are going to expect (even if it's only 
recommended by IEEE).  So

Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>
Acked-by: Palmer Dabbelt <palmer@rivosinc.com>

in case you want to pick it up.  I guess we should backport this too?

Maybe we should also have some sort of arch-independent 
`double __builtin_float_to_double_with_nan_sign_bits(float)` sort of 
thing?  Then we could just use it everywhere rather than duplicating 
this logic all over the place.

> My patch uses copysign unconditionally, to avoid branching on isnan. I
> don't know if that's the right choice.

IMO it's fine: it looks like this can get inlined so having the slightly 
shorter code sequence would help, and it's on an IO path so I doubt 
unconditionally executing the extra conversion instructions really 
matters.

  reply	other threads:[~2024-05-07 16:24 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 [this message]
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
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=mhng-cddb7452-a13e-4858-8f77-5bfe8a90fa0d@palmer-ri-x1c9 \
    --to=palmer@dabbelt.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=fantasquex@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    /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).