public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: FX <fxcoudert@gmail.com>
To: GCC Mailing List <gcc@gcc.gnu.org>
Cc: Joseph Myers <joseph@codesourcery.com>
Subject: Weird behaviour (bug?) of __builtin_nans()
Date: Fri, 31 Dec 2021 17:13:17 +0100	[thread overview]
Message-ID: <A556189D-CA50-4B8B-9F2B-311687A91F86@gmail.com> (raw)

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

             reply	other threads:[~2021-12-31 16:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-31 16:13 FX [this message]
2021-12-31 17:05 ` Joseph Myers
2021-12-31 19:14   ` FX

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=A556189D-CA50-4B8B-9F2B-311687A91F86@gmail.com \
    --to=fxcoudert@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /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).