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