Visual C++ 中是否有 X64 固有的 8 位原子 CAS (cmpxchg)? [英] Is there an 8-bit atomic CAS (cmpxchg) intrinsic for X64 in Visual C++?

查看:24
本文介绍了Visual C++ 中是否有 X64 固有的 8 位原子 CAS (cmpxchg)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在 32 位 Visual Studio C++ 中是可能的.由于 64 位版本的 Visual Studio C++ 不支持内联 ASM,因此是否有使用内在函数的 64 位等效项?

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

但这似乎并不存在.

推荐答案

不,那不存在.不过,如果需要,您可以在外实现它.

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

这篇关于Visual C++ 中是否有 X64 固有的 8 位原子 CAS (cmpxchg)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