__m128* pSrc1 = (__m128*) string;
__m128 m0 = _mm_set_ps1(0); //null character
while(1)
{
__m128 result = __m128 _mm_cmpeq_ss(*pSrc1, m0);
//if character is \0 then break
//do some stuff here
pSrc1++;
}
我有一个字符串,其长度可以是16的倍数。 如果_mm_cmpeq_ss返回相等,我该怎么摆脱循环?
答案 0 :(得分:9)
如果您在第一次遇到\0
时试图突破循环,那么您需要执行以下操作:
__m128i* pSrc1 = (__m128i *)string; // init pointer to start of string
__m128i m0 = _mm_set1_epi8(0); // vector of 16 `\0` characters
while (1)
{
__m128i v0 = _mm_loadu_si128(pSrc1); // get 16 chars from string
__m128i v1 = _mm_cmpeq_epi8(v0, m0); // compare all 16 chars with '\0'
int vmask = _mm_movemask_epi8(v1); // get 16 comparison result bits
if (vmask != 0) // if any bit is 1
break; // we found a `\0`, break out of loop
pSrc1++; // next 16 characters...
}
如果您只想在某些位置测试\0
个字符而忽略其他任何字符,那么您可以将if (vmask != 0)
测试更改为符合您特定要求的内容。