SSE _mm_load_ps导致分段错误
SSE _mm_load_ps causing segmentation faults
所以我在这个学习使用 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;
}
第 23 行是 arr1 = _mm_load_ps(temp1+4)。对我来说很奇怪,我可以做一个或另一个,但不能同时做两个。任何帮助将不胜感激,谢谢!
你的问题是你声明了一个指针__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;
}
(1) m_result
只是一个狂野的指针:
__m128 m, *m_result;
将所有出现的*m_result
更改为m_result
并删除m_result++;
。(m_result
只是一个临时向量变量,随后要存储到 temp3
中)。
(2) 您的两个商店可能未对齐,因为temp3
无法保证对齐 - 任一更改:
float temp3[8];
自:
float temp3[8] __attribute__((__aligned__(16)));
或使用_mm_storeu_ps
:
_mm_storeu_ps(temp3, m_result);
^^^
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?