https://gcc.gnu.org/g:e1167aef8d7477fe3aa152cdc61a62b92a1c4f3b
commit r17-6070-ge1167aef8d7477fe3aa152cdc61a62b92a1c4f3b Author: Kyrylo Tkachov <[email protected]> Date: Mon Nov 9 11:14:11 2026 +0100 internal-fn: Fix the high half of widening clmul on narrow _BitInt [PR128111] 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. 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]> Diff: --- gcc/internal-fn.cc | 30 +++++++++-------- gcc/testsuite/gcc.dg/bitint-147.c | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 14 deletions(-) diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index c0d59c25c9b8..d277a07eaf7f 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -6353,25 +6353,30 @@ expand_CLMUL_WIDEN (internal_fn, gcall *stmt) unsigned int prec0 = get_min_precision (arg0, UNSIGNED); unsigned int prec1 = get_min_precision (arg1, UNSIGNED); unsigned int prec = GET_MODE_PRECISION (mode); + unsigned int type_prec = TYPE_PRECISION (TREE_TYPE (arg0)); rtx op0 = expand_normal (arg0); rtx op1 = expand_normal (arg1); rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); rtx bitint_mask = NULL_RTX; - if (TYPE_PRECISION (TREE_TYPE (arg0)) != prec) + if (type_prec != prec) bitint_mask - = immed_wide_int_const (wi::mask (TYPE_PRECISION (TREE_TYPE (arg0)), - false, prec), mode); + = immed_wide_int_const (wi::mask (type_prec, false, prec), mode); if (prec0 + prec1 - 1 <= prec) { - rtx res = expand_clmul (mode, mode, op0, op1, prec0, prec1); + /* The whole product fits in MODE. For a type narrower than MODE + the bits above its precision are the high half. */ + rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1); + rtx reshi = const0_rtx; + if (prec0 + prec1 - 1 > type_prec) + reshi = expand_shift (RSHIFT_EXPR, mode, reslo, 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); + reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX); + write_complex_part (target, reslo, false, true); + write_complex_part (target, reshi, true, false); return; } if (optab_handler (clmul_widen_optab, mode) != CODE_FOR_nothing - && TYPE_PRECISION (TREE_TYPE (arg0)) == prec) + && type_prec == prec) { rtx_insn *start = get_last_insn (); rtx reslo = gen_reg_rtx (mode); @@ -6399,8 +6404,7 @@ expand_CLMUL_WIDEN (internal_fn, gcall *stmt) if (bitint_mask) reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX); write_complex_part (target, reslo, false, true); - rtx reshi = expand_shift (RSHIFT_EXPR, wmode, res, - TYPE_PRECISION (TREE_TYPE (arg0)), + rtx reshi = expand_shift (RSHIFT_EXPR, wmode, res, type_prec, NULL_RTX, 1); reshi = convert_modes (mode, wmode, reshi, 1); write_complex_part (target, reshi, true, false); @@ -6467,10 +6471,8 @@ expand_CLMUL_WIDEN (internal_fn, gcall *stmt) write_complex_part (target, reslom, false, true); rtx reshi = expand_binop (mode, xor_optab, h, t1, NULL_RTX, 1, olw); reshi = expand_shift (LSHIFT_EXPR, mode, reshi, - prec - TYPE_PRECISION (TREE_TYPE (arg0)), - NULL_RTX, 1); - reslo = expand_shift (RSHIFT_EXPR, mode, reslo, - TYPE_PRECISION (TREE_TYPE (arg0)), NULL_RTX, 1); + prec - type_prec, NULL_RTX, 1); + reslo = expand_shift (RSHIFT_EXPR, mode, reslo, type_prec, NULL_RTX, 1); reshi = expand_binop (mode, ior_optab, reshi, reslo, NULL_RTX, 1, olw); reshi = expand_and (mode, reshi, bitint_mask, NULL_RTX); write_complex_part (target, reshi, true, false); diff --git a/gcc/testsuite/gcc.dg/bitint-147.c b/gcc/testsuite/gcc.dg/bitint-147.c new file mode 100644 index 000000000000..7f2e7506935d --- /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; +}
