blob: 37484831e3088c433833c98f30df2f7f28842310 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
David 'Digit' Turner754c1782009-12-02 17:38:41 -080013 * 4. Neither the name of the University nor the names of its contributors
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080014 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
David 'Digit' Turner754c1782009-12-02 17:38:41 -080030#include <sys/cdefs.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <stdlib.h>
32
David 'Digit' Turner754c1782009-12-02 17:38:41 -080033#ifdef I_AM_QSORT_R
34typedef int cmp_t(void *, const void *, const void *);
35#else
36typedef int cmp_t(const void *, const void *);
37#endif
38static inline char *med3(char *, char *, char *, cmp_t *, void *);
39static inline void swapfunc(char *, char *, int, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
41#define min(a, b) (a) < (b) ? a : b
42
43/*
44 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
45 */
46#define swapcode(TYPE, parmi, parmj, n) { \
47 long i = (n) / sizeof (TYPE); \
David 'Digit' Turner754c1782009-12-02 17:38:41 -080048 TYPE *pi = (TYPE *) (parmi); \
49 TYPE *pj = (TYPE *) (parmj); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050 do { \
David 'Digit' Turner754c1782009-12-02 17:38:41 -080051 TYPE t = *pi; \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052 *pi++ = *pj; \
53 *pj++ = t; \
54 } while (--i > 0); \
55}
56
57#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
58 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
59
David 'Digit' Turner754c1782009-12-02 17:38:41 -080060static inline void
61swapfunc(a, b, n, swaptype)
62 char *a, *b;
63 int n, swaptype;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064{
David 'Digit' Turner754c1782009-12-02 17:38:41 -080065 if(swaptype <= 1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066 swapcode(long, a, b, n)
67 else
68 swapcode(char, a, b, n)
69}
70
71#define swap(a, b) \
72 if (swaptype == 0) { \
73 long t = *(long *)(a); \
74 *(long *)(a) = *(long *)(b); \
75 *(long *)(b) = t; \
76 } else \
77 swapfunc(a, b, es, swaptype)
78
79#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
80
David 'Digit' Turner754c1782009-12-02 17:38:41 -080081#ifdef I_AM_QSORT_R
82#define CMP(t, x, y) (cmp((t), (x), (y)))
83#else
84#define CMP(t, x, y) (cmp((x), (y)))
85#endif
86
87static inline char *
88med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
89#ifndef I_AM_QSORT_R
90__unused
91#endif
92)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093{
David 'Digit' Turner754c1782009-12-02 17:38:41 -080094 return CMP(thunk, a, b) < 0 ?
95 (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
96 :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097}
98
David 'Digit' Turner754c1782009-12-02 17:38:41 -080099#ifdef I_AM_QSORT_R
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100void
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800101qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
102#else
103#define thunk NULL
104void
105qsort(void *a, size_t n, size_t es, cmp_t *cmp)
106#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107{
108 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800109 size_t d, r;
110 int cmp_result;
111 int swaptype, swap_cnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
113loop: SWAPINIT(a, es);
114 swap_cnt = 0;
115 if (n < 7) {
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800116 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
117 for (pl = pm;
118 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119 pl -= es)
120 swap(pl, pl - es);
121 return;
122 }
123 pm = (char *)a + (n / 2) * es;
124 if (n > 7) {
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800125 pl = a;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 pn = (char *)a + (n - 1) * es;
127 if (n > 40) {
128 d = (n / 8) * es;
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800129 pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
130 pm = med3(pm - d, pm, pm + d, cmp, thunk);
131 pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132 }
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800133 pm = med3(pl, pm, pn, cmp, thunk);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134 }
135 swap(a, pm);
136 pa = pb = (char *)a + es;
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800137
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138 pc = pd = (char *)a + (n - 1) * es;
139 for (;;) {
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800140 while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
141 if (cmp_result == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142 swap_cnt = 1;
143 swap(pa, pb);
144 pa += es;
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800145 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146 pb += es;
147 }
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800148 while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
149 if (cmp_result == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150 swap_cnt = 1;
151 swap(pc, pd);
152 pd -= es;
153 }
154 pc -= es;
155 }
156 if (pb > pc)
157 break;
158 swap(pb, pc);
159 swap_cnt = 1;
160 pb += es;
161 pc -= es;
162 }
163 if (swap_cnt == 0) { /* Switch to insertion sort */
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800164 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
165 for (pl = pm;
166 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167 pl -= es)
168 swap(pl, pl - es);
169 return;
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800170 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171
172 pn = (char *)a + n * es;
173 r = min(pa - (char *)a, pb - pa);
174 vecswap(a, pb - r, r);
175 r = min(pd - pc, pn - pd - (int)es);
176 vecswap(pb, pn - r, r);
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800177 if ((r = pb - pa) > es)
178#ifdef I_AM_QSORT_R
179 qsort_r(a, r / es, es, thunk, cmp);
180#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181 qsort(a, r / es, es, cmp);
David 'Digit' Turner754c1782009-12-02 17:38:41 -0800182#endif
183 if ((r = pd - pc) > es) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184 /* Iterate rather than recurse to save stack space */
185 a = pn - r;
186 n = r / es;
187 goto loop;
188 }
189/* qsort(pn - r, r / es, es, cmp);*/
190}