-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path15-00_error_correcting_codes.go
74 lines (61 loc) · 1.61 KB
/
15-00_error_correcting_codes.go
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
package hd
// CheckBits produces 6bits of parity checks.
// It is simple Hamming Code based error detection and correction scheme.
func CheckBits(u uint32) uint32 {
p := [6]uint32{}
p[0] = u
p[0] ^= p[0] >> 2
p[0] ^= p[0] >> 4
p[0] ^= p[0] >> 8
p[0] ^= p[0] >> 16
t := u ^ (u >> 1)
p[1] = t
p[1] ^= p[1] >> 4
p[1] ^= p[1] >> 8
p[1] ^= p[1] >> 16
t ^= t >> 2
p[2] = t
p[2] ^= p[2] >> 8
p[2] ^= p[2] >> 16
t ^= t >> 4
p[3] = t
p[3] ^= p[3] >> 16
t ^= t >> 8
p[4] = t
p[5] = p[4]
p[5] ^= p[5] >> 16
var pp uint32
pp |= (p[0] >> 1) & 1
pp |= (p[1] >> 1) & 2
pp |= (p[2] >> 2) & 4
pp |= (p[3] >> 5) & 8
pp |= (p[4] >> 12) & 16
pp |= (p[5] & 1) << 5
pp ^= (-(u & 1) & 0x3F)
return pp
}
// Syndrome in Hamming based error checking code
func Syndrome(pr, u uint32) uint32 {
p := CheckBits(u)
syn := p ^ (pr & 0x3F)
return syn
}
// Correct bits in pr that contain 32 "information" bits stored in ur and 7 "check" bits stored in pr.
// Check bits are extracted and error is corrected in returned result.
// This can correct up to 1 wrong bit.
// This is based on Hamming error correcting code.
// It returns corrected bits and number of error bits detected (0,1,2).
// TODO: 2 bits error detection is not working.
func Correct(pr, u uint32) (cr uint32, errb uint8) {
// TODO: why parity for detection of 2 errors is not working?
syn := Syndrome(pr, u)
if syn == 0 {
return u, 0
}
// One error ocurred, but error is in check bits, so no correction of "information" bits required.
if ((syn - 1) & syn) == 0 {
return u, 1
}
b := syn - 31 - (syn >> 5) // map syn to range [0, 31]
return u ^ (1 << b), 1
}