Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell a C or a C++ compiler that pointers are not aliased

I have function that receives an array of pointers like so:

void foo(int *ptrs[], int num, int size)
{ 
  /* The body is an example only  */
     for (int i = 0; i < size; ++i) { 
       for (int j = 0; j < num-1; ++j)
         ptrs[num-1][i] += ptrs[j][i];
     }
}

What I want to convey to the compiler is that the pointers ptrs[i] are not aliases of each other and that the arrays ptrs[i] do not overlap. How shall I do this ? My ulterior motive is to encourage automatic vectorization.

Also, is there a way to get the same effect as __restrict__ on an iterator of a std::vector ?

like image 303
san Avatar asked Sep 07 '11 03:09

san


1 Answers

restrict, unlike the more common const, is a property of the pointer rather than the data pointed to. It therefore belongs on the right side of the '*' declarator-modifier. [] in a parameter declaration is another way to write *. Putting these things together, you should be able to get the effect you want with this function prototype:

void foo(int *restrict *restrict ptrs, int num, int size)
{
   /* body */
}

and no need for new names. (Not tested. Your mileage may vary. restrict is a pure optimization hint and may not actually do anything constructive with your compiler.)

like image 170
zwol Avatar answered Nov 15 '22 23:11

zwol