gpt4 book ai didi

delphi - 如何在Delphi XE3中使用align-data-move SSE?

转载 作者:行者123 更新时间:2023-12-03 14:54:01 25 4
gpt4 key购买 nike

我试图运行以下命令,

type
Vector = array [1..4] of Single;

{$CODEALIGN 16}
function add4(const a, b: Vector): Vector; register; assembler;
asm
movaps xmm0, [a]
movaps xmm1, [b]
addps xmm0, xmm1
movaps [@result], xmm0
end;

它在movaps上给出了访问冲突,据我所知,如果内存位置是16对齐的,movaps是可以信任的。如果movups(不需要对齐),它就没有问题。

所以我的问题是,在 Delphi XE3 中,{$CODEALIGN} 在这种情况下似乎不起作用。

编辑

很奇怪...我尝试了以下操作。

program Project3;

{$APPTYPE CONSOLE}

uses
windows; // if not using windows, no errors at all

type
Vector = array [1..4] of Single;

function add4(const a, b: Vector): Vector;
asm
movaps xmm0, [a]
movaps xmm1, [b]
addps xmm0, xmm1
movaps [@result], xmm0
end;

procedure test();
var
v1, v2: vector;
begin
v1[1] := 1;
v2[1] := 1;
v1 := add4(v1,v2); // this works
end;

var
a, b, c: Vector;

begin
{$ifndef cpux64}
{$MESSAGE FATAL 'this example is for x64 target only'}
{$else}
test();
c := add4(a, b); // throw out AV here
{$endif}
end.

如果不添加“使用窗口”,则一切正常。如果“使用窗口”,那么它将在 c := add4(a, b) 处抛出异常,但不会在 test() 处抛出异常。

谁能解释一下?

编辑现在这对我来说一切都有意义了。 Delphi XE3 - 64 位的结论是

  1. X64 上的堆栈帧设置为 16 字节(根据需要),{$CODEALIGN 16} 将 proc/fun 的代码对齐到 16 字节。
  2. 动态数组位于堆中,可以使用 SetMinimumBlockAlignment(mba16byte) 将其设置为 16 对齐
  3. 但是,堆栈变量并不总是 16 字节对齐,例如,如果在上例中在 v1、v2 之前声明一个整数变量,例如test(),该示例将无法运行

最佳答案

您需要数据按 16 字节对齐。这需要一些照顾和关注。您可以确保堆分配器与 16 字节对齐。但是您无法确保编译器会对堆栈分配的变量进行 16 字节对齐,因为您的数组的对齐属性为 4,即其元素的大小。在其他结构内声明的任何变量也将具有 4 字节对齐。这是一个很难清除的障碍。

我认为您无法在当前可用的编译器版本中解决您的问题。至少不会,除非你放弃堆栈分配的变量,我猜这是一颗难以下咽的药丸。您可能会幸运地使用外部汇编器。

关于delphi - 如何在Delphi XE3中使用align-data-move SSE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801313/

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