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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <[email protected]>:

https://gcc.gnu.org/g:69649895b1e7c99e0b926460026b0fbc2d161d6f

commit r17-6053-g69649895b1e7c99e0b926460026b0fbc2d161d6f
Author: Jakub Jelinek <[email protected]>
Date:   Mon Nov 9 10:13:03 2026 +0100

    internal-fn: Optimize __builtin_clmulg (x, ~0ULL) [PR128545]

    __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.

    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.

    Reviewed-by: Richard Biener <[email protected]>

Reply via email to