]> gcc.gnu.org Git - gcc.git/blame - gcc/sort.cc
vect: Move VMAT_LOAD_STORE_LANES handlings from final loop nest
[gcc.git] / gcc / sort.cc
CommitLineData
82a04cd1 1/* Platform-independent deterministic sort function.
aeee4812 2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
82a04cd1
AM
3 Contributed by Alexander Monakov.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3, or (at your option) any
10later version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21/* This implements a sort function suitable for GCC use cases:
22 - signature-compatible to C qsort, but relaxed contract:
23 - may apply the comparator to elements in a temporary buffer
24 - may abort on allocation failure
25 - deterministic (but not necessarily stable)
26 - fast, especially for common cases (0-5 elements of size 8 or 4)
27
28 The implementation uses a network sort for up to 5 elements and
29 a merge sort on top of that. Neither stage has branches depending on
30 comparator result, trading extra arithmetic for branch mispredictions. */
31
32#ifdef GENERATOR_FILE
33#include "bconfig.h"
34#else
35#include "config.h"
36#endif
37
38#include "system.h"
39
82a04cd1
AM
40#ifdef __GNUC__
41#define noinline __attribute__ ((__noinline__))
42#else
43#define noinline
44#endif
45
46/* C-style qsort comparator function type. */
47typedef int cmp_fn (const void *, const void *);
48
49/* Structure holding read-mostly (read-only in netsort) context. */
50struct sort_ctx
51{
52 cmp_fn *cmp; // pointer to comparator
53 char *out; // output buffer
54 size_t n; // number of elements
55 size_t size; // element size
a6405b11 56 size_t nlim; // limit for network sort
82a04cd1
AM
57};
58
ce0454d9
AM
59/* Like sort_ctx, but for use with qsort_r-style comparators. Several
60 functions in this file are templates that work with either context type. */
61struct sort_r_ctx
62{
63 void *data;
64 sort_r_cmp_fn *cmp_;
65 char *out;
66 size_t n;
67 size_t size;
68 size_t nlim;
69 int cmp (const void *a, const void *b)
70 {
71 return cmp_ (a, b, data);
72 }
73};
74
82a04cd1
AM
75/* Helper for netsort. Permute, possibly in-place, 2 or 3 elements,
76 placing E0 to C->OUT, E1 to C->OUT + C->SIZE, and so on. */
ce0454d9 77template<typename sort_ctx>
82a04cd1
AM
78static void
79reorder23 (sort_ctx *c, char *e0, char *e1, char *e2)
80{
c86ed2f1
AM
81#define REORDER_23(TYPE, STRIDE, OFFSET) \
82do { \
83 TYPE t0, t1; \
84 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
85 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
86 char *out = c->out + OFFSET; \
22d9c880 87 if (LIKELY (c->n == 3)) \
21925ac1 88 memmove (out + 2*STRIDE, e2 + OFFSET, sizeof (TYPE));\
c86ed2f1
AM
89 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
90 memcpy (out, &t1, sizeof (TYPE)); \
82a04cd1
AM
91} while (0)
92
22d9c880 93 if (LIKELY (c->size == sizeof (size_t)))
c86ed2f1 94 REORDER_23 (size_t, sizeof (size_t), 0);
22d9c880 95 else if (LIKELY (c->size == sizeof (int)))
c86ed2f1 96 REORDER_23 (int, sizeof (int), 0);
82a04cd1
AM
97 else
98 {
99 size_t offset = 0, step = sizeof (size_t);
100 for (; offset + step <= c->size; offset += step)
c86ed2f1 101 REORDER_23 (size_t, c->size, offset);
82a04cd1 102 for (; offset < c->size; offset++)
c86ed2f1 103 REORDER_23 (char, c->size, offset);
82a04cd1
AM
104 }
105}
106
107/* Like reorder23, but permute 4 or 5 elements. */
ce0454d9 108template<typename sort_ctx>
82a04cd1
AM
109static void
110reorder45 (sort_ctx *c, char *e0, char *e1, char *e2, char *e3, char *e4)
111{
c86ed2f1
AM
112#define REORDER_45(TYPE, STRIDE, OFFSET) \
113do { \
114 TYPE t0, t1, t2, t3; \
115 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
116 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
117 memcpy (&t2, e2 + OFFSET, sizeof (TYPE)); \
118 memcpy (&t3, e3 + OFFSET, sizeof (TYPE)); \
119 char *out = c->out + OFFSET; \
22d9c880 120 if (LIKELY (c->n == 5)) \
21925ac1 121 memmove (out + 4*STRIDE, e4 + OFFSET, sizeof (TYPE));\
c86ed2f1
AM
122 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
123 memcpy (out, &t1, sizeof (TYPE)); out += STRIDE; \
124 memcpy (out, &t2, sizeof (TYPE)); out += STRIDE; \
125 memcpy (out, &t3, sizeof (TYPE)); \
82a04cd1
AM
126} while (0)
127
22d9c880 128 if (LIKELY (c->size == sizeof (size_t)))
c86ed2f1 129 REORDER_45 (size_t, sizeof (size_t), 0);
22d9c880 130 else if (LIKELY (c->size == sizeof (int)))
c86ed2f1 131 REORDER_45 (int, sizeof (int), 0);
82a04cd1
AM
132 else
133 {
134 size_t offset = 0, step = sizeof (size_t);
135 for (; offset + step <= c->size; offset += step)
c86ed2f1 136 REORDER_45 (size_t, c->size, offset);
82a04cd1 137 for (; offset < c->size; offset++)
c86ed2f1 138 REORDER_45 (char, c->size, offset);
82a04cd1
AM
139 }
140}
141
142/* Helper for netsort. Invoke comparator CMP on E0 and E1.
143 Return E0^E1 if E0 compares less than E1, zero otherwise.
144 This is noinline to avoid code growth and confine invocation
145 to a single call site, assisting indirect branch prediction. */
ce0454d9 146template<typename sort_ctx>
82a04cd1 147noinline static intptr_t
ce0454d9 148cmp1 (char *e0, char *e1, sort_ctx *c)
82a04cd1
AM
149{
150 intptr_t x = (intptr_t)e0 ^ (intptr_t)e1;
ce0454d9 151 return x & (c->cmp (e0, e1) >> 31);
82a04cd1
AM
152}
153
154/* Execute network sort on 2 to 5 elements from IN, placing them into C->OUT.
155 IN may be equal to C->OUT, in which case elements are sorted in place. */
ce0454d9 156template<typename sort_ctx>
82a04cd1
AM
157static void
158netsort (char *in, sort_ctx *c)
159{
160#define CMP(e0, e1) \
161do { \
ce0454d9 162 intptr_t x = cmp1 (e1, e0, c); \
82a04cd1
AM
163 e0 = (char *)((intptr_t)e0 ^ x); \
164 e1 = (char *)((intptr_t)e1 ^ x); \
165} while (0)
166
167 char *e0 = in, *e1 = e0 + c->size, *e2 = e1 + c->size;
168 CMP (e0, e1);
22d9c880 169 if (LIKELY (c->n == 3))
82a04cd1
AM
170 {
171 CMP (e1, e2);
172 CMP (e0, e1);
173 }
174 if (c->n <= 3)
175 return reorder23 (c, e0, e1, e2);
176 char *e3 = e2 + c->size, *e4 = e3 + c->size;
22d9c880 177 if (LIKELY (c->n == 5))
82a04cd1
AM
178 {
179 CMP (e3, e4);
180 CMP (e2, e4);
181 }
182 CMP (e2, e3);
22d9c880 183 if (LIKELY (c->n == 5))
82a04cd1
AM
184 {
185 CMP (e0, e3);
186 CMP (e1, e4);
187 }
188 CMP (e0, e2);
189 CMP (e1, e3);
190 CMP (e1, e2);
191 reorder45 (c, e0, e1, e2, e3, e4);
192}
193
194/* Execute merge sort on N elements from IN, placing them into OUT,
195 using TMP as temporary storage if IN is equal to OUT.
196 This is a stable sort if netsort is used only for 2 or 3 elements. */
ce0454d9 197template<typename sort_ctx>
82a04cd1
AM
198static void
199mergesort (char *in, sort_ctx *c, size_t n, char *out, char *tmp)
200{
22d9c880 201 if (LIKELY (n <= c->nlim))
82a04cd1
AM
202 {
203 c->out = out;
204 c->n = n;
205 return netsort (in, c);
206 }
207 size_t nl = n / 2, nr = n - nl, sz = nl * c->size;
208 char *mid = in + sz, *r = out + sz, *l = in == out ? tmp : in;
209 /* Sort the right half, outputting to right half of OUT. */
210 mergesort (mid, c, nr, r, tmp);
211 /* Sort the left half, leaving left half of OUT free. */
212 mergesort (in, c, nl, l, mid);
213 /* Merge sorted halves given by L, R to [OUT, END). */
214#define MERGE_ELTSIZE(SIZE) \
215do { \
216 intptr_t mr = c->cmp (r, l) >> 31; \
217 intptr_t lr = (intptr_t)l ^ (intptr_t)r; \
218 lr = (intptr_t)l ^ (lr & mr); \
219 out = (char *)memcpy (out, (char *)lr, SIZE); \
220 out += SIZE; \
221 r += mr & SIZE; \
222 if (r == out) return; \
223 l += ~mr & SIZE; \
224} while (r != end)
225
22d9c880 226 if (LIKELY (c->cmp (r, l + (r - out) - c->size) < 0))
82a04cd1
AM
227 {
228 char *end = out + n * c->size;
22d9c880 229 if (sizeof (size_t) == 8 && LIKELY (c->size == 8))
82a04cd1 230 MERGE_ELTSIZE (8);
22d9c880 231 else if (LIKELY (c->size == 4))
82a04cd1
AM
232 MERGE_ELTSIZE (4);
233 else
234 MERGE_ELTSIZE (c->size);
235 }
236 memcpy (out, l, r - out);
237}
238
ce0454d9 239#if CHECKING_P
1e007fa5
JM
240 /* Don't complain about cast from void* to function pointer. */
241#pragma GCC diagnostic push
242#pragma GCC diagnostic ignored "-Wconditionally-supported"
243
ce0454d9
AM
244/* Adapter for using two-argument comparators in functions expecting the
245 three-argument sort_r_cmp_fn type. */
246static int
247cmp2to3 (const void *a, const void *b, void *c)
248{
249 return ((cmp_fn *)c) (a, b);
250}
251#endif
252
253/* Replacement for C qsort. */
82a04cd1
AM
254void
255gcc_qsort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
256{
257 if (n < 2)
258 return;
a6405b11
AM
259 size_t nlim = 5;
260 bool stable = (ssize_t) size < 0;
261 if (stable)
262 nlim = 3, size = ~size;
82a04cd1 263 char *base = (char *)vbase;
a6405b11 264 sort_ctx c = {cmp, base, n, size, nlim};
82a04cd1
AM
265 long long scratch[32];
266 size_t bufsz = (n / 2) * size;
267 void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
268 mergesort (base, &c, n, base, (char *)buf);
269 if (buf != scratch)
270 free (buf);
71acd8b9 271#if CHECKING_P
ce0454d9 272 qsort_chk (vbase, n, size, cmp2to3, (void*)cmp);
1e007fa5 273#pragma GCC diagnostic pop
ce0454d9
AM
274#endif
275}
276
277/* Substitute for Glibc qsort_r. */
278void
279gcc_sort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data)
280{
281 if (n < 2)
282 return;
367f52dc
RB
283 size_t nlim = 5;
284 bool stable = (ssize_t) size < 0;
285 if (stable)
286 nlim = 3, size = ~size;
ce0454d9 287 char *base = (char *)vbase;
367f52dc 288 sort_r_ctx c = {data, cmp, base, n, size, nlim};
ce0454d9
AM
289 long long scratch[32];
290 size_t bufsz = (n / 2) * size;
291 void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
292 mergesort (base, &c, n, base, (char *)buf);
293 if (buf != scratch)
294 free (buf);
295#if CHECKING_P
296 qsort_chk (vbase, n, size, cmp, data);
71acd8b9 297#endif
82a04cd1 298}
a6405b11 299
ce0454d9 300/* Stable sort, signature-compatible to C qsort. */
a6405b11
AM
301void
302gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
303{
304 gcc_qsort (vbase, n, ~size, cmp);
305}
367f52dc
RB
306
307/* Stable sort, signature-compatible to Glibc qsort_r. */
308void
309gcc_stablesort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp,
310 void *data)
311{
312 gcc_sort_r (vbase, n, ~size, cmp, data);
313}
This page took 3.602059 seconds and 5 git commands to generate.