2011-07-14 14 views
4

我試圖使用AVX固有的解壓縮指令_m256_unpacklo_ps_m256_unpackhi_ps交錯16個浮點值。我得到的結果很奇怪,或者是因爲我不瞭解AVX應該如何解封,或者因爲某些東西不能正常工作。AVX的意外結果_m256_unpack * _ps解壓內部

我所看到的是,當我嘗試,例如,解開低位來自兩個向量,v1和v2花車,到第三,V3,我看到以下內容:

如果V1是[a b c d e f g h] 且v1 [i j k l m n o p]

然後v3 = _m256_unpacklo_ps(v1, v2)結果 [a i b j e m f n]

當我預計V3會給[a i b j c k d l]

我的預期不正確嗎?還是我錯誤地使用了這個?還是有其他的故障?

一些測試代碼:

#include <immintrin.h> 
#include <iostream> 

int main() 
{ 

    float output[16], input1[8], input2[8]; 
    __m256 vec1, vec2, vec3, vec4; 

    vec1 = _mm256_set_ps(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f); 
    vec2 = _mm256_set_ps(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f); 

    _mm256_store_ps(input1, vec1); 
    _mm256_store_ps(input2, vec2); 

    vec3 = _mm256_unpacklo_ps(vec1, vec2); 
    vec4 = _mm256_unpackhi_ps(vec1, vec2); 

    _mm256_store_ps(output, vec3); 
    _mm256_store_ps(output + 8, vec4); 

    std::cout << "interleaving:" << std::endl; 
    for (unsigned i = 0; i < 8; ++i) 
    std::cout << input1[i] << " "; 
    std::cout << std::endl; 

    std::cout << "with:" << std::endl; 
    for (unsigned i = 0; i < 8; ++i) 
    std::cout << input2[i] << " "; 
    std::cout << std::endl; 

    std::cout << "= " << std::endl; 
    for (unsigned i = 0; i < 16; ++i) 
    std::cout << output[i] << " "; 
    std::cout << std::endl; 
} 

我使用GCC 4.5.2編譯。

在此先感謝您的幫助! - 賈斯汀

回答

3

它的行爲如預期。

爲了得到[A I BĴÇķd 1],則需要使用:

A = unpacklo_ps(v1,v2) 

B = unpackhi_ps(v1,v2)然後使用

C=_mm256_permute2f128_ps(A,B,0x20)

從兩者得到所期望的128個比特。

+0

爲了得到另一半,使用_mm256_permute2f128_ps(A,B,0x31) –