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.c40
1 files changed, 18 insertions, 22 deletions
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c
index c06bd54054..2a972c0eb0 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.8 2003/06/02 20:18:38 millert 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 (*)());
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
@@ -61,12 +56,12 @@ 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(a, b, n, swaptype)
66 char *a, *b; 61 char *a, *b;
67 int n, swaptype; 62 int n, swaptype;
68{ 63{
69 if(swaptype <= 1) 64 if (swaptype <= 1)
70 swapcode(long, a, b, n) 65 swapcode(long, a, b, n)
71 else 66 else
72 swapcode(char, a, b, n) 67 swapcode(char, a, b, n)
@@ -82,7 +77,7 @@ swapfunc(a, b, n, swaptype)
82 77
83#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 78#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
84 79
85static inline char * 80static __inline char *
86med3(a, b, c, cmp) 81med3(a, b, c, cmp)
87 char *a, *b, *c; 82 char *a, *b, *c;
88 int (*cmp)(); 83 int (*cmp)();
@@ -93,27 +88,28 @@ med3(a, b, c, cmp)
93} 88}
94 89
95void 90void
96qsort(a, n, es, cmp) 91qsort(aa, n, es, cmp)
97 void *a; 92 void *aa;
98 size_t n, es; 93 size_t n, es;
99 int (*cmp)(); 94 int (*cmp)();
100{ 95{
101 char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 96 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
102 int d, r, swaptype, swap_cnt; 97 int d, r, swaptype, swap_cnt;
98 register char *a = aa;
103 99
104loop: SWAPINIT(a, es); 100loop: SWAPINIT(a, es);
105 swap_cnt = 0; 101 swap_cnt = 0;
106 if (n < 7) { 102 if (n < 7) {
107 for (pm = a + es; pm < (char *) a + n * es; pm += es) 103 for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
108 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 104 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
109 pl -= es) 105 pl -= es)
110 swap(pl, pl - es); 106 swap(pl, pl - es);
111 return; 107 return;
112 } 108 }
113 pm = a + (n / 2) * es; 109 pm = (char *)a + (n / 2) * es;
114 if (n > 7) { 110 if (n > 7) {
115 pl = a; 111 pl = (char *)a;
116 pn = a + (n - 1) * es; 112 pn = (char *)a + (n - 1) * es;
117 if (n > 40) { 113 if (n > 40) {
118 d = (n / 8) * es; 114 d = (n / 8) * es;
119 pl = med3(pl, pl + d, pl + 2 * d, cmp); 115 pl = med3(pl, pl + d, pl + 2 * d, cmp);
@@ -123,9 +119,9 @@ loop: SWAPINIT(a, es);
123 pm = med3(pl, pm, pn, cmp); 119 pm = med3(pl, pm, pn, cmp);
124 } 120 }
125 swap(a, pm); 121 swap(a, pm);
126 pa = pb = a + es; 122 pa = pb = (char *)a + es;
127 123
128 pc = pd = a + (n - 1) * es; 124 pc = pd = (char *)a + (n - 1) * es;
129 for (;;) { 125 for (;;) {
130 while (pb <= pc && (r = cmp(pb, a)) <= 0) { 126 while (pb <= pc && (r = cmp(pb, a)) <= 0) {
131 if (r == 0) { 127 if (r == 0) {
@@ -151,14 +147,14 @@ loop: SWAPINIT(a, es);
151 pc -= es; 147 pc -= es;
152 } 148 }
153 if (swap_cnt == 0) { /* Switch to insertion sort */ 149 if (swap_cnt == 0) { /* Switch to insertion sort */
154 for (pm = a + es; pm < (char *) a + n * es; pm += es) 150 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
155 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 151 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
156 pl -= es) 152 pl -= es)
157 swap(pl, pl - es); 153 swap(pl, pl - es);
158 return; 154 return;
159 } 155 }
160 156
161 pn = a + n * es; 157 pn = (char *)a + n * es;
162 r = min(pa - (char *)a, pb - pa); 158 r = min(pa - (char *)a, pb - pa);
163 vecswap(a, pb - r, r); 159 vecswap(a, pb - r, r);
164 r = min(pd - pc, pn - pd - es); 160 r = min(pd - pc, pn - pd - es);