| OLD | NEW |
| 1 //===- subzero/crosstest/test_sync_atomic.cpp - Implementation for tests --===// | 1 //===- subzero/crosstest/test_sync_atomic.cpp - Implementation for tests --===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This aims to test that all the atomic RMW instructions and compare and swap | 10 // This aims to test that all the atomic RMW instructions and compare and swap |
| 11 // work across the allowed atomic types. This uses the __sync_* builtins | 11 // work across the allowed atomic types. This uses the __sync_* builtins |
| 12 // to test the atomic operations. | 12 // to test the atomic operations. |
| 13 // | 13 // |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 | 15 |
| 16 #include <stdint.h> | 16 #include <stdint.h> |
| 17 | 17 |
| 18 #include <cstdlib> | 18 #include <cstdlib> |
| 19 | 19 |
| 20 #include "test_sync_atomic.h" | 20 #include "test_sync_atomic.h" |
| 21 | 21 |
| 22 #define X(inst, type) \ | 22 #define X(inst, type) \ |
| 23 type test_##inst(bool fetch_first, volatile type *ptr, type a) { \ | 23 type test_##inst(bool fetch_first, volatile type *ptr, type a) { \ |
| 24 if (fetch_first) { \ | 24 if (fetch_first) { \ |
| 25 return __sync_fetch_and_##inst(ptr, a); \ | 25 return __sync_fetch_and_##inst(ptr, a); \ |
| 26 } else { \ | 26 } else { \ |
| 27 return __sync_##inst##_and_fetch(ptr, a); \ | 27 return __sync_##inst##_and_fetch(ptr, a); \ |
| 28 } \ | 28 } \ |
| 29 } \ | 29 } \ |
| 30 type test_alloca_##inst(bool fetch, volatile type *ptr, type a) { \ | 30 type test_alloca_##inst(bool fetch, volatile type *ptr, type a) { \ |
| 31 const size_t buf_size = 8; \ | 31 const size_t buf_size = 8; \ |
| 32 type buf[buf_size]; \ | 32 type buf[buf_size]; \ |
| 33 for (size_t i = 0; i < buf_size; ++i) { \ | 33 for (size_t i = 0; i < buf_size; ++i) { \ |
| 34 if (fetch) { \ | 34 if (fetch) { \ |
| 35 buf[i] = __sync_fetch_and_##inst(ptr, a); \ | 35 buf[i] = __sync_fetch_and_##inst(ptr, a); \ |
| 36 } else { \ | 36 } else { \ |
| 37 buf[i] = __sync_##inst##_and_fetch(ptr, a); \ | 37 buf[i] = __sync_##inst##_and_fetch(ptr, a); \ |
| 38 } \ | 38 } \ |
| 39 } \ | 39 } \ |
| 40 type sum = 0; \ | 40 type sum = 0; \ |
| 41 for (size_t i = 0; i < buf_size; ++i) { \ | 41 for (size_t i = 0; i < buf_size; ++i) { \ |
| 42 sum += buf[i]; \ | 42 sum += buf[i]; \ |
| 43 } \ | 43 } \ |
| 44 return sum; \ | 44 return sum; \ |
| 45 } \ | 45 } \ |
| 46 type test_const_##inst(bool fetch, volatile type *ptr, type ign) { \ | 46 type test_const_##inst(bool fetch, volatile type *ptr, type ign) { \ |
| 47 if (fetch) { \ | 47 if (fetch) { \ |
| 48 return __sync_fetch_and_##inst(ptr, 42); \ | 48 return __sync_fetch_and_##inst(ptr, 42); \ |
| 49 } else { \ | 49 } else { \ |
| 50 return __sync_##inst##_and_fetch(ptr, 99); \ | 50 return __sync_##inst##_and_fetch(ptr, 99); \ |
| 51 } \ | 51 } \ |
| 52 } | 52 } |
| 53 | 53 |
| 54 FOR_ALL_RMWOP_TYPES(X) | 54 FOR_ALL_RMWOP_TYPES(X) |
| 55 #undef X | 55 #undef X |
| 56 | 56 |
| 57 #define X(type) \ | 57 #define X(type) \ |
| 58 type test_val_cmp_swap(volatile type *ptr, type oldval, type newval) { \ | 58 type test_val_cmp_swap(volatile type *ptr, type oldval, type newval) { \ |
| 59 return __sync_val_compare_and_swap(ptr, oldval, newval); \ | 59 return __sync_val_compare_and_swap(ptr, oldval, newval); \ |
| 60 } \ | 60 } \ |
| 61 type test_val_cmp_swap_loop(volatile type *ptr, type oldval, type newval) { \ | 61 type test_val_cmp_swap_loop(volatile type *ptr, type oldval, type newval) { \ |
| 62 type prev; \ | 62 type prev; \ |
| 63 type succeeded_first_try = 1; \ | 63 type succeeded_first_try = 1; \ |
| 64 while (1) { \ | 64 while (1) { \ |
| 65 prev = __sync_val_compare_and_swap(ptr, oldval, newval); \ | 65 prev = __sync_val_compare_and_swap(ptr, oldval, newval); \ |
| 66 if (prev == oldval) \ | 66 if (prev == oldval) \ |
| 67 break; \ | 67 break; \ |
| 68 succeeded_first_try = 0; \ | 68 succeeded_first_try = 0; \ |
| 69 oldval = prev; \ | 69 oldval = prev; \ |
| 70 } \ | 70 } \ |
| 71 return succeeded_first_try; \ | 71 return succeeded_first_try; \ |
| 72 } | 72 } |
| 73 | 73 |
| 74 ATOMIC_TYPE_TABLE | 74 ATOMIC_TYPE_TABLE |
| 75 #undef X | 75 #undef X |
| OLD | NEW |