Fast 24-bit array -> 32-bit array conversion?

后端 未结 4 1753
终归单人心
终归单人心 2020-12-08 22:47

Quick Summary:

I have an array of 24-bit values. Any suggestion on how to quickly expand the individual 24-bit array elements into 32-bit

4条回答
  •  我在风中等你
    2020-12-08 23:31

    The code below should be pretty fast. It copies 4 pixels in each iteration, using only 32-bit read/write instructions. The source and destination pointers should be aligned to 32 bits.

    uint32_t *src = ...;
    uint32_t *dst = ...;
    
    for (int i=0; i>24) | (sb<<8);
        dst[i+2] = (sb>>16) | (sc<<16);
        dst[i+3] = sc>>8;
    
        src += 3;
    }
    

    Edit:

    Here is a way to do this using the SSSE3 instructions PSHUFB and PALIGNR. The code is written using compiler intrinsics, but it shouldn't be hard to translate to assembly if needed. It copies 16 pixels in each iteration. The source and destination pointers Must be aligned to 16 bytes, or it will fault. If they aren't aligned, you can make it work by replacing _mm_load_si128 with _mm_loadu_si128 and _mm_store_si128 with _mm_storeu_si128, but this will be slower.

    #include 
    #include 
    
    __m128i *src = ...;
    __m128i *dst = ...;
    __m128i mask = _mm_setr_epi8(0,1,2,-1, 3,4,5,-1, 6,7,8,-1, 9,10,11,-1);
    
    for (int i=0; i

    SSSE3 (not to be confused with SSE3) will require a relatively new processor: Core 2 or newer, and I believe AMD doesn't support it yet. Performing this with SSE2 instructions only will take a lot more operations, and may not be worth it.

提交回复
热议问题