1 /* Copyright (C) 2011 by Valentin Ochs
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 /* Minor changes by Rich Felker for integration in musl, 2011-04-27. */
24 /* Smoothsort, an adaptive variant of Heapsort. Memory usage: O(1).
25 Run time: Worst case O(n log n), close to O(n) in the mostly-sorted case. */
33 #define ntz(x) a_ctz_l((x))
35 typedef int (*cmpfun)(const void *, const void *, void *);
37 static inline int pntz(size_t p[2]) {
38 int r = ntz(p[0] - 1);
39 if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {
45 static void cycle(size_t width, unsigned char* ar[], int n)
47 unsigned char tmp[256];
57 l = sizeof(tmp) < width ? sizeof(tmp) : width;
58 memcpy(ar[n], ar[0], l);
59 for(i = 0; i < n; i++) {
60 memcpy(ar[i], ar[i + 1], l);
67 /* shl() and shr() need n > 0 */
68 static inline void shl(size_t p[2], int n)
70 if(n >= 8 * sizeof(size_t)) {
71 n -= 8 * sizeof(size_t);
76 p[1] |= p[0] >> (sizeof(size_t) * 8 - n);
80 static inline void shr(size_t p[2], int n)
82 if(n >= 8 * sizeof(size_t)) {
83 n -= 8 * sizeof(size_t);
88 p[0] |= p[1] << (sizeof(size_t) * 8 - n);
92 static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[])
94 unsigned char *rt, *lf;
95 unsigned char *ar[14 * sizeof(size_t) + 1];
101 lf = head - width - lp[pshift - 2];
103 if(cmp(ar[0], lf, arg) >= 0 && cmp(ar[0], rt, arg) >= 0) {
106 if(cmp(lf, rt, arg) >= 0) {
119 static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], int pshift, int trusty, size_t lp[])
121 unsigned char *stepson,
124 unsigned char *ar[14 * sizeof(size_t) + 1];
132 while(p[0] != 1 || p[1] != 0) {
133 stepson = head - lp[pshift];
134 if(cmp(stepson, ar[0], arg) <= 0) {
137 if(!trusty && pshift > 1) {
139 lf = head - width - lp[pshift - 2];
140 if(cmp(rt, stepson, arg) >= 0 || cmp(lf, stepson, arg) >= 0) {
154 sift(head, width, cmp, arg, pshift, lp);
158 void __qsort_r(void *base, size_t nel, size_t width, cmpfun cmp, void *arg)
160 size_t lp[12*sizeof(size_t)];
161 size_t i, size = width * nel;
162 unsigned char *head, *high;
163 size_t p[2] = {1, 0};
170 high = head + size - width;
172 /* Precompute Leonardo numbers, scaled by element width */
173 for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
176 if((p[0] & 3) == 3) {
177 sift(head, width, cmp, arg, pshift, lp);
181 if(lp[pshift - 1] >= high - head) {
182 trinkle(head, width, cmp, arg, p, pshift, 0, lp);
184 sift(head, width, cmp, arg, pshift, lp);
200 trinkle(head, width, cmp, arg, p, pshift, 0, lp);
202 while(pshift != 1 || p[0] != 1 || p[1] != 0) {
212 trinkle(head - lp[pshift] - width, width, cmp, arg, p, pshift + 1, 1, lp);
215 trinkle(head - width, width, cmp, arg, p, pshift, 1, lp);
221 weak_alias(__qsort_r, qsort_r);