Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. uint16_t AtomicExchange(uint16_t *a, uint16_t new_val) {
  2.   // InterlockedExchange16 seems unavailable on some MSVS installations.
  3.   // Everybody stand back, I pretend to know inline assembly!
  4.   // FIXME: I assume VC is smart enough to save/restore eax/ecx?
  5. #ifdef _MSC_VER
  6.   __asm {
  7.     mov eax, a
  8.     mov cx, new_val
  9.     xchg [eax], cx  ; NOLINT
  10.     mov new_val, cx
  11.   }
  12. #else
  13.   __asm__("xchgw %[new_val], %[a]" : [new_val] "+r" (new_val) : [a] "m" (*a) : "memory");
  14. #endif
  15.   return new_val;
  16. }
Add Comment
Please, Sign In to add comment