CRC4: use proper CRC4 table to avoid bit-reversal of each byte
In commit 9bd2c9ffe7cf82c5d0a12406db018717d9b78858 we fixed the CRC4
computation by bit-reversing every byte before using it in the CRC
table. This is of course a waste of CPU cycles. Let's just compute
the CRC4 table slightly different (thanks to Dietter):
The following commands using pycrc from pycrc.org were used:
./pycrc.py --width=4 --poly=0x3 --reflect-in=false --reflect-out=false --xor-out=0 --xor-in=0 --algorithm table-driven --generate c -o crc4itu.c
./pycrc.py --width=4 --poly=0x3 --reflect-in=false --reflect-out=false --xor-out=0 --xor-in=0 --algorithm table-driven --generate h -o crc4itu.h
diff --git a/src/osmo_e1.c b/src/osmo_e1.c
index a328eb1..2b83622 100644
--- a/src/osmo_e1.c
+++ b/src/osmo_e1.c
@@ -109,7 +109,7 @@
e1i->tx.crc4_error = false;
e1i->tx.frame_nr = 0;
e1i->tx.crc4_last_smf = 0;
- e1i->tx.crc4 = 0;
+ e1i->tx.crc4 = crc4itu_init();
e1i->rx.frame_nr = 0;
memset(&e1i->rx.ts0_history, 0, sizeof(e1i->rx.ts0_history));
@@ -279,9 +279,9 @@
/* mask off the C bits */
if (is_correct_fas(ts0))
ts0 &= 0x7F;
- e1i->tx.crc4 = crc4itu(e1i->tx.crc4, &ts0, 1);
+ e1i->tx.crc4 = crc4itu_update(e1i->tx.crc4, &ts0, 1);
/* add the remaining bytes/bits */
- e1i->tx.crc4 = crc4itu(e1i->tx.crc4, out_frame+1, ARRAY_SIZE(e1i->ts)-1);
+ e1i->tx.crc4 = crc4itu_update(e1i->tx.crc4, out_frame+1, ARRAY_SIZE(e1i->ts)-1);
}
/*! Pull one to-be-transmitted E1 frame (256bits) from the E1 instance
@@ -395,10 +395,10 @@
if (smf2)
offset = 8;
- crc |= (e1i->rx.ts0_history[0+offset] >> 7) << 0;
- crc |= (e1i->rx.ts0_history[2+offset] >> 7) << 1;
- crc |= (e1i->rx.ts0_history[4+offset] >> 7) << 2;
- crc |= (e1i->rx.ts0_history[6+offset] >> 7) << 3;
+ crc |= (e1i->rx.ts0_history[0+offset] >> 7) << 3;
+ crc |= (e1i->rx.ts0_history[2+offset] >> 7) << 2;
+ crc |= (e1i->rx.ts0_history[4+offset] >> 7) << 1;
+ crc |= (e1i->rx.ts0_history[6+offset] >> 7) << 0;
return crc;
}
@@ -412,9 +412,9 @@
/* mask off the C bits */
if (is_correct_fas(ts0))
ts0 &= 0x7F;
- e1i->rx.crc4 = crc4itu(e1i->rx.crc4, &ts0, 1);
+ e1i->rx.crc4 = crc4itu_update(e1i->rx.crc4, &ts0, 1);
/* add the remaining bytes/bits */
- e1i->rx.crc4 = crc4itu(e1i->rx.crc4, rx_frame+1, ARRAY_SIZE(e1i->ts)-1);
+ e1i->rx.crc4 = crc4itu_update(e1i->rx.crc4, rx_frame+1, ARRAY_SIZE(e1i->ts)-1);
}
/* FSM State handler */
@@ -499,7 +499,7 @@
}
/* rotate computed CRC4 one further */
e1i->rx.crc4_last_smf = e1i->rx.crc4;
- e1i->rx.crc4 = 0;
+ e1i->rx.crc4 = crc4itu_init();
break;
default:
break;