6

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.

Marinos An
  • 9,481
  • 6
  • 63
  • 96
Adisak
  • 6,708
  • 38
  • 46

2 Answers2

3

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.

harrymc
  • 1,069
  • 8
  • 33
  • At the time I was using 2010 which doesn't have this intrinsic. I'm using 2012 now though so all is better :-) – Adisak Feb 14 '14 at 01:05
3

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
Bo Persson
  • 90,663
  • 31
  • 146
  • 203