public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/113578] New: Incorrect signbit for -nan on RISC-V
@ 2024-01-24 11:40 redi at gcc dot gnu.org
  2024-01-24 11:41 ` [Bug target/113578] Incorrect sign printed " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-24 11:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

            Bug ID: 113578
           Summary: Incorrect signbit for -nan on RISC-V
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
                CC: doko at gcc dot gnu.org
  Target Milestone: ---
            Target: riscv64-linux-gnu

int main()
{
  float f = std::bit_cast<float>(0xfff80000);
  __builtin_printf("%f %d\n", f, __builtin_signbit(f));
}


With no optimization this prints:

nan 1


With optimization it prints:

-nan 1


i.e. signbit is always set, but sometimes the nan is printed as positive.

g++ -v:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-linux-gnu/11/lto-wrapper
Target: riscv64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-11
--program-prefix=riscv64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-libitm --disable-libquadmath
--disable-libquadmath-support --enable-plugin --enable-default-pie
--with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --disable-multilib --with-arch=rv64gc --with-abi=lp64d
--enable-checking=release --build=riscv64-linux-gnu --host=riscv64-linux-gnu
--target=riscv64-linux-gnu --with-build-config=bootstrap-lto-lean
--enable-link-serialization=4
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 

Originally reported to
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1059340

I've only tested this with Ubuntu's gcc, I haven't tried a build of upstream
GCC, nor a newer version. Sorry if it's Debian specific.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
@ 2024-01-24 11:41 ` redi at gcc dot gnu.org
  2024-01-24 12:28 ` jsm28 at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-24 11:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Incorrect signbit for -nan  |Incorrect sign printed for
                   |on RISC-V                   |-nan on RISC-V

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It could also be a glibc bug for RISC-V of course.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
  2024-01-24 11:41 ` [Bug target/113578] Incorrect sign printed " redi at gcc dot gnu.org
@ 2024-01-24 12:28 ` jsm28 at gcc dot gnu.org
  2024-01-24 17:34 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2024-01-24 12:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #2 from Joseph S. Myers <jsm28 at gcc dot gnu.org> ---
If a conversion from float to double (for passing in variable arguments) occurs
at runtime on RISC-V, that will produce a positive-signed NaN (that's what the
RISC-V floating-point instructions do). Cf.
https://sourceware.org/bugzilla/show_bug.cgi?id=29501 (for strfrom functions).

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
  2024-01-24 11:41 ` [Bug target/113578] Incorrect sign printed " redi at gcc dot gnu.org
  2024-01-24 12:28 ` jsm28 at gcc dot gnu.org
@ 2024-01-24 17:34 ` pinskia at gcc dot gnu.org
  2024-01-24 17:42 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-24 17:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
and since __builtin_printf is variadic function, floats are promoted to double
causing the removal of the payload of NaN on RISCV at runtime ...

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-01-24 17:34 ` pinskia at gcc dot gnu.org
@ 2024-01-24 17:42 ` redi at gcc dot gnu.org
  2024-01-24 17:56 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-24 17:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
So the reason the sign isn't lost for -O1 is that the conversion to double
happens at compile-time and a negative nan is compiled into the executable,
rather than being converted by the processor at runtime.

Huh, OK. I guess Matthias can close his Debian bug then.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-01-24 17:42 ` redi at gcc dot gnu.org
@ 2024-01-24 17:56 ` redi at gcc dot gnu.org
  2024-05-10 11:48 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-24 17:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Maybe we could make it work for std::ostream with something like:

--- a/libstdc++-v3/include/std/ostream
+++ b/libstdc++-v3/include/std/ostream
@@ -233,7 +233,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
        // _GLIBCXX_RESOLVE_LIB_DEFECTS
        // 117. basic_ostream uses nonexistent num_put member functions.
