https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126625

            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 = 0;

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

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

  return reflected_value;
}
```

Reply via email to