gpt4 book ai didi

c++ - 使用内部函数时程序崩溃

转载 作者:行者123 更新时间:2023-11-28 02:49:51 25 4
gpt4 key购买 nike

我刚开始使用内部函数,所以我不确定为什么我的程序会崩溃。我能够构建该程序,但当我运行它时,我只看到“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 ==========

最佳答案

问题是您的“源”(数组 x)未与 SSE 指令要求的大小对齐。

您可以使用“未对齐”加载指令来解决这个问题,或者您可以使用 __declspec(align(n)) 来解决这个问题。 ,例如:

    float __declspec(align(16)) x[N];
float __declspec(align(16)) y[N];

现在您的 xy 数组对齐到 16 字节,并且可以通过 SSE 指令访问 [当然是在 4 的倍数的索引上]。请注意,采用内存参数的一般 SSE 指令不允许未对齐访问,因此例如 _mm_max_ps 要求第二个参数(按 Intel 顺序,第一个按 AT&T 顺序)是对齐的内存位置。

关于c++ - 使用内部函数时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com