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