keyword:restrict

为了帮助编译器肯定存储器相关性,能够使用关键字restrict来限定指针、引用或数组。关键字restrict是对指针、引用或数组的一种限定。使用restrict关键字是为了确保其限定的指针在声明的范围内,是指向一个特定对象的惟一指针,及这个指针不会和其它指针指向存储器的同一地址。这使编译器更容易肯定是否有别名信息,从而更好地优化代码。
下例中,关键字restrict的使用告知编译器func1中指针a和b所指向的存储器范围不会交叠,这代表经过指针变量a和b对存储器的访问不会冲突,即对一个指针变量的写操做不会影响另外一个指针变量的读操做。
void func1(int * restrict a, int * restrict b)
{
/* func1’s code here */
}数组

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.app

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 pointersthis

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 arraysrest

void func2(int c[restrict], int d[restrict])
{
int i;
for(i = 0; i < 64; i++)
{
c[i] += d[i];
d[i] += 1;
}
}code

下面的文字来自flybird:orm

关键字restrict的使用能够经过下面两个程序来讲明 。
以下程序:两个均完成2个16位短型数据数组的矢量和对象

程序1:blog

void vecsum( short *sum, short *in1, short *in2, unsigned int N)
{
int i;
for(i=0;i<N;i++)
sum[i]=in1[i]+in2[i];
}

程序2:

void vecsum(short * restrict sum, restrict short * in1, restrict short * in2,unsigned int N)
{
int i;
for (i=0;i<N;i++)
sum[i]=in1[i]+in2[i];
}

编译器在编译程序1时,没法判断指针*sum与指针*in1,*in2是否独立。此时,编译器采起保守的办法,认为他们是相关的,即:认为*sum指向的存储区与*in1,in2指向的存储区可能混迭。这时编译出的代码必须执行完前一次写,而后才能开始下一次读取。在编译程序2的时候restrict代表指针*in1,*in2是独立的,*sum不会指向他们的存储区,于是能够并行进行多个数据的读取与求和。这两种程序编译出的代码执行速度相差极大。

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息