x86_64 peculiarities

Integer overflows

When multiplying integers, be careful:

// Allocate 8GiB
void *buf = malloc(8 * 1024 * 1024 * 1024);

// Access the memory
char *buf_bptr = (char *)buf;
buf_bptr[1 * 1024 * 1024 * 1024] = 0;
// *surprise*, segfault 

Do this instead:

// Allocate 8GiB
void *buf = malloc(8UL * 1024 * 1024 * 1024);

// Access the memory
char *buf_bptr = (char *)buf;
buf_bptr[1 * 1024 * 1024 * 1024] = 0;
// *surprise*, no segfault 

Vector stores are not aligned

Vector stores that are not aligned to the boundary of their word size result in a segmentation fault.

For example, the movnt instruction takes a destination address and a source register (ymm):

_mm256_stream_si256(__m256i *dest, __m256i *ymm0);

In case dest is not aligned at the instruction’s word boundary the processor would generate a general protection fault and kernel will kill your application with a segfault.