Line data Source code
1 : /*-
2 : * Copyright (c) 1992, 1993
3 : * The Regents of the University of California. All rights reserved.
4 : *
5 : * This code is derived from software contributed to Berkeley by
6 : * Peter McIlroy.
7 : *
8 : * Redistribution and use in source and binary forms, with or without
9 : * modification, are permitted provided that the following conditions
10 : * are met:
11 : * 1. Redistributions of source code must retain the above copyright
12 : * notice, this list of conditions and the following disclaimer.
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : * 3. Neither the name of the University nor the names of its contributors
17 : * may be used to endorse or promote products derived from this software
18 : * without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 : * SUCH DAMAGE.
31 : */
32 :
33 : #include <config.h>
34 : #include "roken.h"
35 :
36 : /*
37 : * Hybrid exponential search/linear search merge sort with hybrid
38 : * natural/pairwise first pass. Requires about .3% more comparisons
39 : * for random data than LSMS with pairwise first pass alone.
40 : * It works for objects as small as two bytes.
41 : */
42 :
43 : #define NATURAL
44 : #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
45 :
46 : /* #define NATURAL to get hybrid natural merge.
47 : * (The default is pairwise merging.)
48 : */
49 :
50 : #include <sys/types.h>
51 :
52 : #include <errno.h>
53 : #include <stdlib.h>
54 : #include <string.h>
55 :
56 : typedef int (*cmp_t)(const void *, const void *, void *);
57 :
58 : static void setup(u_char *, u_char *, size_t, size_t, cmp_t, void *);
59 : static void insertionsort(u_char *, size_t, size_t, cmp_t, void *);
60 :
61 : #define ISIZE sizeof(int)
62 : #define PSIZE sizeof(u_char *)
63 : #define ICOPY_LIST(src, dst, last) \
64 : do \
65 : *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
66 : while(src < last)
67 : #define ICOPY_ELT(src, dst, i) \
68 : do \
69 : *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
70 : while (i -= ISIZE)
71 :
72 : #define CCOPY_LIST(src, dst, last) \
73 : do \
74 : *dst++ = *src++; \
75 : while (src < last)
76 : #define CCOPY_ELT(src, dst, i) \
77 : do \
78 : *dst++ = *src++; \
79 : while (i -= 1)
80 :
81 : /*
82 : * Find the next possible pointer head. (Trickery for forcing an array
83 : * to do double duty as a linked list when objects do not align with word
84 : * boundaries.
85 : */
86 : /* Assumption: PSIZE is a power of 2. */
87 : #define EVAL(p) (u_char **) \
88 : ((((uintptr_t)p + PSIZE - 1) & ~(PSIZE - 1)))
89 :
90 : /*
91 : * Arguments are as for qsort_r, except thunk is moved to the last
92 : * parameter for glibc compatibility. See:
93 : * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=214248
94 : */
95 : int
96 209 : mergesort_r(void *base, size_t nmemb, size_t size, cmp_t cmp, void *thunk)
97 : {
98 11 : size_t i;
99 11 : int sense;
100 11 : int big, iflag;
101 11 : u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
102 11 : u_char *list2, *list1, *p2, *p, *last, **p1;
103 209 : u_char *freeme = NULL;
104 :
105 209 : if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
106 0 : errno = EINVAL;
107 0 : return (-1);
108 : }
109 :
110 209 : if (nmemb == 0)
111 0 : return (0);
112 :
113 : /*
114 : * XXX
115 : * Stupid subtraction for the Cray.
116 : */
117 209 : iflag = 0;
118 209 : if (!(size % ISIZE) && !(((uintptr_t)base) % ISIZE))
119 209 : iflag = 1;
120 :
121 209 : if ((list2 = freeme = malloc(nmemb * size + PSIZE)) == NULL)
122 0 : return (-1);
123 :
124 209 : list1 = base;
125 209 : setup(list1, list2, nmemb, size, cmp, thunk);
126 209 : last = list2 + nmemb * size;
127 209 : i = big = 0;
128 247 : while (*EVAL(list2) != last) {
129 38 : l2 = list1;
130 38 : p1 = EVAL(list1);
131 95 : for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
132 57 : p2 = *EVAL(p2);
133 57 : f1 = l2;
134 57 : f2 = l1 = list1 + (p2 - list2);
135 57 : if (p2 != last)
136 38 : p2 = *EVAL(p2);
137 57 : l2 = list1 + (p2 - list2);
138 133 : while (f1 < l1 && f2 < l2) {
139 76 : if (cmp(f1, f2, thunk) <= 0) {
140 54 : q = f2;
141 54 : b = f1, t = l1;
142 54 : sense = -1;
143 : } else {
144 19 : q = f1;
145 19 : b = f2, t = l2;
146 19 : sense = 0;
147 : }
148 76 : if (!big) { /* here i = 0 */
149 114 : while ((b += size) < t && cmp(q, b, thunk) >sense)
150 38 : if (++i == 6) {
151 0 : big = 1;
152 0 : goto EXPONENTIAL;
153 : }
154 : } else {
155 0 : EXPONENTIAL: for (i = size; ; i <<= 1)
156 0 : if ((p = (b + i)) >= t) {
157 0 : if ((p = t - size) > b &&
158 0 : cmp(q, p, thunk) <= sense)
159 0 : t = p;
160 : else
161 0 : b = p;
162 0 : break;
163 0 : } else if (cmp(q, p, thunk) <= sense) {
164 0 : t = p;
165 0 : if (i == size)
166 0 : big = 0;
167 0 : goto FASTCASE;
168 : } else
169 0 : b = p;
170 0 : while (t > b+size) {
171 0 : i = (((t - b) / size) >> 1) * size;
172 0 : if (cmp(q, p = b + i, thunk) <= sense)
173 0 : t = p;
174 : else
175 0 : b = p;
176 : }
177 0 : goto COPY;
178 0 : FASTCASE: while (i > size)
179 0 : if (cmp(q,
180 0 : p = b + (i >>= 1), thunk) <= sense)
181 0 : t = p;
182 : else
183 0 : b = p;
184 0 : COPY: b = t;
185 : }
186 76 : i = size;
187 76 : if (q == f1) {
188 19 : if (iflag) {
189 38 : ICOPY_LIST(f2, tp2, b);
190 38 : ICOPY_ELT(f1, tp2, i);
191 : } else {
192 0 : CCOPY_LIST(f2, tp2, b);
193 0 : CCOPY_ELT(f1, tp2, i);
194 : }
195 : } else {
196 57 : if (iflag) {
197 190 : ICOPY_LIST(f1, tp2, b);
198 114 : ICOPY_ELT(f2, tp2, i);
199 : } else {
200 0 : CCOPY_LIST(f1, tp2, b);
201 0 : CCOPY_ELT(f2, tp2, i);
202 : }
203 : }
204 : }
205 57 : if (f2 < l2) {
206 38 : if (iflag)
207 114 : ICOPY_LIST(f2, tp2, l2);
208 : else
209 0 : CCOPY_LIST(f2, tp2, l2);
210 19 : } else if (f1 < l1) {
211 19 : if (iflag)
212 190 : ICOPY_LIST(f1, tp2, l1);
213 : else
214 0 : CCOPY_LIST(f1, tp2, l1);
215 : }
216 57 : *p1 = l2;
217 : }
218 38 : tp2 = list1; /* swap list1, list2 */
219 38 : list1 = list2;
220 38 : list2 = tp2;
221 38 : last = list2 + nmemb*size;
222 : }
223 209 : if (base == list2)
224 0 : memmove(list2, list1, nmemb*size);
225 209 : free(freeme);
226 209 : return (0);
227 : }
228 :
229 : #define swap(a, b) { \
230 : s = b; \
231 : i = size; \
232 : do { \
233 : tmp = *a; *a++ = *s; *s++ = tmp; \
234 : } while (--i); \
235 : a -= size; \
236 : }
237 : #define reverse(bot, top) { \
238 : s = top; \
239 : do { \
240 : i = size; \
241 : do { \
242 : tmp = *bot; *bot++ = *s; *s++ = tmp; \
243 : } while (--i); \
244 : s -= size2; \
245 : } while(bot < s); \
246 : }
247 :
248 : /*
249 : * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
250 : * increasing order, list2 in a corresponding linked list. Checks for runs
251 : * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
252 : * is defined. Otherwise simple pairwise merging is used.)
253 : */
254 : void
255 209 : setup(u_char *list1, u_char *list2, size_t n, size_t size, cmp_t cmp, void *thunk)
256 : {
257 11 : int i, length, size2, tmp, sense;
258 11 : u_char *f1, *f2, *s, *l2, *last, *p2;
259 :
260 209 : size2 = size*2;
261 209 : if (n <= 5) {
262 190 : insertionsort(list1, n, size, cmp, thunk);
263 190 : *EVAL(list2) = (u_char*) list2 + n*size;
264 190 : return;
265 : }
266 : /*
267 : * Avoid running pointers out of bounds; limit n to evens
268 : * for simplicity.
269 : */
270 19 : i = 4 + (n & 1);
271 19 : insertionsort(list1 + (n - i) * size, i, size, cmp, thunk);
272 19 : last = list1 + size * (n - i);
273 19 : *EVAL(list2 + (last - list1)) = list2 + n * size;
274 :
275 : #ifdef NATURAL
276 19 : p2 = list2;
277 19 : f1 = list1;
278 19 : sense = (cmp(f1, f1 + size, thunk) > 0);
279 38 : for (; f1 < last; sense = !sense) {
280 19 : length = 2;
281 : /* Find pairs with same sense. */
282 38 : for (f2 = f1 + size2; f2 < last; f2 += size2) {
283 19 : if ((cmp(f2, f2+ size, thunk) > 0) != sense)
284 0 : break;
285 19 : length += 2;
286 : }
287 19 : if (length < THRESHOLD) { /* Pairwise merge */
288 2 : do {
289 38 : p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
290 38 : if (sense > 0)
291 0 : swap (f1, f1 + size);
292 38 : } while ((f1 += size2) < f2);
293 : } else { /* Natural merge */
294 0 : l2 = f2;
295 0 : for (f2 = f1 + size2; f2 < l2; f2 += size2) {
296 0 : if ((cmp(f2-size, f2, thunk) > 0) != sense) {
297 0 : p2 = *EVAL(p2) = f2 - list1 + list2;
298 0 : if (sense > 0)
299 0 : reverse(f1, f2-size);
300 0 : f1 = f2;
301 : }
302 : }
303 0 : if (sense > 0)
304 0 : reverse (f1, f2-size);
305 0 : f1 = f2;
306 0 : if (f2 < last || cmp(f2 - size, f2, thunk) > 0)
307 0 : p2 = *EVAL(p2) = f2 - list1 + list2;
308 : else
309 0 : p2 = *EVAL(p2) = list2 + n*size;
310 : }
311 : }
312 : #else /* pairwise merge only. */
313 : for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
314 : p2 = *EVAL(p2) = p2 + size2;
315 : if (cmp (f1, f1 + size) > 0)
316 : swap(f1, f1 + size);
317 : }
318 : #endif /* NATURAL */
319 : }
320 :
321 : /*
322 : * This is to avoid out-of-bounds addresses in sorting the
323 : * last 4 elements.
324 : */
325 : static void
326 209 : insertionsort(u_char *a, size_t n, size_t size, cmp_t cmp, void *thunk)
327 : {
328 11 : u_char *ai, *s, *t, *u, tmp;
329 11 : int i;
330 :
331 513 : for (ai = a+size; --n >= 1; ai += size)
332 627 : for (t = ai; t > a; t -= size) {
333 513 : u = t - size;
334 513 : if (cmp(u, t, thunk) <= 0)
335 180 : break;
336 2617 : swap(u, t);
337 : }
338 209 : }
|