问题描述
我有一个用例,其中我有一个位数组,例如,每个位都表示为8位整数.uint8_t data[] = {0,1,0,1,0,1,0,1};
我想通过仅提取每个值的lsb来创建一个整数.我知道使用int _mm_movemask_pi8 (__m64 a)
函数可以创建掩码,但是此内在函数仅占用字节的msb而不是lsb.是否有类似的内在方法或有效方法来提取lsb以创建单个8位整数?
I have a use case, where I have array of bits each bit is represented as 8 bit integer for example uint8_t data[] = {0,1,0,1,0,1,0,1};
I want to create a single integer by extracting only lsb of each value. I know that using int _mm_movemask_pi8 (__m64 a)
function I can create a mask but this intrinsic only takes a msb of a byte not lsb. Is there a similar intrinsic or efficient method to extract lsb to create single 8 bit integer?
推荐答案
没有直接的方法,但是显然您可以简单地将lsb移入msb,然后将其提取:
There is no direct way to do it, but obviously you can simply shift the lsb into the msb and then extract it:
_mm_movemask_pi8(_mm_slli_si64(x, 7))
这几天使用MMX很奇怪,应该避免使用.
Using MMX these days is strange and should probably be avoided.
这是SSE2版本,仍仅读取8个字节:
Here is an SSE2 version, still reading only 8 bytes:
int lsb_mask8(uint8_t* bits) {
__m128i x = _mm_loadl_epi64((__m128i*)bits);
return _mm_movemask_epi8(_mm_slli_epi64(x, 7));
}
使用SSE2代替MMX避免了EMMS
Using SSE2 instead of MMX avoids the needs for EMMS
这篇关于如何从__m64值的lsb创建8位掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!