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