158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1992, 1993 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 958f0484fSRodney W. Grimes * are met: 1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15580b4d18SEd Maste * 3. Neither the name of the University nor the names of its contributors 1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1758f0484fSRodney W. Grimes * without specific prior written permission. 1858f0484fSRodney W. Grimes * 1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2958f0484fSRodney W. Grimes * SUCH DAMAGE. 3058f0484fSRodney W. Grimes */ 3158f0484fSRodney W. Grimes 3258f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3358f0484fSRodney W. Grimes static char sccsid[] = "@(#)qsort.c 8.1 (Berkeley) 6/4/93"; 3458f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 35333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 36333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3758f0484fSRodney W. Grimes 380d2fabfcSEdward Tomasz Napierala #include <errno.h> 390d2fabfcSEdward Tomasz Napierala #include <stdint.h> 4058f0484fSRodney W. Grimes #include <stdlib.h> 410d2fabfcSEdward Tomasz Napierala #include <string.h> 420d2fabfcSEdward Tomasz Napierala #include "libc_private.h" 4358f0484fSRodney W. Grimes 440d2fabfcSEdward Tomasz Napierala #if defined(I_AM_QSORT_R) 45eca67d51SGarrett Wollman typedef int cmp_t(void *, const void *, const void *); 460d2fabfcSEdward Tomasz Napierala #elif defined(I_AM_QSORT_S) 470d2fabfcSEdward Tomasz Napierala typedef int cmp_t(const void *, const void *, void *); 48eca67d51SGarrett Wollman #else 49c05ac53bSDavid E. O'Brien typedef int cmp_t(const void *, const void *); 50eca67d51SGarrett Wollman #endif 51eca67d51SGarrett Wollman static inline char *med3(char *, char *, char *, cmp_t *, void *); 5258f0484fSRodney W. Grimes 532eaea119SPedro F. Giffuni #define MIN(a, b) ((a) < (b) ? a : b) 5458f0484fSRodney W. Grimes 5558f0484fSRodney W. Grimes /* 5658f0484fSRodney W. Grimes * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". 5758f0484fSRodney W. Grimes */ 5858f0484fSRodney W. Grimes 5958f0484fSRodney W. Grimes static inline void 6066092616SKonstantin Belousov swapfunc(char *a, char *b, size_t es) 6158f0484fSRodney W. Grimes { 6266092616SKonstantin Belousov char t; 6366092616SKonstantin Belousov 6466092616SKonstantin Belousov do { 6566092616SKonstantin Belousov t = *a; 6666092616SKonstantin Belousov *a++ = *b; 6766092616SKonstantin Belousov *b++ = t; 6866092616SKonstantin Belousov } while (--es > 0); 6958f0484fSRodney W. Grimes } 7058f0484fSRodney W. Grimes 719382fabfSPedro F. Giffuni #define vecswap(a, b, n) \ 7266092616SKonstantin Belousov if ((n) > 0) swapfunc(a, b, n) 7358f0484fSRodney W. Grimes 740d2fabfcSEdward Tomasz Napierala #if defined(I_AM_QSORT_R) 75eca67d51SGarrett Wollman #define CMP(t, x, y) (cmp((t), (x), (y))) 760d2fabfcSEdward Tomasz Napierala #elif defined(I_AM_QSORT_S) 770d2fabfcSEdward Tomasz Napierala #define CMP(t, x, y) (cmp((x), (y), (t))) 78eca67d51SGarrett Wollman #else 79eca67d51SGarrett Wollman #define CMP(t, x, y) (cmp((x), (y))) 80eca67d51SGarrett Wollman #endif 81eca67d51SGarrett Wollman 8258f0484fSRodney W. Grimes static inline char * 83eca67d51SGarrett Wollman med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk 840d2fabfcSEdward Tomasz Napierala #if !defined(I_AM_QSORT_R) && !defined(I_AM_QSORT_S) 85eca67d51SGarrett Wollman __unused 86eca67d51SGarrett Wollman #endif 87eca67d51SGarrett Wollman ) 8858f0484fSRodney W. Grimes { 89eca67d51SGarrett Wollman return CMP(thunk, a, b) < 0 ? 90eca67d51SGarrett Wollman (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) 91eca67d51SGarrett Wollman :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); 9258f0484fSRodney W. Grimes } 9358f0484fSRodney W. Grimes 94*cbcfe28fSAlex Richardson /* 95*cbcfe28fSAlex Richardson * The actual qsort() implementation is static to avoid preemptible calls when 96*cbcfe28fSAlex Richardson * recursing. Also give them different names for improved debugging. 97*cbcfe28fSAlex Richardson */ 980d2fabfcSEdward Tomasz Napierala #if defined(I_AM_QSORT_R) 99*cbcfe28fSAlex Richardson #define local_qsort local_qsort_r 1000d2fabfcSEdward Tomasz Napierala #elif defined(I_AM_QSORT_S) 101*cbcfe28fSAlex Richardson #define local_qsort local_qsort_s 102eca67d51SGarrett Wollman #endif 103*cbcfe28fSAlex Richardson static void 104*cbcfe28fSAlex Richardson local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) 10558f0484fSRodney W. Grimes { 10658f0484fSRodney W. Grimes char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 107ca1578f0SXin LI size_t d1, d2; 108ac48ad2eSDavid Schultz int cmp_result; 10966092616SKonstantin Belousov int swap_cnt; 11058f0484fSRodney W. Grimes 11166092616SKonstantin Belousov loop: 11258f0484fSRodney W. Grimes swap_cnt = 0; 11358f0484fSRodney W. Grimes if (n < 7) { 11409a8dfa2SBruce Evans for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 115eca67d51SGarrett Wollman for (pl = pm; 116eca67d51SGarrett Wollman pl > (char *)a && CMP(thunk, pl - es, pl) > 0; 11758f0484fSRodney W. Grimes pl -= es) 11866092616SKonstantin Belousov swapfunc(pl, pl - es, es); 11958f0484fSRodney W. Grimes return; 12058f0484fSRodney W. Grimes } 12109a8dfa2SBruce Evans pm = (char *)a + (n / 2) * es; 12258f0484fSRodney W. Grimes if (n > 7) { 12358f0484fSRodney W. Grimes pl = a; 12409a8dfa2SBruce Evans pn = (char *)a + (n - 1) * es; 12558f0484fSRodney W. Grimes if (n > 40) { 126ca1578f0SXin LI size_t d = (n / 8) * es; 127ca1578f0SXin LI 128eca67d51SGarrett Wollman pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); 129eca67d51SGarrett Wollman pm = med3(pm - d, pm, pm + d, cmp, thunk); 130eca67d51SGarrett Wollman pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); 13158f0484fSRodney W. Grimes } 132eca67d51SGarrett Wollman pm = med3(pl, pm, pn, cmp, thunk); 13358f0484fSRodney W. Grimes } 13466092616SKonstantin Belousov swapfunc(a, pm, es); 13509a8dfa2SBruce Evans pa = pb = (char *)a + es; 13658f0484fSRodney W. Grimes 13709a8dfa2SBruce Evans pc = pd = (char *)a + (n - 1) * es; 13858f0484fSRodney W. Grimes for (;;) { 139ac48ad2eSDavid Schultz while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) { 140ac48ad2eSDavid Schultz if (cmp_result == 0) { 14158f0484fSRodney W. Grimes swap_cnt = 1; 14266092616SKonstantin Belousov swapfunc(pa, pb, es); 14358f0484fSRodney W. Grimes pa += es; 14458f0484fSRodney W. Grimes } 14558f0484fSRodney W. Grimes pb += es; 14658f0484fSRodney W. Grimes } 147ac48ad2eSDavid Schultz while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) { 148ac48ad2eSDavid Schultz if (cmp_result == 0) { 14958f0484fSRodney W. Grimes swap_cnt = 1; 15066092616SKonstantin Belousov swapfunc(pc, pd, es); 15158f0484fSRodney W. Grimes pd -= es; 15258f0484fSRodney W. Grimes } 15358f0484fSRodney W. Grimes pc -= es; 15458f0484fSRodney W. Grimes } 15558f0484fSRodney W. Grimes if (pb > pc) 15658f0484fSRodney W. Grimes break; 15766092616SKonstantin Belousov swapfunc(pb, pc, es); 15858f0484fSRodney W. Grimes swap_cnt = 1; 15958f0484fSRodney W. Grimes pb += es; 16058f0484fSRodney W. Grimes pc -= es; 16158f0484fSRodney W. Grimes } 16258f0484fSRodney W. Grimes if (swap_cnt == 0) { /* Switch to insertion sort */ 16309a8dfa2SBruce Evans for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 164eca67d51SGarrett Wollman for (pl = pm; 165eca67d51SGarrett Wollman pl > (char *)a && CMP(thunk, pl - es, pl) > 0; 16658f0484fSRodney W. Grimes pl -= es) 16766092616SKonstantin Belousov swapfunc(pl, pl - es, es); 16858f0484fSRodney W. Grimes return; 16958f0484fSRodney W. Grimes } 17058f0484fSRodney W. Grimes 17109a8dfa2SBruce Evans pn = (char *)a + n * es; 172ca1578f0SXin LI d1 = MIN(pa - (char *)a, pb - pa); 173ca1578f0SXin LI vecswap(a, pb - d1, d1); 174ca1578f0SXin LI d1 = MIN(pd - pc, pn - pd - es); 175ca1578f0SXin LI vecswap(pb, pn - d1, d1); 176ca1578f0SXin LI 177ca1578f0SXin LI d1 = pb - pa; 178ca1578f0SXin LI d2 = pd - pc; 179ca1578f0SXin LI if (d1 <= d2) { 180ca1578f0SXin LI /* Recurse on left partition, then iterate on right partition */ 181ca1578f0SXin LI if (d1 > es) { 182*cbcfe28fSAlex Richardson local_qsort(a, d1 / es, es, cmp, thunk); 183ca1578f0SXin LI } 184ca1578f0SXin LI if (d2 > es) { 18558f0484fSRodney W. Grimes /* Iterate rather than recurse to save stack space */ 186ca1578f0SXin LI /* qsort(pn - d2, d2 / es, es, cmp); */ 187ca1578f0SXin LI a = pn - d2; 188ca1578f0SXin LI n = d2 / es; 18958f0484fSRodney W. Grimes goto loop; 19058f0484fSRodney W. Grimes } 191ca1578f0SXin LI } else { 192ca1578f0SXin LI /* Recurse on right partition, then iterate on left partition */ 193ca1578f0SXin LI if (d2 > es) { 194*cbcfe28fSAlex Richardson local_qsort(pn - d2, d2 / es, es, cmp, thunk); 195ca1578f0SXin LI } 196ca1578f0SXin LI if (d1 > es) { 197ca1578f0SXin LI /* Iterate rather than recurse to save stack space */ 198ca1578f0SXin LI /* qsort(a, d1 / es, es, cmp); */ 199ca1578f0SXin LI n = d1 / es; 200ca1578f0SXin LI goto loop; 201ca1578f0SXin LI } 202ca1578f0SXin LI } 20358f0484fSRodney W. Grimes } 204*cbcfe28fSAlex Richardson 205*cbcfe28fSAlex Richardson #if defined(I_AM_QSORT_R) 206*cbcfe28fSAlex Richardson void 207*cbcfe28fSAlex Richardson qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) 208*cbcfe28fSAlex Richardson { 209*cbcfe28fSAlex Richardson local_qsort_r(a, n, es, cmp, thunk); 210*cbcfe28fSAlex Richardson } 211*cbcfe28fSAlex Richardson #elif defined(I_AM_QSORT_S) 212*cbcfe28fSAlex Richardson errno_t 213*cbcfe28fSAlex Richardson qsort_s(void *a, rsize_t n, rsize_t es, cmp_t *cmp, void *thunk) 214*cbcfe28fSAlex Richardson { 215*cbcfe28fSAlex Richardson if (n > RSIZE_MAX) { 216*cbcfe28fSAlex Richardson __throw_constraint_handler_s("qsort_s : n > RSIZE_MAX", EINVAL); 217*cbcfe28fSAlex Richardson return (EINVAL); 218*cbcfe28fSAlex Richardson } else if (es > RSIZE_MAX) { 219*cbcfe28fSAlex Richardson __throw_constraint_handler_s("qsort_s : es > RSIZE_MAX", 220*cbcfe28fSAlex Richardson EINVAL); 221*cbcfe28fSAlex Richardson return (EINVAL); 222*cbcfe28fSAlex Richardson } else if (n != 0) { 223*cbcfe28fSAlex Richardson if (a == NULL) { 224*cbcfe28fSAlex Richardson __throw_constraint_handler_s("qsort_s : a == NULL", 225*cbcfe28fSAlex Richardson EINVAL); 226*cbcfe28fSAlex Richardson return (EINVAL); 227*cbcfe28fSAlex Richardson } else if (cmp == NULL) { 228*cbcfe28fSAlex Richardson __throw_constraint_handler_s("qsort_s : cmp == NULL", 229*cbcfe28fSAlex Richardson EINVAL); 230*cbcfe28fSAlex Richardson return (EINVAL); 231*cbcfe28fSAlex Richardson } 232*cbcfe28fSAlex Richardson } 233*cbcfe28fSAlex Richardson 234*cbcfe28fSAlex Richardson local_qsort_s(a, n, es, cmp, thunk); 235*cbcfe28fSAlex Richardson return (0); 236*cbcfe28fSAlex Richardson } 237*cbcfe28fSAlex Richardson #else 238*cbcfe28fSAlex Richardson void 239*cbcfe28fSAlex Richardson qsort(void *a, size_t n, size_t es, cmp_t *cmp) 240*cbcfe28fSAlex Richardson { 241*cbcfe28fSAlex Richardson local_qsort(a, n, es, cmp, NULL); 242*cbcfe28fSAlex Richardson } 243*cbcfe28fSAlex Richardson #endif 244