7

我有以下代码来找到最大值

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
}

任何人都可以给出一些想法。

4

1 回答 1

11

因此,您的代码会在固定长度的浮点数数组中找到最大值。好的。

有 _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? for that)。

它应该以这种方式工作:

步骤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)
于 2013-03-06T04:43:21.923 回答