TIDSP中的关键字restrict
2010-03-05 15:51
351 查看
转载自: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不会指向他们的存储区,因而可以并行进行多个数据的读取与求和。这两种程序编译出的代码执行速度相差极大。
为了帮助编译器确定存储器相关性,可以使用关键字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.
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; } }
下面的文字来自flybird:
关键字restrict的使用可以通过下面两个程序来说明 。
如下程序:两个均完成2个16位短型数据数组的矢量和
程序1:
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不会指向他们的存储区,因而可以并行进行多个数据的读取与求和。这两种程序编译出的代码执行速度相差极大。
相关文章推荐
- restrict关键字
- restrict关键字
- TI_DSP链接命令文件(*.cmd)的介绍
- 121-基于TI DSP TMS320DM8148的全高清1080P 60fs的视频编解码系统 机器人主板
- C语言中restrict关键字学习
- C语言的5种存储类以及关键字volatile、restrict
- TI DSP TMS320C66x学习笔记之VLIB测试数据(三)
- register、volatile、restrict关键字
- DSP程序开发与优化经验之一:TI多核处理DSP C66XX开发经验
- TI DSP--内存空间的分配和指定
- 164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡
- 关键字restrict(C99)
- C99中的restrict关键字
- 关键字 -- restrict
- C/C++ 中的特殊关键字:volatile, register, restrict, __attribute__
- c中restrict关键字的理解
- 选购TI DM642 DSP开发板和仿真器
- C语言中关键字restrict的用法(linux 为 __restrict)
- 175-基于TI DSP TMS320C6455、Xilinx V5 FPGA XC5VSX95T的高速数据处理核心板
- 204-基于Xilinx Virtex-6 XC6VLX240T 和TI DSP TMS320C6678的信号处理板