https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128410
Bug ID: 128410
Summary: Optimize std::bit_repeat with constant second argument
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: jakub at gcc dot gnu.org
Target Milestone: ---
bit_repeat is currently implemented as:
template<__unsigned_integer _Tp>
[[nodiscard]] constexpr _Tp
bit_repeat(_Tp __x, int __l)
{
__glibcxx_assert( __l > 0 );
constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
if (__l >= _Nd)
return __x;
__x &= ((_Tp)1 << __l) - 1;
do
{
__x |= __x << __l;
__l <<= 1;
}
while (__l < _Nd);
return __x;
}
which works fine during constant evaluation, or I think is also reasonable if
both arguments are non-constant (there is a possibility to precompute array of
bit_repeat(_Tp(1), n) for n in [1, _Nd-1], and wonder if one wants to have say
.rodata array of 64 8-byte values for unsigned long etc.
When one calls bit_repeat(x, 4), we already fold it into the optimal (x & 15) *
0x1000100010001ULL and similar, but not e.g. for bit_repeat(x, 3), wonder if we
couldn't pattern recognize this and if __l here is optimized into constant,
optimize it inside of GCC.
Or introduce a builtin/ifn for this?