I'm using the GCC compiler to load packed 16-bit data (i.e. two 16-bit words in a 32-bit value). My pointer is to a 32-bit type because I want to load two 16-bit values in a single cycle but the pointer is not necessarily aligned to a 4 byte boundary. GNU GCC generates LDRD to load two 32-bit values. This results in an alignment fault a run-time. Is there a compile option to prevent the compiler from using LDRD ? or an option to control alignment assumptions? Otherwise what's the best way to get the compiler to load packed data efficiently.
__attrubute__ ((packed)) is the way you control alignment assumptions.
Make sure that "My pointer" is a pointer to a packed 32-bit type.
Thanks for the response Robert. I don't think that works for GCC, or at least not the GCC version that's part of STMCubeIDE currently, The compiler appears not to recognize 'packed'. I can see from CMSIS that other compilers, like ARM CC, support this. Should this work for GCC? I found an inelegant way around this using inline ASM LDR instructions to do my loading.
I use that in GCC.
struct __attribute__((packed)) unaligned_struct { uint64_t Value; } uint8_t byte_buffer[100] uint64_t data;; data = *((unligned_struct *) &byte_buffer[55]);
Hi Robert, Yes a modified version of the above works, thanks. I note that for GCC it appears that the packed attribute is only accepted when dealing with structures. I thought normally this refers to padding between the elements rather than packed in the SIMD sense.