From: Kyrylo Tkachov <[email protected]>

expand_CLMUL_WIDEN takes a shortcut when the whole product fits in the
mode of the operands and stores zero as the high half.  The test
compares against the precision of the mode.  For an unsigned _BitInt
narrower than its mode the bits of the product above the precision of
the type are the high half, so the shortcut returns zero where it
should not.  Operands of known small range hit the same path.

For example, with unsigned _BitInt(3) a = 7 and b = 7:

  unsigned _BitInt(3) hi;
  unsigned _BitInt(3) lo = __builtin_clmulg (a, b, &hi);

the product is 0b10101, so lo is 0b101 and hi is 0b10.  The expansion
returned hi == 0.

Compute the high half as the product shifted right by the precision of
the type when the product needs more bits than the type has.

Bootstrapped and tested on aarch64-linux-gnu.
Ok for trunk?
Thanks,
Kyrill

Cc: Jakub Jelinek <[email protected]>

gcc/ChangeLog:

        PR middle-end/128111
        * internal-fn.cc (expand_CLMUL_WIDEN): Compute the high half when
        the product fits in the mode but not in the type.

gcc/testsuite/ChangeLog:

        PR middle-end/128111
        * gcc.dg/bitint-147.c: New test.

Signed-off-by: Kyrylo Tkachov <[email protected]>
---
 gcc/internal-fn.cc                |  8 +++-
 gcc/testsuite/gcc.dg/bitint-147.c | 71 +++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/bitint-147.c

diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index c0d59c25c9b..2274e3a8270 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -6361,13 +6361,19 @@ expand_CLMUL_WIDEN (internal_fn, gcall *stmt)
     bitint_mask
       = immed_wide_int_const (wi::mask (TYPE_PRECISION (TREE_TYPE (arg0)),
                                        false, prec), mode);
+  unsigned int type_prec = TYPE_PRECISION (TREE_TYPE (arg0));
   if (prec0 + prec1 - 1 <= prec)
     {
+      /* The whole product fits in MODE.  For a type narrower than MODE
+        the bits above its precision are the high half.  */
       rtx res = expand_clmul (mode, mode, op0, op1, prec0, prec1);
+      rtx reshi = const0_rtx;
+      if (prec0 + prec1 - 1 > type_prec)
+       reshi = expand_shift (RSHIFT_EXPR, mode, res, type_prec, NULL_RTX, 1);
       if (bitint_mask)
        res = expand_and (mode, res, bitint_mask, NULL_RTX);
       write_complex_part (target, res, false, true);
-      write_complex_part (target, const0_rtx, true, false);
+      write_complex_part (target, reshi, true, false);
       return;
     }
   if (optab_handler (clmul_widen_optab, mode) != CODE_FOR_nothing
diff --git a/gcc/testsuite/gcc.dg/bitint-147.c 
b/gcc/testsuite/gcc.dg/bitint-147.c
new file mode 100644
index 00000000000..7f2e7506935
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bitint-147.c
@@ -0,0 +1,71 @@
+/* Test the high half of __builtin_clmulg on _BitInt types narrower than
+   the mode they live in.  */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-std=c23 -O2" } */
+
+#define REF(N)                                                         \
+__attribute__((noipa)) void                                            \
+ref##N (unsigned _BitInt(N) a, unsigned _BitInt(N) b,                  \
+       unsigned _BitInt(N) *lo, unsigned _BitInt(N) *hi)               \
+{                                                                      \
+  unsigned long long r = 0;                                            \
+  for (int i = 0; i < N; i++)                                          \
+    if ((b >> i) & 1)                                                  \
+      r ^= (unsigned long long) a << i;                                        
\
+  *lo = r;                                                             \
+  *hi = r >> N;                                                                
\
+}                                                                      \
+__attribute__((noipa)) void                                            \
+tst##N (unsigned _BitInt(N) a, unsigned _BitInt(N) b,                  \
+       unsigned _BitInt(N) *lo, unsigned _BitInt(N) *hi)               \
+{                                                                      \
+  *lo = __builtin_clmulg (a, b, hi);                                   \
+}                                                                      \
+/* Operands with a small known range.  */                              \
+__attribute__((noipa)) void                                            \
+tstr##N (unsigned _BitInt(N) a, unsigned _BitInt(N) b,                 \
+        unsigned _BitInt(N) *lo, unsigned _BitInt(N) *hi)              \
+{                                                                      \
+  *lo = __builtin_clmulg ((unsigned _BitInt(N)) (a & 0xf),             \
+                         (unsigned _BitInt(N)) (b & 0xf), hi);         \
+}                                                                      \
+__attribute__((noipa)) void                                            \
+check##N (void)                                                                
\
+{                                                                      \
+  for (unsigned int i = 0; i < (1U << N); i++)                         \
+    for (unsigned int j = 0; j < (1U << N); j++)                       \
+      {                                                                        
\
+       unsigned _BitInt(N) a = i, b = j, l, h, rl, rh;                 \
+       tst##N (a, b, &l, &h);                                          \
+       ref##N (a, b, &rl, &rh);                                        \
+       if (l != rl || h != rh)                                         \
+         __builtin_abort ();                                           \
+       tstr##N (a, b, &l, &h);                                         \
+       ref##N (a & 0xf, b & 0xf, &rl, &rh);                            \
+       if (l != rl || h != rh)                                         \
+         __builtin_abort ();                                           \
+      }                                                                        
\
+}
+
+#if __BITINT_MAXWIDTH__ >= 8
+REF (2)
+REF (3)
+REF (4)
+REF (5)
+REF (6)
+REF (7)
+#endif
+
+int
+main ()
+{
+#if __BITINT_MAXWIDTH__ >= 8
+  check2 ();
+  check3 ();
+  check4 ();
+  check5 ();
+  check6 ();
+  check7 ();
+#endif
+  return 0;
+}
-- 
2.54.0 (Apple Git-157)

Reply via email to