Re: Framebuffer: Draw a PSF character
Posted: Sat Mar 13, 2021 1:25 pm
You can use unaligned access on x86 (obviously; it's clear that this works at the assembly level) but you have to tell the compiler that you're doing that (e.g., via __attribute__(packed), or you can access the ptr via memcpy; GCC will translate the memcpy call to a usual load/store and this is well-defined according to the standard). GCC is certainly allowed to assume that accesses are aligned, for example when auto vectorizing code (as alignment is demanded by the C standard and __alignof__(int) == 4).
For an example where GCC indeed assumes aligned access on x86 and "miscompiles" code that uses non-aligned pointers, see this stack overflow answer: https://stackoverflow.com/a/47512025 (note that the question is misleading in this context -- the issue is unrelated to mmap):
EDIT: a feature request ("When the underlying ISA does not force pointer alignment, option to make GCC not assume it") to make unaligned access defined behavior via a GCC command line flag was closed as WONTFIX last year.
For an example where GCC indeed assumes aligned access on x86 and "miscompiles" code that uses non-aligned pointers, see this stack overflow answer: https://stackoverflow.com/a/47512025 (note that the question is misleading in this context -- the issue is unrelated to mmap):
Additionally, here is a minimal Godbolt example where GCC assumes that pointers are aligned: https://godbolt.org/z/ezx4oo. GCC optimizes this function to always return 1 (see the assembly code) but if pointers are unaligned, this function can return other results, such as 257.gcc4.8 makes a loop prologue that tries to reach an alignment boundary, but it assumes that uint16_t *p is 2-byte aligned, i.e. that some number of scalar iterations will make the pointer 16-byte aligned.
EDIT: a feature request ("When the underlying ISA does not force pointer alignment, option to make GCC not assume it") to make unaligned access defined behavior via a GCC command line flag was closed as WONTFIX last year.