我想使用NEON内在函数来优化以下代码。
uint32x4_t c1; // 4 elements, each element is 0 or 1
uint32x4_t c2; // 4 elements, each element is 0 or 1
uint8_t pack = 0; // unsigned char, for result
/* some code /*
// need optimizing
pack |= (vgetq_lane_u32(c1, 0);
pack |= (vgetq_lane_u32(c1, 1) << 1;
pack |= (vgetq_lane_u32(c1, 2) << 2;
pack |= (vgetq_lane_u32(c1, 3) << 3;
pack |= (vgetq_lane_u32(c2, 0) << 4;
pack |= (vgetq_lane_u32(c2, 1) << 5;
pack |= (vgetq_lane_u32(c2, 2) << 6;
pack |= (vgetq_lane_u32(c2, 3) << 7;
我认为需要一些内在函数或OR向量的所有通道。 有人可以给我一些提示吗?
答案 0 :(得分:2)
您可以按向个别位数移动矢量中的每个元素。
const int32x4_t shifter1 = {0, 1, 2, 3};
const int32x4_t shifter2 = {4, 5, 6, 7};
.
.
.
c1 = vshlq_u32(c1, shifter1);
c2 = vshlq_u32(c2, shifter2);
c1 = vorrq_u32(c1, c2);
pack |= vgetq_lane_u32(c1, 0) | vgetq_lane_u32(c1, 1) | vgetq_lane_u32(c1, 2) | vgetq_lane_u32(c1, 3);
这应该可以解决问题,最后一行符合编译器的质量