summaryrefslogtreecommitdiff
path: root/sysdeps/m68k/m68020/bits/atomic.h
blob: 6b6db71465abaa5cdc2670d88fa2ecfc13d9abcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Andreas Schwab <schwab@suse.de>, 2003.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <stdint.h>


typedef int8_t atomic8_t;
typedef uint8_t uatomic8_t;
typedef int_fast8_t atomic_fast8_t;
typedef uint_fast8_t uatomic_fast8_t;

typedef int16_t atomic16_t;
typedef uint16_t uatomic16_t;
typedef int_fast16_t atomic_fast16_t;
typedef uint_fast16_t uatomic_fast16_t;

typedef int32_t atomic32_t;
typedef uint32_t uatomic32_t;
typedef int_fast32_t atomic_fast32_t;
typedef uint_fast32_t uatomic_fast32_t;

typedef int64_t atomic64_t;
typedef uint64_t uatomic64_t;
typedef int_fast64_t atomic_fast64_t;
typedef uint_fast64_t uatomic_fast64_t;

typedef intptr_t atomicptr_t;
typedef uintptr_t uatomicptr_t;
typedef intmax_t atomic_max_t;
typedef uintmax_t uatomic_max_t;

#define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) __ret;						      \
     __asm __volatile ("cas%.b %0,%2,%1"				      \
		       : "=d" (__ret), "+m" (*(mem))			      \
		       : "d" (newval), "0" (oldval));			      \
     __ret; })

#define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) __ret;						      \
     __asm __volatile ("cas%.w %0,%2,%1"				      \
		       : "=d" (__ret), "+m" (*(mem))			      \
		       : "d" (newval), "0" (oldval));			      \
     __ret; })

#define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) __ret;						      \
     __asm __volatile ("cas%.l %0,%2,%1"				      \
		       : "=d" (__ret), "+m" (*(mem))			      \
		       : "d" (newval), "0" (oldval));			      \
     __ret; })

# define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) __ret;						      \
     __typeof (mem) __memp = (mem);					      \
     __asm __volatile ("cas2%.l %0:%R0,%1:%R1,(%2):(%3)"		      \
		       : "=d" (__ret)					      \
		       : "d" (newval), "r" (__memp),			      \
			 "r" ((char *) __memp + 4), "0" (oldval)	      \
		       : "memory");					      \
     __ret; })

#define atomic_exchange_acq(mem, newvalue) \
  ({ __typeof (*(mem)) __result = *(mem);				      \
     if (sizeof (*(mem)) == 1)						      \
       __asm __volatile ("1: cas%.b %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem))		      \
			 : "d" (newvalue), "0" (__result));		      \
     else if (sizeof (*(mem)) == 2)					      \
       __asm __volatile ("1: cas%.w %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem))		      \
			 : "d" (newvalue), "0" (__result));		      \
     else if (sizeof (*(mem)) == 4)					      \
       __asm __volatile ("1: cas%.l %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem))		      \
			 : "d" (newvalue), "0" (__result));		      \
     else								      \
       {								      \
	 __typeof (mem) __memp = (mem);					      \
	 __asm __volatile ("1: cas2%.l %0:%R0,%1:%R1,(%2):(%3);"	      \
			   "   jbne 1b"					      \
			   : "=d" (__result)				      \
			   : "d" (newvalue), "r" (__memp),		      \
			     "r" ((char *) __memp + 4), "0" (__result)	      \
			   : "memory");					      \
       }								      \
     __result; })

#define atomic_exchange_and_add(mem, value) \
  ({ __typeof (*(mem)) __result = *(mem);				      \
     __typeof (*(mem)) __temp;						      \
     if (sizeof (*(mem)) == 1)						      \
       __asm __volatile ("1: move%.b %0,%2;"				      \
			 "   add%.b %3,%2;"				      \
			 "   cas%.b %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem)),		      \
			   "=&d" (__temp)				      \
			 : "d" (value), "0" (__result));		      \
     else if (sizeof (*(mem)) == 2)					      \
       __asm __volatile ("1: move%.w %0,%2;"				      \
			 "   add%.w %3,%2;"				      \
			 "   cas%.w %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem)),		      \
			   "=&d" (__temp)				      \
			 : "d" (value), "0" (__result));		      \
     else if (sizeof (*(mem)) == 4)					      \
       __asm __volatile ("1: move%.l %0,%2;"				      \
			 "   add%.l %3,%2;"				      \
			 "   cas%.l %0,%2,%1;"				      \
			 "   jbne 1b"					      \
			 : "=d" (__result), "+m" (*(mem)),		      \
			   "=&d" (__temp)				      \
			 : "d" (value), "0" (__result));		      \
     else								      \
       {								      \
	 __typeof (mem) __memp = (mem);					      \
	 __asm __volatile ("1: move%.l %0,%1;"				      \
			   "   move%.l %R0,%R1;"			      \
			   "   add%.l %2,%1;"				      \
			   "   addx%.l %R2,%R1;"			      \
			   "   cas2%.l %0:%R0,%1:%R1,(%3):(%4);"	      \
			   "   jbne 1b"					      \
			   : "=d" (__result), "=&d" (__temp)		      \
			   : "d" (value), "r" (__memp),			      \
			     "r" ((char *) __memp + 4), "0" (__result)	      \
			   : "memory");					      \
       }								      \
     __result; })

