1*1bb76ff1Sjsg /* rev 1.15 of lib/libc/stdlib/qsort.c */
2*1bb76ff1Sjsg /*-
3*1bb76ff1Sjsg * Copyright (c) 1992, 1993
4*1bb76ff1Sjsg * The Regents of the University of California. All rights reserved.
5*1bb76ff1Sjsg *
6*1bb76ff1Sjsg * Redistribution and use in source and binary forms, with or without
7*1bb76ff1Sjsg * modification, are permitted provided that the following conditions
8*1bb76ff1Sjsg * are met:
9*1bb76ff1Sjsg * 1. Redistributions of source code must retain the above copyright
10*1bb76ff1Sjsg * notice, this list of conditions and the following disclaimer.
11*1bb76ff1Sjsg * 2. Redistributions in binary form must reproduce the above copyright
12*1bb76ff1Sjsg * notice, this list of conditions and the following disclaimer in the
13*1bb76ff1Sjsg * documentation and/or other materials provided with the distribution.
14*1bb76ff1Sjsg * 3. Neither the name of the University nor the names of its contributors
15*1bb76ff1Sjsg * may be used to endorse or promote products derived from this software
16*1bb76ff1Sjsg * without specific prior written permission.
17*1bb76ff1Sjsg *
18*1bb76ff1Sjsg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*1bb76ff1Sjsg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*1bb76ff1Sjsg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*1bb76ff1Sjsg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*1bb76ff1Sjsg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*1bb76ff1Sjsg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*1bb76ff1Sjsg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*1bb76ff1Sjsg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*1bb76ff1Sjsg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*1bb76ff1Sjsg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*1bb76ff1Sjsg * SUCH DAMAGE.
29*1bb76ff1Sjsg */
30*1bb76ff1Sjsg
31*1bb76ff1Sjsg #include <sys/types.h>
32*1bb76ff1Sjsg #include <sys/systm.h>
33*1bb76ff1Sjsg
34*1bb76ff1Sjsg static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
35*1bb76ff1Sjsg static __inline void swapfunc(char *, char *, size_t, int);
36*1bb76ff1Sjsg
37*1bb76ff1Sjsg #define min(a, b) (a) < (b) ? a : b
38*1bb76ff1Sjsg
39*1bb76ff1Sjsg /*
40*1bb76ff1Sjsg * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
41*1bb76ff1Sjsg */
42*1bb76ff1Sjsg #define swapcode(TYPE, parmi, parmj, n) { \
43*1bb76ff1Sjsg size_t i = (n) / sizeof (TYPE); \
44*1bb76ff1Sjsg TYPE *pi = (TYPE *) (parmi); \
45*1bb76ff1Sjsg TYPE *pj = (TYPE *) (parmj); \
46*1bb76ff1Sjsg do { \
47*1bb76ff1Sjsg TYPE t = *pi; \
48*1bb76ff1Sjsg *pi++ = *pj; \
49*1bb76ff1Sjsg *pj++ = t; \
50*1bb76ff1Sjsg } while (--i > 0); \
51*1bb76ff1Sjsg }
52*1bb76ff1Sjsg
53*1bb76ff1Sjsg #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
54*1bb76ff1Sjsg es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
55*1bb76ff1Sjsg
56*1bb76ff1Sjsg static __inline void
swapfunc(char * a,char * b,size_t n,int swaptype)57*1bb76ff1Sjsg swapfunc(char *a, char *b, size_t n, int swaptype)
58*1bb76ff1Sjsg {
59*1bb76ff1Sjsg if (swaptype <= 1)
60*1bb76ff1Sjsg swapcode(long, a, b, n)
61*1bb76ff1Sjsg else
62*1bb76ff1Sjsg swapcode(char, a, b, n)
63*1bb76ff1Sjsg }
64*1bb76ff1Sjsg
65*1bb76ff1Sjsg #define swap(a, b) \
66*1bb76ff1Sjsg if (swaptype == 0) { \
67*1bb76ff1Sjsg long t = *(long *)(a); \
68*1bb76ff1Sjsg *(long *)(a) = *(long *)(b); \
69*1bb76ff1Sjsg *(long *)(b) = t; \
70*1bb76ff1Sjsg } else \
71*1bb76ff1Sjsg swapfunc(a, b, es, swaptype)
72*1bb76ff1Sjsg
73*1bb76ff1Sjsg #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
74*1bb76ff1Sjsg
75*1bb76ff1Sjsg static __inline char *
med3(char * a,char * b,char * c,int (* cmp)(const void *,const void *))76*1bb76ff1Sjsg med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
77*1bb76ff1Sjsg {
78*1bb76ff1Sjsg return cmp(a, b) < 0 ?
79*1bb76ff1Sjsg (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
80*1bb76ff1Sjsg :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
81*1bb76ff1Sjsg }
82*1bb76ff1Sjsg
83*1bb76ff1Sjsg static void
qsort(void * aa,size_t n,size_t es,int (* cmp)(const void *,const void *))84*1bb76ff1Sjsg qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
85*1bb76ff1Sjsg {
86*1bb76ff1Sjsg char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
87*1bb76ff1Sjsg int cmp_result, swaptype;
88*1bb76ff1Sjsg size_t d, r, s;
89*1bb76ff1Sjsg char *a = aa;
90*1bb76ff1Sjsg
91*1bb76ff1Sjsg loop: SWAPINIT(a, es);
92*1bb76ff1Sjsg if (n < 7) {
93*1bb76ff1Sjsg for (pm = a + es; pm < a + n * es; pm += es)
94*1bb76ff1Sjsg for (pl = pm; pl > a && cmp(pl - es, pl) > 0;
95*1bb76ff1Sjsg pl -= es)
96*1bb76ff1Sjsg swap(pl, pl - es);
97*1bb76ff1Sjsg return;
98*1bb76ff1Sjsg }
99*1bb76ff1Sjsg pm = a + (n / 2) * es;
100*1bb76ff1Sjsg if (n > 7) {
101*1bb76ff1Sjsg pl = a;
102*1bb76ff1Sjsg pn = a + (n - 1) * es;
103*1bb76ff1Sjsg if (n > 40) {
104*1bb76ff1Sjsg d = (n / 8) * es;
105*1bb76ff1Sjsg pl = med3(pl, pl + d, pl + 2 * d, cmp);
106*1bb76ff1Sjsg pm = med3(pm - d, pm, pm + d, cmp);
107*1bb76ff1Sjsg pn = med3(pn - 2 * d, pn - d, pn, cmp);
108*1bb76ff1Sjsg }
109*1bb76ff1Sjsg pm = med3(pl, pm, pn, cmp);
110*1bb76ff1Sjsg }
111*1bb76ff1Sjsg swap(a, pm);
112*1bb76ff1Sjsg pa = pb = a + es;
113*1bb76ff1Sjsg
114*1bb76ff1Sjsg pc = pd = a + (n - 1) * es;
115*1bb76ff1Sjsg for (;;) {
116*1bb76ff1Sjsg while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
117*1bb76ff1Sjsg if (cmp_result == 0) {
118*1bb76ff1Sjsg swap(pa, pb);
119*1bb76ff1Sjsg pa += es;
120*1bb76ff1Sjsg }
121*1bb76ff1Sjsg pb += es;
122*1bb76ff1Sjsg }
123*1bb76ff1Sjsg while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
124*1bb76ff1Sjsg if (cmp_result == 0) {
125*1bb76ff1Sjsg swap(pc, pd);
126*1bb76ff1Sjsg pd -= es;
127*1bb76ff1Sjsg }
128*1bb76ff1Sjsg pc -= es;
129*1bb76ff1Sjsg }
130*1bb76ff1Sjsg if (pb > pc)
131*1bb76ff1Sjsg break;
132*1bb76ff1Sjsg swap(pb, pc);
133*1bb76ff1Sjsg pb += es;
134*1bb76ff1Sjsg pc -= es;
135*1bb76ff1Sjsg }
136*1bb76ff1Sjsg
137*1bb76ff1Sjsg pn = a + n * es;
138*1bb76ff1Sjsg r = min(pa - a, pb - pa);
139*1bb76ff1Sjsg vecswap(a, pb - r, r);
140*1bb76ff1Sjsg r = min(pd - pc, pn - pd - es);
141*1bb76ff1Sjsg vecswap(pb, pn - r, r);
142*1bb76ff1Sjsg /*
143*1bb76ff1Sjsg * To save stack space we sort the smaller side of the partition first
144*1bb76ff1Sjsg * using recursion and eliminate tail recursion for the larger side.
145*1bb76ff1Sjsg */
146*1bb76ff1Sjsg r = pb - pa;
147*1bb76ff1Sjsg s = pd - pc;
148*1bb76ff1Sjsg if (r < s) {
149*1bb76ff1Sjsg /* Recurse for 1st side, iterate for 2nd side. */
150*1bb76ff1Sjsg if (s > es) {
151*1bb76ff1Sjsg if (r > es)
152*1bb76ff1Sjsg qsort(a, r / es, es, cmp);
153*1bb76ff1Sjsg a = pn - s;
154*1bb76ff1Sjsg n = s / es;
155*1bb76ff1Sjsg goto loop;
156*1bb76ff1Sjsg }
157*1bb76ff1Sjsg } else {
158*1bb76ff1Sjsg /* Recurse for 2nd side, iterate for 1st side. */
159*1bb76ff1Sjsg if (r > es) {
160*1bb76ff1Sjsg if (s > es)
161*1bb76ff1Sjsg qsort(pn - s, s / es, es, cmp);
162*1bb76ff1Sjsg n = r / es;
163*1bb76ff1Sjsg goto loop;
164*1bb76ff1Sjsg }
165*1bb76ff1Sjsg }
166*1bb76ff1Sjsg }
167*1bb76ff1Sjsg
168*1bb76ff1Sjsg void
sort(void * a,size_t n,size_t es,int (* cmp)(const void *,const void *),void * x)169*1bb76ff1Sjsg sort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *),
170*1bb76ff1Sjsg void *x)
171*1bb76ff1Sjsg {
172*1bb76ff1Sjsg KASSERT(x == NULL);
173*1bb76ff1Sjsg qsort(a, n, es, cmp);
174*1bb76ff1Sjsg }
175