Replace __LP64__ with check for pointer size
[alexxy/gromacs.git] / src / external / thread_mpi / include / thread_mpi / atomic / gcc_x86.h
1 /*
2    This source code file is part of thread_mpi.
3    Written by Sander Pronk, Erik Lindahl, and possibly others.
4
5    Copyright (c) 2009, Sander Pronk, Erik Lindahl.
6    All rights reserved.
7
8    Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions are met:
10    1) Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12    2) Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15    3) Neither the name of the copyright holders nor the
16    names of its contributors may be used to endorse or promote products
17    derived from this software without specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
20    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22    DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
23    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30    If you want to redistribute modifications, please consider that
31    scientific software is very special. Version control is crucial -
32    bugs must be traceable. We will be happy to consider code for
33    inclusion in the official distribution, but derived work should not
34    be called official thread_mpi. Details are found in the README & COPYING
35    files.
36  */
37
38
39
40 #include <limits.h>
41 #include <stdint.h>
42 /* This code is executed for x86 and x86-64, with these compilers:
43  * GNU
44  * Intel
45  * Pathscale
46  * All these support GCC-style inline assembly.
47  * We also use this section for the documentation.
48  */
49
50
51 #if 0
52 /* Only gcc and Intel support this check, otherwise set it to true (skip doc) */
53 #if (!defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined DOXYGEN)
54 #define __builtin_constant_p(i) (1)
55 #endif
56 #endif
57
58 /* we put all of these on their own cache line by padding the data structure
59    to the size of a cache line on x86 (64 bytes): */
60 #define TMPI_SIZEOF_X86_CACHE_LINE 64
61 typedef struct tMPI_Atomic
62 {
63     int  value;
64     char padding[TMPI_SIZEOF_X86_CACHE_LINE-sizeof(int)];
65 } tMPI_Atomic_t;
66
67 typedef struct tMPI_Atomic_ptr
68 {
69     void* value;
70     char  padding[TMPI_SIZEOF_X86_CACHE_LINE-sizeof(void*)];
71 } tMPI_Atomic_ptr_t;
72
73 typedef struct tMPI_Spinlock
74 {
75     unsigned int lock;
76     char         padding[TMPI_SIZEOF_X86_CACHE_LINE-sizeof(unsigned int)];
77 } tMPI_Spinlock_t;
78
79
80 #define TMPI_SPINLOCK_INITIALIZER   { 0 }
81
82 #define TMPI_ATOMIC_HAVE_NATIVE_SPINLOCK
83
84
85
86 /* these are guaranteed to be  atomic on x86 and x86_64 */
87 #define tMPI_Atomic_get(a)  ((a)->value)
88 #define tMPI_Atomic_set(a, i)  (((a)->value) = (i))
89
90 #define tMPI_Atomic_ptr_get(a)  ((a)->value)
91 #define tMPI_Atomic_ptr_set(a, i)  (((a)->value) = (void*)(i))
92
93
94 /* do the intrinsics.
95
96    We disable this for 32-bit builds because the target may be 80386,
97    which didn't have cmpxchg, etc (they were introduced as only as 'recently'
98    as the 486, and gcc on some Linux versions still target 80386 by default).
99
100    We also specifically check for icc, because intrinsics are not always
101    supported there.
102
103    llvm has issues with inline assembly and also in 32 bits has support for
104    the gcc intrinsics */
105 #if ( ( (TMPI_GCC_VERSION >= 40100) && defined(__x86_64__) &&  \
106     !defined(__INTEL_COMPILER) )  || defined(__llvm__) )
107 #include "gcc_intrinsics.h"
108
109 #else
110 /* older versions of gcc don't support atomic intrinsics */
111
112 #ifndef __MIC__
113 #define tMPI_Atomic_memory_barrier() __asm__ __volatile__("sfence;" : : : "memory")
114 #else
115 /* MIC is in-order and does not need nor support sfense */
116 #define tMPI_Atomic_memory_barrier() __asm__ __volatile__("" ::: "memory")
117 #endif
118
119 #define TMPI_ATOMIC_HAVE_NATIVE_FETCH_ADD
120 static inline int tMPI_Atomic_fetch_add(tMPI_Atomic_t *a, int i)
121 {
122     volatile int res = i;
123     /* volatile because we read and write back to the same variable in the
124        asm section.  some compilers requires this to be volatile */
125     __asm__ __volatile__("lock ; xaddl %0, %1;"      /* swap-add */
126                          : "=r" (res)                /* with register as
127                                                         output*/
128                          : "m" (a->value), "0" (res) /* and memory as input */
129                          : "memory");
130     return res;
131 }
132
133 #define TMPI_ATOMIC_HAVE_NATIVE_ADD_RETURN
134 static inline int tMPI_Atomic_add_return(tMPI_Atomic_t *a, int i)
135 {
136     int          orig = i;
137     volatile int res  = i;
138
139     __asm__ __volatile__("lock ; xaddl %0, %1;"
140                          : "=r" (res)
141                          : "m" (a->value), "0" (res)
142                          :  "memory");
143     return res + orig; /* then add again from the right value */
144 }
145
146
147
148 static inline int tMPI_Atomic_cas(tMPI_Atomic_t *a, int oldval, int newval)
149 {
150     int prev;
151
152     __asm__ __volatile__("lock ; cmpxchgl %1,%2"
153                          : "=a" (prev)
154                          : "q" (newval), "m" (a->value), "0" (oldval)
155                          : "memory");
156
157     return prev == oldval;
158 }
159
160 static inline int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t *a,
161                                       void              *oldval,
162                                       void              *newval)
163 {
164     void* prev;
165 #if (defined(__x86_64__) && !defined(__ILP32__))
166     __asm__ __volatile__("lock ; cmpxchgq %1,%2"
167                          : "=a" (prev)
168                          : "q" (newval), "m" (a->value), "0" (oldval)
169                          : "memory");
170 #elif (defined(__x86_64__) && defined(__ILP32__)) || defined(__i386__)
171     __asm__ __volatile__("lock ; cmpxchgl %1,%2"
172                          : "=a" (prev)
173                          : "q" (newval), "m" (a->value), "0" (oldval)
174                          : "memory");
175 #else
176 #    error Cannot detect whether this is a 32-bit or 64-bit x86 build.
177 #endif
178     return prev == oldval;
179 }
180
181 #endif /* end of check for gcc intrinsics */
182
183
184 #define TMPI_ATOMIC_HAVE_NATIVE_SWAP
185 /* do the swap fns; we told the intrinsics that we have them. */
186 static inline int tMPI_Atomic_swap(tMPI_Atomic_t *a, int b)
187 {
188     volatile int ret = b;
189     __asm__ __volatile__("\txchgl %0, %1;"
190                          : "+r" (ret), "+m" (a->value)
191                          :
192                          : "memory");
193     return (int)ret;
194 }
195
196 static inline void *tMPI_Atomic_ptr_swap(tMPI_Atomic_ptr_t *a, void *b)
197 {
198     void *volatile *ret = (void* volatile*)b;
199 #if (defined(__x86_64__) && !defined(__ILP32__))
200     __asm__ __volatile__("\txchgq %0, %1;"
201                          : "+r" (ret), "+m" (a->value)
202                          :
203                          : "memory");
204 #elif (defined(__x86_64__) && defined(__ILP32__)) || defined(__i386__)
205     __asm__ __volatile__("\txchgl %0, %1;"
206                          : "+r" (ret), "+m" (a->value)
207                          :
208                          : "memory");
209 #else
210 #    error Cannot detect whether this is a 32-bit or 64-bit x86 build.
211 #endif
212     return (void*)ret;
213 }
214
215
216
217 /* spinlocks : */
218
219 static inline void tMPI_Spinlock_init(tMPI_Spinlock_t *x)
220 {
221     x->lock = 0;
222 }
223
224
225
226 static inline void tMPI_Spinlock_lock(tMPI_Spinlock_t *x)
227 {
228     /* this is a spinlock with a double loop, as recommended by Intel
229        it pauses in the outer loop (the one that just checks for the
230        availability of the lock), and thereby reduces bus contention and
231        prevents the pipeline from flushing. */
232     __asm__ __volatile__("1:\tcmpl $0, %0\n"    /* check the lock */
233                          "\tje 2f\n"            /* try to lock if it is
234                                                    free by jumping forward */
235                          "\tpause\n"            /* otherwise: small pause
236                                                    as recommended by Intel */
237                          "\tjmp 1b\n"           /* and jump back */
238
239                          "2:\tmovl $1, %%eax\n" /* set eax to 1, the locked
240                                                    value of the lock */
241                          "\txchgl %%eax, %0\n"  /* atomically exchange
242                                                    eax with the lock value */
243                          "\tcmpl $0, %%eax\n"   /* compare the exchanged
244                                                    value with 0 */
245                          "\tjne 1b"             /* jump backward if we didn't
246                                                    just lock */
247                          : "+m" (x->lock)       /* input & output var */
248                          :
249                          : "%eax", "memory"     /* we changed memory */
250                          );
251 }
252
253
254
255 static inline void tMPI_Spinlock_unlock(tMPI_Spinlock_t *x)
256 {
257     /* this is apparently all that is needed for unlocking a lock */
258     __asm__ __volatile__(
259         "\n\tmovl $0, %0\n"
260         : "=m" (x->lock) : : "memory" );
261 }
262
263
264
265 static inline int tMPI_Spinlock_trylock(tMPI_Spinlock_t *x)
266 {
267     int old_value = 1;
268
269     __asm__ __volatile__("\tmovl %2, %0\n"     /* set eax to 1, the locked
270                                                   value of the lock */
271                          "\txchgl %0, %1\n"    /* atomically exchange
272                                                   eax with the address in
273                                                   rdx. */
274                          : "+r" (old_value), "+m" (x->lock)
275                          : "i" (1)
276                          : "memory");
277     return (old_value);
278 }
279
280
281
282 static inline int tMPI_Spinlock_islocked(const tMPI_Spinlock_t *x)
283 {
284     return ( (*((volatile int*)(&(x->lock)))) != 0);
285 }
286
287
288 static inline void tMPI_Spinlock_wait(tMPI_Spinlock_t *x)
289 {
290     /* this is the spinlock without the xchg.  */
291     __asm__ __volatile__("1:\tcmpl $0, %0\n" /* check the lock */
292                          "\tje 2f\n"         /* try to lock if it is
293                                                 free by jumping forward */
294                          "\tpause\n"         /* otherwise: small pause
295                                                 as recommended by Intel */
296                          "\tjmp 1b\n"        /* and jump back */
297                          "2:\tnop\n"         /* jump target for end
298                                                 of wait */
299                          : "+m" (x->lock)    /* input & output var */
300                          :
301                          : "memory"          /* we changed memory */
302                          );
303 #if 0
304     do
305     {
306         tMPI_Atomic_memory_barrier();
307     }
308     while (tMPI_Spinlock_islocked(x));
309 #endif
310 }