GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/stdlib/qsort.c Lines: 75 81 92.6 %
Date: 2017-11-07 Branches: 61 88 69.3 %

Line Branch Exec Source
1
/*	$OpenBSD: qsort.c,v 1.18 2017/05/30 14:54:09 millert Exp $ */
2
/*-
3
 * Copyright (c) 1992, 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include <sys/types.h>
32
#include <stdlib.h>
33
34
static __inline char	*med3(char *, char *, char *, int (*)(const void *, const void *));
35
static __inline void	 swapfunc(char *, char *, size_t, int);
36
37
#define min(a, b)	(a) < (b) ? a : b
38
39
/*
40
 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
41
 *
42
 * This version differs from Bentley & McIlroy in the following ways:
43
 *   1. The partition value is swapped into a[0] instead of being
44
 *	stored out of line.
45
 *
46
 *   2. The swap function can swap 32-bit aligned elements on 64-bit
47
 *	platforms instead of swapping them as byte-aligned.
48
 *
49
 *   3. It uses David Musser's introsort algorithm to fall back to
50
 *	heapsort(3) when the recursion depth reaches 2*lg(n + 1).
51
 *	This avoids quicksort's quadratic behavior for pathological
52
 *	input without appreciably changing the average run time.
53
 *
54
 *   4. Tail recursion is eliminated when sorting the larger of two
55
 *	subpartitions to save stack space.
56
 */
57
#define SWAPTYPE_BYTEV	1
58
#define SWAPTYPE_INTV	2
59
#define SWAPTYPE_LONGV	3
60
#define SWAPTYPE_INT	4
61
#define SWAPTYPE_LONG	5
62
63
#define TYPE_ALIGNED(TYPE, a, es)			\
64
	(((char *)a - (char *)0) % sizeof(TYPE) == 0 && es % sizeof(TYPE) == 0)
65
66
#define swapcode(TYPE, parmi, parmj, n) { 		\
67
	size_t i = (n) / sizeof (TYPE); 		\
68
	TYPE *pi = (TYPE *) (parmi); 			\
69
	TYPE *pj = (TYPE *) (parmj); 			\
70
	do { 						\
71
		TYPE	t = *pi;			\
72
		*pi++ = *pj;				\
73
		*pj++ = t;				\
74
        } while (--i > 0);				\
75
}
76
77
static __inline void
78
swapfunc(char *a, char *b, size_t n, int swaptype)
79
{
80

1338582
	switch (swaptype) {
81
	case SWAPTYPE_INT:
82
	case SWAPTYPE_INTV:
83
		swapcode(int, a, b, n);
84
		break;
85
	case SWAPTYPE_LONG:
86
	case SWAPTYPE_LONGV:
87
1338638
		swapcode(long, a, b, n);
88
669291
		break;
89
	default:
90
		swapcode(char, a, b, n);
91
		break;
92
	}
93
669291
}
94
95
#define swap(a, b)	do {				\
96
	switch (swaptype) {				\
97
	case SWAPTYPE_INT: {				\
98
		int t = *(int *)(a);			\
99
		*(int *)(a) = *(int *)(b);		\
100
		*(int *)(b) = t;			\
101
		break;					\
102
	    }						\
103
	case SWAPTYPE_LONG: {				\
104
		long t = *(long *)(a);			\
105
		*(long *)(a) = *(long *)(b);		\
106
		*(long *)(b) = t;			\
107
		break;					\
108
	    }						\
109
	default:					\
110
		swapfunc(a, b, es, swaptype);		\
111
	}						\
112
} while (0)
113
114
#define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
115
116
static __inline char *
117
med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
118
{
119
3709944
	return cmp(a, b) < 0 ?
120
1895792
	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
121
886666
              :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
122
}
123
124
static void
125
introsort(char *a, size_t n, size_t es, size_t maxdepth, int swaptype,
126
    int (*cmp)(const void *, const void *))
