1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-25 18:30:21 +00:00

Adds necessary standalone #imports; makes safe for signed types.

This commit is contained in:
Thomas Harte 2020-04-25 22:21:10 -04:00
parent 65a3783dd2
commit a5d1941d28

View File

@ -9,6 +9,9 @@
#ifndef LFSR_h #ifndef LFSR_h
#define LFSR_h #define LFSR_h
#include <cstdint>
#include <cstdlib>
namespace Numeric { namespace Numeric {
template <typename IntType> struct LSFRPolynomial {}; template <typename IntType> struct LSFRPolynomial {};
@ -41,11 +44,12 @@ template <typename IntType = uint64_t, IntType polynomial = LSFRPolynomial<IntTy
Constructs an LFSR with a random initial value. Constructs an LFSR with a random initial value.
*/ */
LFSR() { LFSR() {
// Randomise the value, ensuring it doesn't end up being 0. // Randomise the value, ensuring it doesn't end up being 0;
// don't set any top bits, in case this is a signed type.
while(!value_) { while(!value_) {
uint8_t *value_byte = reinterpret_cast<uint8_t *>(&value_); uint8_t *value_byte = reinterpret_cast<uint8_t *>(&value_);
for(size_t c = 0; c < sizeof(IntType); ++c) { for(size_t c = 0; c < sizeof(IntType); ++c) {
*value_byte = uint8_t(uint64_t(rand()) * 255 / RAND_MAX); *value_byte = uint8_t(uint64_t(rand()) * 127 / RAND_MAX);
++value_byte; ++value_byte;
} }
} }