40934e5b1566c60a6fb7ecb25287fff3b13f74d0
[alexxy/gromacs.git] / src / gromacs / legacyheaders / thread_mpi / atomic / cce_intrinsics.h
1 /*
2    This source code file is part of thread_mpi.
3    Original for gcc written by Sander Pronk, Erik Lindahl, and possibly
4    others. Modified for the Cray compiler by Daniel Landau.
5
6
7    Copyright (c) 2009, Sander Pronk, Erik Lindahl.
8    Copyright 2014, Cray Inc.
9    All rights reserved.
10
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions are met:
13    1) Redistributions of source code must retain the above copyright
14    notice, this list of conditions and the following disclaimer.
15    2) Redistributions in binary form must reproduce the above copyright
16    notice, this list of conditions and the following disclaimer in the
17    documentation and/or other materials provided with the distribution.
18    3) Neither the name of the copyright holders nor the
19    names of its contributors may be used to endorse or promote products
20    derived from this software without specific prior written permission.
21
22    THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
23    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25    DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
26    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33    If you want to redistribute modifications, please consider that
34    scientific software is very special. Version control is crucial -
35    bugs must be traceable. We will be happy to consider code for
36    inclusion in the official distribution, but derived work should not
37    be called official thread_mpi. Details are found in the README & COPYING
38    files.
39  */
40
41 #include <intrinsics.h>
42
43 #define tMPI_Atomic_memory_barrier() __builtin_ia32_mfence()
44
45 TMPI_EXPORT
46 static inline int tMPI_Atomic_cas(tMPI_Atomic_t *a, int oldval, int newval)
47 {
48     return __sync_val_compare_and_swap(&(a->value), oldval, newval) == oldval;
49 }
50
51 TMPI_EXPORT
52 static inline int tMPI_Atomic_ptr_cas(tMPI_Atomic_ptr_t* a, void *oldval,
53                                       void *newval)
54 {
55     return __sync_val_compare_and_swap((size_t*)&(a->value), (size_t)oldval, (size_t)newval) == (size_t)oldval;
56 }
57
58 TMPI_EXPORT
59 static inline int tMPI_Atomic_add_return(tMPI_Atomic_t *a, volatile int i)
60 {
61     return __sync_add_and_fetch( &(a->value), i);
62 }
63 #define TMPI_ATOMIC_HAVE_NATIVE_ADD_RETURN
64
65
66 TMPI_EXPORT
67 static inline int tMPI_Atomic_fetch_add(tMPI_Atomic_t *a, volatile int i)
68 {
69     return __sync_fetch_and_add( &(a->value), i);
70 }
71 #define TMPI_ATOMIC_HAVE_NATIVE_FETCH_ADD