Skip to content

optimize parity calculations in SerialPIO (#2932) #2933

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Conversation

ahmedarif193
Copy link
Contributor

use LUT for faster parity checks, reduces CPU load with multiple instances

Copy link
Owner

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but this is pretty expensive in terms of ROM vs. what it's (hopefully) accelerating. 256 bytes of bytewide 1s and 0s just feels wasteful. And since it's in ROM it could be a very slow access (XIP is nice, but nobody's going to call it fast...).

I'm not sure that parity on any bitrate that the SerialPIO operates at is a limiting factor, but if you really think it is then why not something simpler with much lower space requirements.

For example, (stolen from http://www.graphics.stanford.edu/%7Eseander/bithacks.html#ParityParallel) you can do parity in ~5 instructions without LUT by utilizing a constant as a parity bitmap:

static int __not_in_flash_func(_parity)(int data) {
    data ^= data >> 4;
    data &= 0xf;
    return (0x6996 >> data) & 1;
}

There shouldn't be a need to mask off bits before doing the math in the above or a LUT. That was there to short-circuit in the original for loop case.

@ahmedarif193
Copy link
Contributor Author

that bit hack is way more brilliant. I'm using this for running 8 rx channels on my rp2040 with real-time constraints alongside other critical tasks. cpu usage matters when you're maxing out a cortex-m0 cpu because that cpu has potential indeed

@ahmedarif193 ahmedarif193 force-pushed the serial-pio-parity-update-1 branch from ec1c8f2 to abbfc4d Compare May 1, 2025 20:38
@ahmedarif193 ahmedarif193 force-pushed the serial-pio-parity-update-1 branch from abbfc4d to 42cc372 Compare May 1, 2025 20:40
Copy link
Owner

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, LGTM!

@earlephilhower earlephilhower merged commit 59614a9 into earlephilhower:master May 1, 2025
26 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants