-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Unaligned access support for pgm_read_word/dword #5692
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
Conversation
The core proper doesn't use pgm_read_word or pgm_read_dword except for one spot in WiFiClientSecureBearSSL (which is not time critical and is simply copying a ~20 word array to RAM containing the supported encryption signatures).
So, given that, I think there's no need for change in the Arduino core proper. BearSSL does use pgm_read_wrd, though, but it has its own private copy of pgmspace.h (since it builds outside the SDK with its own Makefile). So no change is needed there, either, as long as the old, only-aligned private copy is used there. Newlib has no use for pgm_read_word/_dword, either, and only need the updated header for future builds (actually already in the repo):
Given all that, I'd say the code is pretty low risk to the base repo. Other people's libraries, though, would need to be examined... |
Link to #5733, this should include fix for that one plus some form of testing of the obscure macros like that one... |
c42fc57
to
2508e21
Compare
Adding -DPGM_READ_UNALIGNED=0 or #define PGM_READ_UNALIGNED 0 will change the default at compile-time to only aligned (faster, but less compatible) macro implementations. Default is still to allow unaligned accesses.
Hi @earlephilhower for what I can see here Bodmer/TFT_eSPI#348 TFT_eSPI library is affected by this change |
Hmm... My MmlMusic library reads tones as float values from PROGMEM and is also affected. |
@maxint-rd please open a new issue with MCVE to get that looked into. |
ESP8266 cores 2.5.1 and 2.5.2 have an alignment issue causing incorrect reading of floats in PROGMEM See esp8266/Arduino#5628 and esp8266/Arduino#5692 By default the 2.5.1 and 2.5.2 cores use pgm_read_float_unaligned() but then the PROGMEM floats are not properly read. The read frequencies were way to high resulting in crackling noise or high pitched sound. Added a conditional redefine for ESP8266 environment which fixes properly reading the float values.
Okay, I just tested the behavior in an MCVE and opened a new issue (#6590). |
Two versions of the multibyte
pgm_read_*
macros are now available:pgm_read_*_aligned()
andpgm_read_*_unaligned()
. The_aligned()
versions are the original ones that require machine-dependent alignment of 2- and 4-byte values and compile into 1-2 instructions, while the_unaligned()
versions have no such restriction (but compile to 15x the instructions and take ~4x to ~15x the cycles)).The standard
pgm_read_(dword/word)
macros are defined to theunaligned()
versions to provide Arduino compatibility out of the box.It looks to be a minimal impact on core code because we don't have a lot of 16- or 32-bit ints in PROGMEM anyway, but a final scrub of core to hardcode
_aligned()
would probably be in order.Fixes #5628