1*f14fb602SLionel Sambuc /* $NetBSD: qsort.c,v 1.22 2012/05/26 21:47:05 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1992, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)qsort.c 8.1 (Berkeley) 6/4/93";
362fe8fb19SBen Gras #else
37*f14fb602SLionel Sambuc __RCSID("$NetBSD: qsort.c,v 1.22 2012/05/26 21:47:05 christos Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras
432fe8fb19SBen Gras #include <assert.h>
442fe8fb19SBen Gras #include <errno.h>
452fe8fb19SBen Gras #include <stdlib.h>
462fe8fb19SBen Gras
47*f14fb602SLionel Sambuc static inline char *med3(char *, char *, char *,
48*f14fb602SLionel Sambuc int (*)(const void *, const void *));
49*f14fb602SLionel Sambuc static inline void swapfunc(char *, char *, size_t, int);
502fe8fb19SBen Gras
512fe8fb19SBen Gras #define min(a, b) (a) < (b) ? a : b
522fe8fb19SBen Gras
532fe8fb19SBen Gras /*
542fe8fb19SBen Gras * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
552fe8fb19SBen Gras */
562fe8fb19SBen Gras #define swapcode(TYPE, parmi, parmj, n) { \
572fe8fb19SBen Gras size_t i = (n) / sizeof (TYPE); \
582fe8fb19SBen Gras TYPE *pi = (TYPE *)(void *)(parmi); \
592fe8fb19SBen Gras TYPE *pj = (TYPE *)(void *)(parmj); \
602fe8fb19SBen Gras do { \
612fe8fb19SBen Gras TYPE t = *pi; \
622fe8fb19SBen Gras *pi++ = *pj; \
632fe8fb19SBen Gras *pj++ = t; \
642fe8fb19SBen Gras } while (--i > 0); \
652fe8fb19SBen Gras }
662fe8fb19SBen Gras
672fe8fb19SBen Gras #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
682fe8fb19SBen Gras es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
692fe8fb19SBen Gras
702fe8fb19SBen Gras static inline void
swapfunc(char * a,char * b,size_t n,int swaptype)712fe8fb19SBen Gras swapfunc(char *a, char *b, size_t n, int swaptype)
722fe8fb19SBen Gras {
732fe8fb19SBen Gras
742fe8fb19SBen Gras if (swaptype <= 1)
752fe8fb19SBen Gras swapcode(long, a, b, n)
762fe8fb19SBen Gras else
772fe8fb19SBen Gras swapcode(char, a, b, n)
782fe8fb19SBen Gras }
792fe8fb19SBen Gras
802fe8fb19SBen Gras #define swap(a, b) \
812fe8fb19SBen Gras if (swaptype == 0) { \
822fe8fb19SBen Gras long t = *(long *)(void *)(a); \
832fe8fb19SBen Gras *(long *)(void *)(a) = *(long *)(void *)(b); \
842fe8fb19SBen Gras *(long *)(void *)(b) = t; \
852fe8fb19SBen Gras } else \
862fe8fb19SBen Gras swapfunc(a, b, es, swaptype)
872fe8fb19SBen Gras
882fe8fb19SBen Gras #define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)
892fe8fb19SBen Gras
902fe8fb19SBen Gras static inline char *
med3(char * a,char * b,char * c,int (* cmp)(const void *,const void *))912fe8fb19SBen Gras med3(char *a, char *b, char *c,
92*f14fb602SLionel Sambuc int (*cmp)(const void *, const void *))
932fe8fb19SBen Gras {
942fe8fb19SBen Gras
952fe8fb19SBen Gras return cmp(a, b) < 0 ?
962fe8fb19SBen Gras (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
972fe8fb19SBen Gras :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
982fe8fb19SBen Gras }
992fe8fb19SBen Gras
1002fe8fb19SBen Gras void
qsort(void * a,size_t n,size_t es,int (* cmp)(const void *,const void *))1012fe8fb19SBen Gras qsort(void *a, size_t n, size_t es,
102*f14fb602SLionel Sambuc int (*cmp)(const void *, const void *))
1032fe8fb19SBen Gras {
1042fe8fb19SBen Gras char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
1052fe8fb19SBen Gras size_t d, r;
1062fe8fb19SBen Gras int swaptype, cmp_result;
1072fe8fb19SBen Gras
108*f14fb602SLionel Sambuc _DIAGASSERT(a != NULL || n == 0 || es == 0);
1092fe8fb19SBen Gras _DIAGASSERT(cmp != NULL);
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras loop: SWAPINIT(a, es);
1122fe8fb19SBen Gras if (n < 7) {
1132fe8fb19SBen Gras for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
1142fe8fb19SBen Gras for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
1152fe8fb19SBen Gras pl -= es)
1162fe8fb19SBen Gras swap(pl, pl - es);
1172fe8fb19SBen Gras return;
1182fe8fb19SBen Gras }
1192fe8fb19SBen Gras pm = (char *) a + (n / 2) * es;
1202fe8fb19SBen Gras if (n > 7) {
1212fe8fb19SBen Gras pl = (char *) a;
1222fe8fb19SBen Gras pn = (char *) a + (n - 1) * es;
1232fe8fb19SBen Gras if (n > 40) {
1242fe8fb19SBen Gras d = (n / 8) * es;
1252fe8fb19SBen Gras pl = med3(pl, pl + d, pl + 2 * d, cmp);
1262fe8fb19SBen Gras pm = med3(pm - d, pm, pm + d, cmp);
1272fe8fb19SBen Gras pn = med3(pn - 2 * d, pn - d, pn, cmp);
1282fe8fb19SBen Gras }
1292fe8fb19SBen Gras pm = med3(pl, pm, pn, cmp);
1302fe8fb19SBen Gras }
1312fe8fb19SBen Gras swap(a, pm);
1322fe8fb19SBen Gras pa = pb = (char *) a + es;
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras pc = pd = (char *) a + (n - 1) * es;
1352fe8fb19SBen Gras for (;;) {
1362fe8fb19SBen Gras while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
1372fe8fb19SBen Gras if (cmp_result == 0) {
1382fe8fb19SBen Gras swap(pa, pb);
1392fe8fb19SBen Gras pa += es;
1402fe8fb19SBen Gras }
1412fe8fb19SBen Gras pb += es;
1422fe8fb19SBen Gras }
1432fe8fb19SBen Gras while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
1442fe8fb19SBen Gras if (cmp_result == 0) {
1452fe8fb19SBen Gras swap(pc, pd);
1462fe8fb19SBen Gras pd -= es;
1472fe8fb19SBen Gras }
1482fe8fb19SBen Gras pc -= es;
1492fe8fb19SBen Gras }
1502fe8fb19SBen Gras if (pb > pc)
1512fe8fb19SBen Gras break;
1522fe8fb19SBen Gras swap(pb, pc);
1532fe8fb19SBen Gras pb += es;
1542fe8fb19SBen Gras pc -= es;
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras
1572fe8fb19SBen Gras pn = (char *) a + n * es;
1582fe8fb19SBen Gras r = min(pa - (char *) a, pb - pa);
1592fe8fb19SBen Gras vecswap(a, pb - r, r);
1602fe8fb19SBen Gras r = min((size_t)(pd - pc), pn - pd - es);
1612fe8fb19SBen Gras vecswap(pb, pn - r, r);
1622fe8fb19SBen Gras if ((r = pb - pa) > es)
1632fe8fb19SBen Gras qsort(a, r / es, es, cmp);
1642fe8fb19SBen Gras if ((r = pd - pc) > es) {
1652fe8fb19SBen Gras /* Iterate rather than recurse to save stack space */
1662fe8fb19SBen Gras a = pn - r;
1672fe8fb19SBen Gras n = r / es;
1682fe8fb19SBen Gras goto loop;
1692fe8fb19SBen Gras }
1702fe8fb19SBen Gras /* qsort(pn - r, r / es, es, cmp);*/
1712fe8fb19SBen Gras }
172