From f912be03353ebd2fd0546c1d4adcc19904fe26a5 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Fri, 28 Sep 2018 10:09:18 +0200 Subject: [PATCH] use constants to avoid mistakes --- bose_chaudhuri_hocquenghem_decoder.hh | 6 ++++-- bose_chaudhuri_hocquenghem_encoder.hh | 5 ++++- reed_solomon_decoder.hh | 3 ++- reed_solomon_encoder.hh | 5 ++++- tests/bch_decoder_test.cc | 8 ++++---- tests/rs_decoder_test.cc | 6 +++--- tests/rs_regression_test.cc | 4 ++-- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/bose_chaudhuri_hocquenghem_decoder.hh b/bose_chaudhuri_hocquenghem_decoder.hh index 1be82cc..0b00603 100644 --- a/bose_chaudhuri_hocquenghem_decoder.hh +++ b/bose_chaudhuri_hocquenghem_decoder.hh @@ -12,13 +12,14 @@ Copyright 2018 Ahmet Inan namespace CODE { -template +template class BoseChaudhuriHocquenghemDecoder { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; + static const int NR = ROOTS; static const int N = GF::N, K = MSG, NP = N - K; private: ReedSolomonErrorCorrection algorithm; @@ -98,13 +99,14 @@ public: } }; -template +template class BoseChaudhuriHocquenghemDecoderReference { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; + static const int NR = ROOTS; static const int N = GF::N, K = MSG, NP = N - K; private: ReedSolomonErrorCorrection algorithm; diff --git a/bose_chaudhuri_hocquenghem_encoder.hh b/bose_chaudhuri_hocquenghem_encoder.hh index c04a5d7..f014e1c 100644 --- a/bose_chaudhuri_hocquenghem_encoder.hh +++ b/bose_chaudhuri_hocquenghem_encoder.hh @@ -79,15 +79,18 @@ public: } }; -template +template class BoseChaudhuriHocquenghemEncoderReference { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; + static const int NR = ROOTS; static const int N = GF::N, K = MSG, NP = N - K; +private: ValueType generator[NP+1]; +public: BoseChaudhuriHocquenghemEncoderReference(std::initializer_list minimal_polynomials) { // $generator(x) = \prod_i(minpoly_i(x))$ diff --git a/reed_solomon_decoder.hh b/reed_solomon_decoder.hh index c6445cf..d469a28 100644 --- a/reed_solomon_decoder.hh +++ b/reed_solomon_decoder.hh @@ -11,13 +11,14 @@ Copyright 2018 Ahmet Inan namespace CODE { -template +template class ReedSolomonDecoder { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; + static const int NR = ROOTS; static const int N = GF::N, K = N - NR, NP = NR; private: ReedSolomonErrorCorrection algorithm; diff --git a/reed_solomon_encoder.hh b/reed_solomon_encoder.hh index 66e44f4..6de493d 100644 --- a/reed_solomon_encoder.hh +++ b/reed_solomon_encoder.hh @@ -9,15 +9,18 @@ Copyright 2018 Ahmet Inan namespace CODE { -template +template class ReedSolomonEncoder { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; + static const int NR = ROOTS; static const int N = GF::N, K = N - NR, NP = NR; +private: IndexType generator[NR+1]; +public: ReedSolomonEncoder() { // $generator = \prod_{i=0}^{NR}(x-pe^{FCR+i})$ diff --git a/tests/bch_decoder_test.cc b/tests/bch_decoder_test.cc index 814d480..ac437db 100644 --- a/tests/bch_decoder_test.cc +++ b/tests/bch_decoder_test.cc @@ -30,7 +30,7 @@ int main() code[i] = target[i]; typedef std::uniform_int_distribution distribution; auto noise = std::bind(distribution(0, BCH::N-1), generator); - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < BCH::NR/2; ++i) { int n = noise(); if (n < BCH::K) CODE::xor_be_bit(code, n, 1); @@ -63,7 +63,7 @@ int main() code[i] = target[i]; typedef std::uniform_int_distribution distribution; auto noise = std::bind(distribution(0, BCH::N-1), generator); - for (int i = 0; i < 12; ++i) { + for (int i = 0; i < BCH::NR/2; ++i) { int n = noise(); if (n < BCH::K) CODE::xor_be_bit(code, n, 1); @@ -90,7 +90,7 @@ int main() code[i] = target[i]; typedef std::uniform_int_distribution distribution; auto noise = std::bind(distribution(0, BCH::N-1), generator); - for (int i = 0; i < 3; ++i) + for (int i = 0; i < BCH::NR/2; ++i) code[noise()] ^= 1; decode(reinterpret_cast(code), reinterpret_cast(code) + BCH::K); for (int i = 0; i < BCH::N; ++i) @@ -113,7 +113,7 @@ int main() code[i] = target[i]; typedef std::uniform_int_distribution distribution; auto noise = std::bind(distribution(0, BCH::N-1), generator); - for (int i = 0; i < 12; ++i) + for (int i = 0; i < BCH::NR/2; ++i) code[noise()] ^= 1; (*decode)(reinterpret_cast(code), reinterpret_cast(code) + BCH::K); for (int i = 0; i < BCH::N; ++i) diff --git a/tests/rs_decoder_test.cc b/tests/rs_decoder_test.cc index 0790da9..bb7e417 100644 --- a/tests/rs_decoder_test.cc +++ b/tests/rs_decoder_test.cc @@ -27,7 +27,7 @@ int main() typedef std::uniform_int_distribution distribution; auto pos = std::bind(distribution(0, RS::N-1), generator); auto val = std::bind(distribution(0, RS::N), generator); - for (int i = 0; i < 2; ++i) + for (int i = 0; i < RS::NR/2; ++i) code[pos()] = val(); decode(code, code + RS::K); for (int i = 0; i < RS::N; ++i) @@ -51,7 +51,7 @@ int main() typedef std::uniform_int_distribution distribution; auto pos = std::bind(distribution(0, RS::N-1), generator); auto val = std::bind(distribution(0, RS::N), generator); - for (int i = 0; i < 8; ++i) + for (int i = 0; i < RS::NR/2; ++i) code[pos()] = val(); decode(code, code + RS::K); for (int i = 0; i < RS::N; ++i) @@ -75,7 +75,7 @@ int main() typedef std::uniform_int_distribution distribution; auto pos = std::bind(distribution(0, RS::N-1), generator); auto val = std::bind(distribution(0, RS::N), generator); - for (int i = 0; i < 32; ++i) + for (int i = 0; i < RS::NR/2; ++i) code[pos()] = val(); (*decode)(code, code + RS::K); for (int i = 0; i < RS::N; ++i) diff --git a/tests/rs_regression_test.cc b/tests/rs_regression_test.cc index be53f8c..dc4d6a9 100644 --- a/tests/rs_regression_test.cc +++ b/tests/rs_regression_test.cc @@ -17,7 +17,7 @@ void rs_test(ENC *encode, DEC *decode, int trials) std::random_device rd; std::default_random_engine generator(rd()); typedef std::uniform_int_distribution distribution; - auto rnd_cnt = std::bind(distribution(0, ENC::NP), generator); + auto rnd_cnt = std::bind(distribution(0, ENC::NR), generator); auto rnd_len = std::bind(distribution(1, ENC::K), generator); auto rnd_val = std::bind(distribution(0, ENC::N), generator); while (--trials) { @@ -49,7 +49,7 @@ void rs_test(ENC *encode, DEC *decode, int trials) else parity[pos-data_len] = rnd_val(); } - int erasures_count = ENC::NP - 2 * error_count; + int erasures_count = ENC::NR - 2 * error_count; typename ENC::value_type erasures[erasures_count]; for (int i = 0; i < erasures_count; ++i) { int pos = rnd_pos();