11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1992, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
3087701Smarkm#include <sys/cdefs.h>
3187701Smarkm__FBSDID("$FreeBSD$");
3287701Smarkm
3387701Smarkm#include <sys/param.h>
341590Srgrimes#include <sys/libkern.h>
3587701Smarkm
3628370Scharnier#ifdef	I_AM_QSORT_R
371590Srgrimestypedef int		cmp_t(void *, const void *, const void *);
381590Srgrimes#else
3987701Smarkmtypedef int		cmp_t(const void *, const void *);
4028370Scharnier#endif
4128370Scharnierstatic __inline char	*med3(char *, char *, char *, cmp_t *, void *);
421590Srgrimesstatic __inline void	 swapfunc(char *, char *, int, int);
431590Srgrimes
4487701Smarkm#define min(a, b)	(a) < (b) ? (a) : (b)
451590Srgrimes
461590Srgrimes/*
471590Srgrimes * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
481590Srgrimes */
491590Srgrimes#define swapcode(TYPE, parmi, parmj, n) { 		\
501590Srgrimes	long i = (n) / sizeof (TYPE); 			\
511590Srgrimes	register TYPE *pi = (TYPE *) (parmi); 		\
52200419Sdelphij	register TYPE *pj = (TYPE *) (parmj); 		\
531590Srgrimes	do { 						\
541590Srgrimes		register TYPE	t = *pi;		\
551590Srgrimes		*pi++ = *pj;				\
561590Srgrimes		*pj++ = t;				\
571590Srgrimes        } while (--i > 0);				\
581590Srgrimes}
59229403Sed
6028370Scharnier#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
611590Srgrimes	es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
621590Srgrimes
631590Srgrimesstatic __inline void
6487701Smarkmswapfunc(char *a, char *b, int n, int swaptype)
651590Srgrimes{
661590Srgrimes	if(swaptype <= 1)
671590Srgrimes		swapcode(long, a, b, n)
6887701Smarkm	else
691590Srgrimes		swapcode(char, a, b, n)
701590Srgrimes}
711590Srgrimes
721590Srgrimes#define swap(a, b)					\
731590Srgrimes	if (swaptype == 0) {				\
741590Srgrimes		long t = *(long *)(a);			\
751590Srgrimes		*(long *)(a) = *(long *)(b);		\
761590Srgrimes		*(long *)(b) = t;			\
771590Srgrimes	} else						\
781590Srgrimes		swapfunc(a, b, es, swaptype)
791590Srgrimes
801590Srgrimes#define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
811590Srgrimes
821590Srgrimes#ifdef I_AM_QSORT_R
831590Srgrimes#define	CMP(t, x, y) (cmp((t), (x), (y)))
841590Srgrimes#else
851590Srgrimes#define	CMP(t, x, y) (cmp((x), (y)))
861590Srgrimes#endif
871590Srgrimes
881590Srgrimesstatic __inline char *
891590Srgrimesmed3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
901590Srgrimes#ifndef	I_AM_QSORT_R
911590Srgrimes__unused
921590Srgrimes#endif
931590Srgrimes)
941590Srgrimes{
951590Srgrimes	return CMP(thunk, a, b) < 0 ?
961590Srgrimes	       (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
971590Srgrimes              :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
981590Srgrimes}
991590Srgrimes
1001590Srgrimes#ifdef I_AM_QSORT_R
1011590Srgrimesvoid
1021590Srgrimesqsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
1031590Srgrimes#else
1041590Srgrimes#define	thunk NULL
1051590Srgrimesvoid
1061590Srgrimesqsort(void *a, size_t n, size_t es, cmp_t *cmp)
1071590Srgrimes#endif
1081590Srgrimes{
1091590Srgrimes	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
1101590Srgrimes	int d, r, swaptype, swap_cnt;
1111590Srgrimes
1121590Srgrimesloop:	SWAPINIT(a, es);
1131590Srgrimes	swap_cnt = 0;
1141590Srgrimes	if (n < 7) {
115		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
116			for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
117			     pl -= es)
118				swap(pl, pl - es);
119		return;
120	}
121	pm = (char *)a + (n / 2) * es;
122	if (n > 7) {
123		pl = a;
124		pn = (char *)a + (n - 1) * es;
125		if (n > 40) {
126			d = (n / 8) * es;
127			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
128			pm = med3(pm - d, pm, pm + d, cmp, thunk);
129			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
130		}
131		pm = med3(pl, pm, pn, cmp, thunk);
132	}
133	swap(a, pm);
134	pa = pb = (char *)a + es;
135
136	pc = pd = (char *)a + (n - 1) * es;
137	for (;;) {
138		while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
139			if (r == 0) {
140				swap_cnt = 1;
141				swap(pa, pb);
142				pa += es;
143			}
144			pb += es;
145		}
146		while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
147			if (r == 0) {
148				swap_cnt = 1;
149				swap(pc, pd);
150				pd -= es;
151			}
152			pc -= es;
153		}
154		if (pb > pc)
155			break;
156		swap(pb, pc);
157		swap_cnt = 1;
158		pb += es;
159		pc -= es;
160	}
161	if (swap_cnt == 0) {  /* Switch to insertion sort */
162		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
163			for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
164			     pl -= es)
165				swap(pl, pl - es);
166		return;
167	}
168
169	pn = (char *)a + n * es;
170	r = min(pa - (char *)a, pb - pa);
171	vecswap(a, pb - r, r);
172	r = min(pd - pc, pn - pd - es);
173	vecswap(pb, pn - r, r);
174	if ((r = pb - pa) > es)
175#ifdef	I_AM_QSORT_R
176		qsort_r(a, r / es, es, thunk, cmp);
177#else
178		qsort(a, r / es, es, cmp);
179#endif
180	if ((r = pd - pc) > es) {
181		/* Iterate rather than recurse to save stack space */
182		a = pn - r;
183		n = r / es;
184		goto loop;
185	}
186}
187