From 1e7a25e1a87af4a263ae2a150674607f646891ae Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Tue, 31 Mar 2020 18:05:10 +0200 Subject: [PATCH] added maximum length sequence --- README.md | 4 ++++ mls.hh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 mls.hh diff --git a/README.md b/README.md index a7d97c1..f05ce92 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ Sometimes we need a sequence of ["random enough"](https://en.wikipedia.org/wiki/ Here a [Pseudorandom number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) can help by prodiving a deterministic and thus repeatable sequence of numbers. [George Marsaglia](https://en.wikipedia.org/wiki/George_Marsaglia) discovered a class of simple and fast pseudorandom number generators, which he called [Xorshift](https://en.wikipedia.org/wiki/Xorshift). +### [mls.hh](mls.hh) + +[Maximum length sequences](https://en.wikipedia.org/wiki/Maximum_length_sequence) have useful [correlation properties](https://en.wikipedia.org/wiki/Maximum_length_sequence#Correlation_property). + ### [bitstream.hh](bitstream.hh) When dealing with unaligned and arbitrary-bit-sized elements in a data stream, the bitwise stream container might help avoiding some headaches. diff --git a/mls.hh b/mls.hh new file mode 100644 index 0000000..b1a3f19 --- /dev/null +++ b/mls.hh @@ -0,0 +1,35 @@ +/* +Maximum length sequence + +Copyright 2020 Ahmet Inan +*/ + +#pragma once + +namespace CODE { + +class MLS +{ + static int hibit(unsigned n) + { + n |= n >> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + return n ^ (n >> 1); + } + int poly, test, reg; +public: + MLS(int poly = 0b100000000000000001001, int reg = 1) : poly(poly), test(hibit(poly)>>1), reg(reg) {} + bool operator()() + { + bool fb = reg & test; + reg <<= 1; + reg ^= fb * poly; + return fb; + } +}; + +} +