#define atomic_add(mem, value) \
  (void) ({ if (sizeof (*(mem)) == 1)					      \
	      __asm __volatile ("add%.b %1,%0"				      \
				: "+m" (*(mem))				      \
				: "id" (value));			      \
	    else if (sizeof (*(mem)) == 2)				      \
	      __asm __volatile ("add%.w %1,%0"				      \
				: "+m" (*(mem))				      \
				: "id" (value));			      \
	    else if (sizeof (*(mem)) == 4)				      \
	      __asm __volatile ("add%.l %1,%0"				      \
				: "+m" (*(mem))				      \
				: "id" (value));			      \
	    else							      \
	      {								      \
		__typeof (mem) __memp = (mem);				      \
		__typeof (*(mem)) __oldval = *__memp;			      \
		__typeof (*(mem)) __temp;				      \
		__asm __volatile ("1: move%.l %0,%1;"			      \
				  "   move%.l %R0,%R1;"			      \
				  "   add%.l %2,%1;"			      \
				  "   addx%.l %R2,%R1;"			      \
				  "   cas2%.l %0:%R0,%1:%R1,(%3):(%4);"	      \
				  "   jbne 1b"				      \
				  : "=d" (__oldval), "=&d" (__temp)	      \
				  : "d" (value), "r" (__memp),		      \
				    "r" ((char *) __memp + 4), "0" (__oldval) \
				  : "memory");				      \
	      }								      \
	    })

#define atomic_increment_and_test(mem) \
  ({ char __result;							      \
     if (sizeof (*(mem)) == 1)						      \
       __asm __volatile ("addq%.b %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else if (sizeof (*(mem)) == 2)					      \
       __asm __volatile ("addq%.w %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else if (sizeof (*(mem)) == 4)					      \
       __asm __volatile ("addq%.l %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else								      \
       {								      \
	 __typeof (mem) __memp = (mem);					      \
	 __typeof (*(mem)) __oldval = *__memp;				      \
	 __typeof (*(mem)) __temp;					      \
	 __asm __volatile ("1: move%.l %1,%2;"				      \
			   "   move%.l %R1,%R2;"			      \
			   "   addq%.l %#1,%2;"				      \
			   "   addx%.l %5,%R2;"				      \
			   "   seq %0;"					      \
			   "   cas2%.l %1:%R1,%2:%R2,(%3):(%4);"	      \
			   "   jbne 1b"					      \
			   : "=&dm" (__result), "=d" (__oldval),	      \
			     "=&d" (__temp)				      \
			   : "r" (__memp), "r" ((char *) __memp + 4),	      \
			     "d" (0), "1" (__oldval)			      \
			   : "memory");					      \
       }								      \
     __result; })

#define atomic_decrement_and_test(mem) \
  ({ char __result;							      \
     if (sizeof (*(mem)) == 1)						      \
       __asm __volatile ("subq%.b %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else if (sizeof (*(mem)) == 2)					      \
       __asm __volatile ("subq%.w %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else if (sizeof (*(mem)) == 4)					      \
       __asm __volatile ("subq%.l %#1,%1; seq %0"			      \
			 : "=dm" (__result), "+m" (*(mem)));		      \
     else								      \
       {								      \
	 __typeof (mem) __memp = (mem);					      \
	 __typeof (*(mem)) __oldval = *__memp;				      \
	 __typeof (*(mem)) __temp;					      \
	 __asm __volatile ("1: move%.l %1,%2;"				      \
			   "   move%.l %R1,%R2;"			      \
			   "   subq%.l %#1,%2;"				      \
			   "   subx%.l %5,%R2;"				      \
			   "   seq %0;"					      \
			   "   cas2%.l %1:%R1,%2:%R2,(%3):(%4);"	      \
			   "   jbne 1b"					      \
			   : "=&dm" (__result), "=d" (__oldval),	      \
			     "=&d" (__temp)				      \
			   : "r" (__memp), "r" ((char *) __memp + 4),	      \
			     "d" (0), "1" (__oldval)			      \
			   : "memory");					      \
       }								      \
     __result; })

#define atomic_bit_set(mem, bit) \
  __asm __volatile ("bfset %0{%1,#1}"					      \
		    : "+m" (*(mem))					      \
		    : "di" (sizeof (*(mem)) * 8 - (bit) - 1))

#define atomic_bit_test_set(mem, bit) \
  ({ char __result;							      \
     __asm __volatile ("bfset %1{%2,#1}; sne %0"			      \
		       : "=dm" (__result), "+m" (*(mem))		      \
		       : "di" (sizeof (*(mem)) * 8 - (bit) - 1));	      \
     __result; })