DSP

TIDSP中的关键字restrict

2019-07-13 10:59发布

转载自:http://ichaochao.com/2008/05/14/dsp_restrict/   为了帮助编译器确定存储器相关性,可以使用关键字restrict来限定指针、引用或数组。关键字restrict是对指针、引用或数组的一种限定。使用restrict关键字是为了确保其限定的指针在声明的范围内,是指向一个特定对象的唯一指针,及这个指针不会和其它指针指向存储器的同一地址。这使编译器更容易确定是否有别名信息,从而更好地优化代码。
下例中,关键字restrict的使用告知编译器func1中指针a和b所指向的存储器范围不会交叠,这表明通过指针变量a和b对存储器的访问不会冲突,即对一个指针变量的写操作不会影响另一个指针变量的读操作。
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 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 下面的文字来自flybird: 关键字restrict的使用可以通过下面两个程序来说明 。
如下程序:两个均完成2个16位短型数据数组的矢量和 程序1: 程序2: 编译器在编译程序1时,无法判断指针*sum与指针*in1,*in2是否独立。此时,编译器采取保守的办法,认为他们是相关的,即:认为*sum指向的存储区与*in1,in2指向的存储区可能混迭。这时编译出的代码必须执行完前一次写,然后才能开始下一次读取。在编译程序2的时候restrict表明指针*in1,*in2是独立的,*sum不会指向他们的存储区,因而可以并行进行多个数据的读取与求和。这两种程序编译出的代码执行速度相差极大。