3
__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 返回相等,我该如何跳出循环?

4

1 回答 1

9

如果您在第一次遇到 a 时试图跳出循环,\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)测试更改为符合您特定要求的内容。

于 2013-01-25T14:55:25.183 回答