1570af302Sopenharmony_ci#define a_ll a_ll 2570af302Sopenharmony_cistatic inline int a_ll(volatile int *p) 3570af302Sopenharmony_ci{ 4570af302Sopenharmony_ci int v; 5570af302Sopenharmony_ci __asm__ __volatile__ ("ldaxr %w0,%1" : "=r"(v) : "Q"(*p)); 6570af302Sopenharmony_ci return v; 7570af302Sopenharmony_ci} 8570af302Sopenharmony_ci 9570af302Sopenharmony_ci#define a_ldar a_ldar 10570af302Sopenharmony_cistatic inline int a_ldar(volatile int *p) 11570af302Sopenharmony_ci{ 12570af302Sopenharmony_ci int v; 13570af302Sopenharmony_ci __asm__ __volatile__ ("ldar %w0,%1" : "=r"(v) : "Q"(*p)); 14570af302Sopenharmony_ci return v; 15570af302Sopenharmony_ci} 16570af302Sopenharmony_ci 17570af302Sopenharmony_ci#define a_sc a_sc 18570af302Sopenharmony_cistatic inline int a_sc(volatile int *p, int v) 19570af302Sopenharmony_ci{ 20570af302Sopenharmony_ci int r; 21570af302Sopenharmony_ci __asm__ __volatile__ ("stlxr %w0,%w2,%1" : "=&r"(r), "=Q"(*p) : "r"(v) : "memory"); 22570af302Sopenharmony_ci return !r; 23570af302Sopenharmony_ci} 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci#define a_barrier a_barrier 26570af302Sopenharmony_cistatic inline void a_barrier() 27570af302Sopenharmony_ci{ 28570af302Sopenharmony_ci __asm__ __volatile__ ("dmb ish" : : : "memory"); 29570af302Sopenharmony_ci} 30570af302Sopenharmony_ci 31570af302Sopenharmony_ci#define a_cas a_cas 32570af302Sopenharmony_cistatic inline int a_cas(volatile int *p, int t, int s) 33570af302Sopenharmony_ci{ 34570af302Sopenharmony_ci int old; 35570af302Sopenharmony_ci do { 36570af302Sopenharmony_ci old = a_ll(p); 37570af302Sopenharmony_ci if (old != t) { 38570af302Sopenharmony_ci a_barrier(); 39570af302Sopenharmony_ci break; 40570af302Sopenharmony_ci } 41570af302Sopenharmony_ci } while (!a_sc(p, s)); 42570af302Sopenharmony_ci return old; 43570af302Sopenharmony_ci} 44570af302Sopenharmony_ci 45570af302Sopenharmony_ci#define a_ll_p a_ll_p 46570af302Sopenharmony_cistatic inline void *a_ll_p(volatile void *p) 47570af302Sopenharmony_ci{ 48570af302Sopenharmony_ci void *v; 49570af302Sopenharmony_ci __asm__ __volatile__ ("ldaxr %0, %1" : "=r"(v) : "Q"(*(void *volatile *)p)); 50570af302Sopenharmony_ci return v; 51570af302Sopenharmony_ci} 52570af302Sopenharmony_ci 53570af302Sopenharmony_ci#define a_sc_p a_sc_p 54570af302Sopenharmony_cistatic inline int a_sc_p(volatile int *p, void *v) 55570af302Sopenharmony_ci{ 56570af302Sopenharmony_ci int r; 57570af302Sopenharmony_ci __asm__ __volatile__ ("stlxr %w0,%2,%1" : "=&r"(r), "=Q"(*(void *volatile *)p) : "r"(v) : "memory"); 58570af302Sopenharmony_ci return !r; 59570af302Sopenharmony_ci} 60570af302Sopenharmony_ci 61570af302Sopenharmony_ci#define a_cas_p a_cas_p 62570af302Sopenharmony_cistatic inline void *a_cas_p(volatile void *p, void *t, void *s) 63570af302Sopenharmony_ci{ 64570af302Sopenharmony_ci void *old; 65570af302Sopenharmony_ci do { 66570af302Sopenharmony_ci old = a_ll_p(p); 67570af302Sopenharmony_ci if (old != t) { 68570af302Sopenharmony_ci a_barrier(); 69570af302Sopenharmony_ci break; 70570af302Sopenharmony_ci } 71570af302Sopenharmony_ci } while (!a_sc_p(p, s)); 72570af302Sopenharmony_ci return old; 73570af302Sopenharmony_ci} 74570af302Sopenharmony_ci 75570af302Sopenharmony_ci#define a_ctz_64 a_ctz_64 76570af302Sopenharmony_cistatic inline int a_ctz_64(uint64_t x) 77570af302Sopenharmony_ci{ 78570af302Sopenharmony_ci __asm__( 79570af302Sopenharmony_ci " rbit %0, %1\n" 80570af302Sopenharmony_ci " clz %0, %0\n" 81570af302Sopenharmony_ci : "=r"(x) : "r"(x)); 82570af302Sopenharmony_ci return x; 83570af302Sopenharmony_ci} 84570af302Sopenharmony_ci 85570af302Sopenharmony_ci#define a_clz_64 a_clz_64 86570af302Sopenharmony_cistatic inline int a_clz_64(uint64_t x) 87570af302Sopenharmony_ci{ 88570af302Sopenharmony_ci __asm__("clz %0, %1" : "=r"(x) : "r"(x)); 89570af302Sopenharmony_ci return x; 90570af302Sopenharmony_ci} 91