summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/qsort.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/qsort.c')
-rw-r--r--src/lib/libc/stdlib/qsort.c71
1 files changed, 29 insertions, 42 deletions
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c
index c06bd54054..f28449fb5b 100644
--- a/src/lib/libc/stdlib/qsort.c
+++ b/src/lib/libc/stdlib/qsort.c
@@ -1,3 +1,4 @@
1/* $OpenBSD: qsort.c,v 1.11 2010/02/08 11:04:07 otto Exp $ */
1/*- 2/*-
2 * Copyright (c) 1992, 1993 3 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
@@ -10,11 +11,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 14 * 3. Neither the name of the University nor the names of its contributors
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software 15 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 16 * without specific prior written permission.
20 * 17 *
@@ -31,16 +28,11 @@
31 * SUCH DAMAGE. 28 * SUCH DAMAGE.
32 */ 29 */
33 30
34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char sccsid[] = "from: @(#)qsort.c 8.1 (Berkeley) 6/4/93";*/
36static char *rcsid = "$Id: qsort.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */
38
39#include <sys/types.h> 31#include <sys/types.h>
40#include <stdlib.h> 32#include <stdlib.h>
41 33
42static inline char *med3 __P((char *, char *, char *, int (*)())); 34static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
43static inline void swapfunc __P((char *, char *, int, int)); 35static __inline void swapfunc(char *, char *, size_t, int);
44 36
45#define min(a, b) (a) < (b) ? a : b 37#define min(a, b) (a) < (b) ? a : b
46 38
@@ -48,11 +40,11 @@ static inline void swapfunc __P((char *, char *, int, int));
48 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". 40 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
49 */ 41 */
50#define swapcode(TYPE, parmi, parmj, n) { \ 42#define swapcode(TYPE, parmi, parmj, n) { \
51 long i = (n) / sizeof (TYPE); \ 43 size_t i = (n) / sizeof (TYPE); \
52 register TYPE *pi = (TYPE *) (parmi); \ 44 TYPE *pi = (TYPE *) (parmi); \
53 register TYPE *pj = (TYPE *) (parmj); \ 45 TYPE *pj = (TYPE *) (parmj); \
54 do { \ 46 do { \
55 register TYPE t = *pi; \ 47 TYPE t = *pi; \
56 *pi++ = *pj; \ 48 *pi++ = *pj; \
57 *pj++ = t; \ 49 *pj++ = t; \
58 } while (--i > 0); \ 50 } while (--i > 0); \
@@ -61,12 +53,10 @@ static inline void swapfunc __P((char *, char *, int, int));
61#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ 53#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
62 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; 54 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
63 55
64static inline void 56static __inline void
65swapfunc(a, b, n, swaptype) 57swapfunc(char *a, char *b, size_t n, int swaptype)
66 char *a, *b;
67 int n, swaptype;
68{ 58{
69 if(swaptype <= 1) 59 if (swaptype <= 1)
70 swapcode(long, a, b, n) 60 swapcode(long, a, b, n)
71 else 61 else
72 swapcode(char, a, b, n) 62 swapcode(char, a, b, n)
@@ -82,10 +72,8 @@ swapfunc(a, b, n, swaptype)
82 72
83#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 73#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
84 74
85static inline char * 75static __inline char *
86med3(a, b, c, cmp) 76med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
87 char *a, *b, *c;
88 int (*cmp)();
89{ 77{
90 return cmp(a, b) < 0 ? 78 return cmp(a, b) < 0 ?
91 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) 79 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
@@ -93,27 +81,26 @@ med3(a, b, c, cmp)
93} 81}
94 82
95void 83void
96qsort(a, n, es, cmp) 84qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
97 void *a;
98 size_t n, es;
99 int (*cmp)();
100{ 85{
101 char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 86 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
102 int d, r, swaptype, swap_cnt; 87 int cmp_result, swaptype, swap_cnt;
88 size_t d, r;
89 char *a = aa;
103 90
104loop: SWAPINIT(a, es); 91loop: SWAPINIT(a, es);
105 swap_cnt = 0; 92 swap_cnt = 0;
106 if (n < 7) { 93 if (n < 7) {
107 for (pm = a + es; pm < (char *) a + n * es; pm += es) 94 for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
108 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 95 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
109 pl -= es) 96 pl -= es)
110 swap(pl, pl - es); 97 swap(pl, pl - es);
111 return; 98 return;
112 } 99 }
113 pm = a + (n / 2) * es; 100 pm = (char *)a + (n / 2) * es;
114 if (n > 7) { 101 if (n > 7) {
115 pl = a; 102 pl = (char *)a;
116 pn = a + (n - 1) * es; 103 pn = (char *)a + (n - 1) * es;
117 if (n > 40) { 104 if (n > 40) {
118 d = (n / 8) * es; 105 d = (n / 8) * es;
119 pl = med3(pl, pl + d, pl + 2 * d, cmp); 106 pl = med3(pl, pl + d, pl + 2 * d, cmp);
@@ -123,20 +110,20 @@ loop: SWAPINIT(a, es);
123 pm = med3(pl, pm, pn, cmp); 110 pm = med3(pl, pm, pn, cmp);
124 } 111 }
125 swap(a, pm); 112 swap(a, pm);
126 pa = pb = a + es; 113 pa = pb = (char *)a + es;
127 114
128 pc = pd = a + (n - 1) * es; 115 pc = pd = (char *)a + (n - 1) * es;
129 for (;;) { 116 for (;;) {
130 while (pb <= pc && (r = cmp(pb, a)) <= 0) { 117 while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
131 if (r == 0) { 118 if (cmp_result == 0) {
132 swap_cnt = 1; 119 swap_cnt = 1;
133 swap(pa, pb); 120 swap(pa, pb);
134 pa += es; 121 pa += es;
135 } 122 }
136 pb += es; 123 pb += es;
137 } 124 }
138 while (pb <= pc && (r = cmp(pc, a)) >= 0) { 125 while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
139 if (r == 0) { 126 if (cmp_result == 0) {
140 swap_cnt = 1; 127 swap_cnt = 1;
141 swap(pc, pd); 128 swap(pc, pd);
142 pd -= es; 129 pd -= es;
@@ -151,14 +138,14 @@ loop: SWAPINIT(a, es);
151 pc -= es; 138 pc -= es;
152 } 139 }
153 if (swap_cnt == 0) { /* Switch to insertion sort */ 140 if (swap_cnt == 0) { /* Switch to insertion sort */
154 for (pm = a + es; pm < (char *) a + n * es; pm += es) 141 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
155 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 142 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
156 pl -= es) 143 pl -= es)
157 swap(pl, pl - es); 144 swap(pl, pl - es);
158 return; 145 return;
159 } 146 }
160 147
161 pn = a + n * es; 148 pn = (char *)a + n * es;
162 r = min(pa - (char *)a, pb - pa); 149 r = min(pa - (char *)a, pb - pa);
163 vecswap(a, pb - r, r); 150 vecswap(a, pb - r, r);
164 r = min(pd - pc, pn - pd - es); 151 r = min(pd - pc, pn - pd - es);