Skip to content

Commit ec1c8f2

Browse files
author
Ahmed ARIF
committed
optimize parity calculations in SerialPIO (#2932)
use LUT for faster parity checks, reduces CPU load with multiple instances
1 parent 227d71e commit ec1c8f2

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

cores/rp2040/SerialPIO.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,32 @@ static PIOProgram *_getRxProgram(int bits) {
6868
}
6969
// ------------------------------------------------------------------------
7070

71-
// TODO - this works, but there must be a faster/better way...
7271
static int __not_in_flash_func(_parity)(int bits, int data) {
73-
int p = 0;
74-
for (int b = 0; b < bits; b++) {
75-
p ^= (data & (1 << b)) ? 1 : 0;
72+
static const uint8_t parity_lut[256] = {
73+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
74+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
75+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
76+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
77+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
78+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
79+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
80+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
81+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
82+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
83+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
84+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
85+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
86+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
87+
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
88+
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
89+
};
90+
91+
switch (bits) {
92+
case 5: return parity_lut[data & 0x1F];
93+
case 6: return parity_lut[data & 0x3F];
94+
case 7: return parity_lut[data & 0x7F];
95+
default: return parity_lut[data & 0xFF];
7696
}
77-
return p;
7897
}
7998

8099
// We need to cache generated SerialPIOs so we can add data to them from

0 commit comments

Comments
 (0)