1

I'm very beginner to NEON intrinsic. I am trying to optimize the algorithm below

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

I am not sure how to do this since I need the result in 32-bit containers and I have source data as 8-bit ( R G B A ), and there is no instruction which can add 8-bits with 32-bits.

Can anyone help me out with this?

I was able to convert them to 32-bits as suggested by Paul's link and do the needful arithmetic. Now I have:

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

result 1/2/3/4 now contains 32-bits (per channel) RGB channels. How can I now combine result 1/2/3/4 to get 8-bits (per channel) RGB channels and put it back to the destination?

5
  • 2
    Possible duplicate of How to convert unsigned char to signed integer by neon
    – Paul R
    Feb 10, 2016 at 11:37
  • I don't understand what this algorithm does. Values (blue, green, red, alpha) will rise all time. *pDst can periodicaly overflowing of 8-bit.
    – ErmIg
    Feb 10, 2016 at 11:54
  • there is a shift also which is essentially a devide so i don't think it will overflow Feb 10, 2016 at 12:47
  • Even with the shift, it's still a continuous accumulation. The writes are inside the loop, and the accumulators are never reset or decreased, so the output is a continuously growing value. Also you said green twice... you mean blue, right?
    – sh1
    Feb 10, 2016 at 20:00
  • yes i meant blue..I agree this is continuous accumulation but we are adding 8 bit number ( *psrc ) to 32 bit number( say red ) initialized with 16 bit ( mentioned in code comment )number so i don't see any problem of overflow unless count is too large or am i missing something here? Feb 10, 2016 at 20:15

1 Answer 1

2

I still haven't understood deep sense of the algorithm, but of course you can optimize it with using 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);
}
1
  • worked like charm thanku so much. small coment you can't use vshrq_n_u32 since it expects const shift i used vshlq_u32 Feb 11, 2016 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.