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