[fpc-devel] Object/Pointer Swap Intrinsic

Sven Barth pascaldragon at googlemail.com
Wed Dec 11 22:43:40 CET 2019


Am 11.12.2019 um 07:26 schrieb J. Gareth Moreton:
> Hi everyone,
>
> I was wondering... is there an intrinsic in Free Pascal that swaps the 
> pointers stored in two object or pointer variables? That is... 
> something like "procedure SwapAddresses(var Obj1: TObject; var Obj2: 
> TObject);" so after it is called, Obj2 now points to Obj1 and vice 
> versa?  Under x86-64, for example, it can be implemented as (Intel 
> notation):
>
> XOR RCX, RDX
> XOR RDX, RCX
> XOR RCX, RDX
>
> Or just
>
> XCHG RCX, RDX

It might be more useful to try to write high level optimization that 
detects the typical swap sequence of

tmp := a;
a := b;
b := tmp;

And optimizes that to the most optimal CPU instructions (by converting 
it to an internal "Swap" intrinsic, similar as it is done for the 
load/modify/store optimizations).

Then any existing code would profit from that as well.

And for your specific use case you could then add such swap procedures 
to cutils.pas or so...

Regards,
Sven


More information about the fpc-devel mailing list