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

Deadly 提交于 2019-12-21 04:49:28

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5796459/is-there-an-8-bit-atomic-cas-cmpxchg-intrinsic-for-x64-in-visual-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!