1 /* $NetBSD: merge.c,v 1.4 1997/07/13 20:16:47 christos 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.4 1997/07/13 20:16:47 christos 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 <sys/types.h> 63 64 #include <errno.h> 65 #include <stdlib.h> 66 #include <string.h> 67 68 static void setup __P((u_char *, u_char *, size_t, size_t, 69 int (*)(const void *, const void *))); 70 static void insertionsort __P((u_char *, size_t, size_t, 71 int (*)(const void *, const void *))); 72 73 #define ISIZE sizeof(int) 74 #define PSIZE sizeof(u_char *) 75 #define ICOPY_LIST(src, dst, last) \ 76 do \ 77 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ 78 while(src < last) 79 #define ICOPY_ELT(src, dst, i) \ 80 do \ 81 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ 82 while (i -= ISIZE) 83 84 #define CCOPY_LIST(src, dst, last) \ 85 do \ 86 *dst++ = *src++; \ 87 while (src < last) 88 #define CCOPY_ELT(src, dst, i) \ 89 do \ 90 *dst++ = *src++; \ 91 while (i -= 1) 92 93 /* 94 * Find the next possible pointer head. (Trickery for forcing an array 95 * to do double duty as a linked list when objects do not align with word 96 * boundaries. 97 */ 98 /* Assumption: PSIZE is a power of 2. */ 99 #define EVAL(p) (u_char **) \ 100 ((u_char *)0 + \ 101 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) 102 103 /* 104 * Arguments are as for qsort. 105 */ 106 int 107 mergesort(base, nmemb, size, cmp) 108 void *base; 109 size_t nmemb; 110 register size_t size; 111 int (*cmp) __P((const void *, const void *)); 112 { 113 register int i, sense; 114 int big, iflag; 115 register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; 116 u_char *list2, *list1, *p2, *p, *last, **p1; 117 118 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ 119 errno = EINVAL; 120 return (-1); 121 } 122 123 /* 124 * XXX 125 * Stupid subtraction for the Cray. 126 */ 127 iflag = 0; 128 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 129 iflag = 1; 130 131 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) 132 return (-1); 133 134 list1 = base; 135 setup(list1, list2, nmemb, size, cmp); 136 last = list2 + nmemb * size; 137 i = big = 0; 138 while (*EVAL(list2) != last) { 139 l2 = list1; 140 p1 = EVAL(list1); 141 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { 142 p2 = *EVAL(p2); 143 f1 = l2; 144 f2 = l1 = list1 + (p2 - list2); 145 if (p2 != last) 146 p2 = *EVAL(p2); 147 l2 = list1 + (p2 - list2); 148 while (f1 < l1 && f2 < l2) { 149 if ((*cmp)(f1, f2) <= 0) { 150 q = f2; 151 b = f1, t = l1; 152 sense = -1; 153 } else { 154 q = f1; 155 b = f2, t = l2; 156 sense = 0; 157 } 158 if (!big) { /* here i = 0 */ 159 #if 0 160 LINEAR: 161 #endif 162 while ((b += size) < t && cmp(q, b) >sense) 163 if (++i == 6) { 164 big = 1; 165 goto EXPONENTIAL; 166 } 167 } else { 168 EXPONENTIAL: for (i = size; ; i <<= 1) 169 if ((p = (b + i)) >= t) { 170 if ((p = t - size) > b && 171 (*cmp)(q, p) <= sense) 172 t = p; 173 else 174 b = p; 175 break; 176 } else if ((*cmp)(q, p) <= sense) { 177 t = p; 178 if (i == size) 179 big = 0; 180 goto FASTCASE; 181 } else 182 b = p; 183 #if 0 184 SLOWCASE: 185 #endif 186 while (t > b+size) { 187 i = (((t - b) / size) >> 1) * size; 188 if ((*cmp)(q, p = b + i) <= sense) 189 t = p; 190 else 191 b = p; 192 } 193 goto COPY; 194 FASTCASE: while (i > size) 195 if ((*cmp)(q, 196 p = b + (i >>= 1)) <= sense) 197 t = p; 198 else 199 b = p; 200 COPY: b = t; 201 } 202 i = size; 203 if (q == f1) { 204 if (iflag) { 205 ICOPY_LIST(f2, tp2, b); 206 ICOPY_ELT(f1, tp2, i); 207 } else { 208 CCOPY_LIST(f2, tp2, b); 209 CCOPY_ELT(f1, tp2, i); 210 } 211 } else { 212 if (iflag) { 213 ICOPY_LIST(f1, tp2, b); 214 ICOPY_ELT(f2, tp2, i); 215 } else { 216 CCOPY_LIST(f1, tp2, b); 217 CCOPY_ELT(f2, tp2, i); 218 } 219 } 220 } 221 if (f2 < l2) { 222 if (iflag) 223 ICOPY_LIST(f2, tp2, l2); 224 else 225 CCOPY_LIST(f2, tp2, l2); 226 } else if (f1 < l1) { 227 if (iflag) 228 ICOPY_LIST(f1, tp2, l1); 229 else 230 CCOPY_LIST(f1, tp2, l1); 231 } 232 *p1 = l2; 233 } 234 tp2 = list1; /* swap list1, list2 */ 235 list1 = list2; 236 list2 = tp2; 237 last = list2 + nmemb*size; 238 } 239 if (base == list2) { 240 memmove(list2, list1, nmemb*size); 241 list2 = list1; 242 } 243 free(list2); 244 return (0); 245 } 246 247 #define swap(a, b) { \ 248 s = b; \ 249 i = size; \ 250 do { \ 251 tmp = *a; *a++ = *s; *s++ = tmp; \ 252 } while (--i); \ 253 a -= size; \ 254 } 255 #define reverse(bot, top) { \ 256 s = top; \ 257 do { \ 258 i = size; \ 259 do { \ 260 tmp = *bot; *bot++ = *s; *s++ = tmp; \ 261 } while (--i); \ 262 s -= size2; \ 263 } while(bot < s); \ 264 } 265 266 /* 267 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of 268 * increasing order, list2 in a corresponding linked list. Checks for runs 269 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL 270 * is defined. Otherwise simple pairwise merging is used.) 271 */ 272 void 273 setup(list1, list2, n, size, cmp) 274 size_t n, size; 275 int (*cmp) __P((const void *, const void *)); 276 u_char *list1, *list2; 277 { 278 int i, length, size2, tmp, sense; 279 u_char *f1, *f2, *s, *l2, *last, *p2; 280 281 size2 = size*2; 282 if (n <= 5) { 283 insertionsort(list1, n, size, cmp); 284 *EVAL(list2) = (u_char*) list2 + n*size; 285 return; 286 } 287 /* 288 * Avoid running pointers out of bounds; limit n to evens 289 * for simplicity. 290 */ 291 i = 4 + (n & 1); 292 insertionsort(list1 + (n - i) * size, i, size, cmp); 293 last = list1 + size * (n - i); 294 *EVAL(list2 + (last - list1)) = list2 + n * size; 295 296 #ifdef NATURAL 297 p2 = list2; 298 f1 = list1; 299 sense = (cmp(f1, f1 + size) > 0); 300 for (; f1 < last; sense = !sense) { 301 length = 2; 302 /* Find pairs with same sense. */ 303 for (f2 = f1 + size2; f2 < last; f2 += size2) { 304 if ((cmp(f2, f2+ size) > 0) != sense) 305 break; 306 length += 2; 307 } 308 if (length < THRESHOLD) { /* Pairwise merge */ 309 do { 310 p2 = *EVAL(p2) = f1 + size2 - list1 + list2; 311 if (sense > 0) 312 swap (f1, f1 + size); 313 } while ((f1 += size2) < f2); 314 } else { /* Natural merge */ 315 l2 = f2; 316 for (f2 = f1 + size2; f2 < l2; f2 += size2) { 317 if ((cmp(f2-size, f2) > 0) != sense) { 318 p2 = *EVAL(p2) = f2 - list1 + list2; 319 if (sense > 0) 320 reverse(f1, f2-size); 321 f1 = f2; 322 } 323 } 324 if (sense > 0) 325 reverse (f1, f2-size); 326 f1 = f2; 327 if (f2 < last || cmp(f2 - size, f2) > 0) 328 p2 = *EVAL(p2) = f2 - list1 + list2; 329 else 330 p2 = *EVAL(p2) = list2 + n*size; 331 } 332 } 333 #else /* pairwise merge only. */ 334 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { 335 p2 = *EVAL(p2) = p2 + size2; 336 if (cmp (f1, f1 + size) > 0) 337 swap(f1, f1 + size); 338 } 339 #endif /* NATURAL */ 340 } 341 342 /* 343 * This is to avoid out-of-bounds addresses in sorting the 344 * last 4 elements. 345 */ 346 static void 347 insertionsort(a, n, size, cmp) 348 u_char *a; 349 size_t n, size; 350 int (*cmp) __P((const void *, const void *)); 351 { 352 u_char *ai, *s, *t, *u, tmp; 353 int i; 354 355 for (ai = a+size; --n >= 1; ai += size) 356 for (t = ai; t > a; t -= size) { 357 u = t - size; 358 if (cmp(u, t) <= 0) 359 break; 360 swap(u, t); 361 } 362 } 363