Skip to content

Remove runtime division from HWFIFO wrap pointer advance paths (Cortex-M0 safe)#3542

Closed
Copilot wants to merge 5 commits intomasterfrom
copilot/remove-division-aeabi-idiv
Closed

Remove runtime division from HWFIFO wrap pointer advance paths (Cortex-M0 safe)#3542
Copilot wants to merge 5 commits intomasterfrom
copilot/remove-division-aeabi-idiv

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 9, 2026

PR #3538 corrected PMA pointer advancement math in wrapped HWFIFO paths, but did so with division in hot paths (lin_even / data_stride), which can emit __aeabi_idiv/__aeabi_uidiv on Cortex-M0. This change keeps the same pointer advancement behavior while removing runtime division.

  • What changed

    • Added a local helper in src/common/tusb_fifo.c:
      • lin_even_to_addr_advance(uint16_t lin_even, uint8_t data_stride)
    • Replaced division-based advancement in both wrapped paths:
      • hwff_push_n()
      • hwff_pull_n()
  • How the conversion now works

    • Converts lin_even bytes to transfer units using shift-based stride handling:
      • stride 4 → >> 2
      • stride 2 → >> 1
      • stride 1 → unchanged
    • Multiplies units by CFG_TUSB_FIFO_HWFIFO_ADDR_STRIDE to compute register address advance.
    • Guards expected stride values (1/2/4) via assertion in helper.
  • Net effect

// Before
HWFIFO_ADDR_NEXT_N(hwfifo, const, (lin_even / data_stride) * CFG_TUSB_FIFO_HWFIFO_ADDR_STRIDE);

// After
HWFIFO_ADDR_NEXT_N(hwfifo, const, lin_even_to_addr_advance(lin_even, data_stride));

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits March 9, 2026 10:21
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove division aeabi_idiv in related functions Remove runtime division from HWFIFO wrap pointer advance paths (Cortex-M0 safe) Mar 9, 2026
@HiFiPhile HiFiPhile marked this pull request as ready for review March 9, 2026 10:45
Copilot AI review requested due to automatic review settings March 9, 2026 10:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the HWFIFO wrap-around pointer advancement logic in tusb_fifo.c to avoid runtime division in hot paths (important for Cortex-M0 where division may pull in __aeabi_*div), while aiming to preserve the corrected behavior introduced in PR #3538.

Changes:

  • Added a small inline helper to convert lin_even byte counts into HWFIFO address-advance values using shift-based stride handling.
  • Updated wrapped-path pointer advancement in hwff_push_n() and hwff_pull_n() to use the helper instead of division.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/common/tusb_fifo.c Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

TinyUSB Average Code Size Metrics

