Skip to content

lib/aeabi_div: Add compact integer division helpers. - #502

Open
dlech wants to merge 2 commits into
pybricks:masterfrom
dlech:compact-division
Open

lib/aeabi_div: Add compact integer division helpers.#502
dlech wants to merge 2 commits into
pybricks:masterfrom
dlech:compact-division

Conversation

@dlech

@dlech dlech commented Sep 12, 2026

Copy link
Copy Markdown
Member

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.

@dlech
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

dlech commented Sep 12, 2026

Copy link
Copy Markdown
Member Author

@laurensvalk, any bright ideas on how to test this to make sure it doesn't affect motor control in a bad way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant