From 2bf8d4ea8476cb55b4202626b2014fd77a21f842 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Fri, 21 Sep 2018 19:26:30 +0200 Subject: [PATCH] use constants to avoid mistakes --- bose_chaudhuri_hocquenghem_decoder.hh | 4 +-- bose_chaudhuri_hocquenghem_encoder.hh | 4 +-- reed_solomon_decoder.hh | 2 +- reed_solomon_encoder.hh | 2 +- tests/bch_decoder_test.cc | 28 ++++++++-------- tests/bch_encoder_test.cc | 20 ++++++------ tests/rs_decoder_test.cc | 46 +++++++++++++-------------- tests/rs_encoder_test.cc | 32 +++++++++---------- 8 files changed, 69 insertions(+), 69 deletions(-) diff --git a/bose_chaudhuri_hocquenghem_decoder.hh b/bose_chaudhuri_hocquenghem_decoder.hh index e0ddd75..7eb507d 100644 --- a/bose_chaudhuri_hocquenghem_decoder.hh +++ b/bose_chaudhuri_hocquenghem_decoder.hh @@ -11,14 +11,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 N = GF::N, NP = N - K; + static const int N = GF::N, K = MSG, NP = N - K; ReedSolomonErrorCorrection algorithm; int compute_syndromes(ValueType *code, ValueType *syndromes) { diff --git a/bose_chaudhuri_hocquenghem_encoder.hh b/bose_chaudhuri_hocquenghem_encoder.hh index 4ae31d0..fba9422 100644 --- a/bose_chaudhuri_hocquenghem_encoder.hh +++ b/bose_chaudhuri_hocquenghem_encoder.hh @@ -11,14 +11,14 @@ Copyright 2018 Ahmet Inan namespace CODE { -template +template class BoseChaudhuriHocquenghemEncoder { public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; - static const int N = GF::N, NP = N - K; + static const int N = GF::N, K = MSG, NP = N - K; ValueType generator[NP+1]; BoseChaudhuriHocquenghemEncoder(std::initializer_list minimal_polynomials) { diff --git a/reed_solomon_decoder.hh b/reed_solomon_decoder.hh index fbbde5c..02a2f78 100644 --- a/reed_solomon_decoder.hh +++ b/reed_solomon_decoder.hh @@ -18,7 +18,7 @@ public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; - static const int N = GF::N, K = N - NR; + static const int N = GF::N, K = N - NR, NP = NR; ReedSolomonErrorCorrection algorithm; int compute_syndromes(ValueType *code, ValueType *syndromes) { diff --git a/reed_solomon_encoder.hh b/reed_solomon_encoder.hh index 36e274f..dee285a 100644 --- a/reed_solomon_encoder.hh +++ b/reed_solomon_encoder.hh @@ -16,7 +16,7 @@ public: typedef typename GF::value_type value_type; typedef typename GF::ValueType ValueType; typedef typename GF::IndexType IndexType; - static const int N = GF::N, K = N - NR; + static const int N = GF::N, K = N - NR, NP = NR; IndexType generator[NR+1]; ReedSolomonEncoder() { diff --git a/tests/bch_decoder_test.cc b/tests/bch_decoder_test.cc index 4aa14be..204ada5 100644 --- a/tests/bch_decoder_test.cc +++ b/tests/bch_decoder_test.cc @@ -20,16 +20,16 @@ int main() typedef CODE::BoseChaudhuriHocquenghemDecoder<6, 1, 5, GF> BCH; GF instance; BCH decode; - uint8_t target[15] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 }; - uint8_t code[15]; - for (int i = 0; i < 15; ++i) + uint8_t target[BCH::N] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 }; + uint8_t code[BCH::N]; + for (int i = 0; i < BCH::N; ++i) code[i] = target[i]; - std::uniform_int_distribution distribution(0, 15); + std::uniform_int_distribution distribution(0, BCH::N-1); auto noise = std::bind(distribution, generator); for (int i = 0; i < 3; ++i) code[noise()] ^= 1; decode(reinterpret_cast(code)); - for (int i = 0; i < 15; ++i) + for (int i = 0; i < BCH::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -38,21 +38,21 @@ int main() typedef CODE::BoseChaudhuriHocquenghemDecoder<24, 1, 65343, GF> BCH; GF *instance = new GF(); BCH *decode = new BCH(); - uint16_t *target = new uint16_t[65535]; - for (int i = 0, s = 0; i < 65343; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff) + uint16_t *target = new uint16_t[BCH::N]; + for (int i = 0, s = 0; i < BCH::K; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff) target[i] = (s^=s>>7)&1; - uint16_t parity[192] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 }; - for (int i = 0; i < 192; ++i) - target[65343+i] = parity[i]; - uint16_t *code = new uint16_t[65535]; - for (int i = 0; i < 65535; ++i) + uint16_t parity[BCH::NP] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 }; + for (int i = 0; i < BCH::NP; ++i) + target[BCH::K+i] = parity[i]; + uint16_t *code = new uint16_t[BCH::N]; + for (int i = 0; i < BCH::N; ++i) code[i] = target[i]; - std::uniform_int_distribution distribution(0, 65535); + std::uniform_int_distribution distribution(0, BCH::N-1); auto noise = std::bind(distribution, generator); for (int i = 0; i < 12; ++i) code[noise()] ^= 1; (*decode)(reinterpret_cast(code)); - for (int i = 0; i < 65535; ++i) + for (int i = 0; i < BCH::N; ++i) assert(code[i] == target[i]); delete[] target; delete[] code; diff --git a/tests/bch_encoder_test.cc b/tests/bch_encoder_test.cc index 80a01e0..e3bd83c 100644 --- a/tests/bch_encoder_test.cc +++ b/tests/bch_encoder_test.cc @@ -17,10 +17,10 @@ int main() typedef CODE::BoseChaudhuriHocquenghemEncoder<6, 1, 5, GF> BCH; GF instance; BCH encode({0b10011, 0b11111, 0b00111}); - uint8_t code[15] = { 1, 1, 0, 0, 1 }; - uint8_t target[15] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 }; + uint8_t code[BCH::N] = { 1, 1, 0, 0, 1 }; + uint8_t target[BCH::N] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 }; encode(reinterpret_cast(code)); - for (int i = 0; i < 15; ++i) + for (int i = 0; i < BCH::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -29,15 +29,15 @@ int main() typedef CODE::BoseChaudhuriHocquenghemEncoder<24, 1, 65343, GF> BCH; GF *instance = new GF(); BCH *encode = new BCH({0b10000000000101101, 0b10000000101110011, 0b10000111110111101, 0b10101101001010101, 0b10001111100101111, 0b11111011110110101, 0b11010111101100101, 0b10111001101100111, 0b10000111010100001, 0b10111010110100111, 0b10011101000101101, 0b10001101011100011}); - uint16_t *code = new uint16_t[65535]; - uint16_t *target = new uint16_t[65535]; - for (int i = 0, s = 0; i < 65343; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff) + uint16_t *code = new uint16_t[BCH::N]; + uint16_t *target = new uint16_t[BCH::N]; + for (int i = 0, s = 0; i < BCH::K; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff) target[i] = code[i] = (s^=s>>7)&1; - uint16_t parity[192] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 }; - for (int i = 0; i < 192; ++i) - target[65343+i] = parity[i]; + uint16_t parity[BCH::NP] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 }; + for (int i = 0; i < BCH::NP; ++i) + target[BCH::K+i] = parity[i]; (*encode)(reinterpret_cast(code)); - for (int i = 0; i < 65535; ++i) + for (int i = 0; i < BCH::N; ++i) assert(code[i] == target[i]); delete[] target; delete[] code; diff --git a/tests/rs_decoder_test.cc b/tests/rs_decoder_test.cc index be1ad16..bd222ec 100644 --- a/tests/rs_decoder_test.cc +++ b/tests/rs_decoder_test.cc @@ -20,16 +20,16 @@ int main() typedef CODE::ReedSolomonDecoder<4, 0, GF> RS; GF instance; RS decode; - uint8_t target[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12 }; - uint8_t code[15]; - for (int i = 0; i < 15; ++i) + uint8_t target[RS::N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12 }; + uint8_t code[RS::N]; + for (int i = 0; i < RS::N; ++i) code[i] = target[i]; - std::uniform_int_distribution distribution(0, 15); + std::uniform_int_distribution distribution(0, RS::N-1); auto noise = std::bind(distribution, generator); for (int i = 0; i < 2; ++i) code[noise()] = noise(); decode(code); - for (int i = 0; i < 15; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -38,21 +38,21 @@ int main() typedef CODE::ReedSolomonDecoder<16, 0, GF> RS; GF instance; RS decode; - uint8_t target[255]; - for (int i = 0; i < 239; ++i) + uint8_t target[RS::N]; + for (int i = 0; i < RS::K; ++i) target[i] = i + 1; - uint8_t parity[16] = { 1, 126, 147, 48, 155, 224, 3, 157, 29, 226, 40, 114, 61, 30, 244, 75 }; - for (int i = 0; i < 16; ++i) - target[239+i] = parity[i]; - uint8_t code[255]; - for (int i = 0; i < 255; ++i) + uint8_t parity[RS::NP] = { 1, 126, 147, 48, 155, 224, 3, 157, 29, 226, 40, 114, 61, 30, 244, 75 }; + for (int i = 0; i < RS::NP; ++i) + target[RS::K+i] = parity[i]; + uint8_t code[RS::N]; + for (int i = 0; i < RS::N; ++i) code[i] = target[i]; - std::uniform_int_distribution distribution(0, 255); + std::uniform_int_distribution distribution(0, RS::N-1); auto noise = std::bind(distribution, generator); for (int i = 0; i < 8; ++i) code[noise()] = noise(); decode(code); - for (int i = 0; i < 255; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -61,21 +61,21 @@ int main() typedef CODE::ReedSolomonDecoder<64, 1, GF> RS; GF *instance = new GF(); RS *decode = new RS(); - uint16_t *target = new uint16_t[65535]; - for (int i = 0; i < 65471; ++i) + uint16_t *target = new uint16_t[RS::N]; + for (int i = 0; i < RS::K; ++i) target[i] = i + 1; - uint16_t parity[64] = { 25271, 26303, 22052, 31318, 31233, 6076, 40148, 29468, 47507, 32655, 12404, 13265, 23901, 38403, 50967, 50433, 40818, 226, 62296, 23636, 56393, 12952, 11476, 44416, 518, 50014, 10037, 57582, 33421, 42654, 54025, 7157, 4826, 52148, 17167, 23294, 6427, 40953, 11168, 35305, 18209, 1868, 39971, 54928, 27566, 1424, 4846, 25347, 34710, 42190, 56452, 21859, 49805, 28028, 41657, 25756, 22014, 24479, 28758, 17438, 12976, 61743, 46735, 1557 }; - for (int i = 0; i < 64; ++i) - target[65471+i] = parity[i]; - uint16_t *code = new uint16_t[65535]; - for (int i = 0; i < 65535; ++i) + uint16_t parity[RS::NP] = { 25271, 26303, 22052, 31318, 31233, 6076, 40148, 29468, 47507, 32655, 12404, 13265, 23901, 38403, 50967, 50433, 40818, 226, 62296, 23636, 56393, 12952, 11476, 44416, 518, 50014, 10037, 57582, 33421, 42654, 54025, 7157, 4826, 52148, 17167, 23294, 6427, 40953, 11168, 35305, 18209, 1868, 39971, 54928, 27566, 1424, 4846, 25347, 34710, 42190, 56452, 21859, 49805, 28028, 41657, 25756, 22014, 24479, 28758, 17438, 12976, 61743, 46735, 1557 }; + for (int i = 0; i < RS::NP; ++i) + target[RS::K+i] = parity[i]; + uint16_t *code = new uint16_t[RS::N]; + for (int i = 0; i < RS::N; ++i) code[i] = target[i]; - std::uniform_int_distribution distribution(0, 65535); + std::uniform_int_distribution distribution(0, RS::N-1); auto noise = std::bind(distribution, generator); for (int i = 0; i < 32; ++i) code[noise()] = noise(); (*decode)(code); - for (int i = 0; i < 65535; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); delete[] target; delete[] code; diff --git a/tests/rs_encoder_test.cc b/tests/rs_encoder_test.cc index f3194dd..661a78b 100644 --- a/tests/rs_encoder_test.cc +++ b/tests/rs_encoder_test.cc @@ -17,10 +17,10 @@ int main() typedef CODE::ReedSolomonEncoder<4, 0, GF> RS; GF instance; RS encode; - uint8_t code[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; - uint8_t target[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12 }; + uint8_t code[RS::N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + uint8_t target[RS::N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12 }; encode(code); - for (int i = 0; i < 15; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -29,14 +29,14 @@ int main() typedef CODE::ReedSolomonEncoder<16, 0, GF> RS; GF instance; RS encode; - uint8_t code[255], target[255]; - for (int i = 0; i < 239; ++i) + uint8_t code[RS::N], target[RS::N]; + for (int i = 0; i < RS::K; ++i) target[i] = code[i] = i + 1; - uint8_t parity[16] = { 1, 126, 147, 48, 155, 224, 3, 157, 29, 226, 40, 114, 61, 30, 244, 75 }; - for (int i = 0; i < 16; ++i) - target[239+i] = parity[i]; + uint8_t parity[RS::NP] = { 1, 126, 147, 48, 155, 224, 3, 157, 29, 226, 40, 114, 61, 30, 244, 75 }; + for (int i = 0; i < RS::NP; ++i) + target[RS::K+i] = parity[i]; encode(code); - for (int i = 0; i < 255; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); } if (1) { @@ -45,15 +45,15 @@ int main() typedef CODE::ReedSolomonEncoder<64, 1, GF> RS; GF *instance = new GF(); RS *encode = new RS(); - uint16_t *code = new uint16_t[65535]; - uint16_t *target = new uint16_t[65535]; - for (int i = 0; i < 65471; ++i) + uint16_t *code = new uint16_t[RS::N]; + uint16_t *target = new uint16_t[RS::N]; + for (int i = 0; i < RS::K; ++i) target[i] = code[i] = i + 1; - uint16_t parity[64] = { 25271, 26303, 22052, 31318, 31233, 6076, 40148, 29468, 47507, 32655, 12404, 13265, 23901, 38403, 50967, 50433, 40818, 226, 62296, 23636, 56393, 12952, 11476, 44416, 518, 50014, 10037, 57582, 33421, 42654, 54025, 7157, 4826, 52148, 17167, 23294, 6427, 40953, 11168, 35305, 18209, 1868, 39971, 54928, 27566, 1424, 4846, 25347, 34710, 42190, 56452, 21859, 49805, 28028, 41657, 25756, 22014, 24479, 28758, 17438, 12976, 61743, 46735, 1557 }; - for (int i = 0; i < 64; ++i) - target[65471+i] = parity[i]; + uint16_t parity[RS::NP] = { 25271, 26303, 22052, 31318, 31233, 6076, 40148, 29468, 47507, 32655, 12404, 13265, 23901, 38403, 50967, 50433, 40818, 226, 62296, 23636, 56393, 12952, 11476, 44416, 518, 50014, 10037, 57582, 33421, 42654, 54025, 7157, 4826, 52148, 17167, 23294, 6427, 40953, 11168, 35305, 18209, 1868, 39971, 54928, 27566, 1424, 4846, 25347, 34710, 42190, 56452, 21859, 49805, 28028, 41657, 25756, 22014, 24479, 28758, 17438, 12976, 61743, 46735, 1557 }; + for (int i = 0; i < RS::NP; ++i) + target[RS::K+i] = parity[i]; (*encode)(code); - for (int i = 0; i < 65535; ++i) + for (int i = 0; i < RS::N; ++i) assert(code[i] == target[i]); delete[] target; delete[] code;