Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use __builtin_assume_aligned in C

Tags:

c

In my C code I have

complex float M[n][n];
complex float *delta = malloc(n * sizeof *delta);
complex float *v = malloc(n * sizeof *v);
for (i = 0; i < n; i++) {
    v[i] -= 2.*delta[j]*M[j][i];
}

where i and n are ints.

It was suggested I use __builtin_assume_aligned to make sure these are aligned in order to help auto-vectorization. However, having looked at the docs I don't understand how to do that.

How would you use it for this code?


The code in this question is extracted from How to help gcc vectorize C code . This is also why I want to try to align things.

like image 469
graffe Avatar asked Jan 14 '17 09:01

graffe


1 Answers

__builtin_assume_aligned is just a hint for gcc that the pointer is already aligned, so that it can typically vectorize the following code; it's not a directive to either malloc or any other memory allocation mechanism, so you might be lying to gcc.

To ensure that you have actually aligned pointers, it's your responsibility to use appropriate mechanisms. So you have to:

  • either malloc and then roundup to the next multiple of your granularity (if not already there)
  • or use __attribute__((aligned(N))) in your declarations (working for sure for heap-allocated, and possibly for stack-allocated variables)
  • or use aligning memory allocation calls like posix_memalign
like image 183
tzot Avatar answered Sep 21 '22 01:09

tzot