+#ifdef __riscv
+       return _M_insert(__builtin_copysign((double)__f,
+                                           (double)-!!__builtin_signbit(__f));
+#else
        return _M_insert(static_cast<double>(__f));
+#endif
       }

       __ostream_type&

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-01-24 17:56 ` redi at gcc dot gnu.org
@ 2024-05-10 11:48 ` redi at gcc dot gnu.org
  2024-05-10 21:32 ` andrew at sifive dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-05-10 11:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Joseph S. Myers from comment #2)
> If a conversion from float to double (for passing in variable arguments)
> occurs at runtime on RISC-V, that will produce a positive-signed NaN (that's
> what the RISC-V floating-point instructions do). Cf.
> https://sourceware.org/bugzilla/show_bug.cgi?id=29501 (for strfrom
> functions).

This seems to happen on Apple M1 chips too.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-05-10 11:48 ` redi at gcc dot gnu.org
@ 2024-05-10 21:32 ` andrew at sifive dot com
  2024-05-13 11:09 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: andrew at sifive dot com @ 2024-05-10 21:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

Andrew Waterman <andrew at sifive dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrew at sifive dot com

--- Comment #7 from Andrew Waterman <andrew at sifive dot com> ---
In the case of the M1, I don't think the problem is that the sign bit isn't
making it to printf; I think printf is choosing not to print the sign.  The
ARMv8 fcvt instruction preserves the sign bit when converting NaNs (unless the
processor is in default-NaN mode, which Mac OS does not seem to use).

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-05-10 21:32 ` andrew at sifive dot com
@ 2024-05-13 11:09 ` redi at gcc dot gnu.org
  2024-05-13 22:36 ` andrew at sifive dot com
  2024-05-15 11:13 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-05-13 11:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Hmm, you could be right. It looks like the printf in Apple libc doesn't print
"-nan" even when not converting from float to double.

According to
https://developer.r-project.org/Blog/content/post/2020-11-02-apple-silicon.html
the Apple silicon NaN payloads are not propagated by default, but I don't know
if that means the signbit is not propagated. I assumed it meant they were
running in default NaN mode.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2024-05-13 11:09 ` redi at gcc dot gnu.org
@ 2024-05-13 22:36 ` andrew at sifive dot com
  2024-05-15 11:13 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: andrew at sifive dot com @ 2024-05-13 22:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

--- Comment #9 from Andrew Waterman <andrew at sifive dot com> ---
For my M1 running Ventura 13.6, NaN payloads _are_ propagated, sign bit
included. This test prints fffc0080:

int main()
{
  volatile long long ll = 0xffff801000000000;
  volatile double d;
  memcpy((char*)&d, (char*)&ll, sizeof(ll));

  volatile float f = d;
  volatile int i;
  memcpy((char*)&i, (char*)&f, sizeof(i));

  printf("%x\n", i);

  return 0;
}

So perhaps Apple has switched from default-NaN mode to NaN-propagating mode in
the time since that R blog post was written.

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

* [Bug target/113578] Incorrect sign printed for -nan on RISC-V
  2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2024-05-13 22:36 ` andrew at sifive dot com
@ 2024-05-15 11:13 ` redi at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2024-05-15 11:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113578

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-05-15
         Resolution|INVALID                     |---
             Status|RESOLVED                    |REOPENED
     Ever confirmed|0                           |1

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I'm reopening this to deal with it as a libstdc++ bug, because LWG seems to
support changing the standard so that this Just Works.
https://cplusplus.github.io/LWG/issue4101

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

end of thread, other threads:[~2024-05-15 11:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-24 11:40 [Bug target/113578] New: Incorrect signbit for -nan on RISC-V redi at gcc dot gnu.org
2024-01-24 11:41 ` [Bug target/113578] Incorrect sign printed " redi at gcc dot gnu.org
2024-01-24 12:28 ` jsm28 at gcc dot gnu.org
2024-01-24 17:34 ` pinskia at gcc dot gnu.org
2024-01-24 17:42 ` redi at gcc dot gnu.org
2024-01-24 17:56 ` redi at gcc dot gnu.org
2024-05-10 11:48 ` redi at gcc dot gnu.org
2024-05-10 21:32 ` andrew at sifive dot com
2024-05-13 11:09 ` redi at gcc dot gnu.org
2024-05-13 22:36 ` andrew at sifive dot com
2024-05-15 11:13 ` redi at gcc dot gnu.org

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