On Mon, 9 Nov 2026, Jakub Jelinek wrote:

> Hi!
> 
> __builtin_clmulg (x, (typeof (x))-1) is parallel prefix XOR
> (aka prefix-XOR or cumulative XOR) and can be computed more efficiently,
> in particular as
>   typeof(x) r = x;
>   for (unsigned i = 1; i < prec; i = i + i)
>     r ^= r << i;
>   return r;
> The following patch implements that and extends somewhat.
> __builtin_clmulg (x, y << n) is equal to __builtin_clmulg (x, y) << n,
> and furthermore, if instead of prec it uses some smaller number, it
> can handle also smaller counts of consecutive 1s in one of the argument.
> Because it uses an O(log_2 n) loop, it can only handle the cases where
> the number of the consecutive 1s in the constant argument is a power of two,
> or if it is non-widening clmul and the constant is all ones shifted left
> by some constant (in that case the extra operation could change only the
> upper half).
> So, the patch computes clz, ctz and popcount on the constant and
> if the sum of those 3 is equal to the operand precision and either
> popcount is power of two or it isn't widening and clz is 0, it optimizes
> the fallback.
> Note, it could handle even non-power of two, but it would need to be done
> in sequential code with remembering all the temporary results, e.g.
> if cnt == 5 it could do
>   r = x;
>   r ^= r << 1;
>   r ^= r << 2;
>   r ^= x << 4;
> or for cnt == 6 it could do
>   r = x;
>   r ^= r << 1;
>   t1 = r;
>   r ^= r << 2;
>   r ^= t1 << 4;
> etc., but I haven't implemented that.
> Also, similar optimization should be done for large/huge _BitInt.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

Thanks,
Richard.

