从 __m128i 中查找最小值/最大值 [英] Find min/max value from a __m128i

查看:40
本文介绍了从 __m128i 中查找最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 SIMD 操作在字节数组中找到最小值/最大值.到目前为止,我已经能够遍历数组并将最小值/最大值存储到 __m128i 变量中,但这意味着我正在寻找的值混合了其他值(准确地说是其他 15 个).

I want to find the minimum/maximum value into an array of byte using SIMD operations. So far I was able to go through the array and store the minimum/maximum value into a __m128i variable, but it means that the value I am looking for is mixed among others (15 others to be exact).

我在这里此处 用于整数,此页面 用于浮动,但我不明白 _mm_shuffle* 是如何工作的.所以我的问题是:

I've found these discussions here and here for integer, and this page for float, but I don't understand how works _mm_shuffle*. So my questions are:

  1. 为了从 __m128i 变量中提取最小/最大字节(或无符号字节)值,我必须执行哪些 SIMD 操作?
  2. _mm_shuffle* 是如何工作的?当我在线查看最小"文档时,我不明白.我知道它与 _MM_SHUFFLE 宏有关,但我不明白这个例子.
  1. What SIMD operations do I have to perform in order to extract the minimum / maximum byte (or unsigned byte) value from the __m128i variable?
  2. How does _mm_shuffle* work? I don't get it when I look to the "minimal" documentation online. I know it is related to the _MM_SHUFFLE macro, but I don't get the example.

推荐答案

以下是 uint8_t 的水平最大值示例:

Here is an example for horizontal max for uint8_t:

#include "tmmintrin.h" // requires SSSE3

__m128i _mm_hmax_epu8(const __m128i v)
{
    __m128i vmax = v;

    vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 1));
    vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 2));
    vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 4));
    vmax = _mm_max_epu8(vmax, _mm_alignr_epi8(vmax, vmax, 8));

    return vmax;
}

将在所有元素中返回最大值.如果您需要该值作为标量,请使用 _mm_extract_epi8.

The max value will be returned in all elements. If you need the value as a scalar then use _mm_extract_epi8.

对于最小值和带符号的最小值/最大值如何调整它应该是相当明显的.

It should be fairly obvious how to adapt this for min, and for signed min/max.

这篇关于从 __m128i 中查找最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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