Debian Codegraph @

libatomic/gload.c

raw
1 /* Copyright (C) 2012-2025 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <[email protected]>.
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_GLOAD
26 #include "libatomic_i.h"
27
28
29 /* If we natively support the load, 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, DEST, SRC, DONE) \
34 if (C2(HAVE_ATOMIC_LDST_,N)) \
35 { \
36 DEST = __atomic_load_n (SRC, __ATOMIC_SEQ_CST); \
37 DONE; \
38 }
39 #else
40 # define EXACT_INLINE(N, DEST, SRC, DONE)
41 #endif
42
43
44 #define EXACT_(N, DEST, SRC, DONE) \
45 do { \
46 EXACT_INLINE (N, DEST, SRC, DONE); \
47 DEST = C3(local_,load_,N) (SRC, smodel); \
48 DONE; \
49 } while (0)
50
51 #define EXACT(N) \
52 do { \
53 if (!C2(HAVE_INT,N)) break; \
54 if ((uintptr_t)mptr & (N - 1)) break; \
55 EXACT_(N, *PTR(N,rptr), PTR(N,mptr), return); \
56 } while (0)
57
58 #define LARGER(N) \
59 do { \
60 if (!C2(HAVE_INT,N)) break; \
61 r = (uintptr_t)mptr & (N - 1); \
62 a = (uintptr_t)mptr & -N; \
63 if (r + n <= N) \
64 EXACT_ (N, u.C2(i,N), PTR(N,a), goto Lfinish); \
65 } while (0)
66
67
68 void
69 libat_load (size_t n, void *mptr, void *rptr, int smodel)
70 {
71 union max_size_u u;
72 uintptr_t r, a;
73
74 switch (n)
75 {
76 case 0: return;
77 case 1: EXACT(1); goto L4;
78 case 2: EXACT(2); goto L4;
79 case 4: EXACT(4); goto L8;
80 case 8: EXACT(8); goto L16;
81 case 16: EXACT(16); break;
82
83 case 3: L4: LARGER(4); /* FALLTHRU */
84 case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
85 case 9 ... 15: L16: LARGER(16); break;
86
87 Lfinish:
88 memcpy (rptr, u.b + r, n);
89 return;
90 }
91
92 pre_seq_barrier (smodel);
93 libat_lock_n (mptr, n);
94
95 memcpy (rptr, mptr, n);
96
97 libat_unlock_n (mptr, n);
98 post_seq_barrier (smodel);
99 }
100
101 EXPORT_ALIAS (load);
102 #undef LAT_GLOAD