GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
internal-fn Performance Win

internal-fn: Optimize __builtin_clmulg with constant patterns

Optimizes `__builtin_clmulg` for specific constant inputs, improving performance.

This patch optimizes the __builtin_clmulg intrinsic for certain constant arguments that represent parallel prefix XOR operations. It identifies patterns like __builtin_clmulg(x, ~0ULL) and rewrites them using a more efficient O(log n) loop. The optimization is applied when the constant argument’s properties (like bit count) match specific criteria, potentially leading to significant performance gains for these cases. The work extends previous optimizations for related bitwise operations.

In Details

This commit targets the expansion of __builtin_clmulg in internal-fn.cc. It recognizes that __builtin_clmulg(x, (typeof (x))-1) is equivalent to a parallel prefix XOR (cumulative XOR) and can be computed more efficiently via a loop: r = x; for (i=1; i<prec; i*=2) r ^= r << i;. The optimization further extends to handle cases where the constant argument is y << n, exploiting the property clmul(x, y << n) == clmul(x, y) << n. It analyzes the constant operand's properties (CLZ, CTZ, POPCOUNT) to determine applicability for these optimized forms, particularly when the number of consec…

For Context
__builtin_clmulg
GCC's intrinsic function for performing a Galois field multiplication of two vectors.
parallel prefix XOR
A computation where each output element is the XOR sum of all input elements up to and including that position.
CLZ
Count Leading Zeros; an intrinsic that counts the number of zero bits from the most significant bit down to the first set bit.
CTZ
Count Trailing Zeros; an intrinsic that counts the number of zero bits from the least significant bit up to the first set bit.
POPCOUNT
Population Count; an intrinsic that counts the number of set bits (1s) in an integer.
Filed Under: optimizationintrinsicperformance