x86/64 alignment, optimization, -Wcast-align

Jeffrey Walton noloader@gmail.com
Tue Oct 25 22:10:00 GMT 2016


On Tue, Oct 25, 2016 at 5:58 PM, Balázs Oroszi <orobalage@gmail.com> wrote:
> In case anyone was wondering, I did the following workaround:
>
> typedef uint32_t uint32_u_t __attribute__((aligned(1))) ;
>
> Effectively this creates a new type uint32_u_t, which has a lowered
> alignment, so GCC won't go vectorizing with aligned vmovdqa and the
> like, crashing if original alignment (in this case 4 bytes) was not
> met.
> I use this type whenever it is uncertain if an array is aligned or
> not, and works perfectly.

Usually, you go the other way: you either (1) align to 16 for stack
based allocations, or (2) allocate on the heap, which has 16-byte
alignment. It allows you the benefit of vectorization and AVX.

You also have to avoid the unaligned data accesses and type punning.
The undefined behavior sanitizers are usually very good about finding
them at runtime. Just compile with -fsanitize=undefined, and then run
your test suite.

Jeff



More information about the Gcc-help mailing list