-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbch.c
74 lines (66 loc) · 1.7 KB
/
bch.c
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
/*\
* Yes, This Is Another Barcode Reader
* Copyright (C) 2013 Quentin SANTOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
\*/
#include "bch.h"
static int leading_bit(unsigned int x);
static int hamming_weight(bch_t code);
static int leading_bit(unsigned int x)
{
return 31 - __builtin_clz(x);
}
bch_t bch_check(bch_t g, bch_t code)
{
int ncode = leading_bit(code);
int ngen = leading_bit(g);
bch_t mask = 1 << ncode;
for (int i = ncode-ngen; i >= 0; i--, mask >>= 1)
if (code & mask)
code ^= g << i;
return code;
}
bch_t bch_encode(bch_t g, bch_t code)
{
int ngen = leading_bit(g);
code <<= ngen;
return code ^ bch_check(g, code);
}
static int hamming_weight(bch_t code)
{
return __builtin_popcount(code);
}
bch_t bch_decode(bch_t g, bch_t code)
{
int n_bits = leading_bit(code) / 2;
int maxn = 1<<n_bits;
bch_t best_v = -1;
int best_d = 64;
for (int v = 0; v < maxn; v++)
{
bch_t test = bch_encode(g, v);
int d = hamming_weight(code ^ test);
if (d < best_d)
{
best_v = v;
best_d = d;
}
else if (d == best_d) // conflict
{
best_v = -1;
}
}
return best_v;
}