https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129023
Bug ID: 129023
Summary: [internal-fn] wrong code at all -O levels:
__builtin_clmulg with a single-bit constant operand
computes clmul (x, 3 << n) (constant-operand shortcut
from r17-6053 runs its do-while body once too often)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 0599jiangyc at gmail dot com
Target Milestone: ---
The following code:
```c
/* __builtin_clmulg with a single-bit constant operand returns clmul(x, 3 << n)
instead of clmul(x, 1 << n).
gcc -O0 repro.c && ./a.out (also -O1/-O2/-O3; plain integer types and
_BitInt)
Expected: exit 0. GCC trunk (r17-6053+, 69649895b): exit 1. */
__attribute__((noipa)) unsigned long f(unsigned long x) {
return __builtin_clmulg(x, 2UL); /* == x << 1 */
}
__attribute__((noipa)) unsigned g(unsigned x, unsigned *hi) {
return __builtin_clmulg(x, 0x80000000U, hi); /* lo == x << 31, hi == x >> 1
*/
}
__attribute__((noipa)) unsigned _BitInt(2) h(unsigned _BitInt(2) x, unsigned
_BitInt(2) *hi) {
return __builtin_clmulg((unsigned _BitInt(2))2, x, hi);
}
int main(void) {
unsigned hi;
unsigned _BitInt(2) hb;
if (f(1) != 2)
return 1;
if (g(1, &hi) != 0x80000000U || hi != 0)
return 1;
if (h(1, &hb) != 2 || hb != 0)
return 1;
return 0;
}
```
Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out (also -O0, -O1, -O3; unsigned long,
unsigned, _BitInt(N <= 128))
(exit 1; e.g. __builtin_clmulg (1UL, 2UL) returns 6 instead of 2)
```
Trunk: https://godbolt.org/z/zG1EYv8hr (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-12)