Update to 4.8.2.
[platform/upstream/gcc48.git] / libatomic / gstore.c
1 /* Copyright (C) 2012-2013 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU Atomic Library (libatomic).
5
6    Libatomic is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 #include "libatomic_i.h"
26
27
28 /* If we natively support the store, and if we're unconcerned with extra
29    barriers (e.g. fully in-order cpu for which barriers are a nop), then
30    go ahead and expand the operation inline.  */
31 #if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__)
32 # define EXACT_INLINE(N)                                        \
33   if (C2(HAVE_ATOMIC_LDST_,N))                                  \
34     {                                                           \
35       __atomic_store_n (PTR(N,mptr), *PTR(N,vptr), smodel);     \
36       return;                                                   \
37     }
38 #else
39 # define EXACT_INLINE(N)
40 #endif
41
42
43 #define EXACT(N)                                                \
44   do {                                                          \
45     if (!C2(HAVE_INT,N)) break;                                 \
46     if ((uintptr_t)mptr & (N - 1)) break;                       \
47     EXACT_INLINE (N);                                           \
48     C3(local_,store_,N) (PTR(N,mptr), *PTR(N,vptr), smodel);    \
49     return;                                                     \
50   } while (0)
51
52
53 #define LARGER(N)                                               \
54   do {                                                          \
55     union max_size_u u, v;                                      \
56     uintptr_t r, a;                                             \
57     if (!C2(HAVE_INT,N)) break;                                 \
58     if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break;                   \
59     r = (uintptr_t)mptr & (N - 1);                              \
60     a = (uintptr_t)mptr & -N;                                   \
61     if (r + n <= N)                                             \
62       {                                                         \
63         pre_barrier (smodel);                                   \
64         /* This load need not be atomic, as the CAS             \
65            below will validate it.  */                          \
66         u.C2(i,N) = *PTR(N,a);                                  \
67         do {                                                    \
68           v = u; memcpy (v.b + r, vptr, n);                     \
69         } while (!(C2(HAVE_ATOMIC_CAS_,N)                       \
70                    ? __atomic_compare_exchange_n (PTR(N,a),     \
71                         &u.C2(i,N), v.C2(i,N), true,            \
72                         __ATOMIC_RELAXED, __ATOMIC_RELAXED)     \
73                    : C3(local_,compare_exchange_,N) (PTR(N,a),  \
74                         &u.C2(i,N), v.C2(i,N),                  \
75                         __ATOMIC_RELAXED, __ATOMIC_RELAXED)));  \
76         post_barrier (smodel);                                  \
77         return;                                                 \
78       }                                                         \
79   } while (0)
80
81
82 void
83 libat_store (size_t n, void *mptr, void *vptr, int smodel)
84 {
85   switch (n)
86     {
87     case 0:                             return;
88     case 1:             EXACT(1);       goto L4;
89     case 2:             EXACT(2);       goto L4;
90     case 4:             EXACT(4);       goto L8;
91     case 8:             EXACT(8);       goto L16;
92     case 16:            EXACT(16);      break;
93
94     case 3: L4:         LARGER(4);      /* FALLTHRU */
95     case 5 ... 7: L8:   LARGER(8);      /* FALLTHRU */
96     case 9 ... 15: L16: LARGER(16);     break;
97     }
98
99   pre_seq_barrier (smodel);
100   libat_lock_n (mptr, n);
101
102   memcpy (mptr, vptr, n);
103
104   libat_unlock_n (mptr, n);
105   post_seq_barrier (smodel);
106 }
107
108 EXPORT_ALIAS (store);