> 2026-11-09  Jakub Jelinek  <[email protected]>
> 
>       PR rtl-optimization/128545
>       * internal-fn.cc (expand_clmul): Optimize
>       clmul (x, ((((T) 1) << M) - 1) << N) fallback.
> 
>       * c-c++-common/builtin-clmulg-3.c: New test.
> 
> --- a/gcc/internal-fn.cc      2026-11-07 19:36:19.412932623 +0100
> +++ b/gcc/internal-fn.cc      2026-11-07 20:55:25.414597841 +0100
> @@ -6090,6 +6090,56 @@ expand_clmul (machine_mode mode, machine
>         delete_insns_since (start);
>       }
>      }
> +  if (CONST_SCALAR_INT_P (op1))
> +    {
> +      wide_int wop1 = rtx_mode_t (op1, op_mode);
> +      unsigned lz = wi::clz (wop1);
> +      unsigned tz = wi::ctz (wop1);
> +      unsigned cnt = wi::popcount (wop1);
> +      if (lz + cnt + tz == op_prec
> +       && (pow2p_hwi (cnt) || (lz == 0 && prec == op_prec)))
> +     {
> +       /* Optimize the cases where one argument is a constant
> +          equal to ((((T) 1) << M) - 1) << N for some M and N
> +          constants.
> +          In this case the clmul (x, ((((T) 1) << M) - 1) << N)
> +          result can be computed as
> +          T r = x;
> +          for (unsigned i = 1; i < M; i = i + i)
> +            r ^= r << i;
> +          return r << N;
> +          If M is not a power of two, it shouldn't be a widening
> +          shift and most significant bit of the constant should be
> +          set.  */
> +       rtx x = convert_modes (mode, op_mode, op0, 1);
> +       rtx res = gen_reg_rtx (mode);
> +       emit_move_insn (res, x);
> +       rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
> +       emit_move_insn (i, const1_rtx);
> +       rtx_code_label *loop_label = gen_label_rtx ();
> +       emit_label (loop_label);
> +       rtx tem = expand_variable_shift (LSHIFT_EXPR, mode, res,
> +                                        make_tree (integer_type_node, i),
> +                                        NULL_RTX, 1);
> +       tem = expand_binop (mode, xor_optab, res, tem, res, 1, olw);
> +       if (tem != res)
> +         emit_move_insn (res, tem);
> +       tem = expand_binop (GET_MODE (i), add_optab, i, i, i, 1, olw);
> +       if (tem != res)
> +         emit_move_insn (i, tem);
> +       do_compare_rtx_and_jump (i, gen_int_mode (cnt, GET_MODE (i)),
> +                                LTU, true, GET_MODE (i), NULL_RTX, NULL,
> +                                loop_label,
> +                                profile_probability::very_likely ());
> +       if (tz)
> +         {
> +           tem = expand_shift (LSHIFT_EXPR, mode, res, tz, res, 1);
> +           if (tem != res)
> +             emit_move_insn (res, tem);
> +         }
> +       return res;
> +     }
> +    }
>    /* Fallback using Kronecker substitution.
>       Depending on the number of active bits in the operand with smaller
>       number of those and the result precision we choose smallest spacing
> --- a/gcc/testsuite/c-c++-common/builtin-clmulg-3.c   2026-11-07 
> 21:22:42.062534781 +0100
> +++ b/gcc/testsuite/c-c++-common/builtin-clmulg-3.c   2026-11-07 
> 21:23:16.517070663 +0100
> @@ -0,0 +1,104 @@
> +/* Test __builtin_clmulg.  */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +[[gnu::noipa]] unsigned long long
> +foo (unsigned long long x)
> +{
> +  return __builtin_clmulg (-1ULL, x);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +bar (unsigned long long x)
> +{
> +  return __builtin_clmulg (0x123456789abcdef0ULL, x);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +baz (unsigned long long x)
> +{
> +  return __builtin_clmulg (x, -1ULL << 15);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +qux (unsigned long long x)
> +{
> +  return __builtin_clmulg (0xffffULL << 11, x);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +fred (unsigned long long x)
> +{
> +  return __builtin_clmulg (0x3fffULL << 11, x);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +corge (unsigned long long x, unsigned long long *y)
> +{
> +  return __builtin_clmulg (-1ULL, x, y);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +waldo (unsigned long long x, unsigned long long *y)
> +{
> +  return __builtin_clmulg (0x123456789abcdef0ULL, x, y);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +garply (unsigned long long x, unsigned long long *y)
> +{
> +  return __builtin_clmulg (x, -1ULL << 15, y);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +xyzzy (unsigned long long x, unsigned long long *y)
> +{
> +  return __builtin_clmulg (0xffffULL << 11, x, y);
> +}
> +
> +[[gnu::noipa]] unsigned long long
> +plugh (unsigned long long x, unsigned long long *y)
> +{
> +  return __builtin_clmulg (0x3fffULL << 11, x, y);
> +}
> +
> +int
> +main ()
> +{
> +#if __SIZEOF_LONG_LONG__ * __CHAR_BIT__ == 64
> +  unsigned long long v = 0xc42a7e1d6b0a2ae7ULL, h, h2;
> +  if (foo (v) != __builtin_clmulg (v, -1ULL)
> +      || bar (v) != __builtin_clmulg (v, 0x123456789abcdef0ULL)
> +      || baz (v) != __builtin_clmulg (v, -1ULL << 15)
> +      || qux (v) != __builtin_clmulg (v, 0xffffULL << 11)
> +      || fred (v) != __builtin_clmulg (v, 0x3fffULL << 11)
> +      || corge (v, &h) != __builtin_clmulg (v, -1ULL, &h2)
> +      || h != h2
> +      || waldo (v, &h) != __builtin_clmulg (v, 0x123456789abcdef0ULL, &h2)
> +      || h != h2
> +      || garply (v, &h) != __builtin_clmulg (v, -1ULL << 15, &h2)
> +      || h != h2
> +      || xyzzy (v, &h) != __builtin_clmulg (v, 0xffffULL << 11, &h2)
> +      || h != h2
> +      || plugh (v, &h) != __builtin_clmulg (v, 0x3fffULL << 11, &h2)
> +      || h != h2)
> +    __builtin_abort ();
> +  v = 0x005c7eb5333a35daULL;
> +  if (foo (v) != __builtin_clmulg (v, -1ULL)
> +      || bar (v) != __builtin_clmulg (v, 0x123456789abcdef0ULL)
> +      || baz (v) != __builtin_clmulg (v, -1ULL << 15)
> +      || qux (v) != __builtin_clmulg (v, 0xffffULL << 11)
> +      || fred (v) != __builtin_clmulg (v, 0x3fffULL << 11)
> +      || corge (v, &h) != __builtin_clmulg (v, -1ULL, &h2)
> +      || h != h2
> +      || waldo (v, &h) != __builtin_clmulg (v, 0x123456789abcdef0ULL, &h2)
> +      || h != h2
> +      || garply (v, &h) != __builtin_clmulg (v, -1ULL << 15, &h2)
> +      || h != h2
> +      || xyzzy (v, &h) != __builtin_clmulg (v, 0xffffULL << 11, &h2)
> +      || h != h2
> +      || plugh (v, &h) != __builtin_clmulg (v, 0x3fffULL << 11, &h2)
> +      || h != h2)
> +    __builtin_abort ();
> +#endif
> +}
> 
>       Jakub
> 
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to