Landing: 421f09e24c9c
Project / Subsystem
gcc / gcc/match
Date
2026-05-14
Author
liuhongt
Commit
421f09e24c9c82a9ff6a61dd0b65687175c90bff
Source
github
Perf win
Yes
Breaking
No
All attributes
- project
- gcc
- subsystem
- gcc/match
- patch_id
- —
- commit_hash
- 421f09e24c9c82a9ff6a61dd0b65687175c90bff
- source_type
- github
- headline
- Fold `popcount(x ^ (x - 1))` to `ctz(x) + 1` when supported
- tldr
- GCC now transforms `popcount(x ^ (x - 1))` into `ctz(x) + 1` when compiling for architectures that directly support the count trailing zeros instruction.
- author
- liuhongt
- outcome
- committed
- performance_win
- true
- breaking_change
- false
- series_id
- —
- series_parts
- []
- tags
-
- • optimization
- • code generation
- • pattern matching
- • ctz
- • popcount
- discussion_id_link
- —
- bugzilla_pr
- —
- date
- 2026-05-14T00:00:00.000Z
The compiler can now replace popcount(x ^ (x - 1)) with ctz(x) + 1 during optimization. This transformation applies when compiling for architectures that directly support the count trailing zeros (ctz) instruction. The change improves performance by utilizing a specialized instruction, where available, to compute the number of trailing zeros in x.