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