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. 13d89a1b60SPoul-Henning Kamp * 4. 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 41b1e34c44SGleb Smirnoff static __inline char *med3(char *, char *, char *, cmp_t *, void *); 428febc6baSAlfred Perlstein static __inline void swapfunc(char *, char *, int, int); 43d89a1b60SPoul-Henning Kamp 4429f19445SAlfred Perlstein #define min(a, b) (a) < (b) ? (a) : (b) 45d89a1b60SPoul-Henning Kamp 46d89a1b60SPoul-Henning Kamp /* 47d89a1b60SPoul-Henning Kamp * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". 48d89a1b60SPoul-Henning Kamp */ 49d89a1b60SPoul-Henning Kamp #define swapcode(TYPE, parmi, parmj, n) { \ 50d89a1b60SPoul-Henning Kamp long i = (n) / sizeof (TYPE); \ 51d89a1b60SPoul-Henning Kamp register TYPE *pi = (TYPE *) (parmi); \ 52d89a1b60SPoul-Henning Kamp register TYPE *pj = (TYPE *) (parmj); \ 53d89a1b60SPoul-Henning Kamp do { \ 54d89a1b60SPoul-Henning Kamp register TYPE t = *pi; \ 55d89a1b60SPoul-Henning Kamp *pi++ = *pj; \ 56d89a1b60SPoul-Henning Kamp *pj++ = t; \ 57d89a1b60SPoul-Henning Kamp } while (--i > 0); \ 58d89a1b60SPoul-Henning Kamp } 59d89a1b60SPoul-Henning Kamp 60d89a1b60SPoul-Henning Kamp #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ 61d89a1b60SPoul-Henning Kamp es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; 62d89a1b60SPoul-Henning Kamp 63c1087c13SBruce Evans static __inline void 64b1e34c44SGleb Smirnoff swapfunc(char *a, char *b, int n, int swaptype) 65d89a1b60SPoul-Henning Kamp { 66d89a1b60SPoul-Henning Kamp if(swaptype <= 1) 67d89a1b60SPoul-Henning Kamp swapcode(long, 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) \ 73d89a1b60SPoul-Henning Kamp if (swaptype == 0) { \ 74d89a1b60SPoul-Henning Kamp long t = *(long *)(a); \ 75d89a1b60SPoul-Henning Kamp *(long *)(a) = *(long *)(b); \ 76d89a1b60SPoul-Henning Kamp *(long *)(b) = t; \ 77d89a1b60SPoul-Henning Kamp } else \ 78d89a1b60SPoul-Henning Kamp swapfunc(a, b, es, swaptype) 79d89a1b60SPoul-Henning Kamp 80d89a1b60SPoul-Henning Kamp #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 81d89a1b60SPoul-Henning Kamp 82b1e34c44SGleb Smirnoff #ifdef I_AM_QSORT_R 83b1e34c44SGleb Smirnoff #define CMP(t, x, y) (cmp((t), (x), (y))) 84b1e34c44SGleb Smirnoff #else 85b1e34c44SGleb Smirnoff #define CMP(t, x, y) (cmp((x), (y))) 86b1e34c44SGleb Smirnoff #endif 87b1e34c44SGleb Smirnoff 88c1087c13SBruce Evans static __inline char * 89b1e34c44SGleb Smirnoff med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk 90b1e34c44SGleb Smirnoff #ifndef I_AM_QSORT_R 91b1e34c44SGleb Smirnoff __unused 92b1e34c44SGleb Smirnoff #endif 93b1e34c44SGleb Smirnoff ) 94d89a1b60SPoul-Henning Kamp { 95b1e34c44SGleb Smirnoff return CMP(thunk, a, b) < 0 ? 96b1e34c44SGleb Smirnoff (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) 97b1e34c44SGleb Smirnoff :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); 98d89a1b60SPoul-Henning Kamp } 99d89a1b60SPoul-Henning Kamp 100b1e34c44SGleb Smirnoff #ifdef I_AM_QSORT_R 101d89a1b60SPoul-Henning Kamp void 102b1e34c44SGleb Smirnoff qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) 103b1e34c44SGleb Smirnoff #else 104b1e34c44SGleb Smirnoff #define thunk NULL 105b1e34c44SGleb Smirnoff void 106b1e34c44SGleb Smirnoff qsort(void *a, size_t n, size_t es, cmp_t *cmp) 107b1e34c44SGleb Smirnoff #endif 108d89a1b60SPoul-Henning Kamp { 109d89a1b60SPoul-Henning Kamp char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 110d89a1b60SPoul-Henning Kamp int d, r, swaptype, swap_cnt; 111d89a1b60SPoul-Henning Kamp 112d89a1b60SPoul-Henning Kamp loop: SWAPINIT(a, es); 113d89a1b60SPoul-Henning Kamp swap_cnt = 0; 114d89a1b60SPoul-Henning Kamp if (n < 7) { 11509a8dfa2SBruce Evans for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 116b1e34c44SGleb Smirnoff for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0; 117d89a1b60SPoul-Henning Kamp pl -= es) 118d89a1b60SPoul-Henning Kamp swap(pl, pl - es); 119d89a1b60SPoul-Henning Kamp return; 120d89a1b60SPoul-Henning Kamp } 12109a8dfa2SBruce Evans pm = (char *)a + (n / 2) * es; 122d89a1b60SPoul-Henning Kamp if (n > 7) { 123d89a1b60SPoul-Henning Kamp pl = a; 12409a8dfa2SBruce Evans pn = (char *)a + (n - 1) * es; 125d89a1b60SPoul-Henning Kamp if (n > 40) { 126d89a1b60SPoul-Henning Kamp d = (n / 8) * es; 127b1e34c44SGleb Smirnoff pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); 128b1e34c44SGleb Smirnoff pm = med3(pm - d, pm, pm + d, cmp, thunk); 129b1e34c44SGleb Smirnoff pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); 130d89a1b60SPoul-Henning Kamp } 131b1e34c44SGleb Smirnoff pm = med3(pl, pm, pn, cmp, thunk); 132d89a1b60SPoul-Henning Kamp } 133d89a1b60SPoul-Henning Kamp swap(a, pm); 13409a8dfa2SBruce Evans pa = pb = (char *)a + es; 135d89a1b60SPoul-Henning Kamp 13609a8dfa2SBruce Evans pc = pd = (char *)a + (n - 1) * es; 137d89a1b60SPoul-Henning Kamp for (;;) { 138b1e34c44SGleb Smirnoff while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) { 139d89a1b60SPoul-Henning Kamp if (r == 0) { 140d89a1b60SPoul-Henning Kamp swap_cnt = 1; 141d89a1b60SPoul-Henning Kamp swap(pa, pb); 142d89a1b60SPoul-Henning Kamp pa += es; 143d89a1b60SPoul-Henning Kamp } 144d89a1b60SPoul-Henning Kamp pb += es; 145d89a1b60SPoul-Henning Kamp } 146b1e34c44SGleb Smirnoff while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) { 147d89a1b60SPoul-Henning Kamp if (r == 0) { 148d89a1b60SPoul-Henning Kamp swap_cnt = 1; 149d89a1b60SPoul-Henning Kamp swap(pc, pd); 150d89a1b60SPoul-Henning Kamp pd -= es; 151d89a1b60SPoul-Henning Kamp } 152d89a1b60SPoul-Henning Kamp pc -= es; 153d89a1b60SPoul-Henning Kamp } 154d89a1b60SPoul-Henning Kamp if (pb > pc) 155d89a1b60SPoul-Henning Kamp break; 156d89a1b60SPoul-Henning Kamp swap(pb, pc); 157d89a1b60SPoul-Henning Kamp swap_cnt = 1; 158d89a1b60SPoul-Henning Kamp pb += es; 159d89a1b60SPoul-Henning Kamp pc -= es; 160d89a1b60SPoul-Henning Kamp } 161d89a1b60SPoul-Henning Kamp if (swap_cnt == 0) { /* Switch to insertion sort */ 16209a8dfa2SBruce Evans for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 163b1e34c44SGleb Smirnoff for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0; 164d89a1b60SPoul-Henning Kamp pl -= es) 165d89a1b60SPoul-Henning Kamp swap(pl, pl - es); 166d89a1b60SPoul-Henning Kamp return; 167d89a1b60SPoul-Henning Kamp } 168d89a1b60SPoul-Henning Kamp 16909a8dfa2SBruce Evans pn = (char *)a + n * es; 170d89a1b60SPoul-Henning Kamp r = min(pa - (char *)a, pb - pa); 171d89a1b60SPoul-Henning Kamp vecswap(a, pb - r, r); 172d89a1b60SPoul-Henning Kamp r = min(pd - pc, pn - pd - es); 173d89a1b60SPoul-Henning Kamp vecswap(pb, pn - r, r); 174d89a1b60SPoul-Henning Kamp if ((r = pb - pa) > es) 175b1e34c44SGleb Smirnoff #ifdef I_AM_QSORT_R 176b1e34c44SGleb Smirnoff qsort_r(a, r / es, es, thunk, cmp); 177b1e34c44SGleb Smirnoff #else 178d89a1b60SPoul-Henning Kamp qsort(a, r / es, es, cmp); 179b1e34c44SGleb Smirnoff #endif 180d89a1b60SPoul-Henning Kamp if ((r = pd - pc) > es) { 181d89a1b60SPoul-Henning Kamp /* Iterate rather than recurse to save stack space */ 182d89a1b60SPoul-Henning Kamp a = pn - r; 183d89a1b60SPoul-Henning Kamp n = r / es; 184d89a1b60SPoul-Henning Kamp goto loop; 185d89a1b60SPoul-Henning Kamp } 186d89a1b60SPoul-Henning Kamp } 187