CPU指令集——获取数组的所有奇数位、所有偶数位

为抽取bayer格式图像的r\g\b做准备,_mm256_shuffle_epi8不能按_mm_shuffle_epi8理解,前者是前后一半分开处理,后者是全部一起处理。

#include <iostream>
#include <intrin.h>

int main()
{
    uint8_t uint8_array[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; //内存顺序0 1 2
    __m128i a = _mm_load_si128((__m128i*)uint8_array);                                  //内存顺序0 1 2
    __m128i b = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);     //内存顺序15 14 13,注意此函数是反序
    __m128i c = _mm_shuffle_epi8(a, b);     //按b索引对a重排,b第一个索引15对应a的15
    uint8_t* uint8_result = (uint8_t*)&c;   //15 14 13
    for (int i = 0; i < 16; i++)
    {
        std::cout << (int)uint8_result[i] << "\n";
    }

    //对a重排,前一半是所有奇数位,后一半是所有偶数位
    __m128i oe = _mm_set_epi8(15, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 0);
    __m128i oe_result = _mm_shuffle_epi8(a, oe);
    uint8_t* uint8_oe_result = (uint8_t*)&oe_result;
    for (int i = 0; i < 16; i++)
    {
        std::cout << (int)uint8_oe_result[i] << "\n";
    }
    uint8_t o_result[8], e_result[8];
    memcpy(o_result, uint8_oe_result, 8 * sizeof(uint8_t));     //所有奇数位的元素
    memcpy(e_result, uint8_oe_result+8, 8 * sizeof(uint8_t));   //所有偶数位的元素
}

 

posted @ 2024-06-13 23:05  夕西行  阅读(56)  评论(0编辑  收藏  举报