Skip to content

Improve reading ADC result #345

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

Merged
merged 1 commit into from
Nov 2, 2020
Merged

Improve reading ADC result #345

merged 1 commit into from
Nov 2, 2020

Conversation

VitSh7
Copy link
Contributor

@VitSh7 VitSh7 commented May 28, 2020

7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from ADCL and ADCH. It produces additionally three eor commands or in some cases two mov commands in the assembly code (see discussion #344). These commands swap register contents before store them to data area. So they are completely unnecessary. Reading ADC result with ADC macro fixes it and gcc generates the right code.

Copy link
Collaborator

@matthijskooijman matthijskooijman left a comment

Choose a reason for hiding this comment

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

Changes look good to me. As analyzed in #344, the original problem is a gcc missed optimization, but since we can't easily fix that, making this change here to fix at least this particular instance of this problem seems reasonable to me. The new code is probably even easier to read than the old code, so that's a small bonus.

One thing I'd like to see improved is the commit message. It now says "Improve", but that suggests that the reading itself is maybe somehow better or more accurate. Also, the detailed commit message has no mention of the improved generated assembly, or gcc failing to optimize this in some cases, which I think would be valuable info for anyone that comes back to this commit in the future trying to figure out why this changed :-)

I verified that this indeed improves the generated assembly using some of the examples from the linked issue.

@VitSh7
Copy link
Contributor Author

VitSh7 commented Nov 1, 2020

Okay, I edited the commit message to give more details.

@matthijskooijman
Copy link
Collaborator

Message looks good, but you now only edited the PR description, not the commit message. Can you also edit the commit message (if you're unsure how, you can do this using git commit --amend and then pushing using --force (or safer, --force-with-lease).

7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from ADCL and ADCH. It produces additionally three eor commands or in some cases two mov commands in the assembly code (see discussion arduino#344). These commands swap register contents before store them to data area. So they are completely unnecessary. Reading ADC result with ADC macro fixes it and gcc generates the right code..
@VitSh7
Copy link
Contributor Author

VitSh7 commented Nov 1, 2020

Oh yeah, sorry, my bad, did this with the same message as well.

@facchinm facchinm merged commit 87236bc into arduino:master Nov 2, 2020
@matthijskooijman matthijskooijman linked an issue Nov 2, 2020 that may be closed by this pull request
unrevre added a commit to unrevre/ArduinoTaikoController that referenced this pull request Nov 7, 2020
from: arduino/ArduinoCore-avr#345

7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from
ADCL and ADCH. It produces additionally three eor commands or in some
cases two mov commands in the assembly code. These commands swap
register contents before store them to data area. So they are
completely unnecessary. Reading ADC result with ADC macro fixes it and
gcc generates the right code..
unrevre added a commit to unrevre/ArduinoTaikoController that referenced this pull request Nov 17, 2020
from: arduino/ArduinoCore-avr#345

7.3.0-atmel3.6.1-arduino7 gcc fails to optimize separate reading from
ADCL and ADCH. It produces additionally three eor commands or in some
cases two mov commands in the assembly code. These commands swap
register contents before store them to data area. So they are
completely unnecessary. Reading ADC result with ADC macro fixes it and
gcc generates the right code..
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.

analogRead - reading ADC result faster
3 participants