[Bug middle-end/126634] New: hwint.h pow2_or_zerop/pow2p_hwi could use popcount_hwi if supported.

kaelfandrew at gmail dot com via Gcc-bugs <[email protected]> Tue, 04 Aug 2026 11:18:37 +0000
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D126634

            Bug ID: 126634
           Summary: hwint.h pow2_or_zerop/pow2p_hwi could use popcount_hwi
                    if supported.
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kaelfandrew at gmail dot com
  Target Milestone: ---

pow2_or_zerop/pow2p_hwi could be:

```
inline bool
pow2_or_zerop (unsigned HOST_WIDE_INT x)
{
#if GCC_VERSION >=3D 3004
  return popcount_hwi (x) < 2;
#else
  return least_bit_hwi (x) =3D=3D x;
#endif
}

inline bool
pow2p_hwi (unsigned HOST_WIDE_INT x)
{
#if GCC_VERSION >=3D 3004
  return popcount_hwi (x) =3D=3D 1;
#else
  return x && pow2_or_zerop (x);
#endif
}
```

Noticed from LLVM optimizer and tree.cc integer_pow2p.=