是否有一个8位的原子CAS(CMPXCHG)内在的X64在Visual C ++?原子、有一个、CAS、Visual

2023-09-08 00:47:47 作者:逃避

下面code是有可能在32位的Visual Studio C ++。是否有一个64位等效采用内部函数,因为内联汇编不支持64位版本的Visual Studio的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);
}

下面instrinsics下的Visual Studio C ++

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.

推荐答案

没有,不存在。你可以乱行尽管实现它,如果需要的话。

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

atomic_msvc_x64.asm

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