使用NEON内在函数进行优化

时间:2016-02-10 10:51:45

标签: c arm simd neon intrinsics

我是NEON内在的初学者。我正在尝试优化下面的算法

    uint32_t blue = 0, red = 0 , green = 0, alpha = 0, factor = 0 , shift = 0;

    // some initial calculation to calculate factor shift and R G B init values all are expected to be initilized with 16 bit unsigned

    //pSRC is 32 bbp flat pixel array and count is total pixels count

     for( int i = 0; i < count; i++ )
     {
        blue  += *psrc++;
        green += *psrc++;
        green += *psrc++;
        alpha += *psrc++;

        *pDest++ = static_cast< uint_8 >( ( blue * factor ) >> shift );
        *pDest++ = static_cast< uint_8 >( ( green * factor ) >> shift );
        *pDest++ = static_cast< uint_8 >( ( red * factor ) >> shift );
        *pDest++ = static_cast< uint_8 >( ( alpha * factor ) >> shift );
     } 

我不知道如何做到这一点,因为我需要32位容器中的结果,并且我将源数据作为8位( R G B A ),并且没有可以添加8位且32位的指令位。

任何人都可以帮我解决这个问题吗?

我能够将它们转换为32位,如Paul的链接所示,并进行必要的算术运算。现在我有:

           uint32x4_t result1 = vshlq_u32(mult1281, shift);
           uint32x4_t result2 = vshlq_u32(mult1282, shift);
           uint32x4_t result3 = vshlq_u32(mult1283, shift);
           uint32x4_t result4 = vshlq_u32(mult1284, shift);

结果1/2/3/4现在包含32位(每通道)RGB通道。我现在如何将结果1/2/3/4组合成8位(每通道)RGB通道并将其放回目的地?

1 个答案:

答案 0 :(得分:1)

我仍然没有深入理解算法,但当然你可以使用NEON来优化它:

uint32_t blue = 0, red = 0, green = 0, alpha = 0, factor = 0, shift = 0;
// some your initializations.
uint32x4_t bgra = { blue, green, red, alpha };
for (int i = 0; i < count; i += 2)
{
    //load 8 8-bit values and unpack to 16-bit
    uint16x8_t src = vmovl_u8(vld1_u8(psrc + i * 4)); 

    //accumulate low 4 values
    bgra = vaddw_u16(bgra, vget_low_u16(src));
    //get low 4 values of dst
    uint32x4_t lo = vshrq_n_u32(vmulq_u32(bgra, vdupq_n_u32(factor)), shift);

    //accumulate high 4 values
    bgra = vaddw_u16(bgra, vget_high_u16(src));
    //get high 4 values of dst
    uint32x4_t hi = vshrq_n_u32(vmulq_u32(bgra, vdupq_n_u32(factor)), shift);

    //pack 8 32-bit values to 8 8-bit.
    uint8x8_t dst = vmovn_u16(vcombine_u16(vmovn_u32(lo), vmovn_u32(hi)));

    //store result
    vst1_u8(pDest + i * 4, dst);
}