One of the common defenses of the abuse of “undefined behavior” by C compilers, and the stupid aliasing rules in the standard is that those things are necessary for optimizations, like using vector operations.  Here’s an example of where gcc ignores the standard and does what the C language semantics require while taking advantage of Intel SIMD instructions. The C code is an example of a common technique in network code.

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
struct message { int code; int value; float data[20]; int checkval; };

 int checksum(struct message *m){
    int i, *p = (int *)m;
    int lim = sizeof(struct message) / sizeof i;
    int c = 0;
    for (i=0 ; i < lim; i++) {   c ^= *(p+i); }
  return c;

}

int main (int argc, char **argv)
{

    struct message m;
    int v;
    fread((char *)&m, sizeof(char), sizeof(m), stdin);
    v = checksum(&m);
    printf("Checksum was %d\n",v);

}
The translation.
checksum:
movdquxmm0, XMMWORDPTR [rdi+32]
movdquxmm2, XMMWORDPTR [rdi+48]
movdquxmm1, XMMWORDPTR [rdi]
movdquxmm3, XMMWORDPTR [rdi+16]
pxorxmm0, xmm2
movdquxmm4, XMMWORDPTR [rdi+64]
moveax, DWORDPTR [rdi+80]
pxorxmm1, xmm3
xoreax, DWORDPTR [rdi+84]
xoreax, DWORDPTR [rdi+88]
pxorxmm0, xmm1
movedx, eax
pxorxmm0, xmm4
movdqaxmm1, xmm0
psrldqxmm1, 8
pxorxmm0, xmm1
movdqaxmm1, xmm0
psrldqxmm1, 4
pxorxmm0, xmm1
movdeax, xmm0
xoreax, edx
ret
See the godbolt page
Vector optimization with C aliasing
Tagged on: