1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
5 #include <internal/tls.h>
8 * Copyright (c) 1980, 1983 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that: (1) source distributions retain this entire copyright
13 * notice and comment, and (2) distributions including binaries display
14 * the following acknowledgement: ``This product includes software
15 * developed by the University of California, Berkeley and its contributors''
16 * in the documentation or other materials provided with the distribution
17 * and in all advertising materials mentioning features or use of this
18 * software. Neither the name of the University nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 * Our own version of the system qsort routine which is faster by an average
29 * of 25%, with lows and highs of 10% and 50%.
30 * The THRESHold below is the insertion sort threshold, and has been adjusted
31 * for records of size 48 bytes.
32 * The MTHREShold is where we stop finding a better median.
35 #define THRESH 4 /* threshold for insertion */
36 #define MTHRESH 6 /* threshold for median */
41 * First, find the median element, and put that one in the first place as the
42 * discriminator. (This "median" is just the median of the first, last and
43 * middle elements). (Using this median instead of the first element is a big
44 * win). Then, the usual partitioning/swapping, followed by moving the
45 * discriminator into the right place. Then, figure out the sizes of the two
46 * partions, do the smaller one recursively and the larger one via a repeat of
47 * this code. Stopping when there are less than THRESH elements in a partition
48 * and cleaning up with an insertion sort (in our caller) is a huge win.
49 * All data swaps are done in-line, which is space-losing but time-saving.
50 * (And there are only three places where this is done).
54 qst(PTHREADDATA pThreadData
, char *base
, char *max
)
62 * At the top here, lo is the number of characters of elements in the
63 * current partition. (Which should be max - base).
64 * Find the median of the first, last, and middle element and make
65 * that the middle element. Set j to largest of first and middle.
66 * If max is larger than that guy, then it's that guy, else compare
67 * max with loser of first and take larger. Things are set up to
68 * prefer the middle, then the first in case of ties.
70 lo
= max
- base
; /* number of elements as chars */
72 mid
= i
= base
+ pThreadData
->qsz
* ((lo
/ pThreadData
->qsz
) >> 1);
73 if (lo
>= pThreadData
->mthresh
)
75 j
= (pThreadData
->qcmp((jj
= base
), i
) > 0 ? jj
: i
);
76 if (pThreadData
->qcmp(j
, (tmp
= max
- pThreadData
->qsz
)) > 0)
78 /* switch to first loser */
79 j
= (j
== jj
? i
: jj
);
80 if (pThreadData
->qcmp(j
, tmp
) < 0)
85 ii
= pThreadData
->qsz
;
94 * Semi-standard quicksort partitioning/swapping
96 for (i
= base
, j
= max
- pThreadData
->qsz
; ; )
98 while (i
< mid
&& pThreadData
->qcmp(i
, mid
) <= 0)
99 i
+= pThreadData
->qsz
;
102 if (pThreadData
->qcmp(mid
, j
) <= 0)
104 j
-= pThreadData
->qsz
;
107 tmp
= i
+ pThreadData
->qsz
; /* value of i after swap */
110 /* j <-> mid, new mid is j */
117 j
-= pThreadData
->qsz
;
127 /* i <-> mid, new mid is i */
129 tmp
= mid
= i
; /* value of i after swap */
130 j
-= pThreadData
->qsz
;
133 ii
= pThreadData
->qsz
;
142 * Look at sizes of the two partitions, do the smaller
143 * one first by recursion, then do the larger one by
144 * making sure lo is its size, base and max are update
145 * correctly, and branching back. But only repeat
146 * (recursively or by branching) if the partition is
147 * of at least size THRESH.
149 i
= (j
= mid
) + pThreadData
->qsz
;
150 if ((lo
= j
- base
) <= (hi
= max
- i
))
152 if (lo
>= pThreadData
->thresh
)
153 qst(pThreadData
, base
, j
);
159 if (hi
>= pThreadData
->thresh
)
160 qst(pThreadData
, i
, max
);
163 } while (lo
>= pThreadData
->thresh
);
168 * First, set up some global parameters for qst to share. Then, quicksort
169 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
175 qsort(void *base0
, size_t n
, size_t size
, int (*compar
)(const void*, const void*))
177 PTHREADDATA pThreadData
;
178 char *base
= (char *)base0
;
179 char c
, *i
, *j
, *lo
, *hi
;
185 pThreadData
= GetThreadData();
187 pThreadData
->qsz
= size
;
188 pThreadData
->qcmp
= compar
;
189 pThreadData
->thresh
= pThreadData
->qsz
* THRESH
;
190 pThreadData
->mthresh
= pThreadData
->qsz
* MTHRESH
;
191 max
= base
+ n
* pThreadData
->qsz
;
194 qst(pThreadData
, base
, max
);
195 hi
= base
+ pThreadData
->thresh
;
202 * First put smallest element, which must be in the first THRESH, in
203 * the first position as a sentinel. This is done just by searching
204 * the first THRESH elements (or the first n if n < THRESH), finding
205 * the min, and swapping it into the first position.
207 for (j
= lo
= base
; (lo
+= pThreadData
->qsz
) < hi
; )
208 if (pThreadData
->qcmp(j
, lo
) > 0)
212 /* swap j into place */
213 for (i
= base
, hi
= base
+ pThreadData
->qsz
; i
< hi
; )
221 * With our sentinel in place, we now run the following hyper-fast
222 * insertion sort. For each remaining element, min, from [1] to [n-1],
223 * set hi to the index of the element AFTER which this one goes.
224 * Then, do the standard insertion sort shift on a character at a time
225 * basis for each element in the frob.
227 for (min
= base
; (hi
= min
+= pThreadData
->qsz
) < max
; )
229 while (pThreadData
->qcmp(hi
-= pThreadData
->qsz
, min
) > 0)
231 if ((hi
+= pThreadData
->qsz
) != min
) {
232 for (lo
= min
+ pThreadData
->qsz
; --lo
>= min
; )
235 for (i
= j
= lo
; (j
-= pThreadData
->qsz
) >= hi
; i
= j
)