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. */
29 #define ntz(x) a_ctz_l((x))
31 typedef int (*cmpfun)(const void *, const void *);
33 static inline int pntz(size_t p[2]) {
34 int r = ntz(p[0] - 1);
35 if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {
41 static void cycle(size_t width, unsigned char* ar[], int n)
43 unsigned char tmp[256];
53 l = sizeof(tmp) < width ? sizeof(tmp) : width;
54 memcpy(ar[n], ar[0], l);
55 for(i = 0; i < n; i++) {
56 memcpy(ar[i], ar[i + 1], l);
63 /* shl() and shr() need n > 0 */
64 static inline void shl(size_t p[2], int n)
66 if(n >= 8 * sizeof(size_t)) {
67 n -= 8 * sizeof(size_t);
72 p[1] |= p[0] >> (sizeof(size_t) * 8 - n);
76 static inline void shr(size_t p[2], int n)
78 if(n >= 8 * sizeof(size_t)) {
79 n -= 8 * sizeof(size_t);
84 p[0] |= p[1] << (sizeof(size_t) * 8 - n);
88 static void sift(unsigned char *head, size_t width, cmpfun cmp, int pshift, size_t lp[])
90 unsigned char *rt, *lf;
91 unsigned char *ar[14 * sizeof(size_t) + 1];
97 lf = head - width - lp[pshift - 2];
99 if((*cmp)(ar[0], lf) >= 0 && (*cmp)(ar[0], rt) >= 0) {
102 if((*cmp)(lf, rt) >= 0) {
115 static void trinkle(unsigned char *head, size_t width, cmpfun cmp, size_t pp[2], int pshift, int trusty, size_t lp[])
117 unsigned char *stepson,
120 unsigned char *ar[14 * sizeof(size_t) + 1];
128 while(p[0] != 1 || p[1] != 0) {
129 stepson = head - lp[pshift];
130 if((*cmp)(stepson, ar[0]) <= 0) {
133 if(!trusty && pshift > 1) {
135 lf = head - width - lp[pshift - 2];
136 if((*cmp)(rt, stepson) >= 0 || (*cmp)(lf, stepson) >= 0) {
150 sift(head, width, cmp, pshift, lp);
154 void qsort(void *base, size_t nel, size_t width, cmpfun cmp)
156 size_t lp[12*sizeof(size_t)];
157 size_t i, size = width * nel;
158 unsigned char *head, *high;
159 size_t p[2] = {1, 0};
166 high = head + size - width;
168 /* Precompute Leonardo numbers, scaled by element width */
169 for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
172 if((p[0] & 3) == 3) {
173 sift(head, width, cmp, pshift, lp);
177 if(lp[pshift - 1] >= high - head) {
178 trinkle(head, width, cmp, p, pshift, 0, lp);
180 sift(head, width, cmp, pshift, lp);
196 trinkle(head, width, cmp, p, pshift, 0, lp);
198 while(pshift != 1 || p[0] != 1 || p[1] != 0) {
208 trinkle(head - lp[pshift] - width, width, cmp, p, pshift + 1, 1, lp);
211 trinkle(head - width, width, cmp, p, pshift, 1, lp);