annotate libhsail-rt/rt/atomics.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* atomic.c -- Builtins for HSAIL atomic instructions for which
kono
parents:
diff changeset
2 there is no feasible direct gcc GENERIC expression.
kono
parents:
diff changeset
3
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
4 Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
5 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
kono
parents:
diff changeset
6 for General Processor Tech.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 Permission is hereby granted, free of charge, to any person obtaining a
kono
parents:
diff changeset
9 copy of this software and associated documentation files
kono
parents:
diff changeset
10 (the "Software"), to deal in the Software without restriction, including
kono
parents:
diff changeset
11 without limitation the rights to use, copy, modify, merge, publish,
kono
parents:
diff changeset
12 distribute, sublicense, and/or sell copies of the Software, and to
kono
parents:
diff changeset
13 permit persons to whom the Software is furnished to do so, subject to
kono
parents:
diff changeset
14 the following conditions:
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 The above copyright notice and this permission notice shall be included
kono
parents:
diff changeset
17 in all copies or substantial portions of the Software.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
kono
parents:
diff changeset
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kono
parents:
diff changeset
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
kono
parents:
diff changeset
22 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kono
parents:
diff changeset
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
kono
parents:
diff changeset
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
kono
parents:
diff changeset
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
kono
parents:
diff changeset
26 */
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #include <stdint.h>
kono
parents:
diff changeset
29 #include <stdio.h>
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #define DO_ATOMICALLY(T, OPERATION) \
kono
parents:
diff changeset
32 int done = 0; \
kono
parents:
diff changeset
33 T old_value; \
kono
parents:
diff changeset
34 T new_value; \
kono
parents:
diff changeset
35 while (!done) \
kono
parents:
diff changeset
36 { \
kono
parents:
diff changeset
37 old_value = *ptr; \
kono
parents:
diff changeset
38 new_value = OPERATION; \
kono
parents:
diff changeset
39 done = __sync_bool_compare_and_swap (ptr, old_value, new_value); \
kono
parents:
diff changeset
40 } \
kono
parents:
diff changeset
41 return old_value
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 int32_t
kono
parents:
diff changeset
44 __hsail_atomic_min_s32 (int32_t *ptr, int32_t a)
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 DO_ATOMICALLY (int32_t, (old_value < a) ? old_value : a);
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 int64_t
kono
parents:
diff changeset
50 __hsail_atomic_min_s64 (int64_t *ptr, int64_t a)
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 DO_ATOMICALLY (int64_t, (old_value < a) ? old_value : a);
kono
parents:
diff changeset
53 }
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 uint32_t
kono
parents:
diff changeset
56 __hsail_atomic_min_u32 (uint32_t *ptr, uint32_t a)
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 DO_ATOMICALLY (uint32_t, (old_value < a) ? old_value : a);
kono
parents:
diff changeset
59 }
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 uint64_t
kono
parents:
diff changeset
62 __hsail_atomic_min_u64 (uint64_t *ptr, uint64_t a)
kono
parents:
diff changeset
63 {
kono
parents:
diff changeset
64 DO_ATOMICALLY (uint64_t, (old_value < a) ? old_value : a);
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 uint32_t
kono
parents:
diff changeset
68 __hsail_atomic_max_u32 (uint32_t *ptr, uint32_t a)
kono
parents:
diff changeset
69 {
kono
parents:
diff changeset
70 DO_ATOMICALLY (uint32_t, (old_value > a) ? old_value : a);
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 int32_t
kono
parents:
diff changeset
74 __hsail_atomic_max_s32 (int32_t *ptr, int32_t a)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 DO_ATOMICALLY (int32_t, (old_value > a) ? old_value : a);
kono
parents:
diff changeset
77 }
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 uint64_t
kono
parents:
diff changeset
80 __hsail_atomic_max_u64 (uint64_t *ptr, uint64_t a)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 DO_ATOMICALLY (uint64_t, (old_value > a) ? old_value : a);
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 int64_t
kono
parents:
diff changeset
86 __hsail_atomic_max_s64 (int64_t *ptr, int64_t a)
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 DO_ATOMICALLY (int64_t, (old_value > a) ? old_value : a);
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 uint32_t
kono
parents:
diff changeset
92 __hsail_atomic_wrapinc_u32 (uint32_t *ptr, uint32_t a)
kono
parents:
diff changeset
93 {
kono
parents:
diff changeset
94 DO_ATOMICALLY (uint32_t, (old_value >= a) ? 0 : (old_value + 1));
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 uint64_t
kono
parents:
diff changeset
98 __hsail_atomic_wrapinc_u64 (uint64_t *ptr, uint64_t a)
kono
parents:
diff changeset
99 {
kono
parents:
diff changeset
100 DO_ATOMICALLY (uint64_t, (old_value >= a) ? 0 : (old_value + 1));
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 uint32_t
kono
parents:
diff changeset
104 __hsail_atomic_wrapdec_u32 (uint32_t *ptr, uint32_t a)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 DO_ATOMICALLY (uint32_t,
kono
parents:
diff changeset
107 ((old_value == 0) || (old_value > a)) ? a : (old_value - 1));
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 uint64_t
kono
parents:
diff changeset
111 __hsail_atomic_wrapdec_u64 (uint64_t *ptr, uint64_t a)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 DO_ATOMICALLY (uint64_t,
kono
parents:
diff changeset
114 ((old_value == 0) || (old_value > a)) ? a : (old_value - 1));
kono
parents:
diff changeset
115 }