#define __xchg_op(x, ptr) \
({ \
\
__typeof(*(ptr)) __ret = (x); \
switch (sizeof(*(ptr))) { \
\
case 1: \
asm volatile("xchgb %b0,%1\n" \
: "+q" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
case 2: \
asm volatile("xchgw %w0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
:: "memory", "cc"); \
break; \
case 4: \
asm volatile("xchgl %k0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
case 8: \
asm volatile("xchgq %0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
} \
__ret; \
({ \
\
__typeof(*(ptr)) __ret = (x); \
switch (sizeof(*(ptr))) { \
\
case 1: \
asm volatile("xchgb %b0,%1\n" \
: "+q" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
case 2: \
asm volatile("xchgw %w0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
:: "memory", "cc"); \
break; \
case 4: \
asm volatile("xchgl %k0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
case 8: \
asm volatile("xchgq %0,%1\n" \
: "+r" (__ret), "+m" (*(ptr)) \
::"memory", "cc"); \
break; \
} \
__ret; \
})
#define xchg(ptr,v) __xchg_op((v), (ptr))