Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an 8-bit atomic CAS (cmpxchg) intrinsic for X64 in Visual C++?

The following code is possible in 32-bit Visual Studio C++. Is there a 64-bit equivalent using intrinsics since inline ASM isn't supported in the 64-bit version of Visual Studio C++?

FORCEINLINE bool bAtomicCAS8(volatile UINT8 *dest, UINT8 oldval, UINT8 newval)
{
    bool result=false;
    __asm
    {
        mov     al,oldval
        mov     edx,dest
        mov     cl,newval
        lock cmpxchg    byte ptr [edx],cl
        setz    result
    }
    return(result);
}

The following instrinsics compile under Visual Studio C++

_InterlockedCompareExchange16
_InterlockedCompareExchange
_InterlockedCompareExchange64
_InterlockedCompareExchange128

What I am looking for is something along the lines of

_InterlockedCompareExchange8

But that doesn't seem to exist.

like image 274
Adisak Avatar asked Apr 26 '11 21:04

Adisak


2 Answers

No, that doesn't exist. You can implement it out-of-line though, if needed.

atomic_msvc_x64.asm

_text   SEGMENT

; char _InterlockedCompareExchange8(volatile char*, char NewValue, char Expected) 
;      - RCX, RDX, R8

_InterlockedCompareExchange8  PROC

    mov    eax,r8d
    lock cmpxchg [rcx],dl
    ret

_InterlockedCompareExchange8  ENDP

_text  ENDS

       END
like image 150
Bo Persson Avatar answered Nov 20 '22 21:11

Bo Persson


Verified for Visual Studio 2012 that this intrinsic does exist :

char _InterlockedCompareExchange8(volatile char*, char NewValue, char Expected)

However, it appears nowhere in the documentation.

like image 22
harrymc Avatar answered Nov 20 '22 19:11

harrymc