1 // SPDX-License-Identifier: BSD-3-Clause
2 /*	$OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
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  * 3. 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 #include <sys/types.h>
32 #include <stdlib.h>
33 static __inline char
34 	*med3(char *, char *, char *, int (*)(const void *, const void *));
35 static __inline void	 swapfunc(char *, char *, int, int);
36 #define min(a, b)	(a) < (b) ? a : b
37 /*
38  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
39  */
40 #define swapcode(TYPE, parmi, parmj, n) { 		\
41 		long i = (n) / sizeof (TYPE); 			\
42 		TYPE *pi = (TYPE *) (parmi); 			\
43 		TYPE *pj = (TYPE *) (parmj); 			\
44 		do { 						\
45 			TYPE	t = *pi;			\
46 			*pi++ = *pj;				\
47 			*pj++ = t;				\
48 		} while (--i > 0);				\
49 }
50 #define SWAPINIT(a, es) swaptype = (uintptr_t)a % sizeof(long) || \
51 		es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
52 static __inline void
swapfunc(char * a,char * b,int n,int swaptype)53 swapfunc(char *a, char *b, int n, int swaptype)
54 {
55 	if (swaptype <= 1)
56 		swapcode(long, a, b, n)
57 		else
58 			swapcode(char, a, b, n)
59 }
60 #define swap(a, b)					\
61 		if (swaptype == 0) {				\
62 			long t = *(long *)(a);			\
63 			*(long *)(a) = *(long *)(b);		\
64 			*(long *)(b) = t;			\
65 		} else						\
66 		swapfunc(a, b, es, swaptype)
67 #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
68 static __inline char *
med3(char * a,char * b,char * c,int (* cmp)(const void *,const void *))69 med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
70 {
71 	return cmp(a, b) < 0 ?
72 			(cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
73 			:(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
74 }
75 void
qsort(void * aa,size_t n,size_t es,int (* cmp)(const void *,const void *))76 qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
77 {
78 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
79 	int d, r, swaptype, swap_cnt;
80 	char *a = aa;
81 	loop:	SWAPINIT(a, es);
82 	swap_cnt = 0;
83 	if (n < 7) {
84 		for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
85 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
86 					pl -= es)
87 				swap(pl, pl - es);
88 		return;
89 	}
90 	pm = (char *)a + (n / 2) * es;
91 	if (n > 7) {
92 		pl = (char *)a;
93 		pn = (char *)a + (n - 1) * es;
94 		if (n > 40) {
95 			d = (n / 8) * es;
96 			pl = med3(pl, pl + d, pl + 2 * d, cmp);
97 			pm = med3(pm - d, pm, pm + d, cmp);
98 			pn = med3(pn - 2 * d, pn - d, pn, cmp);
99 		}
100 		pm = med3(pl, pm, pn, cmp);
101 	}
102 	swap(a, pm);
103 	pa = pb = (char *)a + es;
104 
105 	pc = pd = (char *)a + (n - 1) * es;
106 	for (;;) {
107 		while (pb <= pc && (r = cmp(pb, a)) <= 0) {
108 			if (r == 0) {
109 				swap_cnt = 1;
110 				swap(pa, pb);
111 				pa += es;
112 			}
113 			pb += es;
114 		}
115 		while (pb <= pc && (r = cmp(pc, a)) >= 0) {
116 			if (r == 0) {
117 				swap_cnt = 1;
118 				swap(pc, pd);
119 				pd -= es;
120 			}
121 			pc -= es;
122 		}
123 		if (pb > pc)
124 			break;
125 		swap(pb, pc);
126 		swap_cnt = 1;
127 		pb += es;
128 		pc -= es;
129 	}
130 	if (swap_cnt == 0) {  /* Switch to insertion sort */
131 		for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
132 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
133 					pl -= es)
134 				swap(pl, pl - es);
135 		return;
136 	}
137 	pn = (char *)a + n * es;
138 	r = min(pa - (char *)a, pb - pa);
139 	vecswap(a, pb - r, r);
140 	r = min(pd - pc, pn - pd - (int)es);
141 	vecswap(pb, pn - r, r);
142 	if ((r = pb - pa) > (int)es)
143 		qsort(a, r / es, es, cmp);
144 	if ((r = pd - pc) > (int)es) {
145 		/* Iterate rather than recurse to save stack space */
146 		a = pn - r;
147 		n = r / es;
148 		goto loop;
149 	}
150 	/* qsort(pn - r, r / es, es, cmp);*/
151 }
152