]> git.ipfire.org Git - thirdparty/gcc.git/blob - libatomic/store_n.c
aarch64: Add codegen support for AdvSIMD faminmax
[thirdparty/gcc.git] / libatomic / store_n.c
1 /* Copyright (C) 2012-2024 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 #define LAT_STORE_N
26 #include "libatomic_i.h"
27
28
29 /* If we support the builtin, just use it. */
30 #if !DONE && SIZE(HAVE_ATOMIC_LDST)
31 void
32 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
33 {
34 if (maybe_specialcase_relaxed(smodel))
35 __atomic_store_n (mptr, newval, __ATOMIC_RELAXED);
36 else if (maybe_specialcase_acqrel(smodel))
37 /* Note that ACQ and ACQ_REL are not valid for store. */
38 __atomic_store_n (mptr, newval, __ATOMIC_RELEASE);
39 else
40 __atomic_store_n (mptr, newval, __ATOMIC_SEQ_CST);
41 }
42
43 #define DONE 1
44 #endif /* HAVE_ATOMIC_STORE */
45
46 /* If we have compare-and-swap, use it perform the store. */
47 #if !DONE && defined(atomic_compare_exchange_n)
48 void
49 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
50 {
51 UTYPE oldval;
52
53 pre_barrier (smodel);
54
55 oldval = *mptr;
56 while (!atomic_compare_exchange_n (mptr, &oldval, newval, true,
57 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
58 continue;
59
60 post_barrier (smodel);
61 }
62
63 #define DONE 1
64 #endif /* atomic_compare_exchange_n */
65
66
67 /* If this type is smaller than word-sized, fall back to a word-sized
68 compare-and-swap loop. */
69 #if !DONE && N < WORDSIZE && defined(atomic_compare_exchange_w)
70 void
71 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
72 {
73 UWORD mask, shift, woldval, wnewval, t, *wptr;
74
75 pre_barrier (smodel);
76
77 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
78 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
79 mask = SIZE(MASK) << shift;
80
81 wnewval = (UWORD)newval << shift;
82 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
83 do
84 {
85 t = (woldval & ~mask) | wnewval;
86 }
87 while (!atomic_compare_exchange_w (wptr, &woldval, t));
88
89 post_barrier (smodel);
90 }
91
92 #define DONE 1
93 #endif /* N < WORDSIZE && atomic_compare_exchange_w */
94
95
96 /* Otherwise, fall back to some sort of protection mechanism. */
97 #if !DONE
98 void
99 SIZE(libat_store) (UTYPE *mptr, UTYPE newval, int smodel)
100 {
101 UWORD magic;
102
103 pre_seq_barrier (smodel);
104 magic = protect_start (mptr);
105
106 *mptr = newval;
107
108 protect_end (mptr, magic);
109 post_seq_barrier (smodel);
110 }
111 #endif
112
113 EXPORT_ALIAS (SIZE(store));
114 #undef LAT_STORE_N