10#ifndef M5_UTILITY_LFSR_HPP
11#define M5_UTILITY_LFSR_HPP
27template <uint32_t N, uint32_t... Taps>
29 static_assert(N >= 1,
"N must be >= 1");
30 static_assert(N <= 64,
"N must be <= 64");
31 static_assert(
sizeof...(Taps) >= 1,
"At least one tap required");
34 template <uint32_t...>
35 struct all_valid : std::true_type {
38 template <uint32_t A, uint32_t... Rest>
39 struct all_valid<A, Rest...> : std::integral_constant<bool, (A >= 1 && A <= N) && all_valid<Rest...>::value> {
42 static_assert(all_valid<Taps...>
::value,
"Taps out of range");
46 template <uint32_t... Ts>
47 static bool taps_xor(
const std::bitset<N>& s)
50 using swallow =
int[];
51 (void)swallow{0, (r ^= s.test(N - Ts), 0)...};
56 using storage_t =
typename uint_least_for_bits<N>::type;
73 template <
typename UL =
unsigned long,
74 typename std::enable_if<(
sizeof(UL) * CHAR_BIT >= 64), std::nullptr_t>::type =
nullptr>
77 return static_cast<storage_t>(_state.to_ulong());
79 template <
typename UL =
unsigned long,
80 typename std::enable_if<(
sizeof(UL) * CHAR_BIT < 64), std::nullptr_t>::type =
nullptr>
83 return static_cast<storage_t>(_state.to_ullong());
90 const bool out = _state.test(0);
91 const bool fb = taps_xor<Taps...>(_state);
93 _state.set(N - 1, fb);
98 uint64_t
step(
const uint32_t nbits)
noexcept
101 for (uint32_t i = 0; i < nbits; ++i) {
102 v |= (uint64_t(
step()) << i);
112 return (uint16_t)
step(16);
117 return (uint32_t)
step(32);
129 return taps_xor<Taps...>(s);
142template <uint32_t N, uint32_t... Taps>
144 static_assert(N >= 1,
"N must be >= 1");
145 static_assert(N <= 64,
"N must be <= 64");
146 static_assert(
sizeof...(Taps) >= 1,
"At least one tap required");
149 template <uint32_t...>
150 struct all_valid : std::true_type {
153 template <uint32_t A, uint32_t... Rest>
154 struct all_valid<A, Rest...> : std::integral_constant<bool, (A >= 1 && A <= N) && all_valid<Rest...>::value> {
157 static_assert(all_valid<Taps...>
::value,
"Taps out of range");
160 template <uint32_t... Ts>
161 static bool taps_xor(
const std::bitset<N>& s)
164 using swallow =
int[];
165 (void)swallow{0, (r ^= s.test(Ts - 1), 0)...};
170 using storage_t =
typename uint_least_for_bits<N>::type;
187 template <
typename UL =
unsigned long,
188 typename std::enable_if<(
sizeof(UL) * CHAR_BIT >= 64), std::nullptr_t>::type =
nullptr>
191 return static_cast<storage_t>(_state.to_ulong());
193 template <
typename UL =
unsigned long,
194 typename std::enable_if<(
sizeof(UL) * CHAR_BIT < 64), std::nullptr_t>::type =
nullptr>
197 return static_cast<storage_t>(_state.to_ullong());
203 const bool out = _state.test(N - 1);
204 const bool fb = taps_xor<Taps...>(_state);
211 uint64_t
step(
const uint32_t nbits)
noexcept
214 for (uint32_t i = 0; i < nbits; ++i) {
215 v |= (uint64_t(
step()) << i);
222 inline uint16_t next16() noexcept
224 return (uint16_t)
step(16);
226 inline uint32_t next32() noexcept
228 return (uint32_t)
step(32);
230 inline uint64_t next64() noexcept
239 return taps_xor<Taps...>(s);
Fibonacci LFSR (left-shift version)
Definition lfsr.hpp:143
bool step() noexcept
Shift 1 step (Left). Returns output bit (MSB before shift)
Definition lfsr.hpp:201
std::bitset< N > state_type_t
State type.
Definition lfsr.hpp:171
uint64_t step(const uint32_t nbits) noexcept
Shift nbits steps and pack outputs LSB-first.
Definition lfsr.hpp:211
typename uint_least_for_bits< N >::type storage_t
uint8/16/32/64_t
Definition lfsr.hpp:170
const state_type_t & state() const noexcept
Gets the state.
Definition lfsr.hpp:181
storage_t value() const
Gets the state value.
Definition lfsr.hpp:189
Fibonacci LFSRs (right-shift version)
Definition lfsr.hpp:28
typename uint_least_for_bits< N >::type storage_t
uint8/16/32/64_t
Definition lfsr.hpp:56
uint16_t next16() noexcept
Shift 16 bits and get.
Definition lfsr.hpp:110
std::bitset< N > state_type_t
State type.
Definition lfsr.hpp:57
storage_t value() const
Gets the state value.
Definition lfsr.hpp:75
uint64_t next64() noexcept
Shift 64 bits and get.
Definition lfsr.hpp:120
uint32_t next32() noexcept
Shift 32 bits and get.
Definition lfsr.hpp:115
uint64_t step(const uint32_t nbits) noexcept
Shift nbit steps and gets.
Definition lfsr.hpp:98
const state_type_t & state() const noexcept
Gets the state.
Definition lfsr.hpp:67
bool step() noexcept
Shift 1 step (Right). Returns output bit (LSB before shift)
Definition lfsr.hpp:87
Top level namespace of M5.
Definition bit_segment.hpp:17