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. */
32 #define ntz(x) a_ctz_l((x))
34 typedef int (*cmpfun)(const void *, const void *);
36 static inline int pntz(size_t p[2]) {
37 int r = ntz(p[0] - 1);
38 if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {
44 static void cycle(size_t width, unsigned char* ar[], int n)
46 unsigned char tmp[256];
56 l = sizeof(tmp) < width ? sizeof(tmp) : width;
57 memcpy(ar[n], ar[0], l);
58 for(i = 0; i < n; i++) {
59 memcpy(ar[i], ar[i + 1], l);
66 /* shl() and shr() need n > 0 */
67 static inline void shl(size_t p[2], int n)
69 if(n >= 8 * sizeof(size_t)) {
70 n -= 8 * sizeof(size_t);
75 p[1] |= p[0] >> (sizeof(size_t) * 8 - n);
79 static inline void shr(size_t p[2], int n)
81 if(n >= 8 * sizeof(size_t)) {
82 n -= 8 * sizeof(size_t);
87 p[0] |= p[1] << (sizeof(size_t) * 8 - n);
91 static void sift(unsigned char *head, size_t width, cmpfun cmp, int pshift, size_t lp[])
93 unsigned char *rt, *lf;
94 unsigned char *ar[14 * sizeof(size_t) + 1];
100 lf = head - width - lp[pshift - 2];
102 if((*cmp)(ar[0], lf) >= 0 && (*cmp)(ar[0], rt) >= 0) {
105 if((*cmp)(lf, rt) >= 0) {
118 static void trinkle(unsigned char *head, size_t width, cmpfun cmp, size_t pp[2], int pshift, int trusty, size_t lp[])
120 unsigned char *stepson,
123 unsigned char *ar[14 * sizeof(size_t) + 1];
131 while(p[0] != 1 || p[1] != 0) {
132 stepson = head - lp[pshift];
133 if((*cmp)(stepson, ar[0]) <= 0) {
136 if(!trusty && pshift > 1) {
138 lf = head - width - lp[pshift - 2];
139 if((*cmp)(rt, stepson) >= 0 || (*cmp)(lf, stepson) >= 0) {
153 sift(head, width, cmp, pshift, lp);
157 void qsort(void *base, size_t nel, size_t width, cmpfun cmp)
159 size_t lp[12*sizeof(size_t)];
160 size_t i, size = width * nel;
161 unsigned char *head, *high;
162 size_t p[2] = {1, 0};
169 high = head + size - width;
171 /* Precompute Leonardo numbers, scaled by element width */
172 for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
175 if((p[0] & 3) == 3) {
176 sift(head, width, cmp, pshift, lp);
180 if(lp[pshift - 1] >= high - head) {
181 trinkle(head, width, cmp, p, pshift, 0, lp);
183 sift(head, width, cmp, pshift, lp);
199 trinkle(head, width, cmp, p, pshift, 0, lp);
201 while(pshift != 1 || p[0] != 1 || p[1] != 0) {
211 trinkle(head - lp[pshift] - width, width, cmp, p, pshift + 1, 1, lp);
214 trinkle(head - width, width, cmp, p, pshift, 1, lp);