double-word compare-and-swap function using cmpxchg16b (D language)
// by Niklas Schulze / nischu7 <n7@pronghorn.org>
//
// requires cmpxchg16b CPU instruction
// required GDC patch: http://bitbucket.org/nischu7/gdc/changeset/d8a2a73fb3d8
bool dwcas16(T, V1, V2)(T* here, const V1 ifThis, const V2 writeThis)
{
asm
{
/*
mov RAX, ifThis; // copy ifThis[0] to RAX
mov RDX, 8[ifThis]; // copy ifThis[1] to RDX
mov RBX, writeThis; // copy writeThis[0] to RBX
mov RCX, 8[writeThis]; // copy writeThis[1] to RCX
*/
// slightly faster?
lea RSI, ifThis;
mov RAX, [RSI];
mov RDX, 8[RSI];
lea RSI, writeThis;
mov RBX, [RSI];
mov RCX, 8[RSI];
mov RSI, here;
lock; // lock always needed to make this op atomic
cmpxchg16b [RSI];
setz AL;
}
}