1*c42dbd0eSchristos /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2867d70fcSchristos This file is part of libctf (imported from Gnulib).
3867d70fcSchristos Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
4867d70fcSchristos
5867d70fcSchristos The GNU C Library is free software; you can redistribute it and/or
6867d70fcSchristos modify it under the terms of the GNU Lesser General Public
7867d70fcSchristos License as published by the Free Software Foundation; either
8867d70fcSchristos version 2.1 of the License, or (at your option) any later version.
9867d70fcSchristos
10867d70fcSchristos The GNU C Library is distributed in the hope that it will be useful,
11867d70fcSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12867d70fcSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13867d70fcSchristos Lesser General Public License for more details.
14867d70fcSchristos
15867d70fcSchristos You should have received a copy of the GNU Lesser General Public
16867d70fcSchristos License along with the GNU C Library; if not, see
17867d70fcSchristos <https://www.gnu.org/licenses/>. */
18867d70fcSchristos
19867d70fcSchristos /* If you consider tuning this algorithm, you should consult first:
20867d70fcSchristos Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
21867d70fcSchristos Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993. */
22867d70fcSchristos
23867d70fcSchristos #ifndef _LIBC
24867d70fcSchristos # include <config.h>
25867d70fcSchristos #endif
26867d70fcSchristos
27867d70fcSchristos #include <limits.h>
28867d70fcSchristos #include <stdlib.h>
29867d70fcSchristos #include <string.h>
30867d70fcSchristos #include "ctf-decls.h"
31867d70fcSchristos
32867d70fcSchristos #ifndef _LIBC
33867d70fcSchristos # define _quicksort ctf_qsort_r
34867d70fcSchristos # define __compar_d_fn_t compar_d_fn_t
35867d70fcSchristos typedef int (*compar_d_fn_t) (const void *, const void *, void *);
36867d70fcSchristos #endif
37867d70fcSchristos
38867d70fcSchristos /* Byte-wise swap two items of size SIZE. */
39867d70fcSchristos #define SWAP(a, b, size) \
40867d70fcSchristos do \
41867d70fcSchristos { \
42867d70fcSchristos size_t __size = (size); \
43867d70fcSchristos char *__a = (a), *__b = (b); \
44867d70fcSchristos do \
45867d70fcSchristos { \
46867d70fcSchristos char __tmp = *__a; \
47867d70fcSchristos *__a++ = *__b; \
48867d70fcSchristos *__b++ = __tmp; \
49867d70fcSchristos } while (--__size > 0); \
50867d70fcSchristos } while (0)
51867d70fcSchristos
52867d70fcSchristos /* Discontinue quicksort algorithm when partition gets below this size.
53867d70fcSchristos This particular magic number was chosen to work best on a Sun 4/260. */
54867d70fcSchristos #define MAX_THRESH 4
55867d70fcSchristos
56867d70fcSchristos /* Stack node declarations used to store unfulfilled partition obligations. */
57867d70fcSchristos typedef struct
58867d70fcSchristos {
59867d70fcSchristos char *lo;
60867d70fcSchristos char *hi;
61867d70fcSchristos } stack_node;
62867d70fcSchristos
63867d70fcSchristos /* The next 4 #defines implement a very fast in-line stack abstraction. */
64867d70fcSchristos /* The stack needs log (total_elements) entries (we could even subtract
65867d70fcSchristos log(MAX_THRESH)). Since total_elements has type size_t, we get as
66867d70fcSchristos upper bound for log (total_elements):
67867d70fcSchristos bits per byte (CHAR_BIT) * sizeof(size_t). */
68867d70fcSchristos #define STACK_SIZE (CHAR_BIT * sizeof(size_t))
69867d70fcSchristos #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
70867d70fcSchristos #define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
71867d70fcSchristos #define STACK_NOT_EMPTY (stack < top)
72867d70fcSchristos
73867d70fcSchristos
74867d70fcSchristos /* Order size using quicksort. This implementation incorporates
75867d70fcSchristos four optimizations discussed in Sedgewick:
76867d70fcSchristos
77867d70fcSchristos 1. Non-recursive, using an explicit stack of pointer that store the
78867d70fcSchristos next array partition to sort. To save time, this maximum amount
79867d70fcSchristos of space required to store an array of SIZE_MAX is allocated on the
80867d70fcSchristos stack. Assuming a 32-bit (64 bit) integer for size_t, this needs
81867d70fcSchristos only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
82867d70fcSchristos Pretty cheap, actually.
83867d70fcSchristos
84867d70fcSchristos 2. Chose the pivot element using a median-of-three decision tree.
85867d70fcSchristos This reduces the probability of selecting a bad pivot value and
86867d70fcSchristos eliminates certain extraneous comparisons.
87867d70fcSchristos
88867d70fcSchristos 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
89867d70fcSchristos insertion sort to order the MAX_THRESH items within each partition.
90867d70fcSchristos This is a big win, since insertion sort is faster for small, mostly
91867d70fcSchristos sorted array segments.
92867d70fcSchristos
93867d70fcSchristos 4. The larger of the two sub-partitions is always pushed onto the
94867d70fcSchristos stack first, with the algorithm then concentrating on the
95867d70fcSchristos smaller partition. This *guarantees* no more than log (total_elems)
96867d70fcSchristos stack size is needed (actually O(1) in this case)! */
97867d70fcSchristos
98867d70fcSchristos void
_quicksort(void * const pbase,size_t total_elems,size_t size,__compar_d_fn_t cmp,void * arg)99867d70fcSchristos _quicksort (void *const pbase, size_t total_elems, size_t size,
100867d70fcSchristos __compar_d_fn_t cmp, void *arg)
101867d70fcSchristos {
102867d70fcSchristos char *base_ptr = (char *) pbase;
103867d70fcSchristos
104867d70fcSchristos const size_t max_thresh = MAX_THRESH * size;
105867d70fcSchristos
106867d70fcSchristos if (total_elems == 0)
107867d70fcSchristos /* Avoid lossage with unsigned arithmetic below. */
108867d70fcSchristos return;
109867d70fcSchristos
110867d70fcSchristos if (total_elems > MAX_THRESH)
111867d70fcSchristos {
112867d70fcSchristos char *lo = base_ptr;
113867d70fcSchristos char *hi = &lo[size * (total_elems - 1)];
114867d70fcSchristos stack_node stack[STACK_SIZE];
115867d70fcSchristos stack_node *top = stack;
116867d70fcSchristos
117867d70fcSchristos PUSH (NULL, NULL);
118867d70fcSchristos
119867d70fcSchristos while (STACK_NOT_EMPTY)
120867d70fcSchristos {
121867d70fcSchristos char *left_ptr;
122867d70fcSchristos char *right_ptr;
123867d70fcSchristos
124867d70fcSchristos /* Select median value from among LO, MID, and HI. Rearrange
125867d70fcSchristos LO and HI so the three values are sorted. This lowers the
126867d70fcSchristos probability of picking a pathological pivot value and
127867d70fcSchristos skips a comparison for both the LEFT_PTR and RIGHT_PTR in
128867d70fcSchristos the while loops. */
129867d70fcSchristos
130867d70fcSchristos char *mid = lo + size * ((hi - lo) / size >> 1);
131867d70fcSchristos
132867d70fcSchristos if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
133867d70fcSchristos SWAP (mid, lo, size);
134867d70fcSchristos if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
135867d70fcSchristos SWAP (mid, hi, size);
136867d70fcSchristos else
137867d70fcSchristos goto jump_over;
138867d70fcSchristos if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
139867d70fcSchristos SWAP (mid, lo, size);
140867d70fcSchristos jump_over:;
141867d70fcSchristos
142867d70fcSchristos left_ptr = lo + size;
143867d70fcSchristos right_ptr = hi - size;
144867d70fcSchristos
145867d70fcSchristos /* Here's the famous ``collapse the walls'' section of quicksort.
146867d70fcSchristos Gotta like those tight inner loops! They are the main reason
147867d70fcSchristos that this algorithm runs much faster than others. */
148867d70fcSchristos do
149867d70fcSchristos {
150867d70fcSchristos while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
151867d70fcSchristos left_ptr += size;
152867d70fcSchristos
153867d70fcSchristos while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
154867d70fcSchristos right_ptr -= size;
155867d70fcSchristos
156867d70fcSchristos if (left_ptr < right_ptr)
157867d70fcSchristos {
158867d70fcSchristos SWAP (left_ptr, right_ptr, size);
159867d70fcSchristos if (mid == left_ptr)
160867d70fcSchristos mid = right_ptr;
161867d70fcSchristos else if (mid == right_ptr)
162867d70fcSchristos mid = left_ptr;
163867d70fcSchristos left_ptr += size;
164867d70fcSchristos right_ptr -= size;
165867d70fcSchristos }
166867d70fcSchristos else if (left_ptr == right_ptr)
167867d70fcSchristos {
168867d70fcSchristos left_ptr += size;
169867d70fcSchristos right_ptr -= size;
170867d70fcSchristos break;
171867d70fcSchristos }
172867d70fcSchristos }
173867d70fcSchristos while (left_ptr <= right_ptr);
174867d70fcSchristos
175867d70fcSchristos /* Set up pointers for next iteration. First determine whether
176867d70fcSchristos left and right partitions are below the threshold size. If so,
177867d70fcSchristos ignore one or both. Otherwise, push the larger partition's
178867d70fcSchristos bounds on the stack and continue sorting the smaller one. */
179867d70fcSchristos
180867d70fcSchristos if ((size_t) (right_ptr - lo) <= max_thresh)
181867d70fcSchristos {
182867d70fcSchristos if ((size_t) (hi - left_ptr) <= max_thresh)
183867d70fcSchristos /* Ignore both small partitions. */
184867d70fcSchristos POP (lo, hi);
185867d70fcSchristos else
186867d70fcSchristos /* Ignore small left partition. */
187867d70fcSchristos lo = left_ptr;
188867d70fcSchristos }
189867d70fcSchristos else if ((size_t) (hi - left_ptr) <= max_thresh)
190867d70fcSchristos /* Ignore small right partition. */
191867d70fcSchristos hi = right_ptr;
192867d70fcSchristos else if ((right_ptr - lo) > (hi - left_ptr))
193867d70fcSchristos {
194867d70fcSchristos /* Push larger left partition indices. */
195867d70fcSchristos PUSH (lo, right_ptr);
196867d70fcSchristos lo = left_ptr;
197867d70fcSchristos }
198867d70fcSchristos else
199867d70fcSchristos {
200867d70fcSchristos /* Push larger right partition indices. */
201867d70fcSchristos PUSH (left_ptr, hi);
202867d70fcSchristos hi = right_ptr;
203867d70fcSchristos }
204867d70fcSchristos }
205867d70fcSchristos }
206867d70fcSchristos
207867d70fcSchristos /* Once the BASE_PTR array is partially sorted by quicksort the rest
208867d70fcSchristos is completely sorted using insertion sort, since this is efficient
209867d70fcSchristos for partitions below MAX_THRESH size. BASE_PTR points to the beginning
210867d70fcSchristos of the array to sort, and END_PTR points at the very last element in
211867d70fcSchristos the array (*not* one beyond it!). */
212867d70fcSchristos
213867d70fcSchristos #define min(x, y) ((x) < (y) ? (x) : (y))
214867d70fcSchristos
215867d70fcSchristos {
216867d70fcSchristos char *const end_ptr = &base_ptr[size * (total_elems - 1)];
217867d70fcSchristos char *tmp_ptr = base_ptr;
218867d70fcSchristos char *thresh = min(end_ptr, base_ptr + max_thresh);
219867d70fcSchristos char *run_ptr;
220867d70fcSchristos
221867d70fcSchristos /* Find smallest element in first threshold and place it at the
222867d70fcSchristos array's beginning. This is the smallest array element,
223867d70fcSchristos and the operation speeds up insertion sort's inner loop. */
224867d70fcSchristos
225867d70fcSchristos for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
226867d70fcSchristos if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
227867d70fcSchristos tmp_ptr = run_ptr;
228867d70fcSchristos
229867d70fcSchristos if (tmp_ptr != base_ptr)
230867d70fcSchristos SWAP (tmp_ptr, base_ptr, size);
231867d70fcSchristos
232867d70fcSchristos /* Insertion sort, running from left-hand-side up to right-hand-side. */
233867d70fcSchristos
234867d70fcSchristos run_ptr = base_ptr + size;
235867d70fcSchristos while ((run_ptr += size) <= end_ptr)
236867d70fcSchristos {
237867d70fcSchristos tmp_ptr = run_ptr - size;
238867d70fcSchristos while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
239867d70fcSchristos tmp_ptr -= size;
240867d70fcSchristos
241867d70fcSchristos tmp_ptr += size;
242867d70fcSchristos if (tmp_ptr != run_ptr)
243867d70fcSchristos {
244867d70fcSchristos char *trav;
245867d70fcSchristos
246867d70fcSchristos trav = run_ptr + size;
247867d70fcSchristos while (--trav >= run_ptr)
248867d70fcSchristos {
249867d70fcSchristos char c = *trav;
250867d70fcSchristos char *hi, *lo;
251867d70fcSchristos
252867d70fcSchristos for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
253867d70fcSchristos *hi = *lo;
254867d70fcSchristos *hi = c;
255867d70fcSchristos }
256867d70fcSchristos }
257867d70fcSchristos }
258867d70fcSchristos }
259867d70fcSchristos }
260