0

I need to implement a wrapper layer between a high level application and a low level sub-system using slightly different typing:

The application produces an array of single vectors:

unit unApplication
type

TVector = record
  x, y, z : single;
end;

TvectorArray = array of Tvector;

procedure someFunc(): tvectorArray;
[...]

while the subsystem expects an array of double vectors. I also implemented typecasting from tvector to Tvectord:

unit unSubSystem
type

TVectorD = record
  x, y, z : double;
  class operator Implicit(value : t3dVector):t3dvectorD;inline;
end;

TvectorDArray = array of TvectorD;

procedure otherFunc(points: tvectorDArray);

implementation 
    class operator T3dVecTorD.Implicit(value : t3dVector):t3dvectorD;
begin
  result.x := value.x;
  result.y := value.y;
  result.z := value.z;
end;

What I am currently doing is like this:

uses unApplication, unsubsystem,...
procedure ConvertValues
var
  singleVecArr : TvectorArray;
  doubleveArr :  TvectorDArray; 
begin
  singleVecArr := somefunc;
  setlength(doubleVecArray, lenght(singlevecArr));
  for i := 0 to length(singlevecArr) -1 do
    doubleVecArray[i] := singleVecArr[i];
end;

Is there a more efficient way to perform these kinds of conversion?

2
  • this sort of thing makes me wonder why you don't just fix the whole thing (by modifying the source code) to use Double throughout. Bye bye conversion overhead. Bye-bye loss of precision. You have the worst of both worlds here. Subsystem? It's one application, use the same type throughout. If you are stuck at the low level with single for some darn good reason, then use it at the higher levels too, because your accuracy is limited to single anyways, at the lowest level.
    – Warren P
    Feb 1, 2011 at 2:20
  • @WarrenP: I would really like to dump the single precision parts, but both the application and the subsystem are 'fixed' and may not be modified. Thats the reason i have to write the wrapper in the first place. Feb 1, 2011 at 9:13

3 Answers 3

1

First of all I would say that you should not attempt any optimisation without first timing. In this case I don't mean timing alternative algorithms, I mean timing the code in question and assessing what proportion of the total time is spent there.

My instincts tell me that the code you show will run for a tiny proportion of the overall time and so optimising it will yield no discernible benefits. I think if you do anything meaningful with each element of this array then that must be true since the cost of converting from single to double will be small compared to floating point operations.

Finally, if perchance this code is a bottleneck, you should consider not converting it at all. My assumption is that you are using standard Delphi floating point operations which map to the 8087 FPU. All such floating point operations happen inside the 8087 floating point stack. Values are converted on entry to either 64 or more normally 80 bit precision. I don't think it would be any slower to load a single than to load a double – in fact it may even be faster due to memory read performance.

1

Assuming that the conversion indeed is the bottleneck, then one way of speeding up the conversion may be to use SSE# instead of the FPU, provided the necessary instruction sets can be assumed to be present on the computers on which this code will run.

For instance, the following would convert one single Vector into one double Vector:

procedure SingleToDoubleVector (var S: TVector; var D: TVectorD);
// @S in EAX
// @D in EDX
asm
  movups    xmm0, [eax]     ;// Load S in xmm0
  movhlps   xmm1,  xmm0     ;// Copy High 2 singles of xmm0 into xmm1
  cvtps2pd  xmm2,  xmm0     ;// Convert Low two singles of xmm0 into doubles in xmm2
  cvtss2sd  xmm3,  xmm1     ;// Convert Lowes single in xmm1 into double in xmm1
  movupd   [edx],  xmm2     ;// Move two doubles in xmm2 into D (.X and .Y)
  movsd    [edx+16],xmm3    ;// Move one double from xmm3 into D.Z
end;

I am not saying that this bit of code is the most efficient way to do it and there are many caveats with using assembly code in general and this code in particular. Note that this code makes assumptions about the alignment of the fields in your records. (It does not make assumptions regarding the alignment of the record as a whole.)

Also, for best results, you would control the alignment of your array/record elements in memory and write the entire conversion loop in assembly, to reduce overheads. Whether this is what you want/can do is another question.

3
  • I doubt that this would be faster. Feb 1, 2011 at 10:50
  • @David - Probably not when processing each TVector element separately (and if memory access is unaligned). But possible in principle.
    – PhiS
    Feb 1, 2011 at 12:05
  • The problem with 3 vectors and SSE#, and I work with them a lot in my job, is that SSE# wants to work with even numbers of them in its SIMD instructions. So if you have a nice packed, aligned array, then you can probably take advantage. That's what you say in your final paragraph. I'd be astounded if this part of the code was actually the bottleneck, but I think you share that opinion! ;-) Feb 1, 2011 at 12:19
0

If modifying the source to produce doubles rather than singles is not possible you can try threading out the process. Try dividing the TArray into two or four equal sized chunks (depending on processor count) and have each thread do the conversion. Doing this will realize almost double or quadruple speed.

Also, is the 'length' call calculated each loop? Maybe place that into a variable to avoid the calculation.

1
  • in the code shown Length is calculated once at the start of the loop. That's part or the spec of a for loop. It would be calculated every time if it was a while loop. Feb 1, 2011 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.