public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108934] bit_cast'ing to long double errors out with "the argument cannot be interpreted" since gcc-12
Date: Wed, 01 Mar 2023 15:56:11 +0000	[thread overview]
Message-ID: <bug-108934-4-dWQpZboswO@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108934-4@http.gcc.gnu.org/bugzilla/>

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
If you are complaining about:
struct S { unsigned long long a[2]; };
struct T { unsigned long long b[6]; };
struct U { unsigned long long c[2]; long double d; unsigned long long e[2]; };

#if __SIZEOF_LONG_DOUBLE__ == 16 && __LDBL_MANT_DIG__ == 64 &&
__SIZEOF_LONG_LONG__ == 8
constexpr long double
foo (S x)
{
  return __builtin_bit_cast (long double, x);
}

constexpr S a = { 0ULL, 0xffffffffffff0000ULL };
constexpr long double b = foo (a);
static_assert (b == 0.0L, "");

constexpr U
bar (T x)
{
  return __builtin_bit_cast (U, x);
}

constexpr T c = { 0ULL, 0ULL, 0ULL, 0xffffffffffff0000ULL, 0ULL, 0ULL };
constexpr U d = bar (c);
static_assert (d.d == 0.0L, "");
#endif

which is an unintended side-effects of the PR104522 changes, then that can be
fixed e.g. with
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 9aaea71a2fc..99882ef820a 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -8873,11 +8873,13 @@ native_interpret_expr (tree type, const unsigned char
*ptr, int len)
             valid values that GCC can't really represent accurately.
             See PR95450.  Even for other modes, e.g. x86 XFmode can have some
             bit combinationations which GCC doesn't preserve.  */
-         unsigned char buf[24];
+         unsigned char buf[24 * 2];
          scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
          int total_bytes = GET_MODE_SIZE (mode);
+         memcpy (buf + 24, ptr, total_bytes);
+         clear_type_padding_in_mask (type, buf + 24);
          if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
-             || memcmp (ptr, buf, total_bytes) != 0)
+             || memcmp (buf + 24, buf, total_bytes) != 0)
            return NULL_TREE;
          return ret;
        }

The intent of the PR104522 change was to verify that the interpreted floating
point value represents the in memory value correctly, but padding bits
shouldn't be considered in that, they can contain anything.  That said, for IBM
double double format or e.g. x86 
extended format there are still many values which can't be interpretted
correctly,
for the latter e.g. pseudo denormals, pseudo infinities, pseudo NaNs and
unnormal values.

  parent reply	other threads:[~2023-03-01 15:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-25 22:58 [Bug c++/108934] New: " janpmoeller at gmx dot de
2023-02-25 23:09 ` [Bug c++/108934] " pinskia at gcc dot gnu.org
2023-02-25 23:09 ` pinskia at gcc dot gnu.org
2023-02-25 23:11 ` pinskia at gcc dot gnu.org
2023-02-25 23:15 ` pinskia at gcc dot gnu.org
2023-03-01 15:56 ` jakub at gcc dot gnu.org [this message]
2023-03-01 15:57 ` [Bug c++/108934] [12/13 Regression] " jakub at gcc dot gnu.org
2023-03-01 16:02 ` jakub at gcc dot gnu.org
2023-03-01 16:19 ` jakub at gcc dot gnu.org
2023-03-02  8:28 ` cvs-commit at gcc dot gnu.org
2023-03-02  8:29 ` [Bug c++/108934] [12 " jakub at gcc dot gnu.org
2023-03-19  5:30 ` cvs-commit at gcc dot gnu.org
2023-03-20 10:28 ` jakub 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-108934-4-dWQpZboswO@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).