VK1ET : Changes to fixBitErrors()

1) Reduce complexity of fixBitErrors()
2) Inline flipBits()
3) Remove validation checks that can never occur.
This commit is contained in:
Malcolm Robb 2013-05-21 13:40:07 +01:00
parent 0a0ba165db
commit 542b94dedb

View file

@ -1328,10 +1328,10 @@ void modesInitErrorInfo() {
} }
*/ */
} }
//
/* Flip a bit, but make sure that the DF field (first 5 bits) // Flip a bit, but make sure that the DF field (first 5 bits)
* is never changed // is never changed
*/ /*
int flipBit(unsigned char *msg, int nbits, int bit) { int flipBit(unsigned char *msg, int nbits, int bit) {
int bytepos, mask; int bytepos, mask;
if ((bit < 0) || (bit >= nbits)) { if ((bit < 0) || (bit >= nbits)) {
@ -1345,50 +1345,41 @@ int flipBit(unsigned char *msg, int nbits, int bit) {
msg[bytepos] ^= mask; msg[bytepos] ^= mask;
return 1; return 1;
} }
*/
/* Search syndrome in table and, if an entry is found, flip the necessary // Search syndrome in table and, if an entry is found, flip the necessary
* bits. Make sure the indices fit into the array, and for 2-bit errors, // bits. Make sure the indices fit into the array, and for 2-bit errors,
* are different. // are different.
* Return number of fixed bits. // Return number of fixed bits.
*/ //
int fixBitErrors(unsigned char *msg, int bits) { int fixBitErrors(unsigned char *msg, int bits) {
struct errorinfo *pei; struct errorinfo *pei;
struct errorinfo ei; struct errorinfo ei;
int bitpos0, bitpos1, offset, res; int bitpos0, bitpos1, offset, res;
ei.syndrome = modesChecksum(msg, bits); ei.syndrome = modesChecksum(msg, bits);
ei.pos0 = -1; ei.pos0 = -1;
ei.pos1 = -1; ei.pos1 = -1;
pei = bsearch(&ei, bitErrorTable, NERRORINFO, pei = bsearch(&ei, bitErrorTable, NERRORINFO,
sizeof(struct errorinfo), cmpErrorInfo); sizeof(struct errorinfo), cmpErrorInfo);
if (pei == NULL) { if (pei == NULL) {
/* Nothing found */ return 0; // No syndrome found
return 0; }
} res = 0;
offset = MODES_LONG_MSG_BITS-bits; offset = MODES_LONG_MSG_BITS-bits;
bitpos0 = pei->pos0; bitpos0 = pei->pos0 - offset;
bitpos1 = pei->pos1; if ((bitpos0 < 0) || (bitpos0 >= bits)) {
res = 0; return 0;
if (bitpos1 >= 0) { /* two-bit error pattern */ }
bitpos0 -= offset; msg[(bitpos0 >> 3)] ^= (1 << (7 - (bitpos0 & 7)));
bitpos1 -= offset; res++;
if ((bitpos0 < 0) || (bitpos0 >= bits) || if (pei->pos1 >= 0) { /* two-bit error pattern */
(bitpos1 < 0) || (bitpos1 >= bits)) { bitpos1 = pei->pos1 - offset;
return res; if ((bitpos1 < 0) || (bitpos1 >= bits)) {
} return 0;
res +=flipBit(msg, bits, bitpos0);
if (bitpos0 != bitpos1) {
res += flipBit(msg, bits, bitpos1);
return res;
}
return res;
} else {
bitpos0 -= offset;
if ((bitpos0 < 0) || (bitpos0 >= bits)) {
return res;
}
res += flipBit(msg, bits, bitpos0);
return res;
} }
msg[(bitpos1 >> 3)] ^= (1 << (7 - (bitpos1 & 7)));
res++;
}
return res;
} }
/* Code for testing the timing: run all possible 1- and 2-bit error /* Code for testing the timing: run all possible 1- and 2-bit error