gpt4 book ai didi

d - SSE 对函数的陌生感

转载 作者:行者123 更新时间:2023-12-04 15:41:53 32 4
gpt4 key购买 nike

我一直在玩 D 的内联汇编器和 SSE,但发现了一些我不明白的东西。当我尝试在声明后立即添加两个 float4 向量时,计算是正确的。如果我把计算放在一个单独的函数中,我会得到一系列 nan s。

//function contents identical to code section in unittest
float4 add(float4 lhs, float4 rhs)
{
float4 res;
auto lhs_addr = &lhs;
auto rhs_addr = &rhs;
asm
{
mov RAX, lhs_addr;
mov RBX, rhs_addr;
movups XMM0, [RAX];
movups XMM1, [RBX];

addps XMM0, XMM1;
movups res, XMM0;
}
return res;
}

unittest
{
float4 lhs = {1, 2, 3, 4};
float4 rhs = {4, 3, 2, 1};

println(add(lhs, rhs)); //float4(nan, nan, nan, nan)

//identical code starts here
float4 res;
auto lhs_addr = &lhs;
auto rhs_addr = &rhs;
asm
{
mov RAX, lhs_addr;
mov RBX, rhs_addr;
movups XMM0, [RAX];
movups XMM1, [RBX];

addps XMM0, XMM1;
movups res, XMM0;
} //end identical code
println(res); //float4(5, 5, 5, 5)
}

该组件在功能上与 this link 相同(据我所知) .

编辑:我正在使用自定义的 float4 结构(目前,它只是一个数组),因为我希望能够拥有像 float4 add(float4 lhs, float rhs) 这样的 add 函数。 .目前,这会导致编译器错误,如下所示:

Error: floating point constant expression expected instead of rhs



注意:我使用的是 DMD 2.071.0

最佳答案

你的代码很奇怪,你用的是什么版本的dmd?这按预期工作:

import std.stdio;
import core.simd;

float4 add(float4 lhs, float4 rhs)
{
float4 res;
auto lhs_addr = &lhs;
auto rhs_addr = &rhs;
asm
{
mov RAX, lhs_addr;
mov RBX, rhs_addr;
movups XMM0, [RAX];
movups XMM1, [RBX];

addps XMM0, XMM1;
movups res, XMM0;
}
return res;
}

void main()
{
float4 lhs = [1, 2, 3, 4];
float4 rhs = [4, 3, 2, 1];

auto r = add(lhs, rhs);
writeln(r.array); //float4(5, 5, 5, 5)

//identical code starts here
float4 res;
auto lhs_addr = &lhs;
auto rhs_addr = &rhs;
asm
{
mov RAX, lhs_addr;
mov RBX, rhs_addr;
movups XMM0, [RAX];
movups XMM1, [RBX];

addps XMM0, XMM1;
movups res, XMM0;
} //end identical code
writeln(res.array); //float4(5, 5, 5, 5)
}

关于d - SSE 对函数的陌生感,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733133/

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