xref: /openbsd/lib/libc/stdlib/qsort.c (revision ee4ffdb6)
1 /*	$OpenBSD: qsort.c,v 1.18 2017/05/30 14:54:09 millert Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <stdlib.h>
33 
34 static __inline char	*med3(char *, char *, char *, int (*)(const void *, const void *));
35 static __inline void	 swapfunc(char *, char *, size_t, int);
36 
37 #define min(a, b)	(a) < (b) ? a : b
38 
39 /*
40  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
41  *
42  * This version differs from Bentley & McIlroy in the following ways:
43  *   1. The partition value is swapped into a[0] instead of being
44  *	stored out of line.
45  *
46  *   2. The swap function can swap 32-bit aligned elements on 64-bit
47  *	platforms instead of swapping them as byte-aligned.
48  *
49  *   3. It uses David Musser's introsort algorithm to fall back to
50  *	heapsort(3) when the recursion depth reaches 2*lg(n + 1).
51  *	This avoids quicksort's quadratic behavior for pathological
52  *	input without appreciably changing the average run time.
53  *
54  *   4. Tail recursion is eliminated when sorting the larger of two
55  *	subpartitions to save stack space.
56  */
57 #define SWAPTYPE_BYTEV	1
58 #define SWAPTYPE_INTV	2
59 #define SWAPTYPE_LONGV	3
60 #define SWAPTYPE_INT	4
61 #define SWAPTYPE_LONG	5
62 
63 #define TYPE_ALIGNED(TYPE, a, es)			\
64 	(((char *)a - (char *)0) % sizeof(TYPE) == 0 && es % sizeof(TYPE) == 0)
65 
66 #define swapcode(TYPE, parmi, parmj, n) { 		\
67 	size_t i = (n) / sizeof (TYPE); 		\
68 	TYPE *pi = (TYPE *) (parmi); 			\
69 	TYPE *pj = (TYPE *) (parmj); 			\
70 	do { 						\
71 		TYPE	t = *pi;			\
72 		*pi++ = *pj;				\
73 		*pj++ = t;				\
74         } while (--i > 0);				\
75 }
76 
77 static __inline void
78 swapfunc(char *a, char *b, size_t n, int swaptype)
79 {
80 	switch (swaptype) {
81 	case SWAPTYPE_INT:
82 	case SWAPTYPE_INTV:
83 		swapcode(int, a, b, n);
84 		break;
85 	case SWAPTYPE_LONG:
86 	case SWAPTYPE_LONGV:
87 		swapcode(long, a, b, n);
88 		break;
89 	default:
90 		swapcode(char, a, b, n);
91 		break;
92 	}
93 }
94 
95 #define swap(a, b)	do {				\
96 	switch (swaptype) {				\
97 	case SWAPTYPE_INT: {				\
98 		int t = *(int *)(a);			\
99 		*(int *)(a) = *(int *)(b);		\
100 		*(int *)(b) = t;			\
101 		break;					\
102 	    }						\
103 	case SWAPTYPE_LONG: {				\
104 		long t = *(long *)(a);			\
105 		*(long *)(a) = *(long *)(b);		\
106 		*(long *)(b) = t;			\
107 		break;					\
108 	    }						\
109 	default:					\
110 		swapfunc(a, b, es, swaptype);		\
111 	}						\
112 } while (0)
113 
114 #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
115 
116 static __inline char *
117 med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
118 {
119 	return cmp(a, b) < 0 ?
120 	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
121               :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
122 }
123 
124 static void
125 introsort(char *a, size_t n, size_t es, size_t maxdepth, int swaptype,
126     int (*cmp)(const void *, const void *))
127 {
128 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
129 	int cmp_result;
130 	size_t r, s;
131 
132 loop:	if (n < 7) {
133 		for (pm = a + es; pm < a + n * es; pm += es)
134 			for (pl = pm; pl > a && cmp(pl - es, pl) > 0;
135 			     pl -= es)
136 				swap(pl, pl - es);
137 		return;
138 	}
139 	if (maxdepth == 0) {
140 		if (heapsort(a, n, es, cmp) == 0)
141 			return;
142 	}
143 	maxdepth--;
144 	pm = a + (n / 2) * es;
145 	if (n > 7) {
146 		pl = a;
147 		pn = a + (n - 1) * es;
148 		if (n > 40) {
149 			s = (n / 8) * es;
150 			pl = med3(pl, pl + s, pl + 2 * s, cmp);
151 			pm = med3(pm - s, pm, pm + s, cmp);
152 			pn = med3(pn - 2 * s, pn - s, pn, cmp);
153 		}
154 		pm = med3(pl, pm, pn, cmp);
155 	}
156 	swap(a, pm);
157 	pa = pb = a + es;
158 	pc = pd = a + (n - 1) * es;
159 	for (;;) {
160 		while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
161 			if (cmp_result == 0) {
162 				swap(pa, pb);
163 				pa += es;
164 			}
165 			pb += es;
166 		}
167 		while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
168 			if (cmp_result == 0) {
169 				swap(pc, pd);
170 				pd -= es;
171 			}
172 			pc -= es;
173 		}
174 		if (pb > pc)
175 			break;
176 		swap(pb, pc);
177 		pb += es;
178 		pc -= es;
179 	}
180 
181 	pn = a + n * es;
182 	r = min(pa - a, pb - pa);
183 	vecswap(a, pb - r, r);
184 	r = min(pd - pc, pn - pd - es);
185 	vecswap(pb, pn - r, r);
186 	/*
187 	 * To save stack space we sort the smaller side of the partition first
188 	 * using recursion and eliminate tail recursion for the larger side.
189 	 */
190 	r = pb - pa;
191 	s = pd - pc;
192 	if (r < s) {
193 		/* Recurse for 1st side, iterate for 2nd side. */
194 		if (s > es) {
195 			if (r > es) {
196 				introsort(a, r / es, es, maxdepth,
197 				    swaptype, cmp);
198 			}
199 			a = pn - s;
200 			n = s / es;
201 			goto loop;
202 		}
203 	} else {
204 		/* Recurse for 2nd side, iterate for 1st side. */
205 		if (r > es) {
206 			if (s > es) {
207 				introsort(pn - s, s / es, es, maxdepth,
208 				    swaptype, cmp);
209 			}
210 			n = r / es;
211 			goto loop;
212 		}
213 	}
214 }
215 
216 void
217 qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
218 {
219 	size_t i, maxdepth = 0;
220 	int swaptype;
221 
222 	/* Approximate 2*ceil(lg(n + 1)) */
223 	for (i = n; i > 0; i >>= 1)
224 		maxdepth++;
225 	maxdepth *= 2;
226 
227 	if (TYPE_ALIGNED(long, a, es))
228 		swaptype = es == sizeof(long) ? SWAPTYPE_LONG : SWAPTYPE_LONGV;
229 	else if (sizeof(int) != sizeof(long) && TYPE_ALIGNED(int, a, es))
230 		swaptype = es == sizeof(int) ? SWAPTYPE_INT : SWAPTYPE_INTV;
231 	else
232 		swaptype = SWAPTYPE_BYTEV;
233 
234 	introsort(a, n, es, maxdepth, swaptype, cmp);
235 
236 }
237 
238 DEF_STRONG(qsort);
239