tree-optimization/115694 - ICE with complex store rewrite
[official-gcc.git] / libatomic / exch_n.c
blob184d3de1009ec68189c13c4b604054e1e0f4e128
1 /* Copyright (C) 2012-2024 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Atomic Library (libatomic).
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.
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.
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.
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/>. */
25 #define LAT_EXCH_N
26 #include "libatomic_i.h"
29 /* If we support the builtin, just use it. */
30 #if !DONE && SIZE(HAVE_ATOMIC_EXCHANGE)
31 UTYPE
32 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
34 if (maybe_specialcase_relaxed(smodel))
35 return __atomic_exchange_n (mptr, newval, __ATOMIC_RELAXED);
36 else if (maybe_specialcase_acqrel(smodel))
37 return __atomic_exchange_n (mptr, newval, __ATOMIC_ACQ_REL);
38 else
39 return __atomic_exchange_n (mptr, newval, __ATOMIC_SEQ_CST);
42 #define DONE 1
43 #endif /* HAVE_ATOMIC_EXCHANGE */
46 #if !DONE && defined(atomic_compare_exchange_n)
47 UTYPE
48 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
50 UTYPE oldval;
52 pre_barrier (smodel);
54 oldval = *mptr;
55 while (!atomic_compare_exchange_n (mptr, &oldval, newval, true,
56 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
57 continue;
59 post_barrier (smodel);
61 return oldval;
64 #define DONE 1
65 #endif /* atomic_compare_exchange_n */
68 /* If this type is smaller than word-sized, fall back to a word-sized
69 compare-and-swap loop. */
70 #if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w)
71 UTYPE
72 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel)
74 UWORD mask, shift, woldval, wnewval, t, *wptr;
76 pre_barrier (smodel);
78 if (N < WORDSIZE)
80 wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
81 shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
82 mask = SIZE(MASK) << shift;
84 else
86 wptr = (UWORD *)mptr;
87 shift = 0;
88 mask = -1;
91 wnewval = (UWORD)newval << shift;
92 woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
95 t = (woldval & ~mask) | wnewval;
97 while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
98 __ATOMIC_RELAXED, __ATOMIC_RELAXED));
100 post_barrier (smodel);
101 return woldval >> shift;
104 #define DONE 1
105 #endif /* HAVE_ATOMIC_CAS && N < WORDSIZE */
108 /* Otherwise, fall back to some sort of protection mechanism. */
109 #if !DONE
110 UTYPE
111 SIZE(libat_exchange) (UTYPE *mptr, UTYPE newval, int smodel UNUSED)
113 UTYPE oldval;
114 UWORD magic;
116 pre_seq_barrier (smodel);
117 magic = protect_start (mptr);
119 oldval = *mptr;
120 *mptr = newval;
122 protect_end (mptr, magic);
123 post_seq_barrier (smodel);
125 return oldval;
127 #endif
129 EXPORT_ALIAS (SIZE(exchange));
130 #undef LAT_EXCH_N