Это не совсем верно, хотя я не уверен, почему. Совет будет велик, как документация CMPXCHG16B довольно минимальна (я не имею никаких руководств Intel ...)CMPXCHG16B правильный?
template<>
inline bool cas(volatile types::uint128_t *src, types::uint128_t cmp, types::uint128_t with)
{
/*
Description:
The CMPXCHG16B instruction compares the 128-bit value in the RDX:RAX and RCX:RBX registers
with a 128-bit memory location. If the values are equal, the zero flag (ZF) is set,
and the RCX:RBX value is copied to the memory location.
Otherwise, the ZF flag is cleared, and the memory value is copied to RDX:RAX.
*/
uint64_t * cmpP = (uint64_t*)&cmp;
uint64_t * withP = (uint64_t*)&with;
unsigned char result = 0;
__asm__ __volatile__ (
"LOCK; CMPXCHG16B %1\n\t"
"SETZ %b0\n\t"
: "=q"(result) /* output */
: "m"(*src), /* input */
//what to compare against
"rax"(((uint64_t) (cmpP[1]))), //lower bits
"rdx"(((uint64_t) (cmpP[0]))),//upper bits
//what to replace it with if it was equal
"rbx"(((uint64_t) (withP[1]))), //lower bits
"rcx"(((uint64_t) (withP[0])))//upper bits
: "memory", "cc", "rax", "rdx", "rbx","rcx" /* clobbered items */
);
return result;
}
При работе с примером я получаю 0, когда оно должно быть 1. Любых идей ?
Спасибо, кучка, имеет смысл. –
Я скопировал и вставил ваш код и при компиляции с помощью «g ++ - 4.7 -g -DDEBUG = 1 -std = C++ 0x -pthread dwcas.c -o dwcas.o -ldl -lpthread» Я получаю dwcas.c : 29: Ошибка: мусор 'ptr 'после выражения. любые идеи почему? –
Это должно быть просто 'lock cmpxchg16b% 1'. Размер в этом случае не нужен, поскольку это подразумевается инструкцией 'cmpxchg16b'. Использование 'oword ptr' подсказывает мне, что вы думали, что это ассемблер _MASM_, который не собирается с помощью сборщика GNU. –