Altivec:_mm_sad_epu8()的模拟

时间:2016-02-12 07:35:52

标签: c++ image-processing simd powerpc altivec

我尝试移植一个SSE函数,该函数获得两个8位无符号整数数组的绝对差值。 它看起来像:

uint64_t AbsDiffSum(const uint8_t * a, const uint8_t * b, size_t size) 
{
    assert(size%16 == 0);
     __m128i _sum = _mm_setzero_si128();
    for(size_t i = 0; i < size; i += 16)
    {
        const __m128i _a = _mm_loadu_si128((__m128i*)(a + i));
        const __m128i _b = _mm_loadu_si128((__m128i*)(b + i));
        _sum = _mm_add_epi64(_sum, _mm_sad_epu8(_a, _b));
    }
    return _mm_cvtsi128_si64(_mm_add_epi64(_sum, _mm_srli_si128(_sum, 8)));
}

主要工作由内在函数_mm_sad_epu8()执行。

Altivec是否有类似物?

1 个答案:

答案 0 :(得分:3)

不幸的是,Altivec没有内在函数_mm_sad_epu8的直接模拟。 但是有可能效仿它:

typedef __vector uint8_t uint8x16_t;
typedef __vector uint32_t uint32x4_t;
const uint8_t K8_01 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

uint64_t AbsDiffSum(const uint8_t * a, const uint8_t * b, size_t size) 
{
    uint32x4_t _sum = {0, 0, 0, 0};
    for(size_t i = 0; i < size; i += 16)
    {
        // Aligned loading of 128-bit vector
        uint8x16_t _a = vec_ld(a + i);
        // Aligned loading of 128-bit vector
        uint8x16_t _b = vec_ld(b + i); 
        // Find absolute difference of two 8-bit unsigned
        uint8x16_t absDifference = vec_sub(vec_max(a, b), vec_min(a, b));
        // Sum result with using of vec_msum
        _sum = vec_msum(absDifference, K8_01, _sum);
    }
    return vec_extract(_sum, 0) + vec_extract(_sum, 1) + 
           vec_extract(_sum, 2) + vec_extract(_sum, 3);
}