[Bug middle-end/126625] New: hwint.cc reflect_hwi () should use __builtin_bitreverse64 if supported.

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

            Bug ID: 126625
           Summary: hwint.cc reflect_hwi () should use
                    __builtin_bitreverse64 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: ---

In gcc/hwint.cc, reflect_hwi () uses a naive approach to emulate
__builtin_bitreverse64 ().

But since r17-523, __builtin_bitreverse64 is now available and reflect_hwi =
can
be:

```
unsigned HOST_WIDE_INT
reflect_hwi (unsigned HOST_WIDE_INT value, unsigned bitwidth)
{
  unsigned HOST_WIDE_INT reflected_value =3D 0;

#if GCC_VERSION >=3D 17000
  if (bitwidth =3D=3D 0
      || bitwidth > 64)
    return 0;

  reflected_value =3D __builtin_bitreverse64 (value) >> (64 - bitwidth);
#else
  /* Loop through each bit in the specified BITWIDTH.  */
  for (size_t i =3D 0; i < bitwidth; i++)
    {
      reflected_value <<=3D 1;
      /* Add the least significant bit of the current value to the
         reflected value.  */
      reflected_value |=3D (value & 1);
      value >>=3D 1;
    }
#endif

  return reflected_value;
}
```=