2 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 1992, 1993
3 dd038bc6 2021-09-21 thomas.ad * The Regents of the University of California. All rights reserved.
5 dd038bc6 2021-09-21 thomas.ad * This code is derived from software contributed to Berkeley by
6 dd038bc6 2021-09-21 thomas.ad * Peter McIlroy.
8 dd038bc6 2021-09-21 thomas.ad * Redistribution and use in source and binary forms, with or without
9 dd038bc6 2021-09-21 thomas.ad * modification, are permitted provided that the following conditions
10 dd038bc6 2021-09-21 thomas.ad * are met:
11 dd038bc6 2021-09-21 thomas.ad * 1. Redistributions of source code must retain the above copyright
12 dd038bc6 2021-09-21 thomas.ad * notice, this list of conditions and the following disclaimer.
13 dd038bc6 2021-09-21 thomas.ad * 2. Redistributions in binary form must reproduce the above copyright
14 dd038bc6 2021-09-21 thomas.ad * notice, this list of conditions and the following disclaimer in the
15 dd038bc6 2021-09-21 thomas.ad * documentation and/or other materials provided with the distribution.
16 dd038bc6 2021-09-21 thomas.ad * 4. Neither the name of the University nor the names of its contributors
17 dd038bc6 2021-09-21 thomas.ad * may be used to endorse or promote products derived from this software
18 dd038bc6 2021-09-21 thomas.ad * without specific prior written permission.
20 dd038bc6 2021-09-21 thomas.ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 dd038bc6 2021-09-21 thomas.ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 dd038bc6 2021-09-21 thomas.ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 dd038bc6 2021-09-21 thomas.ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 dd038bc6 2021-09-21 thomas.ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 dd038bc6 2021-09-21 thomas.ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 dd038bc6 2021-09-21 thomas.ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 dd038bc6 2021-09-21 thomas.ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 dd038bc6 2021-09-21 thomas.ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 dd038bc6 2021-09-21 thomas.ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 dd038bc6 2021-09-21 thomas.ad * SUCH DAMAGE.
33 dd038bc6 2021-09-21 thomas.ad #include <sys/cdefs.h>
36 dd038bc6 2021-09-21 thomas.ad * Hybrid exponential search/linear search merge sort with hybrid
37 dd038bc6 2021-09-21 thomas.ad * natural/pairwise first pass. Requires about .3% more comparisons
38 dd038bc6 2021-09-21 thomas.ad * for random data than LSMS with pairwise first pass alone.
39 dd038bc6 2021-09-21 thomas.ad * It works for objects as small as two bytes.
42 dd038bc6 2021-09-21 thomas.ad #define NATURAL
43 dd038bc6 2021-09-21 thomas.ad #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
45 dd038bc6 2021-09-21 thomas.ad /* #define NATURAL to get hybrid natural merge.
46 dd038bc6 2021-09-21 thomas.ad * (The default is pairwise merging.)
49 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
51 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
52 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
53 dd038bc6 2021-09-21 thomas.ad #include <string.h>
55 c4e2e0d0 2022-07-16 thomas #include "got_compat.h"
57 dd038bc6 2021-09-21 thomas.ad static void setup(unsigned char *, unsigned char *, size_t, size_t,
58 dd038bc6 2021-09-21 thomas.ad int (*)(const void *, const void *));
59 dd038bc6 2021-09-21 thomas.ad static void insertionsort(unsigned char *, size_t, size_t,
60 dd038bc6 2021-09-21 thomas.ad int (*)(const void *, const void *));
62 dd038bc6 2021-09-21 thomas.ad #define ISIZE sizeof(int)
63 dd038bc6 2021-09-21 thomas.ad #define PSIZE sizeof(unsigned char *)
64 dd038bc6 2021-09-21 thomas.ad #define ICOPY_LIST(src, dst, last) \
66 dd038bc6 2021-09-21 thomas.ad *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
67 dd038bc6 2021-09-21 thomas.ad while(src < last)
68 dd038bc6 2021-09-21 thomas.ad #define ICOPY_ELT(src, dst, i) \
70 dd038bc6 2021-09-21 thomas.ad *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
71 dd038bc6 2021-09-21 thomas.ad while (i -= ISIZE)
73 dd038bc6 2021-09-21 thomas.ad #define CCOPY_LIST(src, dst, last) \
75 dd038bc6 2021-09-21 thomas.ad *dst++ = *src++; \
76 dd038bc6 2021-09-21 thomas.ad while (src < last)
77 dd038bc6 2021-09-21 thomas.ad #define CCOPY_ELT(src, dst, i) \
79 dd038bc6 2021-09-21 thomas.ad *dst++ = *src++; \
80 dd038bc6 2021-09-21 thomas.ad while (i -= 1)
83 dd038bc6 2021-09-21 thomas.ad * Find the next possible pointer head. (Trickery for forcing an array
84 dd038bc6 2021-09-21 thomas.ad * to do double duty as a linked list when objects do not align with word
85 dd038bc6 2021-09-21 thomas.ad * boundaries.
87 dd038bc6 2021-09-21 thomas.ad /* Assumption: PSIZE is a power of 2. */
88 dd038bc6 2021-09-21 thomas.ad #define EVAL(p) (unsigned char **) \
89 dd038bc6 2021-09-21 thomas.ad ((unsigned char *)0 + \
90 dd038bc6 2021-09-21 thomas.ad (((unsigned char *)p + PSIZE - 1 - \
91 dd038bc6 2021-09-21 thomas.ad (unsigned char *)0) & ~(PSIZE - 1)))
94 dd038bc6 2021-09-21 thomas.ad * Arguments are as for qsort.
97 dd038bc6 2021-09-21 thomas.ad mergesort(void *base, size_t nmemb, size_t size,
98 dd038bc6 2021-09-21 thomas.ad int (*cmp)(const void *, const void *))
100 dd038bc6 2021-09-21 thomas.ad size_t i;
101 dd038bc6 2021-09-21 thomas.ad int sense;
102 dd038bc6 2021-09-21 thomas.ad int big, iflag;
103 dd038bc6 2021-09-21 thomas.ad unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
104 dd038bc6 2021-09-21 thomas.ad unsigned char *list2, *list1, *p2, *p, *last, **p1;
106 dd038bc6 2021-09-21 thomas.ad if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
107 dd038bc6 2021-09-21 thomas.ad errno = EINVAL;
108 dd038bc6 2021-09-21 thomas.ad return (-1);
111 dd038bc6 2021-09-21 thomas.ad if (nmemb == 0)
112 dd038bc6 2021-09-21 thomas.ad return (0);
116 dd038bc6 2021-09-21 thomas.ad * Stupid subtraction for the Cray.
118 dd038bc6 2021-09-21 thomas.ad iflag = 0;
119 dd038bc6 2021-09-21 thomas.ad if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
120 dd038bc6 2021-09-21 thomas.ad iflag = 1;
122 dd038bc6 2021-09-21 thomas.ad if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
123 dd038bc6 2021-09-21 thomas.ad return (-1);
125 dd038bc6 2021-09-21 thomas.ad list1 = base;
126 dd038bc6 2021-09-21 thomas.ad setup(list1, list2, nmemb, size, cmp);
127 dd038bc6 2021-09-21 thomas.ad last = list2 + nmemb * size;
128 dd038bc6 2021-09-21 thomas.ad i = big = 0;
129 dd038bc6 2021-09-21 thomas.ad while (*EVAL(list2) != last) {
130 dd038bc6 2021-09-21 thomas.ad l2 = list1;
131 dd038bc6 2021-09-21 thomas.ad p1 = EVAL(list1);
132 dd038bc6 2021-09-21 thomas.ad for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
133 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2);
134 dd038bc6 2021-09-21 thomas.ad f1 = l2;
135 dd038bc6 2021-09-21 thomas.ad f2 = l1 = list1 + (p2 - list2);
136 dd038bc6 2021-09-21 thomas.ad if (p2 != last)
137 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2);
138 dd038bc6 2021-09-21 thomas.ad l2 = list1 + (p2 - list2);
139 dd038bc6 2021-09-21 thomas.ad while (f1 < l1 && f2 < l2) {
140 dd038bc6 2021-09-21 thomas.ad if ((*cmp)(f1, f2) <= 0) {
141 dd038bc6 2021-09-21 thomas.ad q = f2;
142 dd038bc6 2021-09-21 thomas.ad b = f1, t = l1;
143 dd038bc6 2021-09-21 thomas.ad sense = -1;
144 dd038bc6 2021-09-21 thomas.ad } else {
145 dd038bc6 2021-09-21 thomas.ad q = f1;
146 dd038bc6 2021-09-21 thomas.ad b = f2, t = l2;
147 dd038bc6 2021-09-21 thomas.ad sense = 0;
149 dd038bc6 2021-09-21 thomas.ad if (!big) { /* here i = 0 */
150 dd038bc6 2021-09-21 thomas.ad while ((b += size) < t && cmp(q, b) >sense)
151 dd038bc6 2021-09-21 thomas.ad if (++i == 6) {
152 dd038bc6 2021-09-21 thomas.ad big = 1;
153 dd038bc6 2021-09-21 thomas.ad goto EXPONENTIAL;
155 dd038bc6 2021-09-21 thomas.ad } else {
156 dd038bc6 2021-09-21 thomas.ad EXPONENTIAL: for (i = size; ; i <<= 1)
157 dd038bc6 2021-09-21 thomas.ad if ((p = (b + i)) >= t) {
158 dd038bc6 2021-09-21 thomas.ad if ((p = t - size) > b &&
159 dd038bc6 2021-09-21 thomas.ad (*cmp)(q, p) <= sense)
160 dd038bc6 2021-09-21 thomas.ad t = p;
162 dd038bc6 2021-09-21 thomas.ad b = p;
163 dd038bc6 2021-09-21 thomas.ad break;
164 dd038bc6 2021-09-21 thomas.ad } else if ((*cmp)(q, p) <= sense) {
165 dd038bc6 2021-09-21 thomas.ad t = p;
166 dd038bc6 2021-09-21 thomas.ad if (i == size)
167 dd038bc6 2021-09-21 thomas.ad big = 0;
168 dd038bc6 2021-09-21 thomas.ad goto FASTCASE;
169 dd038bc6 2021-09-21 thomas.ad } else
170 dd038bc6 2021-09-21 thomas.ad b = p;
171 dd038bc6 2021-09-21 thomas.ad while (t > b+size) {
172 dd038bc6 2021-09-21 thomas.ad i = (((t - b) / size) >> 1) * size;
173 dd038bc6 2021-09-21 thomas.ad if ((*cmp)(q, p = b + i) <= sense)
174 dd038bc6 2021-09-21 thomas.ad t = p;
176 dd038bc6 2021-09-21 thomas.ad b = p;
178 dd038bc6 2021-09-21 thomas.ad goto COPY;
179 dd038bc6 2021-09-21 thomas.ad FASTCASE: while (i > size)
180 dd038bc6 2021-09-21 thomas.ad if ((*cmp)(q,
181 dd038bc6 2021-09-21 thomas.ad p = b + (i >>= 1)) <= sense)
182 dd038bc6 2021-09-21 thomas.ad t = p;
184 dd038bc6 2021-09-21 thomas.ad b = p;
185 dd038bc6 2021-09-21 thomas.ad COPY: b = t;
187 dd038bc6 2021-09-21 thomas.ad i = size;
188 dd038bc6 2021-09-21 thomas.ad if (q == f1) {
189 dd038bc6 2021-09-21 thomas.ad if (iflag) {
190 dd038bc6 2021-09-21 thomas.ad ICOPY_LIST(f2, tp2, b);
191 dd038bc6 2021-09-21 thomas.ad ICOPY_ELT(f1, tp2, i);
192 dd038bc6 2021-09-21 thomas.ad } else {
193 dd038bc6 2021-09-21 thomas.ad CCOPY_LIST(f2, tp2, b);
194 dd038bc6 2021-09-21 thomas.ad CCOPY_ELT(f1, tp2, i);
196 dd038bc6 2021-09-21 thomas.ad } else {
197 dd038bc6 2021-09-21 thomas.ad if (iflag) {
198 dd038bc6 2021-09-21 thomas.ad ICOPY_LIST(f1, tp2, b);
199 dd038bc6 2021-09-21 thomas.ad ICOPY_ELT(f2, tp2, i);
200 dd038bc6 2021-09-21 thomas.ad } else {
201 dd038bc6 2021-09-21 thomas.ad CCOPY_LIST(f1, tp2, b);
202 dd038bc6 2021-09-21 thomas.ad CCOPY_ELT(f2, tp2, i);
206 dd038bc6 2021-09-21 thomas.ad if (f2 < l2) {
207 dd038bc6 2021-09-21 thomas.ad if (iflag)
208 dd038bc6 2021-09-21 thomas.ad ICOPY_LIST(f2, tp2, l2);
210 dd038bc6 2021-09-21 thomas.ad CCOPY_LIST(f2, tp2, l2);
211 dd038bc6 2021-09-21 thomas.ad } else if (f1 < l1) {
212 dd038bc6 2021-09-21 thomas.ad if (iflag)
213 dd038bc6 2021-09-21 thomas.ad ICOPY_LIST(f1, tp2, l1);
215 dd038bc6 2021-09-21 thomas.ad CCOPY_LIST(f1, tp2, l1);
217 dd038bc6 2021-09-21 thomas.ad *p1 = l2;
219 dd038bc6 2021-09-21 thomas.ad tp2 = list1; /* swap list1, list2 */
220 dd038bc6 2021-09-21 thomas.ad list1 = list2;
221 dd038bc6 2021-09-21 thomas.ad list2 = tp2;
222 dd038bc6 2021-09-21 thomas.ad last = list2 + nmemb*size;
224 dd038bc6 2021-09-21 thomas.ad if (base == list2) {
225 dd038bc6 2021-09-21 thomas.ad memmove(list2, list1, nmemb*size);
226 dd038bc6 2021-09-21 thomas.ad list2 = list1;
228 dd038bc6 2021-09-21 thomas.ad free(list2);
229 dd038bc6 2021-09-21 thomas.ad return (0);
232 dd038bc6 2021-09-21 thomas.ad #define swap(a, b) { \
233 dd038bc6 2021-09-21 thomas.ad s = b; \
234 dd038bc6 2021-09-21 thomas.ad i = size; \
235 dd038bc6 2021-09-21 thomas.ad do { \
236 dd038bc6 2021-09-21 thomas.ad tmp = *a; *a++ = *s; *s++ = tmp; \
237 dd038bc6 2021-09-21 thomas.ad } while (--i); \
238 dd038bc6 2021-09-21 thomas.ad a -= size; \
240 dd038bc6 2021-09-21 thomas.ad #define reverse(bot, top) { \
241 dd038bc6 2021-09-21 thomas.ad s = top; \
242 dd038bc6 2021-09-21 thomas.ad do { \
243 dd038bc6 2021-09-21 thomas.ad i = size; \
244 dd038bc6 2021-09-21 thomas.ad do { \
245 dd038bc6 2021-09-21 thomas.ad tmp = *bot; *bot++ = *s; *s++ = tmp; \
246 dd038bc6 2021-09-21 thomas.ad } while (--i); \
247 dd038bc6 2021-09-21 thomas.ad s -= size2; \
248 dd038bc6 2021-09-21 thomas.ad } while(bot < s); \
252 dd038bc6 2021-09-21 thomas.ad * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
253 dd038bc6 2021-09-21 thomas.ad * increasing order, list2 in a corresponding linked list. Checks for runs
254 dd038bc6 2021-09-21 thomas.ad * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
255 dd038bc6 2021-09-21 thomas.ad * is defined. Otherwise simple pairwise merging is used.)
257 dd038bc6 2021-09-21 thomas.ad static void
258 dd038bc6 2021-09-21 thomas.ad setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size,
259 dd038bc6 2021-09-21 thomas.ad int (*cmp)(const void *, const void *))
261 dd038bc6 2021-09-21 thomas.ad int i, length, size2, tmp, sense;
262 dd038bc6 2021-09-21 thomas.ad unsigned char *f1, *f2, *s, *l2, *last, *p2;
264 dd038bc6 2021-09-21 thomas.ad size2 = size*2;
265 dd038bc6 2021-09-21 thomas.ad if (n <= 5) {
266 dd038bc6 2021-09-21 thomas.ad insertionsort(list1, n, size, cmp);
267 dd038bc6 2021-09-21 thomas.ad *EVAL(list2) = (unsigned char*) list2 + n*size;
268 dd038bc6 2021-09-21 thomas.ad return;
271 dd038bc6 2021-09-21 thomas.ad * Avoid running pointers out of bounds; limit n to evens
272 dd038bc6 2021-09-21 thomas.ad * for simplicity.
274 dd038bc6 2021-09-21 thomas.ad i = 4 + (n & 1);
275 dd038bc6 2021-09-21 thomas.ad insertionsort(list1 + (n - i) * size, i, size, cmp);
276 dd038bc6 2021-09-21 thomas.ad last = list1 + size * (n - i);
277 dd038bc6 2021-09-21 thomas.ad *EVAL(list2 + (last - list1)) = list2 + n * size;
279 dd038bc6 2021-09-21 thomas.ad #ifdef NATURAL
280 dd038bc6 2021-09-21 thomas.ad p2 = list2;
281 dd038bc6 2021-09-21 thomas.ad f1 = list1;
282 dd038bc6 2021-09-21 thomas.ad sense = (cmp(f1, f1 + size) > 0);
283 dd038bc6 2021-09-21 thomas.ad for (; f1 < last; sense = !sense) {
284 dd038bc6 2021-09-21 thomas.ad length = 2;
285 dd038bc6 2021-09-21 thomas.ad /* Find pairs with same sense. */
286 dd038bc6 2021-09-21 thomas.ad for (f2 = f1 + size2; f2 < last; f2 += size2) {
287 dd038bc6 2021-09-21 thomas.ad if ((cmp(f2, f2+ size) > 0) != sense)
288 dd038bc6 2021-09-21 thomas.ad break;
289 dd038bc6 2021-09-21 thomas.ad length += 2;
291 dd038bc6 2021-09-21 thomas.ad if (length < THRESHOLD) { /* Pairwise merge */
293 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
294 dd038bc6 2021-09-21 thomas.ad if (sense > 0)
295 dd038bc6 2021-09-21 thomas.ad swap (f1, f1 + size);
296 dd038bc6 2021-09-21 thomas.ad } while ((f1 += size2) < f2);
297 dd038bc6 2021-09-21 thomas.ad } else { /* Natural merge */
298 dd038bc6 2021-09-21 thomas.ad l2 = f2;
299 dd038bc6 2021-09-21 thomas.ad for (f2 = f1 + size2; f2 < l2; f2 += size2) {
300 dd038bc6 2021-09-21 thomas.ad if ((cmp(f2-size, f2) > 0) != sense) {
301 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = f2 - list1 + list2;
302 dd038bc6 2021-09-21 thomas.ad if (sense > 0)
303 dd038bc6 2021-09-21 thomas.ad reverse(f1, f2-size);
304 dd038bc6 2021-09-21 thomas.ad f1 = f2;
307 dd038bc6 2021-09-21 thomas.ad if (sense > 0)
308 dd038bc6 2021-09-21 thomas.ad reverse (f1, f2-size);
309 dd038bc6 2021-09-21 thomas.ad f1 = f2;
310 dd038bc6 2021-09-21 thomas.ad if (f2 < last || cmp(f2 - size, f2) > 0)
311 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = f2 - list1 + list2;
313 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = list2 + n*size;
316 dd038bc6 2021-09-21 thomas.ad #else /* pairwise merge only. */
317 dd038bc6 2021-09-21 thomas.ad for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
318 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = p2 + size2;
319 dd038bc6 2021-09-21 thomas.ad if (cmp (f1, f1 + size) > 0)
320 dd038bc6 2021-09-21 thomas.ad swap(f1, f1 + size);
322 dd038bc6 2021-09-21 thomas.ad #endif /* NATURAL */
326 dd038bc6 2021-09-21 thomas.ad * This is to avoid out-of-bounds addresses in sorting the
327 dd038bc6 2021-09-21 thomas.ad * last 4 elements.
329 dd038bc6 2021-09-21 thomas.ad static void
330 dd038bc6 2021-09-21 thomas.ad insertionsort(unsigned char *a, size_t n, size_t size,
331 dd038bc6 2021-09-21 thomas.ad int (*cmp)(const void *, const void *))
333 dd038bc6 2021-09-21 thomas.ad unsigned char *ai, *s, *t, *u, tmp;
334 dd038bc6 2021-09-21 thomas.ad int i;
336 dd038bc6 2021-09-21 thomas.ad for (ai = a+size; --n >= 1; ai += size)
337 dd038bc6 2021-09-21 thomas.ad for (t = ai; t > a; t -= size) {
338 dd038bc6 2021-09-21 thomas.ad u = t - size;
339 dd038bc6 2021-09-21 thomas.ad if (cmp(u, t) <= 0)
340 dd038bc6 2021-09-21 thomas.ad break;
341 dd038bc6 2021-09-21 thomas.ad swap(u, t);