Advertisement
Guest User

__uatomic_add_return

a guest
Jan 15th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. static inline __attribute__((always_inline))
  2. unsigned long __uatomic_add_return(void *addr, unsigned long val,
  3.                                  int len)
  4. {
  5.         switch (len) {
  6.         case 1:
  7.         {
  8.                 unsigned char result = val;
  9.  
  10.                 __asm__ __volatile__(
  11.                 "lock; xaddb %1, %0"
  12.                         : "+m"(*__hp(addr)), "+q" (result)
  13.                         :
  14.                         : "memory");
  15.                 return result + (unsigned char)val;
  16.         }
  17.         case 2:
  18.         {
  19.                 unsigned short result = val;
  20.  
  21.                 __asm__ __volatile__(
  22.                 "lock; xaddw %1, %0"
  23.                         : "+m"(*__hp(addr)), "+r" (result)
  24.                         :
  25.                         : "memory");
  26.                 return result + (unsigned short)val;
  27.         }
  28.         case 4:
  29.         {
  30.                 unsigned int result = val;
  31.  
  32.                 __asm__ __volatile__(
  33.                 "lock; xaddl %1, %0"
  34.                         : "+m"(*__hp(addr)), "+r" (result)
  35.                         :
  36.                         : "memory");
  37.                 return result + (unsigned int)val;
  38.         }
  39. #if (CAA_BITS_PER_LONG == 64)
  40.         case 8:
  41.         {
  42.                 unsigned long result = val;
  43.  
  44.                 __asm__ __volatile__(
  45.                 "lock; xaddq %1, %0"
  46.                         : "+m"(*__hp(addr)), "+r" (result)
  47.                         :
  48.                         : "memory");
  49.                 return result + (unsigned long)val;
  50.         }
  51. #endif
  52.         }
  53.         /*
  54.          * generate an illegal instruction. Cannot catch this with
  55.          * linker tricks when optimizations are disabled.
  56.          */
  57.         __asm__ __volatile__("ud2");
  58.         return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement