Optimize pressedKeyswitchCount()

Instead of manually counting set bits, use gcc's `__builtin_popcount()` for a -
supposedly - more efficient count.

Also initialize the counter to zero, while there.

Fixes #11.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/427/head^2
Gergely Nagy 7 years ago
parent e0498c52b3
commit 56fdd2c555

@ -235,15 +235,10 @@ bool ErgoDox::isKeyswitchPressed(uint8_t keyIndex) {
} }
uint8_t ErgoDox::pressedKeyswitchCount() { uint8_t ErgoDox::pressedKeyswitchCount() {
uint8_t count; uint8_t count = 0;
for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t r = 0; r < ROWS; r++) {
if (!keyState_[r]) count += __builtin_popcount(keyState_[r]);
continue;
for (uint8_t c = 0; c < COLS; c++) {
count += bitRead(keyState_[r], c);
}
} }
return count; return count;
} }

Loading…
Cancel
Save