internal-fn: Optimize fallback for __builtin_clmulg (x, x)
Optimize carry-less multiplication of a value with itself using bit expansion.
This commit introduces an optimization for the __builtin_clmulg(x, x) case, where a value is carry-less multiplied by itself. This optimization is applied on top of a previous patch that enhanced the fallback mechanism for IFN_CLMUL and IFN_CLMUL_WIDEN. If the bitexpand_optab is available in hardware, __builtin_clmulg(x, x) can be efficiently implemented using __builtin_bitexpandg with a specific mask (0x5555555555555555ULL for 64-bit unsigned integers). This mask effectively duplicates each bit of x to adjacent positions. The optimization is particularly beneficial on architectures like x86 with BMI2 instructions, where pdep might offer better performance than PCLMULDQ for this specific operation, as pdep is not a vector instruction.
In Details
This patch extends expand_clmul in internal-fn.cc by adding an OP_EQ argument. When this flag is true (indicating op0 and op1 are equal), it attempts to optimize clmul(x, x) using IFN_BITEXPAND. The specific pattern used is bitexpand(x, std::bit_repeat(1, 2)), which for a 64-bit unsigned integer translates to IFN_BITEXPAND(x, 0x5555555555555555ULL). This is predicated on bitexpand_optab being implemented in hardware. The clmulg fallback on x86 with BMI2 can leverage pdep for this, which is often more efficient than vector pclmuldq for this specific non-vectorizable p…
- __builtin_clmulg
- A GCC intrinsic function that performs a carry-less multiplication, often used in cryptographic algorithms.
- bit expansion
- An operation that replicates bits of an input value into specific positions in the output, often used in cryptography and specialized data manipulation.
- IFN_CLMUL
- An internal GCC function code representing a carry-less multiplication operation.
- IFN_BITEXPAND
- An internal GCC function code representing a bit expansion operation.
- BMI2 (Bit Manipulation Instructions 2)
- An extension to the x86 instruction set that includes powerful bit manipulation instructions like
PDEP(parallel bits deposit) andPEXT(parallel bits extract). - PCLMULDQ
- An SSE4.2 instruction that performs carry-less multiplication of two 128-bit operands.