27 #define ntz(x) a_ctz_l((x)) 29 typedef int (*
cmpfun)(
const void *,
const void *);
31 static inline int pntz(
size_t p[2]) {
32 int r =
ntz(p[0] - 1);
33 if(r != 0 || (r = 8*
sizeof(
size_t) +
ntz(p[1])) != 8*
sizeof(
size_t)) {
39 static void cycle(
size_t width,
unsigned char* ar[],
int n)
41 unsigned char tmp[256];
51 l =
sizeof(tmp) < width ?
sizeof(tmp) : width;
52 memcpy(ar[n], ar[0], l);
53 for(i = 0; i < n; i++) {
54 memcpy(ar[i], ar[i + 1], l);
62 static inline void shl(
size_t p[2],
int n)
64 if(n >= 8 *
sizeof(
size_t)) {
70 p[1] |= p[0] >> (
sizeof(
size_t) * 8 - n);
74 static inline void shr(
size_t p[2],
int n)
76 if(n >= 8 *
sizeof(
size_t)) {
82 p[0] |= p[1] << (
sizeof(
size_t) * 8 - n);
86 static void sift(
unsigned char *head,
size_t width,
int (*cmp)(
const void *,
const void *),
int pshift,
size_t lp[])
88 unsigned char *rt, *lf;
89 unsigned char *ar[14 *
sizeof(
size_t) + 1];
95 lf = head - width - lp[pshift - 2];
97 if((*cmp)(ar[0], lf) >= 0 && (*cmp)(ar[0], rt) >= 0) {
100 if((*cmp)(lf, rt) >= 0) {
113 static void trinkle(
unsigned char *head,
size_t width,
int (*cmp)(
const void *,
const void *),
size_t pp[2],
int pshift,
int trusty,
size_t lp[])
115 unsigned char *stepson,
118 unsigned char *ar[14 *
sizeof(
size_t) + 1];
126 while(p[0] != 1 || p[1] != 0) {
127 stepson = head - lp[pshift];
128 if((*cmp)(stepson, ar[0]) <= 0) {
131 if(!trusty && pshift > 1) {
133 lf = head - width - lp[pshift - 2];
134 if((*cmp)(rt, stepson) >= 0 || (*cmp)(lf, stepson) >= 0) {
148 sift(head, width, cmp, pshift, lp);
152 void qsort(
void *
base,
size_t nel,
size_t width,
int (*cmp)(
const void *,
const void *))
154 size_t lp[12*
sizeof(
size_t)];
155 size_t i, size = width * nel;
156 unsigned char *head, *high;
157 size_t p[2] = {1, 0};
164 high = head + size - width;
167 for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
170 if((p[0] & 3) == 3) {
171 sift(head, width, cmp, pshift, lp);
175 if(lp[pshift - 1] >= high - head) {
176 trinkle(head, width, cmp, p, pshift, 0, lp);
178 sift(head, width, cmp, pshift, lp);
194 trinkle(head, width, cmp, p, pshift, 0, lp);
196 while(pshift != 1 || p[0] != 1 || p[1] != 0) {
206 trinkle(head - lp[pshift] - width, width, cmp, p, pshift + 1, 1, lp);
209 trinkle(head - width, width, cmp, p, pshift, 1, lp);
static void shl(size_t p[2], int n)
char base
This version is stamped on May 10, 2016.
static void trinkle(unsigned char *head, size_t width, int(*cmp)(const void *, const void *), size_t pp[2], int pshift, int trusty, size_t lp[])
void qsort(void *base, size_t nel, size_t width, int(*cmp)(const void *, const void *))
static int pntz(size_t p[2])
static void shr(size_t p[2], int n)
static void cycle(size_t width, unsigned char *ar[], int n)
int(* cmpfun)(const void *, const void *)
static void sift(unsigned char *head, size_t width, int(*cmp)(const void *, const void *), int pshift, size_t lp[])