使用 sse 内在函数时如何中断循环? [英] how to break from a loop when using sse intrinsics?

查看:33
本文介绍了使用 sse 内在函数时如何中断循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string whose length can be a multiple of 16. How do I break out of the loop if _mm_cmpeq_ss returns equal?

推荐答案

如果您在第一次遇到 \0 时试图跳出循环,那么您需要做一些事情像这样:

If you're trying to break out of the loop when you first encounter a \0 then you'll need to do something like this:

__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) 测试更改为符合您的特定要求.

If you only want to test for \0 characters in certain positions and ignore any others then you can change the if (vmask != 0) test to something which matches your specific requirements.

这篇关于使用 sse 内在函数时如何中断循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