<xstd/ints/bit/bit_repeat.hpp>¶
xstd::bit_repeat¶
#ifdef XSTD_HAS_STD_BIT_PERMUTATIONS
// (1)
template<class T>
requires ints::detail::std_permutes_bits<T>
constexpr auto bit_repeat(T x, int l) -> T;
#endif
// (2)
template<unsigned_integer T>
requires (not ints::detail::std_permutes_bits<T>)
constexpr auto bit_repeat(T x, int l) -> T;
Repeats the low bits of an unsigned integer across its width.
- The forwarding overload, which calls
std::bit_repeat. - The template, for every other
unsigned_integer.
Parameters¶
| Parameter | Meaning |
|---|---|
x |
the value whose low bits are repeated |
l |
the period, the number of low bits repeated |
Constraints: As for bit_reverse.
Preconditions: l is greater than zero.
Return value¶
Returns: repeat(x, l), P3104R5's sum of x[n mod l]ยท2^n over
n in [0, N): the low l bits of x repeated across the width.
Exceptions¶
- Throws: Nothing, for a
Twhose operators throw nothing. - It is not
noexcept, for any type, the forwarding overload included.
Remarks¶
- A call that violates the precondition is not a core constant expression, whether or not
NDEBUGis defined, as P3104R6 [bit.permute]/8 adds. - Where
__cpp_lib_bitopsis at least202607Landstd::bit_compress,std::bit_expand,std::bit_repeatandstd::bit_reverseall takeT, the call isstd::bit_repeat(x, l), and the template does not takeT. - The template has no block kernel: it masks the low
lbits and doubles the pattern until it fills the width, so it runsceil(log2(N / l))shift-or steps.
Notes¶
- A period of one copies the lowest bit everywhere, and a period of
Nor more,INT_MAXincluded, returnsx. - The paper's examples hold:
bit_repeat(uint32_t{0xc}, 4)is0xcccc'cccc, the alternating masks of itscountr_zeroarebit_repeat((1u << i) - 1, 2 * i), and thatcountr_zeroagrees with xstd's. - A call is a constant expression for every type it takes.
- The result has type
T, not theintthat integral promotion makes of a narrowT.
See also¶
| Entity | Relation |
|---|---|
bit_expand |
interleaves through masks it builds |
bit_reverse, bit_compress |
the other permutations |
| Notation | what N and x[n] mean here |
| P3104R6 | the paper that proposes std::bit_repeat |