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