moved constructor generator argument to template parameter

This commit is contained in:
Ahmet Inan 2020-06-14 20:43:45 +02:00
commit b54be1c997
6 changed files with 22 additions and 22 deletions

View file

@ -8,7 +8,7 @@ Copyright 2020 Ahmet Inan <inan@aicodix.de>
namespace CODE { namespace CODE {
template <int N, int K, int T> template <int N, int K, int G, int T>
class ShortBCHCodeDecoder class ShortBCHCodeDecoder
{ {
static const int P = N - K; static const int P = N - K;
@ -19,11 +19,11 @@ class ShortBCHCodeDecoder
static_assert(N < 8 * sizeof(err[0]), "codeword type not wide enough"); static_assert(N < 8 * sizeof(err[0]), "codeword type not wide enough");
static_assert(P < 8 * sizeof(par[0]), "parity type not wide enough"); static_assert(P < 8 * sizeof(par[0]), "parity type not wide enough");
static_assert(T > 0 && T <= 4, "unsupported radius T"); static_assert(T > 0 && T <= 4, "unsupported radius T");
static int modgen(int inp, int gen) static int modgen(int inp)
{ {
for (int i = K-1; i >= 0; --i) { for (int i = K-1; i >= 0; --i) {
int tmp = inp >> (i+P); int tmp = inp >> (i+P);
inp ^= (tmp & 1) * (gen << i); inp ^= (tmp & 1) * (G << i);
} }
return inp; return inp;
} }
@ -36,19 +36,19 @@ class ShortBCHCodeDecoder
return sum; return sum;
} }
public: public:
ShortBCHCodeDecoder(int generator) ShortBCHCodeDecoder()
{ {
for (int i = 0; i < W; ++i) for (int i = 0; i < W; ++i)
par[i] = modgen(i << P, generator); par[i] = modgen(i << P);
err[0] = 0; err[0] = 0;
for (int a = 1<<(N-1); T >= 1 && a; a >>= 1) { for (int a = 1<<(N-1); T >= 1 && a; a >>= 1) {
err[modgen(a, generator)] = a; err[modgen(a)] = a;
for (int b = a >> 1; T >= 2 && b; b >>= 1) { for (int b = a >> 1; T >= 2 && b; b >>= 1) {
err[modgen(a|b, generator)] = a|b; err[modgen(a|b)] = a|b;
for (int c = b >> 1; T >= 3 && c; c >>= 1) { for (int c = b >> 1; T >= 3 && c; c >>= 1) {
err[modgen(a|b|c, generator)] = a|b|c; err[modgen(a|b|c)] = a|b|c;
for (int d = c >> 1; T >= 4 && d; d >>= 1) { for (int d = c >> 1; T >= 4 && d; d >>= 1) {
err[modgen(a|b|c|d, generator)] = a|b|c|d; err[modgen(a|b|c|d)] = a|b|c|d;
} }
} }
} }

View file

@ -8,7 +8,7 @@ Copyright 2020 Ahmet Inan <inan@aicodix.de>
namespace CODE { namespace CODE {
template <int N, int K> template <int N, int K, int G>
class ShortBCHCodeEncoder class ShortBCHCodeEncoder
{ {
static const int P = N - K; static const int P = N - K;
@ -16,19 +16,19 @@ class ShortBCHCodeEncoder
short par[W]; short par[W];
static_assert(N < 8 * sizeof(int), "codeword type not wide enough"); static_assert(N < 8 * sizeof(int), "codeword type not wide enough");
static_assert(P < 8 * sizeof(par[0]), "parity type not wide enough"); static_assert(P < 8 * sizeof(par[0]), "parity type not wide enough");
static int modgen(int inp, int gen) static int modgen(int inp)
{ {
for (int i = K-1; i >= 0; --i) { for (int i = K-1; i >= 0; --i) {
int tmp = inp >> (i+P); int tmp = inp >> (i+P);
inp ^= (tmp & 1) * (gen << i); inp ^= (tmp & 1) * (G << i);
} }
return inp; return inp;
} }
public: public:
ShortBCHCodeEncoder(int generator) ShortBCHCodeEncoder()
{ {
for (int i = 0; i < W; ++i) for (int i = 0; i < W; ++i)
par[i] = modgen(i << P, generator); par[i] = modgen(i << P);
} }
int operator()(int msg) int operator()(int msg)
{ {

View file

@ -17,7 +17,7 @@ int main()
if (1) { if (1) {
// Perfect binary Golay code using x^11+x^9+x^7+x^6+x^5+x+1 // Perfect binary Golay code using x^11+x^9+x^7+x^6+x^5+x+1
const int N = 23, K = 12, T = 3, POLY = 0b101011100011; const int N = 23, K = 12, T = 3, POLY = 0b101011100011;
CODE::ShortBCHCodeDecoder<N, K, T> decode(POLY); CODE::ShortBCHCodeDecoder<N, K, POLY, T> decode;
int target = 0b10101101101111011111001; int target = 0b10101101101111011111001;
int damaged = target; int damaged = target;
typedef std::uniform_int_distribution<int> distribution; typedef std::uniform_int_distribution<int> distribution;
@ -30,7 +30,7 @@ int main()
if (1) { if (1) {
// Perfect binary Golay code using x^11+x^10+x^6+x^5+x^4+x^2+1 // Perfect binary Golay code using x^11+x^10+x^6+x^5+x^4+x^2+1
const int N = 23, K = 12, T = 3, POLY = 0b110001110101; const int N = 23, K = 12, T = 3, POLY = 0b110001110101;
CODE::ShortBCHCodeDecoder<N, K, T> decode(POLY); CODE::ShortBCHCodeDecoder<N, K, POLY, T> decode;
int target = 0b10101101101100100010101; int target = 0b10101101101100100010101;
int damaged = target; int damaged = target;
typedef std::uniform_int_distribution<int> distribution; typedef std::uniform_int_distribution<int> distribution;
@ -43,7 +43,7 @@ int main()
if (1) { if (1) {
// NASA INTRO BCH(15, 5) T=3 // NASA INTRO BCH(15, 5) T=3
const int N = 15, K = 5, T = 3, POLY = 0b10100110111; const int N = 15, K = 5, T = 3, POLY = 0b10100110111;
CODE::ShortBCHCodeDecoder<N, K, T> decode(POLY); CODE::ShortBCHCodeDecoder<N, K, POLY, T> decode;
int target = 0b110010001111010; int target = 0b110010001111010;
int damaged = target; int damaged = target;
typedef std::uniform_int_distribution<int> distribution; typedef std::uniform_int_distribution<int> distribution;

View file

@ -22,7 +22,7 @@ int popcnt(TYPE x)
template <int N, int K, int T, int POLY> template <int N, int K, int T, int POLY>
void bch_test(int message, int target) void bch_test(int message, int target)
{ {
CODE::ShortBCHCodeEncoder<N, K> encode(POLY); CODE::ShortBCHCodeEncoder<N, K, POLY> encode;
int hist[N+1] = { 0 }; int hist[N+1] = { 0 };
for (int j = 0; j < (1 << K); ++j) for (int j = 0; j < (1 << K); ++j)
for (int i = j + 1; i < (1 << K); ++i) for (int i = j + 1; i < (1 << K); ++i)

View file

@ -14,8 +14,8 @@ Copyright 2020 Ahmet Inan <inan@aicodix.de>
template <int N, int K, int T, int POLY> template <int N, int K, int T, int POLY>
void bch_test(int trials) void bch_test(int trials)
{ {
CODE::ShortBCHCodeEncoder<N, K> encode(POLY); CODE::ShortBCHCodeEncoder<N, K, POLY> encode;
CODE::ShortBCHCodeDecoder<N, K, T> decode(POLY); CODE::ShortBCHCodeDecoder<N, K, POLY, T> decode;
std::random_device rd; std::random_device rd;
typedef std::default_random_engine generator; typedef std::default_random_engine generator;
typedef std::uniform_int_distribution<int> distribution; typedef std::uniform_int_distribution<int> distribution;

View file

@ -54,8 +54,8 @@ int popcnt(TYPE x)
int main() int main()
{ {
CODE::ShortBCHCodeEncoder<CODE_LEN, DATA_LEN> encode(GEN_POLY); CODE::ShortBCHCodeEncoder<CODE_LEN, DATA_LEN, GEN_POLY> encode;
CODE::ShortBCHCodeDecoder<CODE_LEN, DATA_LEN, RADIUS_T> decode(GEN_POLY); CODE::ShortBCHCodeDecoder<CODE_LEN, DATA_LEN, GEN_POLY, RADIUS_T> decode;
std::random_device rd; std::random_device rd;
std::default_random_engine generator(rd()); std::default_random_engine generator(rd());