2013-10-19 38 views
0

我試圖從_mm_cmpistrm返回一個_m128i位掩碼,它指示128位源中的哪些字節包含空格。但是,我遇到了一些問題,因爲當我將第一個和第三個字節設置爲空格時,得到的結果爲{255,255,0,0,0,0,0,0 ....},而不是{255,0,255 ,0,0,0,...} ....SSE4 _mm_cmpistrm查找字符串中的字符(完整代碼示例)

有人可以幫助/啓發嗎?

#include <stdio.h> 
#include <nmmintrin.h> 
#include <iostream> 

using namespace std; 

int main() 
{ 
    __m128i a, b; 

    const int mode = _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT; 

    a.m128i_u8[15] = 't'; 
    a.m128i_u8[14] = 'e'; 
    a.m128i_u8[13] = 's'; 
    a.m128i_u8[12] = 'o'; 
    a.m128i_u8[11] = 'd'; 
    a.m128i_u8[10] = 'i'; 
    a.m128i_u8[9] = 'f'; 
    a.m128i_u8[8] = 'g'; 
    a.m128i_u8[7] = 't'; 
    a.m128i_u8[6] = 'd'; 
    a.m128i_u8[5] = 'b'; 
    a.m128i_u8[4] = 'n'; 
    a.m128i_u8[3] = 'd'; 
    a.m128i_u8[2] = ' '; 
    a.m128i_u8[1] = 'i'; 
    a.m128i_u8[0] = ' '; 

    b.m128i_u8[15] = ' '; 
    b.m128i_u8[14] = ' '; 
    b.m128i_u8[13] = ' '; 
    b.m128i_u8[12] = ' '; 
    b.m128i_u8[11] = ' '; 
    b.m128i_u8[10] = ' '; 
    b.m128i_u8[9] = ' '; 
    b.m128i_u8[8] = ' '; 
    b.m128i_u8[7] = ' '; 
    b.m128i_u8[6] = ' '; 
    b.m128i_u8[5] = ' '; 
    b.m128i_u8[4] = ' '; 
    b.m128i_u8[3] = ' '; 
    b.m128i_u8[2] = ' '; 
    b.m128i_u8[1] = ' '; 
    b.m128i_u8[0] = ' '; 

    __m128i returnValue = _mm_cmpistrm(a, b, mode); 

    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[0])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[1])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[2])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[3])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[4])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[5])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[6])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[7])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[8])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[9])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[10])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[11])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[12])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[13])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[14])) << endl; 
    std::cout << int(static_cast<unsigned char>(returnValue.m128i_u8[15])) << endl; 

    return 0; 
} 

回答

1

兩個可能的問題:

  • 我相信結果是低階結果向量的兩個字節填充16位字段,所以你的結果在這種情況下是16個TRUE標誌。

  • 您正在使用_SIDD_CMP_EQUAL_ANY,我想您可能需要使用_SIDD_CMP_EQUAL_EACH - 如果您進行此更改,我認爲您的結果將爲5,0,0,0,...表示元素0和0處的匹配2.

注:雖然我做了很多工作,與SSE和AVX我從來沒有用過這些字符串指令所以我只是立足於英特爾文檔的快速細讀上面的意見。