Get member of __m128 by index?

后端 未结 4 815
旧时难觅i
旧时难觅i 2020-12-05 10:28

I\'ve got some code, originally given to me by someone working with MSVC, and I\'m trying to get it to work on Clang. Here\'s the function that I\'m having trouble with:

4条回答
  •  广开言路
    2020-12-05 10:58

    Use

    template
    float vectorGetByIndex( __m128 V) {
        union {
            __m128 v;    
            float a[4];  
        } converter;
        converter.v = V;
        return converter.a[i];
    }
    

    which will work regardless of the available instruction set.

    Note: Even if SSE4.1 is available and i is a compile time constant, you can't use pextract etc. this way, because these instructions extract a 32-bit integer, not a float:

    // broken code starts here
    template
    float vectorGetByIndex( __m128 V) {
        return _mm_extract_epi32(V, i);
    }
    // broken code ends here
    

    I don't delete it because it is a useful reminder how to not do things.

提交回复
热议问题