Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Core] Simplify audio_duration_to_ms() and audio_ms_to_duration(), reduce firmware size by a few bytes. #21427

Merged
merged 6 commits into from
Jul 7, 2023

Conversation

getreuer
Copy link
Contributor

@getreuer getreuer commented Jul 2, 2023

This commit simplifies audio_duration_to_ms() and audio_ms_to_duration() and reduces firmware size by a few bytes.

Description

This commit simplifies the audio duration unit conversion functions audio_duration_to_ms() and audio_ms_to_duration() to reduce firmware size. While the existing code has separate implementations for AVR vs. ARM, this revision is a single shared integer implementation for both.

This revision has the same accuracy and supported numerical range as before. This is verified by new unit tests added with this commit. I've also added comments with a brief math analysis to clarify when these conversions are sure to work without overflow.

This revision costs a few bytes less in firmware size.

Checked firmware size with qmk compile before vs. after:

keyboard before after delta
ARV rhino 23374 23370 4 bytes smaller
Moonlander (ARM) 69106 69098 8 bytes smaller

Checked number of instructions with godbolt (AVR results compiled with AVR gcc 13.1.0 -Os, ARM results with ARM 32-bit gcc 13.1 -Os):

function before after delta
AVR audio_duration_to_ms 24 19 5 instructions fewer
AVR audio_ms_to_duration 24 21 3 instructions fewer
ARM audio_duration_to_ms 16 9 7 instructions fewer
ARM audio_ms_to_duration 17 8 9 instructions fewer

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout/userspace (addition or update)
  • Documentation

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

getreuer added 4 commits July 1, 2023 23:07
This commit simplifies `audio_duration_to_ms()` and
`audio_ms_to_duration()` to reduce firmware size. The revised
implementation is a shared integer implementation for both ARM and AVR.
It has the same accuracy and supported numerical range as before,
verified by new unit tests added with this commit. It costs a few bytes
less in firware size:

Checked with `qmk compile` before vs. after:

keyboard              before   after   delta
ARV rhino             23374    23370   4 bytes smaller
Moonlander (ARM)      69106    69098   8 bytes smaller

Checked with godbolt.org Compiler Explorer (AVR results compiled with
AVR gcc 13.1.0 -Os, ARM results with ARM 32-bit gcc 13.1 -Os):

function                   before   after   delta
AVR audio_duration_to_ms   24       19      5 instructions fewer
AVR audio_ms_to_duration   24       21      3 instructions fewer
ARM audio_duration_to_ms   16        9      7 instructions fewer
ARM audio_ms_to_duration   17        8      9 instructions fewer
@github-actions github-actions bot added the core label Jul 2, 2023
Copy link
Member

@drashna drashna left a comment

Choose a reason for hiding this comment

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

No noticeable difference in sound quality from what I can tell.

@drashna drashna requested a review from a team July 3, 2023 06:48
@tzarc tzarc merged commit a8a87a0 into qmk:develop Jul 7, 2023
@getreuer getreuer deleted the core/audio_bpm_opt branch July 7, 2023 15:34
jesperhellberg pushed a commit to jesperhellberg/qmk_firmware that referenced this pull request Sep 9, 2023
thismarvin pushed a commit to thismarvin/qmk_firmware that referenced this pull request Sep 27, 2023
akeep pushed a commit to akeep/qmk_firmware that referenced this pull request Oct 2, 2023
csolje pushed a commit to csolje/qmk_firmware that referenced this pull request Oct 21, 2023
autoferrit pushed a commit to SpaceRockMedia/bastardkb-qmk that referenced this pull request Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants