xref: /netbsd-src/sys/lib/libsa/qsort.c (revision 10497fd285fb21c25f76e74beeb53b30864bc3f3)
13c2fbd85Sabs /*-
23c2fbd85Sabs  * Copyright (c) 1992, 1993
33c2fbd85Sabs  *	The Regents of the University of California.  All rights reserved.
43c2fbd85Sabs  *
53c2fbd85Sabs  * Redistribution and use in source and binary forms, with or without
63c2fbd85Sabs  * modification, are permitted provided that the following conditions
73c2fbd85Sabs  * are met:
83c2fbd85Sabs  * 1. Redistributions of source code must retain the above copyright
93c2fbd85Sabs  *    notice, this list of conditions and the following disclaimer.
103c2fbd85Sabs  * 2. Redistributions in binary form must reproduce the above copyright
113c2fbd85Sabs  *    notice, this list of conditions and the following disclaimer in the
123c2fbd85Sabs  *    documentation and/or other materials provided with the distribution.
133c2fbd85Sabs  * 3. All advertising materials mentioning features or use of this software
143c2fbd85Sabs  *    must display the following acknowledgement:
153c2fbd85Sabs  *	This product includes software developed by the University of
163c2fbd85Sabs  *	California, Berkeley and its contributors.
173c2fbd85Sabs  * 4. Neither the name of the University nor the names of its contributors
183c2fbd85Sabs  *    may be used to endorse or promote products derived from this software
193c2fbd85Sabs  *    without specific prior written permission.
203c2fbd85Sabs  *
213c2fbd85Sabs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
223c2fbd85Sabs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
233c2fbd85Sabs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243c2fbd85Sabs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
253c2fbd85Sabs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
263c2fbd85Sabs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
273c2fbd85Sabs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
283c2fbd85Sabs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293c2fbd85Sabs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
303c2fbd85Sabs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313c2fbd85Sabs  * SUCH DAMAGE.
323c2fbd85Sabs  *
333c2fbd85Sabs  * $FreeBSD: src/sys/libkern/qsort.c,v 1.12 2002/11/09 12:55:06 alfred Exp $
34*10497fd2Schristos  * $NetBSD: qsort.c,v 1.5 2019/03/31 20:08:45 christos Exp $
353c2fbd85Sabs  */
363c2fbd85Sabs 
373c2fbd85Sabs #include <sys/cdefs.h>
383c2fbd85Sabs #include <sys/types.h>
393c2fbd85Sabs 
403c2fbd85Sabs typedef int		 cmp_t(const void *, const void *);
410f0296d8Sperry static inline char	*med3(char *, char *, char *, cmp_t *);
420f0296d8Sperry static inline void	 swapfunc(char *, char *, int, int);
433c2fbd85Sabs 
44*10497fd2Schristos #define min(a, b)	(int)(a) < (int)(b) ? (int)(a) : (int)(b)
453c2fbd85Sabs 
463c2fbd85Sabs void qsort(void *a, size_t n, size_t es, cmp_t *cmp);
473c2fbd85Sabs 
483c2fbd85Sabs /*
493c2fbd85Sabs  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
503c2fbd85Sabs  */
513c2fbd85Sabs #define swapcode(TYPE, parmi, parmj, n) { 		\
523c2fbd85Sabs 	long i = (n) / sizeof (TYPE); 			\
533c2fbd85Sabs 	register TYPE *pi = (TYPE *)(parmi); 		\
543c2fbd85Sabs 	register TYPE *pj = (TYPE *)(parmj); 		\
553c2fbd85Sabs 	do { 						\
563c2fbd85Sabs 		register TYPE	t = *pi;		\
573c2fbd85Sabs 		*pi++ = *pj;				\
583c2fbd85Sabs 		*pj++ = t;				\
593c2fbd85Sabs 	} while (--i > 0);				\
603c2fbd85Sabs }
613c2fbd85Sabs 
623c2fbd85Sabs #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
633c2fbd85Sabs 	es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
643c2fbd85Sabs 
650f0296d8Sperry static inline void
swapfunc(char * a,char * b,int n,int swaptype)661c038e68Sisaki swapfunc(char *a, char *b, int n, int swaptype)
673c2fbd85Sabs {
683c2fbd85Sabs 	if(swaptype <= 1)
693c2fbd85Sabs 		swapcode(long, a, b, n)
703c2fbd85Sabs 	else
713c2fbd85Sabs 		swapcode(char, a, b, n)
723c2fbd85Sabs }
733c2fbd85Sabs 
743c2fbd85Sabs #define swap(a, b)					\
753c2fbd85Sabs 	if (swaptype == 0) {				\
763c2fbd85Sabs 		long t = *(long *)(a);			\
773c2fbd85Sabs 		*(long *)(a) = *(long *)(b);		\
783c2fbd85Sabs 		*(long *)(b) = t;			\
793c2fbd85Sabs 	} else						\
803c2fbd85Sabs 		swapfunc(a, b, es, swaptype)
813c2fbd85Sabs 
823c2fbd85Sabs #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
833c2fbd85Sabs 
840f0296d8Sperry static inline char *
med3(char * a,char * b,char * c,cmp_t * cmp)851c038e68Sisaki med3(char *a, char *b, char *c, cmp_t *cmp)
863c2fbd85Sabs {
873c2fbd85Sabs 	return cmp(a, b) < 0 ?
883c2fbd85Sabs 	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
893c2fbd85Sabs 	      :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
903c2fbd85Sabs }
913c2fbd85Sabs 
923c2fbd85Sabs void
qsort(void * a,size_t n,size_t es,cmp_t * cmp)933c2fbd85Sabs qsort(void *a, size_t n, size_t es, cmp_t *cmp)
943c2fbd85Sabs {
953c2fbd85Sabs 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
963c2fbd85Sabs 	int d, r, swaptype, swap_cnt;
973c2fbd85Sabs 
981c038e68Sisaki loop:
991c038e68Sisaki 	SWAPINIT(a, es);
1003c2fbd85Sabs 	swap_cnt = 0;
1013c2fbd85Sabs 	if (n < 7) {
1023c2fbd85Sabs 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
1033c2fbd85Sabs 			for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
1043c2fbd85Sabs 			     pl -= es)
1053c2fbd85Sabs 				swap(pl, pl - es);
1063c2fbd85Sabs 		return;
1073c2fbd85Sabs 	}
1083c2fbd85Sabs 	pm = (char *)a + (n / 2) * es;
1093c2fbd85Sabs 	if (n > 7) {
1103c2fbd85Sabs 		pl = a;
1113c2fbd85Sabs 		pn = (char *)a + (n - 1) * es;
1123c2fbd85Sabs 		if (n > 40) {
1133c2fbd85Sabs 			d = (n / 8) * es;
1143c2fbd85Sabs 			pl = med3(pl, pl + d, pl + 2 * d, cmp);
1153c2fbd85Sabs 			pm = med3(pm - d, pm, pm + d, cmp);
1163c2fbd85Sabs 			pn = med3(pn - 2 * d, pn - d, pn, cmp);
1173c2fbd85Sabs 		}
1183c2fbd85Sabs 		pm = med3(pl, pm, pn, cmp);
1193c2fbd85Sabs 	}
1203c2fbd85Sabs 	swap(a, pm);
1213c2fbd85Sabs 	pa = pb = (char *)a + es;
1223c2fbd85Sabs 
1233c2fbd85Sabs 	pc = pd = (char *)a + (n - 1) * es;
1243c2fbd85Sabs 	for (;;) {
1253c2fbd85Sabs 		while (pb <= pc && (r = cmp(pb, a)) <= 0) {
1263c2fbd85Sabs 			if (r == 0) {
1273c2fbd85Sabs 				swap_cnt = 1;
1283c2fbd85Sabs 				swap(pa, pb);
1293c2fbd85Sabs 				pa += es;
1303c2fbd85Sabs 			}
1313c2fbd85Sabs 			pb += es;
1323c2fbd85Sabs 		}
1333c2fbd85Sabs 		while (pb <= pc && (r = cmp(pc, a)) >= 0) {
1343c2fbd85Sabs 			if (r == 0) {
1353c2fbd85Sabs 				swap_cnt = 1;
1363c2fbd85Sabs 				swap(pc, pd);
1373c2fbd85Sabs 				pd -= es;
1383c2fbd85Sabs 			}
1393c2fbd85Sabs 			pc -= es;
1403c2fbd85Sabs 		}
1413c2fbd85Sabs 		if (pb > pc)
1423c2fbd85Sabs 			break;
1433c2fbd85Sabs 		swap(pb, pc);
1443c2fbd85Sabs 		swap_cnt = 1;
1453c2fbd85Sabs 		pb += es;
1463c2fbd85Sabs 		pc -= es;
1473c2fbd85Sabs 	}
1483c2fbd85Sabs 	if (swap_cnt == 0) {  /* Switch to insertion sort */
1493c2fbd85Sabs 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
1503c2fbd85Sabs 			for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
1513c2fbd85Sabs 			     pl -= es)
1523c2fbd85Sabs 				swap(pl, pl - es);
1533c2fbd85Sabs 		return;
1543c2fbd85Sabs 	}
1553c2fbd85Sabs 
1563c2fbd85Sabs 	pn = (char *)a + n * es;
1573c2fbd85Sabs 	r = min(pa - (char *)a, pb - pa);
1583c2fbd85Sabs 	vecswap(a, pb - r, r);
1593c2fbd85Sabs 	r = min(pd - pc, pn - pd - es);
1603c2fbd85Sabs 	vecswap(pb, pn - r, r);
161*10497fd2Schristos 	if ((size_t)(r = pb - pa) > es)
1623c2fbd85Sabs 		qsort(a, r / es, es, cmp);
163*10497fd2Schristos 	if ((size_t)(r = pd - pc) > es) {
1643c2fbd85Sabs 		/* Iterate rather than recurse to save stack space */
1653c2fbd85Sabs 		a = pn - r;
1663c2fbd85Sabs 		n = r / es;
1673c2fbd85Sabs 		goto loop;
1683c2fbd85Sabs 	}
1693c2fbd85Sabs /*		qsort(pn - r, r / es, es, cmp);*/
1703c2fbd85Sabs }
171