ref: 82dc85253c9ab0f7b9925c69e4b065ca4347ab04
dir: /posix-sun4u/cas.c/
#include "u.h"
#include "libc.h"
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
int
cas(int *x, int old, int new)
{
#if __has_builtin(__atomic_compare_exchange_n)
return __atomic_compare_exchange_n(x, &old, new, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
#else
int result;
__asm__ __volatile__(
"casa [%2] %%asi, %3, %1\n"
"cmp %3, %1\n"
"mov %4, %0\n"
"movne %4, 0, %0"
: "=&r" (result), "+r" (new), "+m" (*x)
: "r" (x), "r" (old)
: "cc");
return result;
#endif
}