]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sort.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / sort.cc
CommitLineData
76ff64b0 1/* Platform-independent deterministic sort function.
fbd26352 2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
76ff64b0 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
40#define likely(cond) __builtin_expect ((cond), 1)
41
42#ifdef __GNUC__
43#define noinline __attribute__ ((__noinline__))
44#else
45#define noinline
46#endif
47
48/* C-style qsort comparator function type. */
49typedef int cmp_fn (const void *, const void *);
50
51/* Structure holding read-mostly (read-only in netsort) context. */
52struct sort_ctx
53{
54 cmp_fn *cmp; // pointer to comparator
55 char *out; // output buffer
56 size_t n; // number of elements
57 size_t size; // element size
ad5d5929 58 size_t nlim; // limit for network sort
76ff64b0 59};
60
61/* Helper for netsort. Permute, possibly in-place, 2 or 3 elements,
62 placing E0 to C->OUT, E1 to C->OUT + C->SIZE, and so on. */
63static void
64reorder23 (sort_ctx *c, char *e0, char *e1, char *e2)
65{
f33bb9d4 66#define REORDER_23(TYPE, STRIDE, OFFSET) \
67do { \
68 TYPE t0, t1; \
69 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
70 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
71 char *out = c->out + OFFSET; \
72 if (likely (c->n == 3)) \
4606c7b9 73 memmove (out + 2*STRIDE, e2 + OFFSET, sizeof (TYPE));\
f33bb9d4 74 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
75 memcpy (out, &t1, sizeof (TYPE)); \
76ff64b0 76} while (0)
77
f33bb9d4 78 if (likely (c->size == sizeof (size_t)))
79 REORDER_23 (size_t, sizeof (size_t), 0);
80 else if (likely (c->size == sizeof (int)))
81 REORDER_23 (int, sizeof (int), 0);
76ff64b0 82 else
83 {
84 size_t offset = 0, step = sizeof (size_t);
85 for (; offset + step <= c->size; offset += step)
f33bb9d4 86 REORDER_23 (size_t, c->size, offset);
76ff64b0 87 for (; offset < c->size; offset++)
f33bb9d4 88 REORDER_23 (char, c->size, offset);
76ff64b0 89 }
90}
91
92/* Like reorder23, but permute 4 or 5 elements. */
93static void
94reorder45 (sort_ctx *c, char *e0, char *e1, char *e2, char *e3, char *e4)
95{
f33bb9d4 96#define REORDER_45(TYPE, STRIDE, OFFSET) \
97do { \
98 TYPE t0, t1, t2, t3; \
99 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
100 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
101 memcpy (&t2, e2 + OFFSET, sizeof (TYPE)); \
102 memcpy (&t3, e3 + OFFSET, sizeof (TYPE)); \
103 char *out = c->out + OFFSET; \
104 if (likely (c->n == 5)) \
4606c7b9 105 memmove (out + 4*STRIDE, e4 + OFFSET, sizeof (TYPE));\
f33bb9d4 106 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
107 memcpy (out, &t1, sizeof (TYPE)); out += STRIDE; \
108 memcpy (out, &t2, sizeof (TYPE)); out += STRIDE; \
109 memcpy (out, &t3, sizeof (TYPE)); \
76ff64b0 110} while (0)
111
f33bb9d4 112 if (likely (c->size == sizeof (size_t)))
113 REORDER_45 (size_t, sizeof (size_t), 0);
114 else if (likely(c->size == sizeof (int)))
115 REORDER_45 (int, sizeof (int), 0);
76ff64b0 116 else
117 {
118 size_t offset = 0, step = sizeof (size_t);
119 for (; offset + step <= c->size; offset += step)
f33bb9d4 120 REORDER_45 (size_t, c->size, offset);
76ff64b0 121 for (; offset < c->size; offset++)
f33bb9d4 122 REORDER_45 (char, c->size, offset);
76ff64b0 123 }
124}
125
126/* Helper for netsort. Invoke comparator CMP on E0 and E1.
127 Return E0^E1 if E0 compares less than E1, zero otherwise.
128 This is noinline to avoid code growth and confine invocation
129 to a single call site, assisting indirect branch prediction. */
130noinline static intptr_t
131cmp1 (char *e0, char *e1, cmp_fn *cmp)
132{
133 intptr_t x = (intptr_t)e0 ^ (intptr_t)e1;
134 return x & (cmp (e0, e1) >> 31);
135}
136
137/* Execute network sort on 2 to 5 elements from IN, placing them into C->OUT.
138 IN may be equal to C->OUT, in which case elements are sorted in place. */
139static void
140netsort (char *in, sort_ctx *c)
141{
142#define CMP(e0, e1) \
143do { \
144 intptr_t x = cmp1 (e1, e0, c->cmp); \
145 e0 = (char *)((intptr_t)e0 ^ x); \
146 e1 = (char *)((intptr_t)e1 ^ x); \
147} while (0)
148
149 char *e0 = in, *e1 = e0 + c->size, *e2 = e1 + c->size;
150 CMP (e0, e1);
151 if (likely (c->n == 3))
152 {
153 CMP (e1, e2);
154 CMP (e0, e1);
155 }
156 if (c->n <= 3)
157 return reorder23 (c, e0, e1, e2);
158 char *e3 = e2 + c->size, *e4 = e3 + c->size;
159 if (likely (c->n == 5))
160 {
161 CMP (e3, e4);
162 CMP (e2, e4);
163 }
164 CMP (e2, e3);
165 if (likely (c->n == 5))
166 {
167 CMP (e0, e3);
168 CMP (e1, e4);
169 }
170 CMP (e0, e2);
171 CMP (e1, e3);
172 CMP (e1, e2);
173 reorder45 (c, e0, e1, e2, e3, e4);
174}
175
176/* Execute merge sort on N elements from IN, placing them into OUT,
177 using TMP as temporary storage if IN is equal to OUT.
178 This is a stable sort if netsort is used only for 2 or 3 elements. */
179static void
180mergesort (char *in, sort_ctx *c, size_t n, char *out, char *tmp)
181{
ad5d5929 182 if (likely (n <= c->nlim))
76ff64b0 183 {
184 c->out = out;
185 c->n = n;
186 return netsort (in, c);
187 }
188 size_t nl = n / 2, nr = n - nl, sz = nl * c->size;
189 char *mid = in + sz, *r = out + sz, *l = in == out ? tmp : in;
190 /* Sort the right half, outputting to right half of OUT. */
191 mergesort (mid, c, nr, r, tmp);
192 /* Sort the left half, leaving left half of OUT free. */
193 mergesort (in, c, nl, l, mid);
194 /* Merge sorted halves given by L, R to [OUT, END). */
195#define MERGE_ELTSIZE(SIZE) \
196do { \
197 intptr_t mr = c->cmp (r, l) >> 31; \
198 intptr_t lr = (intptr_t)l ^ (intptr_t)r; \
199 lr = (intptr_t)l ^ (lr & mr); \
200 out = (char *)memcpy (out, (char *)lr, SIZE); \
201 out += SIZE; \
202 r += mr & SIZE; \
203 if (r == out) return; \
204 l += ~mr & SIZE; \
205} while (r != end)
206
207 if (likely (c->cmp(r, l + (r - out) - c->size) < 0))
208 {
209 char *end = out + n * c->size;
210 if (sizeof (size_t) == 8 && likely (c->size == 8))
211 MERGE_ELTSIZE (8);
212 else if (likely (c->size == 4))
213 MERGE_ELTSIZE (4);
214 else
215 MERGE_ELTSIZE (c->size);
216 }
217 memcpy (out, l, r - out);
218}
219
220void
221gcc_qsort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
222{
223 if (n < 2)
224 return;
ad5d5929 225 size_t nlim = 5;
226 bool stable = (ssize_t) size < 0;
227 if (stable)
228 nlim = 3, size = ~size;
76ff64b0 229 char *base = (char *)vbase;
ad5d5929 230 sort_ctx c = {cmp, base, n, size, nlim};
76ff64b0 231 long long scratch[32];
232 size_t bufsz = (n / 2) * size;
233 void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
234 mergesort (base, &c, n, base, (char *)buf);
235 if (buf != scratch)
236 free (buf);
8c16143c 237#if CHECKING_P
238 qsort_chk (vbase, n, size, cmp);
239#endif
76ff64b0 240}
ad5d5929 241
242void
243gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
244{
245 gcc_qsort (vbase, n, ~size, cmp);
246}