1*1bb92983SJerome Forissier // SPDX-License-Identifier: BSD-3-Clause 2b0104773SPascal Brand /* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ 3b0104773SPascal Brand /*- 4b0104773SPascal Brand * Copyright (c) 1992, 1993 5b0104773SPascal Brand * The Regents of the University of California. All rights reserved. 6b0104773SPascal Brand * 7b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 8b0104773SPascal Brand * modification, are permitted provided that the following conditions 9b0104773SPascal Brand * are met: 10b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright 11b0104773SPascal Brand * notice, this list of conditions and the following disclaimer. 12b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright 13b0104773SPascal Brand * notice, this list of conditions and the following disclaimer in the 14b0104773SPascal Brand * documentation and/or other materials provided with the distribution. 15b0104773SPascal Brand * 3. Neither the name of the University nor the names of its contributors 16b0104773SPascal Brand * may be used to endorse or promote products derived from this software 17b0104773SPascal Brand * without specific prior written permission. 18b0104773SPascal Brand * 19b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20b0104773SPascal Brand * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23b0104773SPascal Brand * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24b0104773SPascal Brand * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25b0104773SPascal Brand * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26b0104773SPascal Brand * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27b0104773SPascal Brand * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28b0104773SPascal Brand * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29b0104773SPascal Brand * SUCH DAMAGE. 30b0104773SPascal Brand */ 31b0104773SPascal Brand #include <sys/types.h> 32b0104773SPascal Brand #include <stdlib.h> 33b0104773SPascal Brand static __inline char 34b0104773SPascal Brand *med3(char *, char *, char *, int (*)(const void *, const void *)); 35b0104773SPascal Brand static __inline void swapfunc(char *, char *, int, int); 36b0104773SPascal Brand #define min(a, b) (a) < (b) ? a : b 37b0104773SPascal Brand /* 38b0104773SPascal Brand * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". 39b0104773SPascal Brand */ 40b0104773SPascal Brand #define swapcode(TYPE, parmi, parmj, n) { \ 41b0104773SPascal Brand long i = (n) / sizeof (TYPE); \ 42b0104773SPascal Brand TYPE *pi = (TYPE *) (parmi); \ 43b0104773SPascal Brand TYPE *pj = (TYPE *) (parmj); \ 44b0104773SPascal Brand do { \ 45b0104773SPascal Brand TYPE t = *pi; \ 46b0104773SPascal Brand *pi++ = *pj; \ 47b0104773SPascal Brand *pj++ = t; \ 48b0104773SPascal Brand } while (--i > 0); \ 49b0104773SPascal Brand } 50b0104773SPascal Brand #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ 51b0104773SPascal Brand es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; 52b0104773SPascal Brand static __inline void 53b0104773SPascal Brand swapfunc(char *a, char *b, int n, int swaptype) 54b0104773SPascal Brand { 55b0104773SPascal Brand if (swaptype <= 1) 56b0104773SPascal Brand swapcode(long, a, b, n) 57b0104773SPascal Brand else 58b0104773SPascal Brand swapcode(char, a, b, n) 59b0104773SPascal Brand } 60b0104773SPascal Brand #define swap(a, b) \ 61b0104773SPascal Brand if (swaptype == 0) { \ 62b0104773SPascal Brand long t = *(long *)(a); \ 63b0104773SPascal Brand *(long *)(a) = *(long *)(b); \ 64b0104773SPascal Brand *(long *)(b) = t; \ 65b0104773SPascal Brand } else \ 66b0104773SPascal Brand swapfunc(a, b, es, swaptype) 67b0104773SPascal Brand #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 68b0104773SPascal Brand static __inline char * 69b0104773SPascal Brand med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) 70b0104773SPascal Brand { 71b0104773SPascal Brand return cmp(a, b) < 0 ? 72b0104773SPascal Brand (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) 73b0104773SPascal Brand :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); 74b0104773SPascal Brand } 75b0104773SPascal Brand void 76b0104773SPascal Brand qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) 77b0104773SPascal Brand { 78b0104773SPascal Brand char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 79b0104773SPascal Brand int d, r, swaptype, swap_cnt; 80b0104773SPascal Brand char *a = aa; 81b0104773SPascal Brand loop: SWAPINIT(a, es); 82b0104773SPascal Brand swap_cnt = 0; 83b0104773SPascal Brand if (n < 7) { 84b0104773SPascal Brand for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) 85b0104773SPascal Brand for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 86b0104773SPascal Brand pl -= es) 87b0104773SPascal Brand swap(pl, pl - es); 88b0104773SPascal Brand return; 89b0104773SPascal Brand } 90b0104773SPascal Brand pm = (char *)a + (n / 2) * es; 91b0104773SPascal Brand if (n > 7) { 92b0104773SPascal Brand pl = (char *)a; 93b0104773SPascal Brand pn = (char *)a + (n - 1) * es; 94b0104773SPascal Brand if (n > 40) { 95b0104773SPascal Brand d = (n / 8) * es; 96b0104773SPascal Brand pl = med3(pl, pl + d, pl + 2 * d, cmp); 97b0104773SPascal Brand pm = med3(pm - d, pm, pm + d, cmp); 98b0104773SPascal Brand pn = med3(pn - 2 * d, pn - d, pn, cmp); 99b0104773SPascal Brand } 100b0104773SPascal Brand pm = med3(pl, pm, pn, cmp); 101b0104773SPascal Brand } 102b0104773SPascal Brand swap(a, pm); 103b0104773SPascal Brand pa = pb = (char *)a + es; 104b0104773SPascal Brand 105b0104773SPascal Brand pc = pd = (char *)a + (n - 1) * es; 106b0104773SPascal Brand for (;;) { 107b0104773SPascal Brand while (pb <= pc && (r = cmp(pb, a)) <= 0) { 108b0104773SPascal Brand if (r == 0) { 109b0104773SPascal Brand swap_cnt = 1; 110b0104773SPascal Brand swap(pa, pb); 111b0104773SPascal Brand pa += es; 112b0104773SPascal Brand } 113b0104773SPascal Brand pb += es; 114b0104773SPascal Brand } 115b0104773SPascal Brand while (pb <= pc && (r = cmp(pc, a)) >= 0) { 116b0104773SPascal Brand if (r == 0) { 117b0104773SPascal Brand swap_cnt = 1; 118b0104773SPascal Brand swap(pc, pd); 119b0104773SPascal Brand pd -= es; 120b0104773SPascal Brand } 121b0104773SPascal Brand pc -= es; 122b0104773SPascal Brand } 123b0104773SPascal Brand if (pb > pc) 124b0104773SPascal Brand break; 125b0104773SPascal Brand swap(pb, pc); 126b0104773SPascal Brand swap_cnt = 1; 127b0104773SPascal Brand pb += es; 128b0104773SPascal Brand pc -= es; 129b0104773SPascal Brand } 130b0104773SPascal Brand if (swap_cnt == 0) { /* Switch to insertion sort */ 131b0104773SPascal Brand for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) 132b0104773SPascal Brand for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 133b0104773SPascal Brand pl -= es) 134b0104773SPascal Brand swap(pl, pl - es); 135b0104773SPascal Brand return; 136b0104773SPascal Brand } 137b0104773SPascal Brand pn = (char *)a + n * es; 138b0104773SPascal Brand r = min(pa - (char *)a, pb - pa); 139b0104773SPascal Brand vecswap(a, pb - r, r); 140b0104773SPascal Brand r = min(pd - pc, pn - pd - (int)es); 141b0104773SPascal Brand vecswap(pb, pn - r, r); 142b0104773SPascal Brand if ((r = pb - pa) > (int)es) 143b0104773SPascal Brand qsort(a, r / es, es, cmp); 144b0104773SPascal Brand if ((r = pd - pc) > (int)es) { 145b0104773SPascal Brand /* Iterate rather than recurse to save stack space */ 146b0104773SPascal Brand a = pn - r; 147b0104773SPascal Brand n = r / es; 148b0104773SPascal Brand goto loop; 149b0104773SPascal Brand } 150b0104773SPascal Brand /* qsort(pn - r, r / es, es, cmp);*/ 151b0104773SPascal Brand } 152