-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmls.hh
More file actions
60 lines (54 loc) · 798 Bytes
/
mls.hh
File metadata and controls
60 lines (54 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Maximum length sequence
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#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) {}
void reset(int r = 1)
{
reg = r;
}
int length()
{
return hibit(poly) - 1;
}
bool bad(int r = 1)
{
reg = r;
int len = length();
for (int i = 1; i < len; ++i) {
(*this)();
if (reg == r)
return true;
}
(*this)();
return reg != r;
}
int next()
{
(*this)();
return reg;
}
bool operator()()
{
bool fb = reg & test;
reg <<= 1;
reg ^= fb * poly;
return fb;
}
};
}