restrict是用来优化的,是C99新加的关键字,常用于指针、数组。
void func1(int * restrict a, int * restrict b)
{
/* func1's code here */
}
其中a、b需没有存储空间上的重叠,即a、b不会在读写发生冲突,从而利于编译器优化,实现并行处理。
TI的说明:
To help the compiler determine memory dependencies, you can qualify a pointer, reference, or array with the restrict keyword. The restrict keyword is a type qualifier that may be applied to pointers, references, and arrays. Its use represents a guarantee by the programmer that within the scope of the pointer declaration the object pointed to can be accessed only by that pointer. Any violation of this guarantee renders the program undefined. This practice helps the compiler optimize certain sections of code because aliasing information can be more easily determined.
In the example that follows, the restrict keyword is used to tell the compiler that the function func1 is never called with the pointers a and b pointing to objects that overlap in memory. You are promising that accesses through a and b will never conflict; this means that a write through one pointer cannot affect a read from any other pointer. The precise semantics of the restrict keyword are described in the 1999 version of the ISO C standard.
Use of the restrict type qualifier with pointers
void func1(int * restrict a, int * restrict b)
{
/* func1's code here */
}
This example illustrates using the restrict keyword when passing arrays to a function. Here, the arrays c and d should not overlap, nor should c and d point to the same array.
Use of the restrict type qualifier with arrays
void func2(int c[restrict], int d[restrict])
{
int i;
for(i = 0; i < 64; i++)
{
c[i] += d[i];
d[i] += 1;
}
}