我使用以下代码来查找最大值
int length = 2000;
float *data;
// data is allocated and initialized
float max = 0.0;
for(int i = 0; i < length; i++)
{
if(data[i] > max)
{
max = data;
}
}我尝试过使用SSE3内部函数对其进行矢量化,但我有点不知道该如何进行比较。
int length = 2000;
float *data;
// data is allocated and initialized
float max = 0.0;
// for time being just assume that length is always mod 4
for(int i = 0; i < length; i+=4)
{
__m128 a = _mm_loadu_ps(data[i]);
__m128 b = _mm_load1_ps(max);
__m128 gt = _mm_cmpgt_ps(a,b);
// Kinda of struck on what to do next
}有没有人能给出一些想法。
发布于 2013-03-06 12:43:22
因此,您的代码将在固定长度的浮点数组中查找最大值。好的。
有一个_mm_max_ps,它给出了两个向量的成对最大值,每个向量有四个浮点数。那么这个怎么样?
int length = 2000;
float *data; // maybe you should just use the SSE type here to avoid copying later
// data is allocated and initialized
// for time being just assume that length is always mod 4
__m128 max = _mm_loadu_ps(data); // load the first 4
for(int i = 4; i < length; i+=4)
{
__m128 cur = _mm_loadu_ps(data + i);
max = _mm_max_ps(max, cur);
}最后,获取max中四个值中最大的一个(请参阅Getting max value in a __m128i vector with SSE? )。
它应该是这样工作的:
步骤1:
[43, 29, 58, 94] (this is max)
[82, 83, 10, 88]
[19, 39, 85, 77]第2步:
[82, 83, 58, 94] (this is max)
[19, 39, 85, 77]第2步:
[82, 83, 85, 94] (this is max)https://stackoverflow.com/questions/15238978
复制相似问题