1 /* Platform-independent deterministic sort function.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 Contributed by Alexander Monakov.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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)
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. */
40 #define likely(cond) __builtin_expect ((cond), 1)
43 #define noinline __attribute__ ((__noinline__))
48 /* C-style qsort comparator function type. */
49 typedef int cmp_fn (const void *, const void *);
51 /* Structure holding read-mostly (read-only in netsort) context. */
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
60 /* Helper for netsort. Permute, possibly in-place, 2 or 3 elements,
61 placing E0 to C->OUT, E1 to C->OUT + C->SIZE, and so on. */
63 reorder23 (sort_ctx
*c
, char *e0
, char *e1
, char *e2
)
65 #define REORDER_23(TYPE, STRIDE, OFFSET) \
68 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
69 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
70 char *out = c->out + OFFSET; \
71 if (likely (c->n == 3)) \
72 memmove (out + 2*STRIDE, e2 + OFFSET, sizeof (TYPE));\
73 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
74 memcpy (out, &t1, sizeof (TYPE)); \
77 if (likely (c
->size
== sizeof (size_t)))
78 REORDER_23 (size_t, sizeof (size_t), 0);
79 else if (likely (c
->size
== sizeof (int)))
80 REORDER_23 (int, sizeof (int), 0);
83 size_t offset
= 0, step
= sizeof (size_t);
84 for (; offset
+ step
<= c
->size
; offset
+= step
)
85 REORDER_23 (size_t, c
->size
, offset
);
86 for (; offset
< c
->size
; offset
++)
87 REORDER_23 (char, c
->size
, offset
);
91 /* Like reorder23, but permute 4 or 5 elements. */
93 reorder45 (sort_ctx
*c
, char *e0
, char *e1
, char *e2
, char *e3
, char *e4
)
95 #define REORDER_45(TYPE, STRIDE, OFFSET) \
97 TYPE t0, t1, t2, t3; \
98 memcpy (&t0, e0 + OFFSET, sizeof (TYPE)); \
99 memcpy (&t1, e1 + OFFSET, sizeof (TYPE)); \
100 memcpy (&t2, e2 + OFFSET, sizeof (TYPE)); \
101 memcpy (&t3, e3 + OFFSET, sizeof (TYPE)); \
102 char *out = c->out + OFFSET; \
103 if (likely (c->n == 5)) \
104 memmove (out + 4*STRIDE, e4 + OFFSET, sizeof (TYPE));\
105 memcpy (out, &t0, sizeof (TYPE)); out += STRIDE; \
106 memcpy (out, &t1, sizeof (TYPE)); out += STRIDE; \
107 memcpy (out, &t2, sizeof (TYPE)); out += STRIDE; \
108 memcpy (out, &t3, sizeof (TYPE)); \
111 if (likely (c
->size
== sizeof (size_t)))
112 REORDER_45 (size_t, sizeof (size_t), 0);
113 else if (likely(c
->size
== sizeof (int)))
114 REORDER_45 (int, sizeof (int), 0);
117 size_t offset
= 0, step
= sizeof (size_t);
118 for (; offset
+ step
<= c
->size
; offset
+= step
)
119 REORDER_45 (size_t, c
->size
, offset
);
120 for (; offset
< c
->size
; offset
++)
121 REORDER_45 (char, c
->size
, offset
);
125 /* Helper for netsort. Invoke comparator CMP on E0 and E1.
126 Return E0^E1 if E0 compares less than E1, zero otherwise.
127 This is noinline to avoid code growth and confine invocation
128 to a single call site, assisting indirect branch prediction. */
129 noinline
static intptr_t
130 cmp1 (char *e0
, char *e1
, cmp_fn
*cmp
)
132 intptr_t x
= (intptr_t)e0
^ (intptr_t)e1
;
133 return x
& (cmp (e0
, e1
) >> 31);
136 /* Execute network sort on 2 to 5 elements from IN, placing them into C->OUT.
137 IN may be equal to C->OUT, in which case elements are sorted in place. */
139 netsort (char *in
, sort_ctx
*c
)
141 #define CMP(e0, e1) \
143 intptr_t x = cmp1 (e1, e0, c->cmp); \
144 e0 = (char *)((intptr_t)e0 ^ x); \
145 e1 = (char *)((intptr_t)e1 ^ x); \
148 char *e0
= in
, *e1
= e0
+ c
->size
, *e2
= e1
+ c
->size
;
150 if (likely (c
->n
== 3))
156 return reorder23 (c
, e0
, e1
, e2
);
157 char *e3
= e2
+ c
->size
, *e4
= e3
+ c
->size
;
158 if (likely (c
->n
== 5))
164 if (likely (c
->n
== 5))
172 reorder45 (c
, e0
, e1
, e2
, e3
, e4
);
175 /* Execute merge sort on N elements from IN, placing them into OUT,
176 using TMP as temporary storage if IN is equal to OUT.
177 This is a stable sort if netsort is used only for 2 or 3 elements. */
179 mergesort (char *in
, sort_ctx
*c
, size_t n
, char *out
, char *tmp
)
185 return netsort (in
, c
);
187 size_t nl
= n
/ 2, nr
= n
- nl
, sz
= nl
* c
->size
;
188 char *mid
= in
+ sz
, *r
= out
+ sz
, *l
= in
== out
? tmp
: in
;
189 /* Sort the right half, outputting to right half of OUT. */
190 mergesort (mid
, c
, nr
, r
, tmp
);
191 /* Sort the left half, leaving left half of OUT free. */
192 mergesort (in
, c
, nl
, l
, mid
);
193 /* Merge sorted halves given by L, R to [OUT, END). */
194 #define MERGE_ELTSIZE(SIZE) \
196 intptr_t mr = c->cmp (r, l) >> 31; \
197 intptr_t lr = (intptr_t)l ^ (intptr_t)r; \
198 lr = (intptr_t)l ^ (lr & mr); \
199 out = (char *)memcpy (out, (char *)lr, SIZE); \
202 if (r == out) return; \
206 if (likely (c
->cmp(r
, l
+ (r
- out
) - c
->size
) < 0))
208 char *end
= out
+ n
* c
->size
;
209 if (sizeof (size_t) == 8 && likely (c
->size
== 8))
211 else if (likely (c
->size
== 4))
214 MERGE_ELTSIZE (c
->size
);
216 memcpy (out
, l
, r
- out
);
220 gcc_qsort (void *vbase
, size_t n
, size_t size
, cmp_fn
*cmp
)
224 char *base
= (char *)vbase
;
225 sort_ctx c
= {cmp
, base
, n
, size
};
226 long long scratch
[32];
227 size_t bufsz
= (n
/ 2) * size
;
228 void *buf
= bufsz
<= sizeof scratch
? scratch
: xmalloc (bufsz
);
229 mergesort (base
, &c
, n
, base
, (char *)buf
);