qsort.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/11/sys/libkern/qsort.c 330897 2018-03-14 03:19:51Z eadler $");
34
35#include <sys/param.h>
36#include <sys/libkern.h>
37
38#ifdef I_AM_QSORT_R
39typedef int		 cmp_t(void *, const void *, const void *);
40#else
41typedef int		 cmp_t(const void *, const void *);
42#endif
43static inline char	*med3(char *, char *, char *, cmp_t *, void *);
44static inline void	 swapfunc(char *, char *, size_t, int, int);
45
46/*
47 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
48 */
49#define	swapcode(TYPE, parmi, parmj, n) {		\
50	size_t i = (n) / sizeof (TYPE);			\
51	TYPE *pi = (TYPE *) (parmi);		\
52	TYPE *pj = (TYPE *) (parmj);		\
53	do { 						\
54		TYPE	t = *pi;		\
55		*pi++ = *pj;				\
56		*pj++ = t;				\
57	} while (--i > 0);				\
58}
59
60#define	SWAPINIT(TYPE, a, es) swaptype_ ## TYPE =	\
61	((char *)a - (char *)0) % sizeof(TYPE) ||	\
62	es % sizeof(TYPE) ? 2 : es == sizeof(TYPE) ? 0 : 1;
63
64static inline void
65swapfunc(char *a, char *b, size_t n, int swaptype_long, int swaptype_int)
66{
67	if (swaptype_long <= 1)
68		swapcode(long, a, b, n)
69	else if (swaptype_int <= 1)
70		swapcode(int, a, b, n)
71	else
72		swapcode(char, a, b, n)
73}
74
75#define	swap(a, b)					\
76	if (swaptype_long == 0) {			\
77		long t = *(long *)(a);			\
78		*(long *)(a) = *(long *)(b);		\
79		*(long *)(b) = t;			\
80	} else if (swaptype_int == 0) {			\
81		int t = *(int *)(a);			\
82		*(int *)(a) = *(int *)(b);		\
83		*(int *)(b) = t;			\
84	} else						\
85		swapfunc(a, b, es, swaptype_long, swaptype_int)
86
87#define	vecswap(a, b, n)				\
88	if ((n) > 0) swapfunc(a, b, n, swaptype_long, swaptype_int)
89
90#ifdef I_AM_QSORT_R
91#define	CMP(t, x, y) (cmp((t), (x), (y)))
92#else
93#define	CMP(t, x, y) (cmp((x), (y)))
94#endif
95
96static inline char *
97med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
98#ifndef I_AM_QSORT_R
99__unused
100#endif
101)
102{
103	return CMP(thunk, a, b) < 0 ?
104	       (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
105	      :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
106}
107
108#ifdef I_AM_QSORT_R
109void
110qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
111#else
112#define	thunk NULL
113void
114qsort(void *a, size_t n, size_t es, cmp_t *cmp)
115#endif
116{
117	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
118	size_t d1, d2;
119	int cmp_result;
120	int swaptype_long, swaptype_int, swap_cnt;
121
122loop:	SWAPINIT(long, a, es);
123	SWAPINIT(int, a, es);
124	swap_cnt = 0;
125	if (n < 7) {
126		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
127			for (pl = pm;
128			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
129			     pl -= es)
130				swap(pl, pl - es);
131		return;
132	}
133	pm = (char *)a + (n / 2) * es;
134	if (n > 7) {
135		pl = a;
136		pn = (char *)a + (n - 1) * es;
137		if (n > 40) {
138			size_t d = (n / 8) * es;
139
140			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
141			pm = med3(pm - d, pm, pm + d, cmp, thunk);
142			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
143		}
144		pm = med3(pl, pm, pn, cmp, thunk);
145	}
146	swap(a, pm);
147	pa = pb = (char *)a + es;
148
149	pc = pd = (char *)a + (n - 1) * es;
150	for (;;) {
151		while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
152			if (cmp_result == 0) {
153				swap_cnt = 1;
154				swap(pa, pb);
155				pa += es;
156			}
157			pb += es;
158		}
159		while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
160			if (cmp_result == 0) {
161				swap_cnt = 1;
162				swap(pc, pd);
163				pd -= es;
164			}
165			pc -= es;
166		}
167		if (pb > pc)
168			break;
169		swap(pb, pc);
170		swap_cnt = 1;
171		pb += es;
172		pc -= es;
173	}
174	if (swap_cnt == 0) {  /* Switch to insertion sort */
175		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
176			for (pl = pm;
177			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
178			     pl -= es)
179				swap(pl, pl - es);
180		return;
181	}
182
183	pn = (char *)a + n * es;
184	d1 = MIN(pa - (char *)a, pb - pa);
185	vecswap(a, pb - d1, d1);
186	d1 = MIN(pd - pc, pn - pd - es);
187	vecswap(pb, pn - d1, d1);
188
189	d1 = pb - pa;
190	d2 = pd - pc;
191	if (d1 <= d2) {
192		/* Recurse on left partition, then iterate on right partition */
193		if (d1 > es) {
194#ifdef I_AM_QSORT_R
195			qsort_r(a, d1 / es, es, thunk, cmp);
196#else
197			qsort(a, d1 / es, es, cmp);
198#endif
199		}
200		if (d2 > es) {
201			/* Iterate rather than recurse to save stack space */
202			/* qsort(pn - d2, d2 / es, es, cmp); */
203			a = pn - d2;
204			n = d2 / es;
205			goto loop;
206		}
207	} else {
208		/* Recurse on right partition, then iterate on left partition */
209		if (d2 > es) {
210#ifdef I_AM_QSORT_R
211			qsort_r(pn - d2, d2 / es, es, thunk, cmp);
212#else
213			qsort(pn - d2, d2 / es, es, cmp);
214#endif
215		}
216		if (d1 > es) {
217			/* Iterate rather than recurse to save stack space */
218			/* qsort(a, d1 / es, es, cmp); */
219			n = d1 / es;
220			goto loop;
221		}
222	}
223}
224