Sign ignored value in _InterlockedCompareExchange documentation

Documentation for _InterlockedCompareExchangespeaks for each parameter

The sign is ignored.

Whether it means that the numbers, such as 0xffffand 0x7fff(16-bit), will be considered equal _InterlockedCompareExchange16, etc. other internal width properties? Or does this mean that intrinsics accept both signed and unsigned integers? Or something else?

If this is not a mistake in the documentation, it seems at least ambiguous.

+4
source share
2 answers

The sign bit is not ignored; it is compared in the same way as other bits.

..CompareExchange.. - . x86 CMPXCHG/CMPXCHG8B, CPU . , .

Windows API, . 32- LONG. 32 :

__declspec(noinline) void WINAPI Number(LONG val)
{
    printf("Number: %5d %#.8x (%d bit)\n", val, val, sizeof(void*) * 8); 
}
__declspec(noinline) INT16 WINAPI GetI16(INT16 num)
{
    return num;
}

...

Number(0xffff); // Not sign extended
const INT16 numi16 = -42;
Number(numi16); // Optimized to 32-bit parameter by the compiler
Number(GetI16(-42)); // Use a helper function to prevent compiler tricks

:

Number: 65535 0x0000ffff (64 bit)
Number:   -42 0xffffffd6 (64 bit)
Number:   -42 0xffffffd6 (64 bit)

32- x86:

; 1040 :    Number(0xffff);

  00022 68 ff ff 00 00  push     65535          ; 0000ffffH
  00027 e8 00 00 00 00  call     ?Number@@YGXJ@Z        ; Number

; 1041 :    const INT16 numi16 = -42;
; 1042 :    Number(numi16);

 0002c  6a d6           push     -42            ; ffffffd6H
 0002e  e8 00 00 00 00  call     ?Number@@YGXJ@Z        ; Number
; 1047 :    Number(GetI16(-42));

 00033  6a d6           push     -42            ; ffffffd6H
 00035  e8 00 00 00 00  call     ?GetI16@@YGFF@Z        ; GetI16
 0003a  0f bf c0        movsx  eax, ax
 0003d  50              push     eax
 0003e  e8 00 00 00 00  call     ?Number@@YGXJ@Z        ; Number

64- x86_64/AMD64:

; 1040 :    Number(0xffff);

  00027 b9 ff ff 00 00  mov  ecx, 65535     ; 0000ffffH
  0002c e8 00 00 00 00  call     ?Number@@YAXJ@Z        ; Number

; 1041 :    const INT16 numi16 = -42;
; 1042 :    Number(numi16);

  00031 b9 d6 ff ff ff  mov  ecx, -42       ; ffffffffffffffd6H
  00036 e8 00 00 00 00  call     ?Number@@YAXJ@Z        ; Number

; 1047 :    Number(GetI16(-42));

  0003b 66 b9 d6 ff     mov  cx, -42        ; ffffffffffffffd6H
  0003f e8 00 00 00 00  call     ?GetI16@@YAFF@Z        ; GetI16
  00044 0f bf c8        movsx    ecx, ax
  00047 e8 00 00 00 00  call     ?Number@@YAXJ@Z        ; Number

, MOVSX , 16- . Windows ABI.

#pragma intrinsic(_InterlockedCompareExchange), . ABI , , Windows ABI, . LOCK CMPXCHG , MOVSX.

+2

the _InterlockedCompareExchange , CMPXCHG. The sign is ignored , 2 equal - , - . > <, =. 0xffff, , 0x7fff

0

Source: https://habr.com/ru/post/1685142/


All Articles