Blame


1 dd038bc6 2021-09-21 thomas.ad /*-
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.
4 dd038bc6 2021-09-21 thomas.ad *
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.
7 dd038bc6 2021-09-21 thomas.ad *
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.
19 dd038bc6 2021-09-21 thomas.ad *
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.
31 dd038bc6 2021-09-21 thomas.ad */
32 dd038bc6 2021-09-21 thomas.ad
33 dd038bc6 2021-09-21 thomas.ad #include <sys/cdefs.h>
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad /*
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.
40 dd038bc6 2021-09-21 thomas.ad */
41 dd038bc6 2021-09-21 thomas.ad
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. */
44 dd038bc6 2021-09-21 thomas.ad
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.)
47 dd038bc6 2021-09-21 thomas.ad */
48 dd038bc6 2021-09-21 thomas.ad
49 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
50 dd038bc6 2021-09-21 thomas.ad
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>
54 c4e2e0d0 2022-07-16 thomas
55 c4e2e0d0 2022-07-16 thomas #include "got_compat.h"
56 dd038bc6 2021-09-21 thomas.ad
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 *));
61 dd038bc6 2021-09-21 thomas.ad
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) \
65 dd038bc6 2021-09-21 thomas.ad do \
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) \
69 dd038bc6 2021-09-21 thomas.ad do \
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)
72 dd038bc6 2021-09-21 thomas.ad
73 dd038bc6 2021-09-21 thomas.ad #define CCOPY_LIST(src, dst, last) \
74 dd038bc6 2021-09-21 thomas.ad do \
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) \
78 dd038bc6 2021-09-21 thomas.ad do \
79 dd038bc6 2021-09-21 thomas.ad *dst++ = *src++; \
80 dd038bc6 2021-09-21 thomas.ad while (i -= 1)
81 dd038bc6 2021-09-21 thomas.ad
82 dd038bc6 2021-09-21 thomas.ad /*
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.
86 dd038bc6 2021-09-21 thomas.ad */
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)))
92 dd038bc6 2021-09-21 thomas.ad
93 dd038bc6 2021-09-21 thomas.ad /*
94 dd038bc6 2021-09-21 thomas.ad * Arguments are as for qsort.
95 dd038bc6 2021-09-21 thomas.ad */
96 dd038bc6 2021-09-21 thomas.ad int
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 *))
99 dd038bc6 2021-09-21 thomas.ad {
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;
105 dd038bc6 2021-09-21 thomas.ad
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);
109 dd038bc6 2021-09-21 thomas.ad }
110 dd038bc6 2021-09-21 thomas.ad
111 dd038bc6 2021-09-21 thomas.ad if (nmemb == 0)
112 dd038bc6 2021-09-21 thomas.ad return (0);
113 dd038bc6 2021-09-21 thomas.ad
114 dd038bc6 2021-09-21 thomas.ad /*
115 dd038bc6 2021-09-21 thomas.ad * XXX
116 dd038bc6 2021-09-21 thomas.ad * Stupid subtraction for the Cray.
117 dd038bc6 2021-09-21 thomas.ad */
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;
121 dd038bc6 2021-09-21 thomas.ad
122 dd038bc6 2021-09-21 thomas.ad if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
123 dd038bc6 2021-09-21 thomas.ad return (-1);
124 dd038bc6 2021-09-21 thomas.ad
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;
148 dd038bc6 2021-09-21 thomas.ad }
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;
154 dd038bc6 2021-09-21 thomas.ad }
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;
161 dd038bc6 2021-09-21 thomas.ad else
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;
175 dd038bc6 2021-09-21 thomas.ad else
176 dd038bc6 2021-09-21 thomas.ad b = p;
177 dd038bc6 2021-09-21 thomas.ad }
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;
183 dd038bc6 2021-09-21 thomas.ad else
184 dd038bc6 2021-09-21 thomas.ad b = p;
185 dd038bc6 2021-09-21 thomas.ad COPY: b = t;
186 dd038bc6 2021-09-21 thomas.ad }
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);
195 dd038bc6 2021-09-21 thomas.ad }
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);
203 dd038bc6 2021-09-21 thomas.ad }
204 dd038bc6 2021-09-21 thomas.ad }
205 dd038bc6 2021-09-21 thomas.ad }
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);
209 dd038bc6 2021-09-21 thomas.ad else
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);
214 dd038bc6 2021-09-21 thomas.ad else
215 dd038bc6 2021-09-21 thomas.ad CCOPY_LIST(f1, tp2, l1);
216 dd038bc6 2021-09-21 thomas.ad }
217 dd038bc6 2021-09-21 thomas.ad *p1 = l2;
218 dd038bc6 2021-09-21 thomas.ad }
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;
223 dd038bc6 2021-09-21 thomas.ad }
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;
227 dd038bc6 2021-09-21 thomas.ad }
228 dd038bc6 2021-09-21 thomas.ad free(list2);
229 dd038bc6 2021-09-21 thomas.ad return (0);
230 dd038bc6 2021-09-21 thomas.ad }
231 dd038bc6 2021-09-21 thomas.ad
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; \
239 dd038bc6 2021-09-21 thomas.ad }
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); \
249 dd038bc6 2021-09-21 thomas.ad }
250 dd038bc6 2021-09-21 thomas.ad
251 dd038bc6 2021-09-21 thomas.ad /*
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.)
256 dd038bc6 2021-09-21 thomas.ad */
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 *))
260 dd038bc6 2021-09-21 thomas.ad {
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;
263 dd038bc6 2021-09-21 thomas.ad
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;
269 dd038bc6 2021-09-21 thomas.ad }
270 dd038bc6 2021-09-21 thomas.ad /*
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.
273 dd038bc6 2021-09-21 thomas.ad */
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;
278 dd038bc6 2021-09-21 thomas.ad
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;
290 dd038bc6 2021-09-21 thomas.ad }
291 dd038bc6 2021-09-21 thomas.ad if (length < THRESHOLD) { /* Pairwise merge */
292 dd038bc6 2021-09-21 thomas.ad do {
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;
305 dd038bc6 2021-09-21 thomas.ad }
306 dd038bc6 2021-09-21 thomas.ad }
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;
312 dd038bc6 2021-09-21 thomas.ad else
313 dd038bc6 2021-09-21 thomas.ad p2 = *EVAL(p2) = list2 + n*size;
314 dd038bc6 2021-09-21 thomas.ad }
315 dd038bc6 2021-09-21 thomas.ad }
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);
321 dd038bc6 2021-09-21 thomas.ad }
322 dd038bc6 2021-09-21 thomas.ad #endif /* NATURAL */
323 dd038bc6 2021-09-21 thomas.ad }
324 dd038bc6 2021-09-21 thomas.ad
325 dd038bc6 2021-09-21 thomas.ad /*
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.
328 dd038bc6 2021-09-21 thomas.ad */
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 *))
332 dd038bc6 2021-09-21 thomas.ad {
333 dd038bc6 2021-09-21 thomas.ad unsigned char *ai, *s, *t, *u, tmp;
334 dd038bc6 2021-09-21 thomas.ad int i;
335 dd038bc6 2021-09-21 thomas.ad
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);
342 dd038bc6 2021-09-21 thomas.ad }
343 dd038bc6 2021-09-21 thomas.ad }