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