atomic_arch.h raw

   1  #define a_cas a_cas
   2  static inline int a_cas(volatile int *p, int t, int s)
   3  {
   4  	__asm__ __volatile__ (
   5  		"lock ; cmpxchg %3, %1"
   6  		: "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" );
   7  	return t;
   8  }
   9  
  10  #define a_swap a_swap
  11  static inline int a_swap(volatile int *p, int v)
  12  {
  13  	__asm__ __volatile__(
  14  		"xchg %0, %1"
  15  		: "=r"(v), "=m"(*p) : "0"(v) : "memory" );
  16  	return v;
  17  }
  18  
  19  #define a_fetch_add a_fetch_add
  20  static inline int a_fetch_add(volatile int *p, int v)
  21  {
  22  	__asm__ __volatile__(
  23  		"lock ; xadd %0, %1"
  24  		: "=r"(v), "=m"(*p) : "0"(v) : "memory" );
  25  	return v;
  26  }
  27  
  28  #define a_and a_and
  29  static inline void a_and(volatile int *p, int v)
  30  {
  31  	__asm__ __volatile__(
  32  		"lock ; and %1, %0"
  33  		: "=m"(*p) : "r"(v) : "memory" );
  34  }
  35  
  36  #define a_or a_or
  37  static inline void a_or(volatile int *p, int v)
  38  {
  39  	__asm__ __volatile__(
  40  		"lock ; or %1, %0"
  41  		: "=m"(*p) : "r"(v) : "memory" );
  42  }
  43  
  44  #define a_inc a_inc
  45  static inline void a_inc(volatile int *p)
  46  {
  47  	__asm__ __volatile__(
  48  		"lock ; incl %0"
  49  		: "=m"(*p) : "m"(*p) : "memory" );
  50  }
  51  
  52  #define a_dec a_dec
  53  static inline void a_dec(volatile int *p)
  54  {
  55  	__asm__ __volatile__(
  56  		"lock ; decl %0"
  57  		: "=m"(*p) : "m"(*p) : "memory" );
  58  }
  59  
  60  #define a_store a_store
  61  static inline void a_store(volatile int *p, int x)
  62  {
  63  	__asm__ __volatile__(
  64  		"mov %1, %0 ; lock ; orl $0,(%%esp)"
  65  		: "=m"(*p) : "r"(x) : "memory" );
  66  }
  67  
  68  #define a_barrier a_barrier
  69  static inline void a_barrier()
  70  {
  71  	__asm__ __volatile__( "" : : : "memory" );
  72  }
  73  
  74  #define a_spin a_spin
  75  static inline void a_spin()
  76  {
  77  	__asm__ __volatile__( "pause" : : : "memory" );
  78  }
  79  
  80  #define a_crash a_crash
  81  static inline void a_crash()
  82  {
  83  	__asm__ __volatile__( "hlt" : : : "memory" );
  84  }
  85  
  86  #define a_ctz_64 a_ctz_64
  87  static inline int a_ctz_64(uint64_t x)
  88  {
  89  	int r;
  90  	__asm__( "bsf %1,%0 ; jnz 1f ; bsf %2,%0 ; add $32,%0\n1:"
  91  		: "=&r"(r) : "r"((unsigned)x), "r"((unsigned)(x>>32)) );
  92  	return r;
  93  }
  94  
  95  #define a_ctz_32 a_ctz_32
  96  static inline int a_ctz_32(uint32_t x)
  97  {
  98  	int r;
  99  	__asm__( "bsf %1,%0" : "=r"(r) : "r"(x) );
 100  	return r;
 101  }
 102  
 103  #define a_clz_32 a_clz_32
 104  static inline int a_clz_32(uint32_t x)
 105  {
 106  	__asm__( "bsr %1,%0 ; xor $31,%0" : "=r"(x) : "r"(x) );
 107  	return x;
 108  }
 109