Use glibc_likely instead __builtin_expect.
[jlayton/glibc.git] / sysdeps / tile / tilepro / bits / atomic.h
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #ifndef _BITS_ATOMIC_H
20 #define _BITS_ATOMIC_H  1
21
22 #include <asm/unistd.h>
23
24 /* 32-bit integer compare-and-exchange. */
25 static __inline __attribute__ ((always_inline))
26 int __atomic_cmpxchg_32 (volatile int *mem, int newval, int oldval)
27 {
28   int result;
29   __asm__ __volatile__ ("swint1"
30                         : "=R00" (result), "=m" (*mem)
31                         : "R10" (__NR_FAST_cmpxchg), "R00" (mem),
32                           "R01" (oldval), "R02" (newval), "m" (*mem)
33                         : "r20", "r21", "r22", "r23", "r24",
34                           "r25", "r26", "r27", "r28", "r29", "memory");
35   return result;
36 }
37
38 #define atomic_compare_and_exchange_val_acq(mem, n, o)                  \
39   ((__typeof (*(mem)))                                                  \
40    ((sizeof (*(mem)) == 4) ?                                            \
41     __atomic_cmpxchg_32 ((int *) (mem), (int) (n), (int) (o)) :         \
42     __atomic_error_bad_argument_size()))
43
44 /* Atomically compute:
45      int old = *ptr;
46      *ptr = (old & mask) + addend;
47      return old;  */
48
49 static __inline __attribute__ ((always_inline))
50 int __atomic_update_32 (volatile int *mem, int mask, int addend)
51 {
52   int result;
53   __asm__ __volatile__ ("swint1"
54                         : "=R00" (result), "=m" (*mem)
55                         : "R10" (__NR_FAST_atomic_update), "R00" (mem),
56                           "R01" (mask), "R02" (addend), "m" (*mem)
57                         : "r20", "r21", "r22", "r23", "r24",
58                           "r25", "r26", "r27", "r28", "r29", "memory");
59   return result;
60 }
61
62 /* Size-checked verson of __atomic_update_32. */
63 #define __atomic_update(mem, mask, addend)                              \
64   ((__typeof (*(mem)))                                                  \
65    ((sizeof (*(mem)) == 4) ?                                            \
66     __atomic_update_32 ((int *) (mem), (int) (mask), (int) (addend)) :  \
67     __atomic_error_bad_argument_size ()))
68
69 #define atomic_exchange_acq(mem, newvalue)              \
70   __atomic_update ((mem), 0, (newvalue))
71 #define atomic_exchange_and_add(mem, value)             \
72   __atomic_update ((mem), -1, (value))
73 #define atomic_and_val(mem, mask)                       \
74   __atomic_update ((mem), (mask), 0)
75 #define atomic_or_val(mem, mask)                        \
76   ({ __typeof (mask) __att1_v = (mask);                 \
77     __atomic_update ((mem), ~__att1_v, __att1_v); })
78
79 #include <sysdeps/tile/bits/atomic.h>
80
81 #endif /* bits/atomic.h */