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

internal-fn: Fix up __builtin_clmulg optimization

Optimize __builtin_clmulg for constant counts by unrolling loops.

This commit refines the optimization for the __builtin_clmulg intrinsic function within GCC. The original implementation of the fallback logic for __builtin_clmulg involved a loop that was not correctly handled when the cnt (count) parameter was a compile-time constant of 1 or 2. For cnt == 1, the function should simply perform a left shift. For cnt == 2, the loop performs one iteration. This patch corrects the implementation by directly emitting the shift for cnt == 1 and manually unrolling the loop for cnt == 2. This avoids generating unnecessary RTL (Register Transfer Language) code and simplifies the emitted instructions, leading to cleaner and potentially more efficient code.

In Details

The expand_clmul function in internal-fn.cc handles __builtin_clmulg. The previous implementation used a do-while loop that was incorrect for compile-time constants cnt = 1 and cnt = 2. For cnt == 1, the intended operation x << N was obscured by the loop. For cnt == 2, the loop performed r ^= r << i; i = i + i; which should result in (x ^ (x << 1)) << N. This patch corrects this by special-casing cnt == 1 to emit res << tz directly, and for cnt == 2, it explicitly generates (x ^ (x << 1)) << tz. This avoids the need for RTL cleanup in these simple cases.

For Context
__builtin_clmulg
A GCC intrinsic function that performs a carry-less multiplication, often used in cryptographic algorithms. It takes two arguments and a count parameter.
RTL (Register Transfer Language)
An intermediate representation used within GCC to describe machine instructions before they are translated into final assembly code.
unroll the loop
A compiler optimization technique where the body of a loop is duplicated one or more times to reduce loop overhead and potentially expose more instruction-level parallelism.
compile-time constant
A value known at compile time, as opposed to a value determined at runtime.
Filed Under: internal-fnclmuloptimization