我知道在C ++中可以使用某些c ++ SSE内在函数进行矢量计算,但是不确定在最新的Delphi编译器中是否可以这样做。
我有一些这样的代码,它们在被多次调用的核心算法中,我想加快这一步。
任何建议,将不胜感激。
type
Vector = array [1..3] of Single;
VectorInt = array [1..3] of Integer;
function TSomeCalculation.getCubePosRound(const xyz: Vector): VectorInt;
begin
// Self.RESINV and Self.RangeMin are type of Vector
Result[1] := Round((xyz[1] - Self.RangeMin[1]) * Self.RESINV[1]);
Result[2] := Round((xyz[2] - Self.RangeMin[2]) * Self.RESINV[2]);
Result[3] := Round((xyz[3] - Self.RangeMin[3]) * Self.RESINV[3]);
end;
最佳答案
速写。注意使用4元素类型。
编辑。将movdqu
更改为movups
(不是必需的)
type
Vector = array [0..3] of Single;
VectorInt = array [0..3] of Integer;
...
var
Form1: TForm1;
Vec: Vector;
VecI: VectorInt;
RESINV: Vector;
RangeMin: Vector;
procedure TForm1.Button6Click(Sender: TObject);
begin
vec[0] := 10;
vec[1] := 12;
vec[2] := 14;
vec[3] := 16;
RangeMin[0] := 4.2;
RangeMin[1] := 5.2;
RangeMin[2] := 6.2;
RangeMin[3] := 7.2;
RESINV[0] := 0;
RESINV[1] := 1.1;
RESINV[2] := 2.2;
RESINV[3] := 3.3;
vecI := getCubePosRound(vec);
end;
function Tform1.getCubePosRound(const xyz: Vector): VectorInt;
asm
movups xmm1, [xyz] //load 16 bytes unaligned
movups xmm2, [RangeMin]
movups xmm3, [RESINV]
subps xmm1, xmm2 //subtract packed singles
mulps xmm1, xmm3 //multiply
cvtps2dq xmm0, xmm1 //rounded according to MXCSR register
movdqu [Result], xmm0 //store 16 bytes
end;
关于delphi - Delphi中的SSE2优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212404/