Reputation: 3598
1 #ifndef ATOMIC_UTILS_H
2 #define ATOMIC_UTILS_H
3
4 #include<cstddef>
5
6 class AtomicUtils
7 {
8 public:
9
10 /**
11 * check if the value at addr is equal to oldval, if so replace it with newval
12 * and return the oldval
13 */
14 static size_t compareAndExchange( volatile size_t* addr, size_t oldval, size_t newval )
15 {
16 size_t ret;
17 __asm__ volatile( "lock cmpxchgl %2, %1\n\t"
18 :"=a"(ret), "+m"(*addr)
19 : "r"(newval), "0"(oldval)
20 : "memory" );
21 return ret;
22 }
23 };
24
25 #endif
In file included from atomics_test.cpp:1: ./atomics.hpp:17:25: error: invalid operand for instruction __asm__ volatile( "lock cmpxchgl %2, %1\n\t" ^ :1:16: note: instantiated into assembly here lock cmpxchgl %rsi, (%rdx) ^~~~~
I'm totally clueless of the error. Could someone help plz ?? I'm using a 64 bit mac book pro with clang.
Upvotes: 3
Views: 1796
Reputation: 8928
The error is that you are using cmpxchgl with 64-bit values (cmpxchgl wants 32-bit values). Try cmpxchgq for 64-bit.
Upvotes: 3