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