10#ifndef M5_UTILITY_LFSR_HPP
11#define M5_UTILITY_LFSR_HPP
26template <uint32_t N, uint32_t... Taps>
28 static_assert(N > 0,
"N must be > 0");
29 static_assert(N <= 64,
"N must be <= 64");
30 static_assert(
sizeof...(Taps) >= 1,
"At least one tap required");
33 template <uint32_t...>
34 struct all_valid : std::true_type {
37 template <uint32_t A, uint32_t... Rest>
38 struct all_valid<A, Rest...> : std::integral_constant<bool, (A >= 1 && A <= N) && all_valid<Rest...>::value> {
41 static_assert(all_valid<Taps...>
::value,
"Taps out of range");
45 template <uint32_t... Ts>
46 static bool taps_xor(
const std::bitset<N>& s)
49 using swallow =
int[];
50 (void)swallow{0, (r ^= s.test(N - Ts), 0)...};
62 explicit FibonacciLFSR_Right(
const state_type_t s) noexcept : _state{s}
74 template <
typename UL =
unsigned long,
75 typename std::enable_if<(
sizeof(UL) * CHAR_BIT >= 64), std::nullptr_t>::type =
nullptr>
78 return static_cast<uint64_t
>(_state.to_ulong());
80 template <
typename UL =
unsigned long,
81 typename std::enable_if<(
sizeof(UL) * CHAR_BIT < 64), std::nullptr_t>::type =
nullptr>
82 inline uint64_t
value()
const
84 return _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 > 0,
"N must be > 0");
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");
161 template <uint32_t... Ts>
162 static bool taps_xor(
const std::bitset<N>& s)
165 using swallow =
int[];
167 (void)swallow{0, (r ^= s.test(Ts - 1), 0)...};
173 using state_type_t = std::bitset<N>;
186 inline const state_type_t&
state() const noexcept
192 template <
typename UL =
unsigned long,
193 typename std::enable_if<(
sizeof(UL) * CHAR_BIT >= 64), std::nullptr_t>::type =
nullptr>
196 return static_cast<uint64_t
>(_state.to_ulong());
198 template <
typename UL =
unsigned long,
199 typename std::enable_if<(
sizeof(UL) * CHAR_BIT < 64), std::nullptr_t>::type =
nullptr>
200 inline uint64_t
value()
const
202 return _state.to_ullong();
208 const bool out = _state.test(N - 1);
209 const bool fb = taps_xor<Taps...>(_state);
216 uint64_t
step(
const uint32_t nbits)
noexcept
219 for (uint32_t i = 0; i < nbits; ++i) {
220 v |= (uint64_t(
step()) << i);
227 inline uint16_t next16() noexcept
229 return (uint16_t)
step(16);
231 inline uint32_t next32() noexcept
233 return (uint32_t)
step(32);
235 inline uint64_t next64() noexcept
242 static inline bool taps_xor_all(
const state_type_t& s)
244 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:206
uint64_t value() const
Gets the state value.
Definition lfsr.hpp:194
uint64_t step(const uint32_t nbits) noexcept
Shift nbits steps and pack outputs LSB-first.
Definition lfsr.hpp:216
const state_type_t & state() const noexcept
Gets the state.
Definition lfsr.hpp:186
Fibonacci LFSRs (right-shift version)
Definition lfsr.hpp:27
uint16_t next16() noexcept
Shift 16 bits and get.
Definition lfsr.hpp:110
std::bitset< N > state_type_t
State type.
Definition lfsr.hpp:55
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:68
uint64_t value() const
Gets the state value.
Definition lfsr.hpp:76
bool step() noexcept
Shift 1 step (Right). Returns output bit (LSB before shift)
Definition lfsr.hpp:88
Top level namespace of M5.
Definition bit_segment.hpp:17