33427ebf33e22ed942c02ff3c2e8b15dfbd7b5d9
[profile/ivi/qtbase.git] / src / corelib / arch / qatomic_x86_64.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Copyright (C) 2011 Thiago Macieira <thiago@kde.org>
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #ifndef QATOMIC_X86_64_H
44 #define QATOMIC_X86_64_H
45
46 #include <QtCore/qgenericatomic.h>
47
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52 #if 0
53 // silence syncqt warnings
54 QT_END_NAMESPACE
55 QT_END_HEADER
56
57 #pragma qt_sync_stop_processing
58 #endif
59
60 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
61 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
62
63 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
64 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
65
66 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
67 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
68
69 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
70 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
71
72 #define Q_ATOMIC_INT32_IS_SUPPORTED
73
74 #define Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
75 #define Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_WAIT_FREE
76
77 #define Q_ATOMIC_INT32_TEST_AND_SET_IS_ALWAYS_NATIVE
78 #define Q_ATOMIC_INT32_TEST_AND_SET_IS_WAIT_FREE
79
80 #define Q_ATOMIC_INT32_FETCH_AND_STORE_IS_ALWAYS_NATIVE
81 #define Q_ATOMIC_INT32_FETCH_AND_STORE_IS_WAIT_FREE
82
83 #define Q_ATOMIC_INT32_FETCH_AND_ADD_IS_ALWAYS_NATIVE
84 #define Q_ATOMIC_INT32_FETCH_AND_ADD_IS_WAIT_FREE
85
86 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
87 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
88
89 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
90 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
91
92 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
93 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
94
95 template<> struct QAtomicIntegerTraits<int> { enum { IsInteger = 1 }; };
96 template<> struct QAtomicIntegerTraits<unsigned int> { enum { IsInteger = 1 }; };
97
98 template <int size> struct QBasicAtomicOps: QGenericAtomicOps<QBasicAtomicOps<size> >
99 {
100     static inline bool isReferenceCountingNative() { return true; }
101     static inline bool isReferenceCountingWaitFree() { return true; }
102     template <typename T> static bool ref(T &_q_value);
103     template <typename T> static bool deref(T &_q_value);
104
105     static inline bool isTestAndSetNative() { return true; }
106     static inline bool isTestAndSetWaitFree() { return true; }
107     template <typename T> static bool testAndSetRelaxed(T &_q_value, T expectedValue, T newValue);
108
109     static inline bool isFetchAndStoreNative() { return true; }
110     static inline bool isFetchAndStoreWaitFree() { return true; }
111     template <typename T> static T fetchAndStoreRelaxed(T &_q_value, T newValue);
112
113     static inline bool isFetchAndAddNative() { return true; }
114     static inline bool isFetchAndAddWaitFree() { return true; }
115     template <typename T> static
116     T fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd);
117 };
118
119 template <typename T> struct QAtomicOps : QBasicAtomicOps<sizeof(T)>
120 {
121     typedef T Type;
122 };
123
124 #if defined(Q_CC_GNU) || defined(Q_CC_INTEL)
125
126 template<> struct QAtomicIntegerTraits<char> { enum { IsInteger = 1 }; };
127 template<> struct QAtomicIntegerTraits<signed char> { enum { IsInteger = 1 }; };
128 template<> struct QAtomicIntegerTraits<unsigned char> { enum { IsInteger = 1 }; };
129 template<> struct QAtomicIntegerTraits<short> { enum { IsInteger = 1 }; };
130 template<> struct QAtomicIntegerTraits<unsigned short> { enum { IsInteger = 1 }; };
131 template<> struct QAtomicIntegerTraits<long> { enum { IsInteger = 1 }; };
132 template<> struct QAtomicIntegerTraits<unsigned long> { enum { IsInteger = 1 }; };
133 template<> struct QAtomicIntegerTraits<long long> { enum { IsInteger = 1 }; };
134 template<> struct QAtomicIntegerTraits<unsigned long long> { enum { IsInteger = 1 }; };
135
136 template<> template<typename T> inline
137 bool QBasicAtomicOps<1>::ref(T &_q_value)
138 {
139     unsigned char ret;
140     asm volatile("lock\n"
141                  "incb %0\n"
142                  "setne %1"
143                  : "=m" (_q_value), "=qm" (ret)
144                  : "m" (_q_value)
145                  : "memory");
146     return ret != 0;
147 }
148
149 template<> template<typename T> inline
150 bool QBasicAtomicOps<2>::ref(T &_q_value)
151 {
152     unsigned char ret;
153     asm volatile("lock\n"
154                  "incw %0\n"
155                  "setne %1"
156                  : "=m" (_q_value), "=qm" (ret)
157                  : "m" (_q_value)
158                  : "memory");
159     return ret != 0;
160 }
161
162 template<> template<typename T> inline
163 bool QBasicAtomicOps<4>::ref(T &_q_value)
164 {
165     unsigned char ret;
166     asm volatile("lock\n"
167                  "incl %0\n"
168                  "setne %1"
169                  : "=m" (_q_value), "=qm" (ret)
170                  : "m" (_q_value)
171                  : "memory");
172     return ret != 0;
173 }
174
175 template<> template<typename T> inline
176 bool QBasicAtomicOps<8>::ref(T &_q_value)
177 {
178     unsigned char ret;
179     asm volatile("lock\n"
180                  "incq %0\n"
181                  "setne %1"
182                  : "=m" (_q_value), "=qm" (ret)
183                  : "m" (_q_value)
184                  : "memory");
185     return ret != 0;
186 }
187
188 template<> template <typename T> inline
189 bool QBasicAtomicOps<1>::deref(T &_q_value)
190 {
191     unsigned char ret;
192     asm volatile("lock\n"
193                  "decb %0\n"
194                  "setne %1"
195                  : "=m" (_q_value), "=qm" (ret)
196                  : "m" (_q_value)
197                  : "memory");
198     return ret != 0;
199 }
200
201 template<> template <typename T> inline
202 bool QBasicAtomicOps<2>::deref(T &_q_value)
203 {
204     unsigned char ret;
205     asm volatile("lock\n"
206                  "decw %0\n"
207                  "setne %1"
208                  : "=m" (_q_value), "=qm" (ret)
209                  : "m" (_q_value)
210                  : "memory");
211     return ret != 0;
212 }
213 template<> template <typename T> inline
214 bool QBasicAtomicOps<4>::deref(T &_q_value)
215 {
216     unsigned char ret;
217     asm volatile("lock\n"
218                  "decl %0\n"
219                  "setne %1"
220                  : "=m" (_q_value), "=qm" (ret)
221                  : "m" (_q_value)
222                  : "memory");
223     return ret != 0;
224 }
225
226 template<> template <typename T> inline
227 bool QBasicAtomicOps<8>::deref(T &_q_value)
228 {
229     unsigned char ret;
230     asm volatile("lock\n"
231                  "decq %0\n"
232                  "setne %1"
233                  : "=m" (_q_value), "=qm" (ret)
234                  : "m" (_q_value)
235                  : "memory");
236     return ret != 0;
237 }
238
239 template<int size> template <typename T> inline
240 bool QBasicAtomicOps<size>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue)
241 {
242     unsigned char ret;
243     asm volatile("lock\n"
244                  "cmpxchg %3,%2\n"
245                  "sete %1\n"
246                  : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
247                  : "r" (newValue), "0" (expectedValue)
248                  : "memory");
249     return ret != 0;
250 }
251
252 template<> template <typename T> inline
253 bool QBasicAtomicOps<1>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue)
254 {
255     unsigned char ret;
256     asm volatile("lock\n"
257                  "cmpxchg %3,%2\n"
258                  "sete %1\n"
259                  : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
260                  : "q" (newValue), "0" (expectedValue)
261                  : "memory");
262     return ret != 0;
263 }
264
265 template<int size> template <typename T> inline
266 T QBasicAtomicOps<size>::fetchAndStoreRelaxed(T &_q_value, T newValue)
267 {
268     asm volatile("xchg %0,%1"
269                  : "=r" (newValue), "+m" (_q_value)
270                  : "0" (newValue)
271                  : "memory");
272     return newValue;
273 }
274
275 template<> template <typename T> inline
276 T QBasicAtomicOps<1>::fetchAndStoreRelaxed(T &_q_value, T newValue)
277 {
278     asm volatile("xchg %0,%1"
279                  : "=q" (newValue), "+m" (_q_value)
280                  : "0" (newValue)
281                  : "memory");
282     return newValue;
283 }
284
285 template<int size> template <typename T> inline
286 T QBasicAtomicOps<size>::fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd)
287 {
288     T result;
289     asm volatile("lock\n"
290                  "xadd %0,%1"
291                  : "=r" (result), "+m" (_q_value)
292                  : "0" (T(valueToAdd * QAtomicAdditiveType<T>::AddScale))
293                  : "memory");
294     return result;
295 }
296
297 template<> template <typename T> inline
298 T QBasicAtomicOps<1>::fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd)
299 {
300     T result;
301     asm volatile("lock\n"
302                  "xadd %0,%1"
303                  : "=q" (result), "+m" (_q_value)
304                  : "0" (T(valueToAdd * QAtomicAdditiveType<T>::AddScale))
305                  : "memory");
306     return result;
307 }
308
309 #define Q_ATOMIC_INT8_IS_SUPPORTED
310
311 #define Q_ATOMIC_INT8_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
312 #define Q_ATOMIC_INT8_REFERENCE_COUNTING_IS_WAIT_FREE
313
314 #define Q_ATOMIC_INT8_TEST_AND_SET_IS_ALWAYS_NATIVE
315 #define Q_ATOMIC_INT8_TEST_AND_SET_IS_WAIT_FREE
316
317 #define Q_ATOMIC_INT8_FETCH_AND_STORE_IS_ALWAYS_NATIVE
318 #define Q_ATOMIC_INT8_FETCH_AND_STORE_IS_WAIT_FREE
319
320 #define Q_ATOMIC_INT8_FETCH_AND_ADD_IS_ALWAYS_NATIVE
321 #define Q_ATOMIC_INT8_FETCH_AND_ADD_IS_WAIT_FREE
322
323 #define Q_ATOMIC_INT16_IS_SUPPORTED
324
325 #define Q_ATOMIC_INT16_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
326 #define Q_ATOMIC_INT16_REFERENCE_COUNTING_IS_WAIT_FREE
327
328 #define Q_ATOMIC_INT16_TEST_AND_SET_IS_ALWAYS_NATIVE
329 #define Q_ATOMIC_INT16_TEST_AND_SET_IS_WAIT_FREE
330
331 #define Q_ATOMIC_INT16_FETCH_AND_STORE_IS_ALWAYS_NATIVE
332 #define Q_ATOMIC_INT16_FETCH_AND_STORE_IS_WAIT_FREE
333
334 #define Q_ATOMIC_INT16_FETCH_AND_ADD_IS_ALWAYS_NATIVE
335 #define Q_ATOMIC_INT16_FETCH_AND_ADD_IS_WAIT_FREE
336
337 #define Q_ATOMIC_INT64_IS_SUPPORTED
338
339 #define Q_ATOMIC_INT64_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
340 #define Q_ATOMIC_INT64_REFERENCE_COUNTING_IS_WAIT_FREE
341
342 #define Q_ATOMIC_INT64_TEST_AND_SET_IS_ALWAYS_NATIVE
343 #define Q_ATOMIC_INT64_TEST_AND_SET_IS_WAIT_FREE
344
345 #define Q_ATOMIC_INT64_FETCH_AND_STORE_IS_ALWAYS_NATIVE
346 #define Q_ATOMIC_INT64_FETCH_AND_STORE_IS_WAIT_FREE
347
348 #define Q_ATOMIC_INT64_FETCH_AND_ADD_IS_ALWAYS_NATIVE
349 #define Q_ATOMIC_INT64_FETCH_AND_ADD_IS_WAIT_FREE
350
351 #else // !Q_CC_INTEL && !Q_CC_GNU
352 #  error "This compiler for x86_64 is not supported"
353 #endif // Q_CC_GNU || Q_CC_INTEL
354
355 QT_END_NAMESPACE
356
357 QT_END_HEADER
358
359 #endif // QATOMIC_X86_64_H