1 /* atomic.c -- Builtins for HSAIL atomic instructions for which
2 there is no feasible direct gcc GENERIC expression.
4 Copyright (C) 2015-2021 Free Software Foundation, Inc.
5 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
6 for General Processor Tech.
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files
10 (the "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #define DO_ATOMICALLY(T, OPERATION) \
38 new_value = OPERATION; \
39 done = __sync_bool_compare_and_swap (ptr, old_value, new_value); \
44 __hsail_atomic_min_s32 (int32_t *ptr, int32_t a)
46 DO_ATOMICALLY (int32_t, (old_value < a) ? old_value : a);
50 __hsail_atomic_min_s64 (int64_t *ptr, int64_t a)
52 DO_ATOMICALLY (int64_t, (old_value < a) ? old_value : a);
56 __hsail_atomic_min_u32 (uint32_t *ptr, uint32_t a)
58 DO_ATOMICALLY (uint32_t, (old_value < a) ? old_value : a);
62 __hsail_atomic_min_u64 (uint64_t *ptr, uint64_t a)
64 DO_ATOMICALLY (uint64_t, (old_value < a) ? old_value : a);
68 __hsail_atomic_max_u32 (uint32_t *ptr, uint32_t a)
70 DO_ATOMICALLY (uint32_t, (old_value > a) ? old_value : a);
74 __hsail_atomic_max_s32 (int32_t *ptr, int32_t a)
76 DO_ATOMICALLY (int32_t, (old_value > a) ? old_value : a);
80 __hsail_atomic_max_u64 (uint64_t *ptr, uint64_t a)
82 DO_ATOMICALLY (uint64_t, (old_value > a) ? old_value : a);
86 __hsail_atomic_max_s64 (int64_t *ptr, int64_t a)
88 DO_ATOMICALLY (int64_t, (old_value > a) ? old_value : a);
92 __hsail_atomic_wrapinc_u32 (uint32_t *ptr, uint32_t a)
94 DO_ATOMICALLY (uint32_t, (old_value >= a) ? 0 : (old_value + 1));
98 __hsail_atomic_wrapinc_u64 (uint64_t *ptr, uint64_t a)
100 DO_ATOMICALLY (uint64_t, (old_value >= a) ? 0 : (old_value + 1));
104 __hsail_atomic_wrapdec_u32 (uint32_t *ptr, uint32_t a)
106 DO_ATOMICALLY (uint32_t,
107 ((old_value == 0) || (old_value > a)) ? a : (old_value - 1));
111 __hsail_atomic_wrapdec_u64 (uint64_t *ptr, uint64_t a)
113 DO_ATOMICALLY (uint64_t,
114 ((old_value == 0) || (old_value > a)) ? a : (old_value - 1));