justyy
justyy

Reputation: 6041

SSE2 optimisation in Delphi?

I know in C++ you can use some c++ SSE intrinsic functions to do vector calculations, but not sure if this is possible in latest Delphi compilers.

I have some code like this and they are in the core algorithms that are being called many many times, I would like to speed up this bit.

Any advices would be appreciated.

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;

Upvotes: 2

Views: 1193

Answers (1)

MBo
MBo

Reputation: 80197

Quick sketch. Note using of 4-element types.

Edit. Changed movdqu to movups (not essential)

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;

Upvotes: 3

Related Questions