public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jan.wassenberg at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/115115] [12/13/14/15 Regression] highway-1.0.7 wrong _mm_cvttps_epi32() constant fold
Date: Fri, 17 May 2024 09:22:36 +0000	[thread overview]
Message-ID: <bug-115115-4-iKwExmkqse@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-115115-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #9 from Jan Wassenberg <jan.wassenberg at gmail dot com> ---
On second thought, we are actually trying to convert out-of-bounds values to
the closest representable. We use the documented behavior of the instruction,
as mentioned in #5, and then correct the result afterwards.

Per the comment in the code below, it seems GCC since v11 or even 10 has been
assuming this is UB, and optimizing out our fix.

I do believe this is compiler misbehavior, rooted in treating the operation as
if it were scalar code. But vector instructions are more modern and have
tighter specs; for example, integers are 2's complement and wraparound for
addition is well-defined in the actual instructions.

Given that at least GCC's constant folding has unexpected results, we will have
to find a workaround. I had previously worried that a floating-point min(input,
(2^63)-1) is not exact. But comparing the float >= 2^63 and if so returning
(2^63)-1 would work, right? The CPU will anyway truncate the float to int.

Our current fixup code:

// For ConvertTo float->int of same size, clamping before conversion would
// change the result because the max integer value is not exactly
representable.
// Instead detect the overflow result after conversion and fix it.
// Generic for all vector lengths.
template <class DI>
HWY_INLINE VFromD<DI> FixConversionOverflow(DI di,
                                            VFromD<RebindToFloat<DI>> original,
                                            VFromD<DI> converted) {
  // Combinations of original and output sign:
  //   --: normal <0 or -huge_val to 80..00: OK
  //   -+: -0 to 0                         : OK
  //   +-: +huge_val to 80..00             : xor with FF..FF to get 7F..FF
  //   ++: normal >0                       : OK
  const VFromD<DI> sign_wrong = AndNot(BitCast(di, original), converted);
#if HWY_COMPILER_GCC_ACTUAL
  // Critical GCC 11 compiler bug (possibly also GCC 10): omits the Xor; also
  // Add() if using that instead. Work around with one more instruction.
  const RebindToUnsigned<DI> du;
  const VFromD<DI> mask = BroadcastSignBit(sign_wrong);
  const VFromD<DI> max = BitCast(di, ShiftRight<1>(BitCast(du, mask)));
  return IfVecThenElse(mask, max, converted);
#else
  return Xor(converted, BroadcastSignBit(sign_wrong));
#endif
}

  parent reply	other threads:[~2024-05-17  9:22 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-15 21:38 [Bug target/115115] New: " slyfox at gcc dot gnu.org
2024-05-15 21:45 ` [Bug target/115115] " slyfox at gcc dot gnu.org
2024-05-15 21:49 ` pinskia at gcc dot gnu.org
2024-05-15 22:00 ` slyfox at gcc dot gnu.org
2024-05-16  0:36 ` liuhongt at gcc dot gnu.org
2024-05-16  7:12 ` rguenth at gcc dot gnu.org
2024-05-16  9:08 ` slyfox at gcc dot gnu.org
2024-05-16  9:14 ` pinskia at gcc dot gnu.org
2024-05-16 21:57 ` slyfox at gcc dot gnu.org
2024-05-17  8:27 ` jan.wassenberg at gmail dot com
2024-05-17  9:22 ` jan.wassenberg at gmail dot com [this message]
2024-05-17 18:10 ` jan.wassenberg at gmail dot com
2024-05-20 13:56 ` slyfox at gcc dot gnu.org
2024-05-20 15:48 ` jan.wassenberg at gmail dot com
2024-06-05  4:09 ` cvs-commit at gcc dot gnu.org
2024-06-05  9:12 ` slyfox at gcc dot gnu.org
2024-06-20  9:15 ` rguenth 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-115115-4-iKwExmkqse@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).