-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFSR.cpp
More file actions
163 lines (138 loc) · 3.91 KB
/
LFSR.cpp
File metadata and controls
163 lines (138 loc) · 3.91 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "LFSR.hxx"
LFSR_indexes::LFSR_indexes():
indexes_list( {
{ 3, 0x000003 },
{ 4, 0x000003 },
{ 5, 0x00001b },
{ 6, 0x00002d },
{ 7, 0x000053 },
{ 8, 0x00004d },
{ 9, 0x0000f9 },
{ 10, 0x00025b },
{ 11, 0x0001e5 },
{ 12, 0x000a69 },
{ 13, 0x000693 },
{ 14, 0x000a65 },
{ 15, 0x004d33 },
{ 16, 0x00a52b },
{ 17, 0x005267 },
{ 18, 0x01927d },
{ 19, 0x052795 },
{ 20, 0x05ff2b },
{ 21, 0x056d79 },
{ 22, 0x1cca5d },
{ 23, 0x26e7ad },
{ 24, 0x399b5d }
})
{}
optional< const LFSR_indexes::LFSR_type >LFSR_indexes::operator[]( const unsigned short&bits )
{
auto it = indexes_list.find( bits );
// perhaps, in a far future, we are going to have a find that do the job directly
if ( it != indexes_list.end() )
return it->second;
else
return nullopt;
}
LFSR_parameters::LFSR_parameters( const unsigned short&number_bits, const LFSR_indexes::LFSR_type &indexes ):
number_bits( number_bits ), indexes( indexes )
{
// Temporary
initial_state[0]=true;
}
LFSR_extract::LFSR_extract():
extract_data({
{ "destination 1" , { 0, 1 } },
{ "destination 2" , { 2 } }
})
{}
optional<pair<const unsigned short,const unsigned short> > LFSR_extract::test_and_required_bits()const
{
set<unsigned short> bits_involved;
bool isOk = true;
unsigned short nbre_bits = 0;
unsigned short the_max = 0;
for_each( extract_data.begin(), extract_data.end(),[&](const pair<string, deque<unsigned short> >&a){
for_each( a.second.begin(), a.second.end(), [&](const unsigned short&b){
if ( bits_involved.find( b ) != bits_involved.end() )
{
bits_involved.insert(b);
nbre_bits += 1;
if ( b > the_max )
the_max = b;
}
else
{
isOk = false;
}
});
});
if ( isOk )
return make_pair(nbre_bits,the_max);
else
return nullopt;
}
void LFSR_histo::operator()(const LFSR_indexes::LFSR_type&a)
{
auto histo_iter = histo.find( a );
if ( histo_iter != histo.end() )
histo_iter->second += 1;
else
histo.insert(make_pair( a , 1 ));
}
deque<pair<string, unsigned long> >LFSR_histo::GetStatistics()const
{
deque<pair<string, unsigned long> >the_return;
unsigned long the_sum = 0;
unsigned long the_N = 0;
for_each( histo.begin(), histo.end(), [&](const pair<LFSR_indexes::LFSR_type,unsigned long>&a){
the_sum+=a.second;
the_N += 1;
});
if ( the_N == 0 )
{
the_return.push_back( make_pair( "Bad", 2 ));
}
else if ( the_sum % the_N == 0 )
{
the_return.push_back( make_pair( "Good", 0 ));
the_return.push_back( make_pair( "Number of occurrences", the_sum / the_N ));
}else
// Until now this didn't happened.
// The code is going to be written on the first problem
the_return.push_back( make_pair( "Bad", 1 ));
the_return.push_back( make_pair( "Number of elements", the_N ));
return the_return;
}
ostream&operator<<(ostream&ostr,const LFSR_histo&a)
{
for( auto the_data: a.histo )
ostr<<the_data.first<<" "<<the_data.second<<"\t";
return ostr;
}
LFSR_iterator::LFSR_iterator( const LFSR_parameters&LFSR_init, const LFSR_extract&extract ):
LFSR_init( LFSR_init ),
extract( extract ),
nbreRuns( 0 ),
current_state( LFSR_init.initial_state ),
maskXor( LFSR_init.indexes )
{}
const LFSR_indexes::LFSR_type LFSR_iterator::operator()()
{
// Select the bits that should be involved in the XOR
bitset<numeric_limits<LFSR_indexes::LFSR_type>::digits >maskedState = current_state & maskXor;
bool xor_result = false;
if ( maskedState.count() % 2 == 1 )
xor_result = true;
// Got problems with g++13 then do it by hands
//current-State = rotr( currentState, 1 );
for ( unsigned short ind = 1; ind < LFSR_init.number_bits ; ind++ )
current_state[ ind - 1 ] = current_state[ ind ];
current_state[ LFSR_init.number_bits - 1 ] = xor_result;
nbreRuns += 1;
return nbreRuns;
}
const bitset<numeric_limits<LFSR_indexes::LFSR_type>::digits >LFSR_iterator::GetState()const
{
return current_state;
}