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.c55
1 files changed, 22 insertions, 33 deletions
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c
index c06bd54054..154c51a86c 100644
--- a/src/lib/libc/stdlib/qsort.c
+++ b/src/lib/libc/stdlib/qsort.c
@@ -10,11 +10,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 13 * 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 14 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 15 * without specific prior written permission.
20 * 16 *
@@ -32,15 +28,14 @@
32 */ 28 */
33 29
34#if defined(LIBC_SCCS) && !defined(lint) 30#if defined(LIBC_SCCS) && !defined(lint)
35/*static char sccsid[] = "from: @(#)qsort.c 8.1 (Berkeley) 6/4/93";*/ 31static char *rcsid = "$OpenBSD: qsort.c,v 1.9 2005/03/30 18:51:49 pat Exp $";
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 */ 32#endif /* LIBC_SCCS and not lint */
38 33
39#include <sys/types.h> 34#include <sys/types.h>
40#include <stdlib.h> 35#include <stdlib.h>
41 36
42static inline char *med3 __P((char *, char *, char *, int (*)())); 37static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
43static inline void swapfunc __P((char *, char *, int, int)); 38static __inline void swapfunc(char *, char *, int, int);
44 39
45#define min(a, b) (a) < (b) ? a : b 40#define min(a, b) (a) < (b) ? a : b
46 41
@@ -49,10 +44,10 @@ static inline void swapfunc __P((char *, char *, int, int));
49 */ 44 */
50#define swapcode(TYPE, parmi, parmj, n) { \ 45#define swapcode(TYPE, parmi, parmj, n) { \
51 long i = (n) / sizeof (TYPE); \ 46 long i = (n) / sizeof (TYPE); \
52 register TYPE *pi = (TYPE *) (parmi); \ 47 TYPE *pi = (TYPE *) (parmi); \
53 register TYPE *pj = (TYPE *) (parmj); \ 48 TYPE *pj = (TYPE *) (parmj); \
54 do { \ 49 do { \
55 register TYPE t = *pi; \ 50 TYPE t = *pi; \
56 *pi++ = *pj; \ 51 *pi++ = *pj; \
57 *pj++ = t; \ 52 *pj++ = t; \
58 } while (--i > 0); \ 53 } while (--i > 0); \
@@ -61,12 +56,10 @@ static inline void swapfunc __P((char *, char *, int, int));
61#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ 56#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
62 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; 57 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
63 58
64static inline void 59static __inline void
65swapfunc(a, b, n, swaptype) 60swapfunc(char *a, char *b, int n, int swaptype)
66 char *a, *b;
67 int n, swaptype;
68{ 61{
69 if(swaptype <= 1) 62 if (swaptype <= 1)
70 swapcode(long, a, b, n) 63 swapcode(long, a, b, n)
71 else 64 else
72 swapcode(char, a, b, n) 65 swapcode(char, a, b, n)
@@ -82,10 +75,8 @@ swapfunc(a, b, n, swaptype)
82 75
83#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 76#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
84 77
85static inline char * 78static __inline char *
86med3(a, b, c, cmp) 79med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
87 char *a, *b, *c;
88 int (*cmp)();
89{ 80{
90 return cmp(a, b) < 0 ? 81 return cmp(a, b) < 0 ?
91 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) 82 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
@@ -93,27 +84,25 @@ med3(a, b, c, cmp)
93} 84}
94 85
95void 86void
96qsort(a, n, es, cmp) 87qsort(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{ 88{
101 char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 89 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
102 int d, r, swaptype, swap_cnt; 90 int d, r, swaptype, swap_cnt;
91 char *a = aa;
103 92
104loop: SWAPINIT(a, es); 93loop: SWAPINIT(a, es);
105 swap_cnt = 0; 94 swap_cnt = 0;
106 if (n < 7) { 95 if (n < 7) {
107 for (pm = a + es; pm < (char *) a + n * es; pm += es) 96 for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
108 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 97 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
109 pl -= es) 98 pl -= es)
110 swap(pl, pl - es); 99 swap(pl, pl - es);
111 return; 100 return;
112 } 101 }
113 pm = a + (n / 2) * es; 102 pm = (char *)a + (n / 2) * es;
114 if (n > 7) { 103 if (n > 7) {
115 pl = a; 104 pl = (char *)a;
116 pn = a + (n - 1) * es; 105 pn = (char *)a + (n - 1) * es;
117 if (n > 40) { 106 if (n > 40) {
118 d = (n / 8) * es; 107 d = (n / 8) * es;
119 pl = med3(pl, pl + d, pl + 2 * d, cmp); 108 pl = med3(pl, pl + d, pl + 2 * d, cmp);
@@ -123,9 +112,9 @@ loop: SWAPINIT(a, es);
123 pm = med3(pl, pm, pn, cmp); 112 pm = med3(pl, pm, pn, cmp);
124 } 113 }
125 swap(a, pm); 114 swap(a, pm);
126 pa = pb = a + es; 115 pa = pb = (char *)a + es;
127 116
128 pc = pd = a + (n - 1) * es; 117 pc = pd = (char *)a + (n - 1) * es;
129 for (;;) { 118 for (;;) {
130 while (pb <= pc && (r = cmp(pb, a)) <= 0) { 119 while (pb <= pc && (r = cmp(pb, a)) <= 0) {
131 if (r == 0) { 120 if (r == 0) {
@@ -151,14 +140,14 @@ loop: SWAPINIT(a, es);
151 pc -= es; 140 pc -= es;
152 } 141 }
153 if (swap_cnt == 0) { /* Switch to insertion sort */ 142 if (swap_cnt == 0) { /* Switch to insertion sort */
154 for (pm = a + es; pm < (char *) a + n * es; pm += es) 143 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
155 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 144 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
156 pl -= es) 145 pl -= es)
157 swap(pl, pl - es); 146 swap(pl, pl - es);
158 return; 147 return;
159 } 148 }
160 149
161 pn = a + n * es; 150 pn = (char *)a + n * es;
162 r = min(pa - (char *)a, pb - pa); 151 r = min(pa - (char *)a, pb - pa);
163 vecswap(a, pb - r, r); 152 vecswap(a, pb - r, r);
164 r = min(pd - pc, pn - pd - es); 153 r = min(pd - pc, pn - pd - es);