]> git.ipfire.org Git - thirdparty/gcc.git/blame - libatomic/gexch.c
[PATCH v2 2/3] RISC-V: setmem for RISCV with V extension
[thirdparty/gcc.git] / libatomic / gexch.c
CommitLineData
a945c346 1/* Copyright (C) 2012-2024 Free Software Foundation, Inc.
48310492
RH
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
6edf6fe7 25#define LAT_GEXCH
48310492
RH
26#include "libatomic_i.h"
27
28
29/* If we natively support the exchange, and if we're unconcerned with extra
30 barriers (e.g. fully in-order cpu for which barriers are a nop), then
31 go ahead and expand the operation inline. */
32#if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__)
33# define EXACT_INLINE(N) \
34 if (C2(HAVE_ATOMIC_EXCHANGE_,N)) \
35 { \
36 *PTR(N,rptr) = __atomic_exchange_n \
94f3ccc8 37 (PTR(N,mptr), *PTR(N,vptr), __ATOMIC_SEQ_CST); \
48310492
RH
38 return; \
39 }
40#else
41# define EXACT_INLINE(N)
42#endif
43
44
45#define EXACT(N) \
46 do { \
47 if (!C2(HAVE_INT,N)) break; \
48 if ((uintptr_t)mptr & (N - 1)) break; \
49 EXACT_INLINE (N); \
50 *PTR(N,rptr) = C3(local_,exchange_,N) \
51 (PTR(N,mptr), *PTR(N,vptr), smodel); \
52 return; \
53 } while (0)
54
55
56#define LARGER(N) \
57 do { \
58 if (!C2(HAVE_INT,N)) break; \
59 if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \
60 r = (uintptr_t)mptr & (N - 1); \
61 a = (uintptr_t)mptr & -N; \
62 if (r + n <= N) \
63 { \
64 pre_barrier (smodel); \
65 u.C2(i,N) = *PTR(N,a); \
66 do { \
67 v = u; \
68 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 goto Lfinish; \
77 } \
78 } while (0)
79
80
81static void __attribute__((noinline))
82libat_exchange_large_inplace (size_t n, void *mptr, void *vptr)
83{
84#define BUF 1024
85
86 char temp[BUF];
87 size_t i = 0;
88
89 for (i = 0; n >= BUF; i += BUF, n -= BUF)
90 {
91 memcpy (temp, mptr + i, BUF);
92 memcpy (mptr + i, vptr + i, BUF);
93 memcpy (vptr + i, temp, BUF);
94 }
95 if (n > 0)
96 {
97 memcpy (temp, mptr + i, n);
98 memcpy (mptr + i, vptr + i, n);
99 memcpy (vptr + i, temp, n);
100 }
101
102#undef BUF
103}
104
105void
106libat_exchange (size_t n, void *mptr, void *vptr, void *rptr, int smodel)
107{
108 union max_size_u u, v;
109 uintptr_t r, a;
110
111 switch (n)
112 {
113 case 0: return;
114 case 1: EXACT(1); goto L4;
115 case 2: EXACT(2); goto L4;
116 case 4: EXACT(4); goto L8;
117 case 8: EXACT(8); goto L16;
118 case 16: EXACT(16); break;
119
120 case 3: L4: LARGER(4); /* FALLTHRU */
121 case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
122 case 9 ... 15: L16: LARGER(16); break;
123
124 Lfinish:
125 post_barrier (smodel);
126 memcpy (rptr, u.b + r, n);
127 return;
128 }
129
130 pre_seq_barrier (smodel);
131 libat_lock_n (mptr, n);
132
133 if (vptr != rptr)
134 {
135 memcpy (rptr, mptr, n);
136 memcpy (mptr, vptr, n);
137 }
138 else
139 libat_exchange_large_inplace (n, mptr, vptr);
140
141 libat_unlock_n (mptr, n);
142 post_seq_barrier (smodel);
143}
144
145EXPORT_ALIAS (exchange);
6edf6fe7 146#undef LAT_GEXCH