The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /*- |
| 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 13 | * 4. Neither the name of the University nor the names of its contributors |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 14 | * 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 30 | #include <sys/cdefs.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 33 | #ifdef I_AM_QSORT_R |
| 34 | typedef int cmp_t(void *, const void *, const void *); |
| 35 | #else |
| 36 | typedef int cmp_t(const void *, const void *); |
| 37 | #endif |
| 38 | static inline char *med3(char *, char *, char *, cmp_t *, void *); |
| 39 | static inline void swapfunc(char *, char *, int, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | |
| 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 48 | TYPE *pi = (TYPE *) (parmi); \ |
| 49 | TYPE *pj = (TYPE *) (parmj); \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | do { \ |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 51 | TYPE t = *pi; \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | *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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 60 | static inline void |
| 61 | swapfunc(a, b, n, swaptype) |
| 62 | char *a, *b; |
| 63 | int n, swaptype; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | { |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 65 | if(swaptype <= 1) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 | 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 81 | #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 | |
| 87 | static inline char * |
| 88 | med3(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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | { |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 94 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | } |
| 98 | |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 99 | #ifdef I_AM_QSORT_R |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | void |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 101 | qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) |
| 102 | #else |
| 103 | #define thunk NULL |
| 104 | void |
| 105 | qsort(void *a, size_t n, size_t es, cmp_t *cmp) |
| 106 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 | { |
| 108 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 109 | size_t d, r; |
| 110 | int cmp_result; |
| 111 | int swaptype, swap_cnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | |
| 113 | loop: SWAPINIT(a, es); |
| 114 | swap_cnt = 0; |
| 115 | if (n < 7) { |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 116 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | pl -= es) |
| 120 | swap(pl, pl - es); |
| 121 | return; |
| 122 | } |
| 123 | pm = (char *)a + (n / 2) * es; |
| 124 | if (n > 7) { |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 125 | pl = a; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | pn = (char *)a + (n - 1) * es; |
| 127 | if (n > 40) { |
| 128 | d = (n / 8) * es; |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 129 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | } |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 133 | pm = med3(pl, pm, pn, cmp, thunk); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 | } |
| 135 | swap(a, pm); |
| 136 | pa = pb = (char *)a + es; |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 137 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | pc = pd = (char *)a + (n - 1) * es; |
| 139 | for (;;) { |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 140 | while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) { |
| 141 | if (cmp_result == 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | swap_cnt = 1; |
| 143 | swap(pa, pb); |
| 144 | pa += es; |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 145 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 146 | pb += es; |
| 147 | } |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 148 | while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) { |
| 149 | if (cmp_result == 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 164 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | pl -= es) |
| 168 | swap(pl, pl - es); |
| 169 | return; |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 170 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 171 | |
| 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' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 177 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 181 | qsort(a, r / es, es, cmp); |
David 'Digit' Turner | 754c178 | 2009-12-02 17:38:41 -0800 | [diff] [blame^] | 182 | #endif |
| 183 | if ((r = pd - pc) > es) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | /* 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 | } |