public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "eggert at cs dot ucla.edu" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/114347] wrong constant folding when casting __bf16 to int
Date: Wed, 20 Mar 2024 06:02:26 +0000	[thread overview]
Message-ID: <bug-114347-4-bU8h8MrSEZ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114347-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #10 from Paul Eggert <eggert at cs dot ucla.edu> ---
(In reply to Jakub Jelinek from comment #6)
> You can use -fexcess-precision=16 if you don't want treating _Float16 and
> __bf16 as having excess precision.  With excess precision, I think the above
> behavior is correct.

So the constant 257.0bf16 has a value that the type __bf16 cannot represent?
Although the C standard allows this sort of thing, it doesn't sound wise.

For example, it breaks a common use of 'typeof' that is given in the GCC manual
section 6.7 "Referring to a Type with 'typeof'". In this program:

  #define MAX(a, b) ((a) > (b) ? (a) : (b))
  #define max(a, b) \
    ({ typeof (a) _a = (a); \
       typeof (b) _b = (b); \
       _a > _b ? _a : _b; })

  int main (void)
  {
    return    max(257.0bf16, 256.0bf16)
           == MAX(257.0bf16, 256.0bf16);
  }

'main' surprisingly returns 0 when I expect pretty much everybody would expect
it to return 1. This is because "max" surprisingly changes the values of its
arguments before comparing, whereas MAX does not.

If GCC's "bf16" and "f16" suffixes arranged for floating point constants to be
representable as __bf16 and _Float16, respectively, we wouldn't have this sort
of confusion. Surely that would be better than the current situation.

  parent reply	other threads:[~2024-03-20  6:02 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-15  4:55 [Bug c/114347] New: " eggert at cs dot ucla.edu
2024-03-15  5:01 ` [Bug middle-end/114347] " pinskia at gcc dot gnu.org
2024-03-15  5:19 ` eggert at cs dot ucla.edu
2024-03-15  9:03 ` rguenth at gcc dot gnu.org
2024-03-15 16:58 ` jsm28 at gcc dot gnu.org
2024-03-18  8:03 ` rguenth at gcc dot gnu.org
2024-03-18  8:16 ` jakub at gcc dot gnu.org
2024-03-18  8:53 ` rguenth at gcc dot gnu.org
2024-03-18  9:06 ` jakub at gcc dot gnu.org
2024-03-18  9:08 ` liuhongt at gcc dot gnu.org
2024-03-20  6:02 ` eggert at cs dot ucla.edu [this message]
2024-03-20  6:19 ` pinskia at gcc dot gnu.org
2024-03-21  0:48 ` cvs-commit at gcc dot gnu.org
2024-03-25 22:21 ` pinskia at gcc dot gnu.org

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=bug-114347-4-bU8h8MrSEZ@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).