public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Weird behaviour (bug?) of __builtin_nans()
@ 2021-12-31 16:13 FX
  2021-12-31 17:05 ` Joseph Myers
  0 siblings, 1 reply; 3+ messages in thread
From: FX @ 2021-12-31 16:13 UTC (permalink / raw)
  To: GCC Mailing List; +Cc: Joseph Myers

Hi,

I think I’ve found a bug in the handling of __builtin_nans() in GCC, but I am aware that this is a tricky area, so before claiming so I would like to check with the experts. Consider the following code:

$ cat v.c 
#include <stdint.h>
#include <stdio.h>

#if 1
typedef double GFC_REAL_8;
#else
#define GFC_REAL_8 double
#endif

GFC_REAL_8 foo (void)
{
  return __builtin_nans("");
}

int main (void) {
  double x;

  x = __builtin_nans ("");
  printf("==> %lX\n", *(uint64_t *) &x);
  x = foo ();
  printf("==> %lX\n", *(uint64_t *) &x);
}
$ gcc v.c -W -Wall && ./a.out
==> 7FF4000000000000
==> 7FF8000000000000


My expectation is: the x variable should be assigned a signalling NaN, both times, and therefore the code should output the same value twice. But as you can see, the second time the NaN is converted to quiet.

What is even more troubling: this behavior only happens if GFC_REAL_8 is typedef’ed to double. If we use the double type directly (change #if 1 to #if 0) then the output is as expected:

==> 7FF4000000000000
==> 7FF4000000000000


What is even more worrying is that, if you keep the typedef, but change the function to go through a variable, then the signalling nan is returned correctly:

typedef double GFC_REAL_8;
GFC_REAL_8 foo (void)
{
  GFC_REAL_8 x = __builtin_nans("");
  return x;
}


---------

The reason I ended up in this rabbit hole is that I am implementing some handling of signalling NaNs in libgfortran. Could someone either confirm that the behavior observed above is a bug, or if not, kindly explain to me why it happens?


Thanks,
FX

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

end of thread, other threads:[~2021-12-31 19:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-31 16:13 Weird behaviour (bug?) of __builtin_nans() FX
2021-12-31 17:05 ` Joseph Myers
2021-12-31 19:14   ` FX

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