SSE3内部函数:如何查找大型浮点数组的最大值 [英] SSE3 intrinsics: How to find the maximum of a large array of floats

查看:142
本文介绍了SSE3内部函数:如何查找大型浮点数组的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code to find the maximum value

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内在函数对其进行矢量化处理,但我对如何进行比较感到惊讶.

I tried vectorizing it by using SSE3 intrinsics, but I am kind of struck on how I should do the comparison.

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
}

任何人都可以提出一些想法.

Can anyone give some idea on it.

推荐答案

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

So your code finds the largest value in a fixed-length array of floats. OK.

有_mm_max_ps,它为您提供了两个向量的成对最大值,每个向量有四个浮点数.那么呢?

There is _mm_max_ps, which gives you the pairwise maxima from two vectors of four floats each. So how about this?

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中四个值中的最大值(请参阅

Finally, grab the largest of the four values in max (see Getting max value in a __m128i vector with SSE? for that).

它应该以这种方式工作:

It should work this way:

第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)

这篇关于SSE3内部函数:如何查找大型浮点数组的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