forked from szechyjs/dsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamming.cpp
83 lines (65 loc) · 2.46 KB
/
Hamming.cpp
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
#include "Hamming.hpp"
// definition outside class declaration
Hamming_10_6_3_data Hamming_10_6_3::data;
// definition outside class declaration
// Should go *after* Hamming_10_6_3_data since Hamming_10_6_3_TableImpl_data constructor uses
// Hamming_10_6_3 which in its turn depends on Hamming_10_6_3_data
Hamming_10_6_3_TableImpl_data Hamming_10_6_3_TableImpl::data;
int Hamming_10_6_3::decode(std::bitset<10>& input)
{
int error_count;
// Compute syndromes
int s0 = ((data.h0 & input).count() & 1) << 3;
int s1 = ((data.h1 & input).count() & 1) << 2;
int s2 = ((data.h2 & input).count() & 1) << 1;
int s3 = ((data.h3 & input).count() & 1);
int parity = s0 | s1 | s2 | s3;
if (parity == 0) {
//std::cout << input << " is OK" << std::endl;
error_count = 0;
} else {
// Error detected, attempt to fix it
int bad_bit_index = data.bad_bit_table[parity];
if (bad_bit_index < 0) {
//std::cout << "Irrecoverable error: " << std::bitset<6>(input.to_string()) << std::endl;
error_count = 2;
} else {
// Error in a data bit, or more than one error
// If there is one erroneous bit, the bad one is the one indicated by parity
if (bad_bit_index < 4) {
// Error detected in a parity bit
//std::cout << "Error on parity bit " << bad_bit_index << std::endl;
error_count = 1;
} else {
input.flip(bad_bit_index);
//std::cout << "Error on data bit " << bad_bit_index << ", hopefully fixed: " << std::bitset<6>(input.to_string()) << std::endl;
error_count = 1;
}
}
}
//std::cout << std::endl;
return error_count;
}
int Hamming_10_6_3::encode(std::bitset<6>& input)
{
// Compute syndromes
int s0 = ((data.gt0 & input).count() & 1) << 3;
int s1 = ((data.gt1 & input).count() & 1) << 2;
int s2 = ((data.gt2 & input).count() & 1) << 1;
int s3 = ((data.gt3 & input).count() & 1);
int parity = s0 | s1 | s2 | s3;
return parity;
}
int Hamming_10_6_3_TableImpl::decode(int input, int* output)
{
assert (input < 1024 && input >= 0);
// Making use of a table...
*output = data.fixed_values[input];
return data.error_counts[input];
}
int Hamming_10_6_3_TableImpl::encode(int input)
{
assert (input < 64 && input >= 0);
// Making use of a table...
return data.encode_parities[input];
}