145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * mergesort() implementation for systems that don't have it. 345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (c) 1992, 1993 545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * The Regents of the University of California. All rights reserved. 645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * This code is derived from software contributed to Berkeley by 845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Peter McIlroy. 945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without 1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions 1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met: 1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright 1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer. 1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright 1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer in the 1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * documentation and/or other materials provided with the distribution. 1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 3. Neither the name of the University nor the names of its contributors 1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * may be used to endorse or promote products derived from this software 2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * without specific prior written permission. 2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUCH DAMAGE. 3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "util.h" 3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#if defined(LIBC_SCCS) && !defined(lint) 3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; 3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /* LIBC_SCCS and not lint */ 3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_MERGESORT 4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm__mergesort 4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef HAVE_MERGESORT 4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Hybrid exponential search/linear search merge sort with hybrid 4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * natural/pairwise first pass. Requires about .3% more comparisons 4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * for random data than LSMS with pairwise first pass alone. 4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * It works for objects as small as two bytes. 5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define NATURAL 5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define THRESHOLD 16 /* Best choice for natural merge cut-off. */ 5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* #define NATURAL to get hybrid natural merge. 5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * (The default is pairwise merging.) 5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <errno.h> 6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <string.h> 6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void setup(unsigned char *, unsigned char *, size_t, size_t, 6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int (*)(const void *, const void *)); 6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void insertionsort(unsigned char *, size_t, size_t, 6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int (*)(const void *, const void *)); 6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define ISIZE sizeof(int) 6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define PSIZE sizeof(unsigned char *) 6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define ICOPY_LIST(src, dst, last) \ 7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do \ 7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ 7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while(src < last) 7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define ICOPY_ELT(src, dst, i) \ 7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do \ 7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ 7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (i -= ISIZE) 7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define CCOPY_LIST(src, dst, last) \ 7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do \ 8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *dst++ = *src++; \ 8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (src < last) 8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define CCOPY_ELT(src, dst, i) \ 8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do \ 8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *dst++ = *src++; \ 8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (i -= 1) 8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Find the next possible pointer head. (Trickery for forcing an array 8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * to do double duty as a linked list when objects do not align with word 9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * boundaries. 9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Assumption: PSIZE is a power of 2. */ 9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define EVAL(p) (unsigned char **) \ 9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ((unsigned char *)0 + \ 9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))) 9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /*HAVE_MERGESORT*/ 9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Arguments are as for qsort. 10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint 10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm__mergesort(void *base, size_t nmemb, size_t size, 10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int (*cmp)(const void *, const void *)) 10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_MERGESORT 10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return mergesort(base, nmemb, size, cmp); 10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else 10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size_t i; 10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int sense; 11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int big, iflag; 11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; 11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned char *list2, *list1, *p2, *p, *last, **p1; 11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ 11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef EINVAL 11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org errno = EINVAL; 11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return (-1); 11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (nmemb == 0) 12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return (0); 12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* 12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * XXX 12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Stupid subtraction for the Cray. 12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org iflag = 0; 12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) 13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org iflag = 1; 13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((list2 = yasm_xmalloc(nmemb * size + PSIZE)) == NULL) 13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return (-1); 13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org list1 = base; 13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org setup(list1, list2, nmemb, size, cmp); 13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org last = list2 + nmemb * size; 13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = 0; 13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org big = 0; 14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (*EVAL(list2) != last) { 14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org l2 = list1; 14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p1 = EVAL(list1); 14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { 14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2); 14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f1 = l2; 14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f2 = l1 = list1 + (p2 - list2); 14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (p2 != last) 14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2); 14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org l2 = list1 + (p2 - list2); 15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (f1 < l1 && f2 < l2) { 15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((*cmp)(f1, f2) <= 0) { 15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org q = f2; 15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = f1, t = l1; 15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org sense = -1; 15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { 15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org q = f1; 15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = f2, t = l2; 15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org sense = 0; 15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!big) { /* here i = 0 */ 16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while ((b += size) < t && cmp(q, b) >sense) 16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (++i == 6) { 16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org big = 1; 16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org goto EXPONENTIAL; 16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { 16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgEXPONENTIAL: for (i = size; ; i <<= 1) 16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((p = (b + i)) >= t) { 16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((p = t - size) > b && 17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org (*cmp)(q, p) <= sense) 17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org t = p; 17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = p; 17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org break; 17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else if ((*cmp)(q, p) <= sense) { 17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org t = p; 17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (i == size) 17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org big = 0; 17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org goto FASTCASE; 18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else 18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = p; 18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org while (t > b+size) { 18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = (((t - b) / size) >> 1) * size; 18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((*cmp)(q, p = b + i) <= sense) 18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org t = p; 18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = p; 18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org goto COPY; 19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgFASTCASE: while (i > size) 19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((*cmp)(q, 19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p = b + (i >>= 1)) <= sense) 19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org t = p; 19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org b = p; 19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgCOPY: b = t; 19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 19845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = size; 19945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (q == f1) { 20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (iflag) { 20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_LIST(f2, tp2, b); 20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_ELT(f1, tp2, i); 20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { 20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_LIST(f2, tp2, b); 20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_ELT(f1, tp2, i); 20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { 20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (iflag) { 20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_LIST(f1, tp2, b); 21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_ELT(f2, tp2, i); 21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { 21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_LIST(f1, tp2, b); 21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_ELT(f2, tp2, i); 21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (f2 < l2) { 21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (iflag) 21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_LIST(f2, tp2, l2); 22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_LIST(f2, tp2, l2); 22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else if (f1 < l1) { 22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (iflag) 22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org ICOPY_LIST(f1, tp2, l1); 22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org CCOPY_LIST(f1, tp2, l1); 22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *p1 = l2; 22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org tp2 = list1; /* swap list1, list2 */ 23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org list1 = list2; 23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org list2 = tp2; 23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org last = list2 + nmemb*size; 23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (base == list2) { 23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org memmove(list2, list1, nmemb*size); 23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org list2 = list1; 23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_xfree(list2); 24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return (0); 24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /*HAVE_MERGESORT*/ 24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 24345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 24445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef HAVE_MERGESORT 24545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 24645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define swap(a, b) { \ 24745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org s = b; \ 24845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = size; \ 24945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do { \ 25045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org tmp = *a; *a++ = *s; *s++ = tmp; \ 25145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } while (--i); \ 25245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org a -= size; \ 25345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 25445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define reverse(bot, top) { \ 25545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org s = top; \ 25645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do { \ 25745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = size; \ 25845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do { \ 25945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org tmp = *bot; *bot++ = *s; *s++ = tmp; \ 26045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } while (--i); \ 26145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org s -= size2; \ 26245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } while(bot < s); \ 26345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 26445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 26545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 26645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of 26745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * increasing order, list2 in a corresponding linked list. Checks for runs 26845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL 26945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * is defined. Otherwise simple pairwise merging is used.) 27045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 27145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid 27245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsetup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, 27345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int (*cmp)(const void *, const void *)) 27445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 27545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size_t i; 27645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned int tmp; 27745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int length, sense; 27845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size_t size2; 27945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned char *f1, *f2, *s, *l2, *last, *p2; 28045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 28145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size2 = size*2; 28245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (n <= 5) { 28345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org insertionsort(list1, n, size, cmp); 28445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *EVAL(list2) = (unsigned char*) list2 + n*size; 28545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return; 28645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 28745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* 28845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Avoid running pointers out of bounds; limit n to evens 28945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * for simplicity. 29045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 29145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org i = 4 + (n & 1); 29245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org insertionsort(list1 + (n - i) * size, i, size, cmp); 29345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org last = list1 + size * (n - i); 29445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *EVAL(list2 + (last - list1)) = list2 + n * size; 29545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 29645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef NATURAL 29745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = list2; 29845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f1 = list1; 29945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org sense = (cmp(f1, f1 + size) > 0); 30045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (; f1 < last; sense = !sense) { 30145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org length = 2; 30245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Find pairs with same sense. */ 30345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (f2 = f1 + size2; f2 < last; f2 += size2) { 30445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((cmp(f2, f2+ size) > 0) != sense) 30545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org break; 30645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org length += 2; 30745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 30845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (length < THRESHOLD) { /* Pairwise merge */ 30945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org do { 31045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2) = f1 + size2 - list1 + list2; 31145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (sense > 0) 31245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org swap (f1, f1 + size); 31345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } while ((f1 += size2) < f2); 31445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } else { /* Natural merge */ 31545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org l2 = f2; 31645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (f2 = f1 + size2; f2 < l2; f2 += size2) { 31745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if ((cmp(f2-size, f2) > 0) != sense) { 31845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2) = f2 - list1 + list2; 31945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (sense > 0) 32045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org reverse(f1, f2-size); 32145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f1 = f2; 32245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 32345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 32445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (sense > 0) 32545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org reverse (f1, f2-size); 32645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f1 = f2; 32745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (f2 < last || cmp(f2 - size, f2) > 0) 32845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2) = f2 - list1 + list2; 32945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 33045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2) = list2 + n*size; 33145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 33245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 33345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else /* pairwise merge only. */ 33445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { 33545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org p2 = *EVAL(p2) = p2 + size2; 33645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (cmp (f1, f1 + size) > 0) 33745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org swap(f1, f1 + size); 33845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 33945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /* NATURAL */ 34045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 34145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 34245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 34345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * This is to avoid out-of-bounds addresses in sorting the 34445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * last 4 elements. 34545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 34645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void 34745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginsertionsort(unsigned char *a, size_t n, size_t size, 34845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org int (*cmp)(const void *, const void *)) 34945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 35045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned char *ai, *s, *t, *u, tmp; 35145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size_t i; 35245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 35345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (ai = a+size; --n >= 1; ai += size) 35445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org for (t = ai; t > a; t -= size) { 35545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org u = t - size; 35645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (cmp(u, t) <= 0) 35745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org break; 35845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org swap(u, t); 35945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 36045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 36145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /*HAVE_MERGESORT*/ 362