1  /* Test we're able use __atomic_fetch_* where possible and verify
       2     we generate correct code.  */
       3  /* { dg-do run } */
       4  /* { dg-options "-std=c11 -pedantic-errors -fdump-tree-original" } */
       5  /* { dg-xfail-run-if "PR97444: stack atomics" { nvptx*-*-* } }*/
       6  
       7  #include <stdatomic.h>
       8  
       9  extern void abort (void);
      10  
      11  static void
      12  test_inc_dec (void)
      13  {
      14    atomic_int i = ATOMIC_VAR_INIT (1);
      15  
      16    i++;
      17    if (i != 2)
      18      abort ();
      19    i--;
      20    if (i != 1)
      21      abort ();
      22    ++i;
      23    if (i != 2)
      24      abort ();
      25    --i;
      26    if (i != 1)
      27      abort ();
      28    if (++i != 2)
      29      abort ();
      30    if (i++ != 2)
      31      abort ();
      32    if (i != 3)
      33      abort ();
      34    if (i-- != 3)
      35      abort ();
      36    if (i != 2)
      37      abort ();
      38  }
      39  
      40  static void
      41  test_add_sub (void)
      42  {
      43    atomic_int i = ATOMIC_VAR_INIT (1);
      44  
      45    i += 2;
      46    if (i != 3)
      47      abort ();
      48    i -= 2;
      49    if (i != 1)
      50      abort ();
      51    if ((i += 2) != 3)
      52      abort ();
      53    if ((i -= 2) != 1)
      54      abort ();
      55  }
      56  
      57  static void
      58  test_and (void)
      59  {
      60    atomic_int i = ATOMIC_VAR_INIT (5);
      61  
      62    i &= 4;
      63    if (i != 4)
      64      abort ();
      65    if ((i &= 4) != 4)
      66      abort ();
      67  }
      68  
      69  static void
      70  test_xor (void)
      71  {
      72    atomic_int i = ATOMIC_VAR_INIT (5);
      73  
      74    i ^= 2;
      75    if (i != 7)
      76      abort ();
      77    if ((i ^= 4) != 3)
      78      abort ();
      79  }
      80  
      81  static void
      82  test_or (void)
      83  {
      84    atomic_int i = ATOMIC_VAR_INIT (5);
      85  
      86    i |= 2;
      87    if (i != 7)
      88      abort ();
      89    if ((i |= 8) != 15)
      90      abort ();
      91  }
      92  
      93  static void
      94  test_ptr (atomic_int *p)
      95  {
      96    ++*p;
      97    if (*p != 2)
      98      abort ();
      99  
     100    *p += 2;
     101    if (*p != 4)
     102      abort ();
     103  
     104    (*p)++;
     105    if (*p != 5)
     106      abort ();
     107  
     108    --*p;
     109    if (*p != 4)
     110      abort ();
     111  
     112    (*p)--;
     113    if (*p != 3)
     114      abort ();
     115  
     116    *p -= 2;
     117    if (*p != 1)
     118      abort ();
     119  
     120    atomic_int j = ATOMIC_VAR_INIT (0);
     121    j += *p;
     122    if (j != 1)
     123      abort ();
     124  
     125    j -= *p;
     126    if (j != 0)
     127      abort ();
     128  }
     129  
     130  int
     131  main (void)
     132  {
     133    atomic_int i = ATOMIC_VAR_INIT (1);
     134    test_inc_dec ();
     135    test_add_sub ();
     136    test_and ();
     137    test_xor ();
     138    test_or ();
     139    test_ptr (&i);
     140  }
     141  
     142  /* { dg-final { scan-tree-dump-not "__atomic_compare_exchange" "original" } } */