(root)/
gcc-13.2.0/
libatomic/
testsuite/
libatomic.c/
atomic-compare-exchange-2.c
       1  /* Test __atomic routines for existence and proper execution on 2 byte 
       2     values with each valid memory model.  */
       3  /* { dg-do run } */
       4  
       5  /* Test the execution of the __atomic_compare_exchange_n builtin for a short.  */
       6  
       7  extern void abort(void);
       8  
       9  short v = 0;
      10  short expected = 0;
      11  short max = ~0;
      12  short desired = ~0;
      13  short zero = 0;
      14  
      15  #define STRONG 0
      16  #define WEAK 1
      17  
      18  int
      19  main ()
      20  {
      21  
      22    if (!__atomic_compare_exchange_n (&v, &expected, max, STRONG , __ATOMIC_RELAXED, __ATOMIC_RELAXED)) 
      23      abort ();
      24    if (expected != 0)
      25      abort ();
      26  
      27    if (__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) 
      28      abort ();
      29    if (expected != max)
      30      abort ();
      31  
      32    if (!__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE)) 
      33      abort ();
      34    if (expected != max)
      35      abort ();
      36    if (v != 0)
      37      abort ();
      38  
      39    if (__atomic_compare_exchange_n (&v, &expected, desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) 
      40      abort ();
      41    if (expected != 0)
      42      abort ();
      43  
      44    if (!__atomic_compare_exchange_n (&v, &expected, desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) 
      45      abort ();
      46    if (expected != 0)
      47      abort ();
      48    if (v != max)
      49      abort ();
      50  
      51    /* Now test the generic version.  */
      52  
      53    v = 0;
      54  
      55    if (!__atomic_compare_exchange (&v, &expected, &max, STRONG, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
      56      abort ();
      57    if (expected != 0)
      58      abort ();
      59  
      60    if (__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) 
      61      abort ();
      62    if (expected != max)
      63      abort ();
      64  
      65    if (!__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE)) 
      66      abort ();
      67    if (expected != max)
      68      abort ();
      69    if (v != 0)
      70      abort ();
      71  
      72    if (__atomic_compare_exchange (&v, &expected, &desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) 
      73      abort ();
      74    if (expected != 0)
      75      abort ();
      76  
      77    if (!__atomic_compare_exchange (&v, &expected, &desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) 
      78      abort ();
      79    if (expected != 0)
      80      abort ();
      81    if (v != max)
      82      abort ();
      83  
      84    return 0;
      85  }