]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: BSD-3-Clause |
0e8cc8bd WJ |
2 | /* |
3 | * Copyright (c) 1992, 1993 | |
4 | * The Regents of the University of California. All rights reserved. | |
0e8cc8bd WJ |
5 | */ |
6 | ||
7 | #include "yportenv.h" | |
753ac610 | 8 | /* #include <linux/string.h> */ |
0e8cc8bd WJ |
9 | |
10 | /* | |
11 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | |
12 | */ | |
753ac610 CM |
13 | #define swapcode(TYPE, parmi, parmj, n) do { \ |
14 | long i = (n) / sizeof(TYPE); \ | |
15 | register TYPE *pi = (TYPE *) (parmi); \ | |
16 | register TYPE *pj = (TYPE *) (parmj); \ | |
17 | do { \ | |
0e8cc8bd WJ |
18 | register TYPE t = *pi; \ |
19 | *pi++ = *pj; \ | |
20 | *pj++ = t; \ | |
4b070809 | 21 | } while (--i > 0); \ |
753ac610 | 22 | } while (0) |
0e8cc8bd WJ |
23 | |
24 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | |
753ac610 | 25 | es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1; |
0e8cc8bd | 26 | |
753ac610 | 27 | static inline void |
0e8cc8bd WJ |
28 | swapfunc(char *a, char *b, int n, int swaptype) |
29 | { | |
4b070809 | 30 | if (swaptype <= 1) |
753ac610 | 31 | swapcode(long, a, b, n); |
0e8cc8bd | 32 | else |
753ac610 | 33 | swapcode(char, a, b, n); |
0e8cc8bd WJ |
34 | } |
35 | ||
753ac610 | 36 | #define yswap(a, b) do { \ |
0e8cc8bd WJ |
37 | if (swaptype == 0) { \ |
38 | long t = *(long *)(a); \ | |
39 | *(long *)(a) = *(long *)(b); \ | |
40 | *(long *)(b) = t; \ | |
41 | } else \ | |
753ac610 CM |
42 | swapfunc(a, b, es, swaptype); \ |
43 | } while (0) | |
0e8cc8bd | 44 | |
753ac610 | 45 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) |
0e8cc8bd | 46 | |
753ac610 | 47 | static inline char * |
0e8cc8bd WJ |
48 | med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) |
49 | { | |
50 | return cmp(a, b) < 0 ? | |
753ac610 CM |
51 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a)) |
52 | : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c)); | |
0e8cc8bd WJ |
53 | } |
54 | ||
55 | #ifndef min | |
753ac610 | 56 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
0e8cc8bd WJ |
57 | #endif |
58 | ||
59 | void | |
60 | yaffs_qsort(void *aa, size_t n, size_t es, | |
61 | int (*cmp)(const void *, const void *)) | |
62 | { | |
63 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | |
64 | int d, r, swaptype, swap_cnt; | |
65 | register char *a = aa; | |
66 | ||
67 | loop: SWAPINIT(a, es); | |
68 | swap_cnt = 0; | |
69 | if (n < 7) { | |
70 | for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) | |
71 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | |
72 | pl -= es) | |
753ac610 | 73 | yswap(pl, pl - es); |
0e8cc8bd WJ |
74 | return; |
75 | } | |
76 | pm = (char *)a + (n / 2) * es; | |
77 | if (n > 7) { | |
78 | pl = (char *)a; | |
79 | pn = (char *)a + (n - 1) * es; | |
80 | if (n > 40) { | |
81 | d = (n / 8) * es; | |
82 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | |
83 | pm = med3(pm - d, pm, pm + d, cmp); | |
84 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | |
85 | } | |
86 | pm = med3(pl, pm, pn, cmp); | |
87 | } | |
753ac610 | 88 | yswap(a, pm); |
0e8cc8bd WJ |
89 | pa = pb = (char *)a + es; |
90 | ||
91 | pc = pd = (char *)a + (n - 1) * es; | |
92 | for (;;) { | |
93 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | |
94 | if (r == 0) { | |
95 | swap_cnt = 1; | |
753ac610 | 96 | yswap(pa, pb); |
0e8cc8bd WJ |
97 | pa += es; |
98 | } | |
99 | pb += es; | |
100 | } | |
101 | while (pb <= pc && (r = cmp(pc, a)) >= 0) { | |
102 | if (r == 0) { | |
103 | swap_cnt = 1; | |
753ac610 | 104 | yswap(pc, pd); |
0e8cc8bd WJ |
105 | pd -= es; |
106 | } | |
107 | pc -= es; | |
108 | } | |
109 | if (pb > pc) | |
110 | break; | |
753ac610 | 111 | yswap(pb, pc); |
0e8cc8bd WJ |
112 | swap_cnt = 1; |
113 | pb += es; | |
114 | pc -= es; | |
115 | } | |
116 | if (swap_cnt == 0) { /* Switch to insertion sort */ | |
117 | for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) | |
4b070809 | 118 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
0e8cc8bd | 119 | pl -= es) |
753ac610 | 120 | yswap(pl, pl - es); |
0e8cc8bd WJ |
121 | return; |
122 | } | |
123 | ||
124 | pn = (char *)a + n * es; | |
125 | r = min(pa - (char *)a, pb - pa); | |
126 | vecswap(a, pb - r, r); | |
127 | r = min((long)(pd - pc), (long)(pn - pd - es)); | |
128 | vecswap(pb, pn - r, r); | |
753ac610 CM |
129 | r = pb - pa; |
130 | if (r > es) | |
0e8cc8bd | 131 | yaffs_qsort(a, r / es, es, cmp); |
753ac610 CM |
132 | r = pd - pc; |
133 | if (r > es) { | |
0e8cc8bd WJ |
134 | /* Iterate rather than recurse to save stack space */ |
135 | a = pn - r; | |
136 | n = r / es; | |
137 | goto loop; | |
138 | } | |
139 | /* yaffs_qsort(pn - r, r / es, es, cmp);*/ | |
140 | } |