127
{
128
	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
129
	int cmp_result;
130
2075452
	size_t r, s;
131
132
1707013
loop:	if (n < 7) {
133
4028042
		for (pm = a + es; pm < a + n * es; pm += es)
134

3563850
			for (pl = pm; pl > a && cmp(pl - es, pl) > 0;
135
			     pl -= es)
136
481328
				swap(pl, pl - es);
137
1037432
		return;
138
	}
139
669581
	if (maxdepth == 0) {
140
292
		if (heapsort(a, n, es, cmp) == 0)
141
292
			return;
142
	}
143
669289
	maxdepth--;
144
669289
	pm = a + (n / 2) * es;
145
669289
	if (n > 7) {
146
		pl = a;
147
636810
		pn = a + (n - 1) * es;
148
636810
		if (n > 40) {
149
96892
			s = (n / 8) * es;
150
96892
			pl = med3(pl, pl + s, pl + 2 * s, cmp);
151
96892
			pm = med3(pm - s, pm, pm + s, cmp);
152
96892
			pn = med3(pn - 2 * s, pn - s, pn, cmp);
153
96892
		}
154
636810
		pm = med3(pl, pm, pn, cmp);
155
636810
	}
156
1338578
	swap(a, pm);
157
669289
	pa = pb = a + es;
158
669289
	pc = pd = a + (n - 1) * es;
159
1270081
	for (;;) {
160

37333798
		while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
161
11176233
			if (cmp_result == 0) {
162
234
				swap(pa, pb);
163
117
				pa += es;
164
117
			}
165
11176233
			pb += es;
166
		}
167

21209314
		while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
168
6446147
			if (cmp_result == 0) {
169
50
				swap(pc, pd);
170
25
				pd -= es;
171
25
			}
172
6446147
			pc -= es;
173
		}
174
1270081
		if (pb > pc)
175
			break;
176
1201584
		swap(pb, pc);
177
600792
		pb += es;
178
600792
		pc -= es;
179
	}
180
181
669289
	pn = a + n * es;
182
669289
	r = min(pa - a, pb - pa);
183
1338564
	vecswap(a, pb - r, r);
184
2007867
	r = min(pd - pc, pn - pd - es);
185
669305
	vecswap(pb, pn - r, r);
186
	/*
187
	 * To save stack space we sort the smaller side of the partition first
188
	 * using recursion and eliminate tail recursion for the larger side.
189
	 */
190
	r = pb - pa;
191
	s = pd - pc;
192
669289
	if (r < s) {
193
		/* Recurse for 1st side, iterate for 2nd side. */
194
82687
		if (s > es) {
195
82687
			if (r > es) {
196
81534
				introsort(a, r / es, es, maxdepth,
197
				    swaptype, cmp);
198
81534
			}
199
82687
			a = pn - s;
200
82687
			n = s / es;
201
82687
			goto loop;
202
		}
203
	} else {
204
		/* Recurse for 2nd side, iterate for 1st side. */
205
586602
		if (r > es) {
206
586600
			if (s > es) {
207
200913
				introsort(pn - s, s / es, es, maxdepth,
208
				    swaptype, cmp);
209
200913
			}
210
586600
			n = r / es;
211
586600
			goto loop;
212
		}
213
	}
214
1037728
}
215
216
void
217
qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
218
{
219
	size_t i, maxdepth = 0;
220
	int swaptype;
221
222
	/* Approximate 2*ceil(lg(n + 1)) */
223
3846597
	for (i = n; i > 0; i >>= 1)
224
790380
		maxdepth++;
225
755279
	maxdepth *= 2;
226
227

1510558
	if (TYPE_ALIGNED(long, a, es))
228
755279
		swaptype = es == sizeof(long) ? SWAPTYPE_LONG : SWAPTYPE_LONGV;
229
	else if (sizeof(int) != sizeof(long) && TYPE_ALIGNED(int, a, es))
230
		swaptype = es == sizeof(int) ? SWAPTYPE_INT : SWAPTYPE_INTV;
231
	else
232
		swaptype = SWAPTYPE_BYTEV;
233
234
755279
	introsort(a, n, es, maxdepth, swaptype, cmp);
235
236
755279
}
237
238
DEF_STRONG(qsort);