lib/aeabi_div: Add compact integer division helpers. - #502
Open
dlech wants to merge 2 commits into
Open
Conversation
dlech
marked this pull request as ready for review
September 12, 2026 01:44
Cortex-M0 has no divide instruction, so every 32-bit integer division calls a libgcc helper. Since GCC 11, the v6-m libgcc only ships speed-optimized helpers: __udivsi3 is fully unrolled and __divsi3 contains its own second copy of the unsigned core, together around 730 bytes. Provide small loop-based replacements that produce identical results, including on division by zero, and enable them on the Move Hub and City Hub. This saves around 640 bytes on each, at the cost of roughly 3 to 5 times slower division, which stays well below 1% of the CPU for the heaviest user (motor control). The new file is compiled without LTO because the compiler generates references to these helpers during LTO code generation, after LTO symbol resolution, so definitions inside an LTO object fail to link.
The path filters in format.yml are supposed to match the paths in tools/codeformat.py, but lib/lego was missing, so a change touching only lib/lego headers did not trigger the format check.
dlech
force-pushed
the
compact-division
branch
from
September 12, 2026 01:51
56fb8eb to
3847bd7
Compare
Member
Author
|
@laurensvalk, any bright ideas on how to test this to make sure it doesn't affect motor control in a bad way? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cortex-M0 has no divide instruction, so every 32-bit integer division calls a libgcc helper. Since GCC 11, the v6-m libgcc only ships speed-optimized helpers: __udivsi3 is fully unrolled and __divsi3 contains its own second copy of the unsigned core, together around 730 bytes. Provide small loop-based replacements that produce identical results, including on division by zero, and enable them on the Move Hub and City Hub. This saves around 640 bytes on each, at the cost of roughly 3 to 5 times slower division, which stays well below 1% of the CPU for the heaviest user (motor control).
The new file is compiled without LTO because the compiler generates references to these helpers during LTO code generation, after LTO symbol resolution, so definitions inside an LTO object fail to link.