使用带阵列限制? [英] Using restrict with arrays?
问题描述
有没有办法告诉C99编译,只有这样,我会定的数组访问是通过使用myArray的[指数]
说是这样的:
Is there a way to tell a C99 compiler that the only way I am going to access given array is by using myarray[index] ? Say something like this:
int heavy_calcualtions(float* restrict range1, float* restrict range2)
{
float __I promise I won't alias this__ tmpvalues[1000] = {0};
....
heavy calculations using range1, range2 and tmpvalues;
....
}
通过使用限制我答应,我不会别名范围1和范围,但我怎么做同样的事情阵列我的函数内声明?
By using restrict I promised that I won't alias range1 and range2 but how do I do the same thing for array declared inside my function ?
推荐答案
为什么不能你下面?您没有访问通过该变量, tmpvalues
相关联的数据,因此它是有效的在code的计算密集型部分使用限制指针。
Why can't you do the following? You are not accessing the data associated with tmpvalues
via that variable, so it is valid to use a restrict pointer in the compute-intensive portion of the code.
#include <stdio.h>
#include <stdlib.h>
int heavy_calcs(int n, float* restrict range1, float* restrict range2)
{
if (n>1000) return 1;
float tmpvalues[1000] = {0};
{
float * restrict ptv = tmpvalues;
for (int i=0; i<n; i++) {
ptv[i] = range1[i] + range2[i];
}
}
return 0;
}
int main(int argc, char * argv[])
{
int n = (argc>1) ? atoi(argv[1]) : 1000;
float * r1 = (float*)malloc(n*sizeof(float));
float * r2 = (float*)malloc(n*sizeof(float));
int rc = heavy_calcs(n,r1,r2);
free(r1);
free(r2);
return rc;
}
我跑这通过英特尔编译器15和它没有任何麻烦,矢量化循环。当然,这个循环是微不足道的相比,要什么,我认为你的是,所以你的里程可能会有所不同。
I ran this through the Intel 15 compiler and it had no trouble vectorizing the loop. Granted, this loop is trivial to compared to what I assume yours is, so your mileage may vary.
这篇关于使用带阵列限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!