Find min/max value from a __m128i

后端 未结 2 785
抹茶落季
抹茶落季 2021-02-10 05:03

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 __m128

2条回答
  •  伪装坚强ぢ
    2021-02-10 05:16

    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;
    }
    

    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.

提交回复
热议问题