diff options
Diffstat (limited to 'src/lib/libc/stdlib/qsort.c')
-rw-r--r-- | src/lib/libc/stdlib/qsort.c | 58 |
1 files changed, 22 insertions, 36 deletions
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c index c06bd54054..bb4a9a11f2 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.10 2005/08/08 08:05:37 espie 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";*/ | ||
36 | static 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 | ||
42 | static inline char *med3 __P((char *, char *, char *, int (*)())); | 34 | static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); |
43 | static inline void swapfunc __P((char *, char *, int, int)); | 35 | static __inline void swapfunc(char *, char *, int, int); |
44 | 36 | ||
45 | #define min(a, b) (a) < (b) ? a : b | 37 | #define min(a, b) (a) < (b) ? a : b |
46 | 38 | ||
@@ -49,10 +41,10 @@ static inline void swapfunc __P((char *, char *, int, int)); | |||
49 | */ | 41 | */ |
50 | #define swapcode(TYPE, parmi, parmj, n) { \ | 42 | #define swapcode(TYPE, parmi, parmj, n) { \ |
51 | long i = (n) / sizeof (TYPE); \ | 43 | long 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 | ||
64 | static inline void | 56 | static __inline void |
65 | swapfunc(a, b, n, swaptype) | 57 | swapfunc(char *a, char *b, int 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 | ||
85 | static inline char * | 75 | static __inline char * |
86 | med3(a, b, c, cmp) | 76 | med3(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,25 @@ med3(a, b, c, cmp) | |||
93 | } | 81 | } |
94 | 82 | ||
95 | void | 83 | void |
96 | qsort(a, n, es, cmp) | 84 | qsort(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 d, r, swaptype, swap_cnt; |
88 | char *a = aa; | ||
103 | 89 | ||
104 | loop: SWAPINIT(a, es); | 90 | loop: SWAPINIT(a, es); |
105 | swap_cnt = 0; | 91 | swap_cnt = 0; |
106 | if (n < 7) { | 92 | if (n < 7) { |
107 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | 93 | for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) |
108 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | 94 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
109 | pl -= es) | 95 | pl -= es) |
110 | swap(pl, pl - es); | 96 | swap(pl, pl - es); |
111 | return; | 97 | return; |
112 | } | 98 | } |
113 | pm = a + (n / 2) * es; | 99 | pm = (char *)a + (n / 2) * es; |
114 | if (n > 7) { | 100 | if (n > 7) { |
115 | pl = a; | 101 | pl = (char *)a; |
116 | pn = a + (n - 1) * es; | 102 | pn = (char *)a + (n - 1) * es; |
117 | if (n > 40) { | 103 | if (n > 40) { |
118 | d = (n / 8) * es; | 104 | d = (n / 8) * es; |
119 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | 105 | pl = med3(pl, pl + d, pl + 2 * d, cmp); |
@@ -123,9 +109,9 @@ loop: SWAPINIT(a, es); | |||
123 | pm = med3(pl, pm, pn, cmp); | 109 | pm = med3(pl, pm, pn, cmp); |
124 | } | 110 | } |
125 | swap(a, pm); | 111 | swap(a, pm); |
126 | pa = pb = a + es; | 112 | pa = pb = (char *)a + es; |
127 | 113 | ||
128 | pc = pd = a + (n - 1) * es; | 114 | pc = pd = (char *)a + (n - 1) * es; |
129 | for (;;) { | 115 | for (;;) { |
130 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | 116 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { |
131 | if (r == 0) { | 117 | if (r == 0) { |
@@ -151,14 +137,14 @@ loop: SWAPINIT(a, es); | |||
151 | pc -= es; | 137 | pc -= es; |
152 | } | 138 | } |
153 | if (swap_cnt == 0) { /* Switch to insertion sort */ | 139 | if (swap_cnt == 0) { /* Switch to insertion sort */ |
154 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | 140 | for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) |
155 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | 141 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
156 | pl -= es) | 142 | pl -= es) |
157 | swap(pl, pl - es); | 143 | swap(pl, pl - es); |
158 | return; | 144 | return; |
159 | } | 145 | } |
160 | 146 | ||
161 | pn = a + n * es; | 147 | pn = (char *)a + n * es; |
162 | r = min(pa - (char *)a, pb - pa); | 148 | r = min(pa - (char *)a, pb - pa); |
163 | vecswap(a, pb - r, r); | 149 | vecswap(a, pb - r, r); |
164 | r = min(pd - pc, pn - pd - es); | 150 | r = min(pd - pc, pn - pd - es); |