PR libstdc++/80137 use std::nextafter instead of looping
[official-gcc.git] / libatomic / gexch.c
blob44745afa28d3424b7e2ea5f09abe6b99844c8c43
1 /* Copyright (C) 2012-2017 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 #include "libatomic_i.h"
28 /* If we natively support the exchange, 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_EXCHANGE_,N)) \
34 { \
35 *PTR(N,rptr) = __atomic_exchange_n \
36 (PTR(N,mptr), *PTR(N,vptr), __ATOMIC_SEQ_CST); \
37 return; \
39 #else
40 # define EXACT_INLINE(N)
41 #endif
44 #define EXACT(N) \
45 do { \
46 if (!C2(HAVE_INT,N)) break; \
47 if ((uintptr_t)mptr & (N - 1)) break; \
48 EXACT_INLINE (N); \
49 *PTR(N,rptr) = C3(local_,exchange_,N) \
50 (PTR(N,mptr), *PTR(N,vptr), smodel); \
51 return; \
52 } while (0)
55 #define LARGER(N) \
56 do { \
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 u.C2(i,N) = *PTR(N,a); \
65 do { \
66 v = u; \
67 memcpy (v.b + r, vptr, n); \
68 } while (!(C2(HAVE_ATOMIC_CAS_,N) \
69 ? __atomic_compare_exchange_n (PTR(N,a), \
70 &u.C2(i,N), v.C2(i,N), true, \
71 __ATOMIC_RELAXED, __ATOMIC_RELAXED) \
72 : C3(local_,compare_exchange_,N) (PTR(N,a), \
73 &u.C2(i,N), v.C2(i,N), \
74 __ATOMIC_RELAXED, __ATOMIC_RELAXED))); \
75 goto Lfinish; \
76 } \
77 } while (0)
80 static void __attribute__((noinline))
81 libat_exchange_large_inplace (size_t n, void *mptr, void *vptr)
83 #define BUF 1024
85 char temp[BUF];
86 size_t i = 0;
88 for (i = 0; n >= BUF; i += BUF, n -= BUF)
90 memcpy (temp, mptr + i, BUF);
91 memcpy (mptr + i, vptr + i, BUF);
92 memcpy (vptr + i, temp, BUF);
94 if (n > 0)
96 memcpy (temp, mptr + i, n);
97 memcpy (mptr + i, vptr + i, n);
98 memcpy (vptr + i, temp, n);
101 #undef BUF
104 void
105 libat_exchange (size_t n, void *mptr, void *vptr, void *rptr, int smodel)
107 union max_size_u u, v;
108 uintptr_t r, a;
110 switch (n)
112 case 0: return;
113 case 1: EXACT(1); goto L4;
114 case 2: EXACT(2); goto L4;
115 case 4: EXACT(4); goto L8;
116 case 8: EXACT(8); goto L16;
117 case 16: EXACT(16); break;
119 case 3: L4: LARGER(4); /* FALLTHRU */
120 case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
121 case 9 ... 15: L16: LARGER(16); break;
123 Lfinish:
124 post_barrier (smodel);
125 memcpy (rptr, u.b + r, n);
126 return;
129 pre_seq_barrier (smodel);
130 libat_lock_n (mptr, n);
132 if (vptr != rptr)
134 memcpy (rptr, mptr, n);
135 memcpy (mptr, vptr, n);
137 else
138 libat_exchange_large_inplace (n, mptr, vptr);
140 libat_unlock_n (mptr, n);
141 post_seq_barrier (smodel);
144 EXPORT_ALIAS (exchange);