File .text .rodata .data .bss size %
cdc_host.c 6610 487 15 1539 8371 5.1%
ehci.c 2763 0 0 6043 7597 4.7%
ncm_device.c 1514 28 1408 5830 7358 4.5%
usbh.c 4653 55 99 960 5734 3.5%
hcd_dwc2.c 4994 33 1 513 5540 3.4%
midi_host.c 1353 7 7 3740 5097 3.1%
video_device.c 4394 5 1852 472 4859 3.0%
audio_device.c 2850 0 1248 1673 4518 2.8%
dcd_dwc2.c 4176 25 0 265 4465 2.7%
ohci.c 1942 0 0 2414 4356 2.7%
dcd_ch32_usbfs.c 1473 0 0 2444 3917 2.4%
hcd_stm32_fsdev.c 3287 0 1 420 3708 2.3%
usbd.c 3215 57 89 275 3555 2.2%
ecm_rndis_device.c 1037 0 1 2272 3310 2.0%
dcd_khci.c 1953 0 0 1290 3243 2.0%
hcd_musb.c 3073 0 0 157 3230 2.0%
dcd_ci_fs.c 1925 0 0 1290 3215 2.0%
dcd_da146xx.c 3067 0 0 144 3211 2.0%
dcd_nrf5x.c 2918 0 0 292 3210 2.0%
hcd_rusb2.c 2923 0 0 245 3168 1.9%
dcd_rusb2.c 2919 0 0 156 3075 1.9%
msc_device.c 2518 108 2286 538 3056 1.9%
hcd_ch32_usbfs.c 2484 0 0 498 2982 1.8%
hcd_khci.c 2442 0 0 449 2891 1.8%
dcd_stm32_fsdev.c 2557 0 0 291 2848 1.7%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 1.7%
dcd_musb.c 2445 0 0 160 2605 1.6%
hcd_samd.c 2220 0 0 324 2544 1.6%
dcd_ci_hs.c 1759 0 0 1344 2538 1.6%
dcd_eptri.c 2271 0 0 259 2530 1.6%
usbtmc_device.c 2176 24 69 291 2500 1.5%
hid_host.c 1206 0 0 1250 2456 1.5%
mtp_device.c 1689 22 1449 579 2275 1.4%
dcd_rp2040.c 858 20 604 655 2137 1.3%
dcd_ch32_usbhs.c 1649 0 0 448 2097 1.3%
msc_host.c 1589 0 0 394 1984 1.2%
dcd_msp430x5xx.c 1798 0 0 176 1974 1.2%
cdc_device.c 1306 16 19 642 1947 1.2%
hcd_rp2040.c 976 73 416 384 1849 1.1%
dcd_lpc17_40.c 1474 0 0 648 1798 1.1%
midi_device.c 1129 0 995 589 1716 1.1%
dcd_nuc505.c 0 0 1531 157 1688 1.0%
dcd_lpc_ip3511.c 1463 0 0 264 1639 1.0%
hub.c 1384 8 8 30 1418 0.9%
dcd_samg.c 1320 0 0 72 1392 0.9%
printer_device.c 813 0 694 530 1341 0.8%
dcd_samd.c 1034 0 0 266 1300 0.8%
dcd_nuc121.c 1168 0 0 101 1269 0.8%
hid_device.c 1118 44 997 115 1233 0.8%
dcd_nuc120.c 1094 0 0 78 1172 0.7%
vendor_device.c 624 0 530 464 1087 0.7%
rp2040_usb.c 172 75 717 4 968 0.6%
dfu_device.c 744 28 712 183 926 0.6%
tusb_fifo.c 864 0 480 0 858 0.5%
typec_stm32.c 820 8 2 12 842 0.5%
dwc2_common.c 602 30 0 0 618 0.4%
usbc.c 420 2 20 166 608 0.4%
usbd_control.c 523 0 474 78 600 0.4%
hcd_pio_usb.c 262 0 240 0 502 0.3%
tusb.c 451 0 384 3 453 0.3%
hcd_ci_hs.c 184 0 0 0 184 0.1%
fsdev_common.c 180 0 0 0 180 0.1%
rusb2_common.c 160 0 16 0 176 0.1%
dfu_rt_device.c 156 0 134 0 156 0.1%
TOTAL 114619 1155 17498 46166 162842 100.0%
Input files
  • cmake-build/cmake-build-adafruit_clue/metrics.json
  • cmake-build/cmake-build-apard32690/metrics.json
  • cmake-build/cmake-build-at32f403a_weact_blackpill/metrics.json
  • cmake-build/cmake-build-at_start_f402/metrics.json
  • cmake-build/cmake-build-at_start_f413/metrics.json
  • cmake-build/cmake-build-at_start_f415/metrics.json
  • cmake-build/cmake-build-at_start_f423/metrics.json
  • cmake-build/cmake-build-at_start_f425/metrics.json
  • cmake-build/cmake-build-at_start_f435/metrics.json
  • cmake-build/cmake-build-at_start_f455/metrics.json
  • cmake-build/cmake-build-b_g474e_dpow1/metrics.json
  • cmake-build/cmake-build-b_u585i_iot2a/metrics.json
  • cmake-build/cmake-build-ch32v103r_r1_1v0/metrics.json
  • cmake-build/cmake-build-ch32v203c_r0_1v0/metrics.json
  • cmake-build/cmake-build-ch32v307v_r1_1v0/metrics.json
  • cmake-build/cmake-build-cynthion_d11/metrics.json
  • cmake-build/cmake-build-da14695_dk_usb/metrics.json
  • cmake-build/cmake-build-double_m33_express/metrics.json
  • cmake-build/cmake-build-ea4088_quickstart/metrics.json
  • cmake-build/cmake-build-ea4357/metrics.json
  • cmake-build/cmake-build-ek_tm4c123gxl/metrics.json
  • cmake-build/cmake-build-feather_stm32f405/metrics.json
  • cmake-build/cmake-build-fomu/metrics.json
  • cmake-build/cmake-build-frdm_k32l2a4s/metrics.json
  • cmake-build/cmake-build-frdm_k64f/metrics.json
  • cmake-build/cmake-build-frdm_kl25z/metrics.json
  • cmake-build/cmake-build-frdm_mcxa153/metrics.json
  • cmake-build/cmake-build-frdm_rw612/metrics.json
  • cmake-build/cmake-build-hpm6750evk2/metrics.json
  • cmake-build/cmake-build-lpcxpresso11u37/metrics.json
  • cmake-build/cmake-build-lpcxpresso1347/metrics.json
  • cmake-build/cmake-build-lpcxpresso1549/metrics.json
  • cmake-build/cmake-build-lpcxpresso1769/metrics.json
  • cmake-build/cmake-build-lpcxpresso18s37/metrics.json
  • cmake-build/cmake-build-lpcxpresso51u68/metrics.json
  • cmake-build/cmake-build-lpcxpresso54114/metrics.json
  • cmake-build/cmake-build-metro_m0_express/metrics.json
  • cmake-build/cmake-build-metro_m4_express/metrics.json
  • cmake-build/cmake-build-metro_m7_1011/metrics.json
  • cmake-build/cmake-build-mm32f327x_mb39/metrics.json
  • cmake-build/cmake-build-msp_exp430f5529lp/metrics.json
  • cmake-build/cmake-build-msp_exp432e401y/metrics.json
  • cmake-build/cmake-build-nutiny_nuc126v/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc120/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc121/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc505/metrics.json
  • cmake-build/cmake-build-portenta_c33/metrics.json
  • cmake-build/cmake-build-raspberry_pi_pico/metrics.json
  • cmake-build/cmake-build-raspberrypi_cm4/metrics.json
  • cmake-build/cmake-build-raspberrypi_zero/metrics.json
  • cmake-build/cmake-build-samg55_xplained/metrics.json
  • cmake-build/cmake-build-sipeed_longan_nano/metrics.json
  • cmake-build/cmake-build-stlinkv3mini/metrics.json
  • cmake-build/cmake-build-stm32c071nucleo/metrics.json
  • cmake-build/cmake-build-stm32f070rbnucleo/metrics.json
  • cmake-build/cmake-build-stm32f103_bluepill/metrics.json
  • cmake-build/cmake-build-stm32f207nucleo/metrics.json
  • cmake-build/cmake-build-stm32f303disco/metrics.json
  • cmake-build/cmake-build-stm32g0b1nucleo/metrics.json
  • cmake-build/cmake-build-stm32h503nucleo/metrics.json
  • cmake-build/cmake-build-stm32h743eval/metrics.json
  • cmake-build/cmake-build-stm32h7s3nucleo/metrics.json
  • cmake-build/cmake-build-stm32l052dap52/metrics.json
  • cmake-build/cmake-build-stm32l412nucleo/metrics.json
  • cmake-build/cmake-build-stm32n6570dk/metrics.json
  • cmake-build/cmake-build-stm32u083cdk/metrics.json
  • cmake-build/cmake-build-stm32wb55nucleo/metrics.json
  • cmake-build/cmake-build-stm32wba_nucleo/metrics.json
  • cmake-build/cmake-build-xmc4500_relax/metrics.json

@hathach
Copy link
Copy Markdown
Owner

hathach commented Mar 10, 2026

replaced by #3545

@hathach hathach closed this Mar 10, 2026
@HiFiPhile HiFiPhile deleted the copilot/remove-division-aeabi-idiv branch March 16, 2026 07:43
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.

4 participants