6

How would you achieve 128-bit atomic operations in x86?

Intel's System Programming Guide, Part 1, 8.1 Locked Atomic Operations specifies guaranteed 16-, 32-, and 64-bit atomic operations. So, can you achieve 128-bit atomic operations by doing 2 64-bit ops with the LOCK prefix? Something like...

LOCK mov 64bits->addr
LOCK mov 64bits->addr+64bits

Aparently SSE has 128-bit XMM registers. Can you just do 128-bit compare-and-swap using these registers?

2
  • 10
    You can do an atomic 16-byte compare-exchange (CMPXCHG16B). Nov 4, 2010 at 16:47
  • Are you trying to do this in 32-bit x86, or in x64?
    – user200783
    Nov 4, 2010 at 19:56

1 Answer 1

11

The LOCK Prefix can not be used in combination with MOV instruction.

The LOCK prefix can be prepended only to the following instructions and only to those forms of the instructions where the destination operand is a memory operand: ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD, and XCHG. Intel Instruction Set Reference

Doing so will generate an Invalid Opcode Exception. So LOCK CMPXCHG16B is the only way here.

2
  • Concerning Intel Instruction Set Reference a 'LOCK' is required. "This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s bus, the destination operand receives a write cycle without regard to the result of the comparison."
    – bkausbk
    Jul 14, 2011 at 7:28
  • @brooksbp: there are use-cases for cmpxchg without lock, e.g. uniprocessor systems for use on memory other than an MMIO port. That also makes it consistent with memory-destination add and other instructions that do have obvious uses other than concurrency. Nov 27, 2017 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.