Fastest de-interleave operation in C?

后端 未结 6 1712
一个人的身影
一个人的身影 2021-01-02 00:30

I have a pointer to an array of bytes mixed that contains the interleaved bytes of two distinct arrays array1 and array2. Say mi

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-02 00:57

    I've only tested this lightly but it seemed at least twice as fast as your version:

    typedef union {
    uint16_t wide;
    struct { uint8_t top; uint8_t bottom; } narrow;
    } my_union;
    
    uint16_t *source = (uint16_t *)mixed;
    for (int i = 0; i < mixedLength/2; i++)
    {
        my_union cursor;
        cursor.wide = source[i];
        array1[i] = cursor.narrow.top;
        array2[i] = cursor.narrow.bottom;
    }
    

    Notice that I wasn't careful with structure packing, but that in this case on this architecture that isn't a problem. Notice also someone might complain at my choice of naming top and bottom; I assume you know which half of which integers you need.

提交回复
热议问题