Dejwi
Dejwi

Reputation: 4487

inline assembler increment 64bit counter

I'd like to increment the 64bit(long type in C++) counter in inline assembly atomically. I know how to do that on 32bit value(int):

asm volatile("lock; incl %0" : "=m" (val) : "m"(val));

But I have no idea how to perform that on long value.

Upvotes: 1

Views: 1432

Answers (2)

Timothy Baldwin
Timothy Baldwin

Reputation: 3675

That should be:

asm volatile("lock; incq %0" : "+m" (val));

Specifying separate operands without constraints that force input into the same location as the output could result in code such as:

val = something;
asm volatile("lock; incq %0" : "=m" (val) : "m"(val));

being optimised wrongly. You may also need a memory clobber to prevent accesses to other variables being moved past the asm.

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92261

moved self answer from the question to an answer

It was quite easy, but I haven't been familiar with x86-64.

asm volatile("lock; incq %0" : "=m" (val) : "m"(val));

Upvotes: 2

Related Questions