SSE _mm_load_ps导致分段错误

Dan*_*Dan 3 c++ sse intrinsics segmentation-fault

因此,我一直在使用这个玩具示例来学习使用SSE内在函数进行编程.我在这里阅读其他主题,有时使用_mm_load_ps函数的分段错误是由于没有正确对齐事情引起的,但我认为应该通过我所做的属性((aligned(16)))来解决.此外,当我在我的代码中注释掉第23或24行(或两者)时,问题就消失了,但显然这会使代码无效.

#include <iostream>
using namespace std;

int main()
{
        float temp1[] __attribute__((__aligned__(16))) = {1.1,1.2,1.3,14.5,3.1,5.2,2.3,3.4};
        float temp2[] __attribute__((__aligned__(16))) = {1.2,2.3,3.4,3.5,1.2,2.3,4.2,2.2};
        float temp3[8];
        __m128 m, *m_result;
        __m128 arr1 = _mm_load_ps(temp1);
        __m128 arr2 = _mm_load_ps(temp2);

        m = _mm_mul_ps(arr1, arr2);
        *m_result = _mm_add_ps(m, m); 
        _mm_store_ps(temp3, *m_result); 
        for(int i = 0; i < 4; i++)
        {   
            cout << temp3[i] << endl;
        }   

        m_result++;
        arr1 = _mm_load_ps(temp1+4);
        arr2 = _mm_load_ps(temp2+4);
        m = _mm_mul_ps(arr1, arr2);
        *m_result = _mm_add_ps(m,m);
        _mm_store_ps(temp3, *m_result); 


        for(int i = 0; i < 4; i++)
        {   
            cout << temp3[i] << endl;
        }   
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

第23行是arr1 = _mm_load_ps(temp1 + 4).我可以做一个或另一个但不是两个都很奇怪.任何帮助将不胜感激,谢谢!

Z b*_*son 6

你的问题是你声明了一个指针,__m128 *m_result但你永远不会为它分配任何空间.稍后你还会m_result++指向另一个尚未分配的内存地址.这里没有理由使用指针.

#include <xmmintrin.h>                 // SSE
#include <iostream>
using namespace std;

int main()
{
        float temp1[] __attribute__((__aligned__(16))) = {1.1,1.2,1.3,14.5,3.1,5.2,2.3,3.4};
        float temp2[] __attribute__((__aligned__(16))) = {1.2,2.3,3.4,3.5,1.2,2.3,4.2,2.2};
        float temp3[8];
        __m128 m, m_result;
        __m128 arr1 = _mm_load_ps(temp1);
        __m128 arr2 = _mm_load_ps(temp2);

        m = _mm_mul_ps(arr1, arr2);
        m_result = _mm_add_ps(m, m); 
        _mm_store_ps(temp3, m_result); 
        for(int i = 0; i < 4; i++)
        {   
            cout << temp3[i] << endl;
        }   

        arr1 = _mm_load_ps(temp1+4);
        arr2 = _mm_load_ps(temp2+4);
        m = _mm_mul_ps(arr1, arr2);
        m_result = _mm_add_ps(m,m);
        _mm_store_ps(temp3, m_result); 


        for(int i = 0; i < 4; i++)
        {   
            cout << temp3[i] << endl;
        }   
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,`temp3`可能未对齐. (2认同)