added Verlet scheme and NxN non-bonded functionality
[alexxy/gromacs.git] / include / thread_mpi / atomic / gcc_intrinsics.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 /* this is for newer versions of gcc that have built-in intrinsics */
40
41 #define tMPI_Atomic_memory_barrier()  __sync_synchronize()
42
43
44 static inline int tMPI_Atomic_add_return(tMPI_Atomic_t *a, volatile int i)
45 {
46     return __sync_add_and_fetch( &(a->value), i);
47 }
48
49 static inline int tMPI_Atomic_fetch_add(tMPI_Atomic_t *a, volatile int i)
50 {
51     return __sync_fetch_and_add( &(a->value), i);
52 }
53
54
55 static inline int tMPI_Atomic_cas(tMPI_Atomic_t *a, int oldval, int newval)
56 {
57     return __sync_bool_compare_and_swap( &(a->value), oldval, newval);
58 }
59
60
61 #if 0 
62 /* these definitions are only used if there's no assembly versions for them:
63    they're inefficient because they use compare-and-swap instead of just
64    swap. */
65 static inline int tMPI_Atomic_swap(tMPI_Atomic_t *a, int b)
66 {
67     int oldval;
68     do
69     {
70         oldval=a->value;
71     } while(__sync_val_compare_and_swap( &(a->value), oldval, b) != oldval);
72
73     return oldval;
74 }
75
76 static inline void* tMPI_Atomic_ptr_swap(tMPI_Atomic_ptr_t *a, void *b)
77 {
78     void *oldval;
79     do
80     {
81         oldval=a->value;
82     } while(__sync_val_compare_and_swap( &(a->value), oldval, b) != oldval);
83
84     return oldval;
85 }
86 #endif
87
88
89
90 static inline int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t* a, void *oldval, 
91                                       void *newval)
92 {
93 #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__)
94     return __sync_bool_compare_and_swap( &(a->value), oldval, newval);
95 #else
96     /* the intel compilers need integer type arguments for compare_and_swap.
97         on the platforms supported by icc, size_t is always the size of
98         a pointer. */
99     return (__sync_bool_compare_and_swap( (size_t*)&(a->value), 
100                                           (size_t)oldval, 
101                                           (size_t)newval) );
102 #endif
103 }
104
105
106