【发布时间】:2014-06-13 14:46:16
【问题描述】:
我是使用内部函数的新手,所以我不确定我的程序为什么会崩溃。我能够构建程序,但是当我运行它时,我只是得到“programname.exe 已停止工作”窗口。
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <intrin.h>
int _tmain(int argc, _TCHAR* argv[])
{
const int N = 128;
float x[N], y[N];
float sum = 0;
for (int i = 0; i < N; i++)
{
x[i] = rand() >> 1;
y[i] = rand() >> 1;
}
float* ptrx = x;
float* ptry = y;
__m128 x1;
x1 = _mm_load_ps(ptrx);
return 0;
}
如果我注释掉 'x1 = _mm_load_ps(ptrx);'行,程序能够运行,所以这就是导致崩溃的原因。
这是构建解决方案时的输出...
1>------ Rebuild All started: Project: intrins2, Configuration: Debug Win32 ------
1> stdafx.cpp
1> intrins2.cpp
1>c:\...\visual studio 2013\projects\intrins2\intrins2\intrins2.cpp(20): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\...\visual studio 2013\projects\intrins2\intrins2\intrins2.cpp(21): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1> intrins2.vcxproj -> c:\...\visual studio 2013\Projects\intrins2\Debug\intrins2.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
【问题讨论】:
-
这可能不太可能,但您执行代码的机器是否可能不支持内在函数?请注意,并非所有处理器都支持所有内在函数:msdn.microsoft.com/de-de/library/yc6byew8%28v=vs.90%29.aspx
-
把
_mm_load_ps(ptrx)改成_mm_loadu_ps(ptrx)或者在64位模式下编译,你的程序就不会崩溃了。 -
谢谢@Zboson 成功了。两者有什么区别?
-
如果我没记错的话,
loadu变体会加载未对齐的变量并使它们对齐。各种 SSE 内容都需要这种对齐方式。在 x64 上,默认情况下所有变量都是对齐的,这就是它在那里工作的原因。 -
@JonB.Jones。
_mm_load_ps要求内存为 16 字节对齐。_mm_loadu_ps没有这样的限制。在 64 位模式下,堆栈是 16 字节对齐的。但是,堆中的内存不一定是 16 字节对齐的。在较旧的 CPU 上_mm_loadu_ps比_mm_load_ps慢得多,即使在对齐的内存上也是如此,但情况不再如此。只需使用_mm_loadu_ps并继续前进。
标签: c++ visual-studio visual-studio-2013 sse intrinsics