1 --- qsort.c.orig 2008-09-24 19:55:30.000000000 -0700
2 +++ qsort.c 2008-09-25 12:28:18.000000000 -0700
3 @@ -34,14 +34,19 @@ static char sccsid[] = "@(#)qsort.c 8.1
4 __FBSDID("$FreeBSD: src/lib/libc/stdlib/qsort.c,v 1.15 2008/01/14 09:21:34 das Exp $");
10 typedef int cmp_t(void *, const void *, const void *);
12 typedef int cmp_t(const void *, const void *);
14 -static inline char *med3(char *, char *, char *, cmp_t *, void *);
15 -static inline void swapfunc(char *, char *, int, int);
17 +static inline char *med3(char *, char *, char *, cmp_t ^, void *) __attribute__((always_inline));
19 +static inline char *med3(char *, char *, char *, cmp_t *, void *) __attribute__((always_inline));
21 +static inline void swapfunc(char *, char *, int, int) __attribute__((always_inline));
23 #define min(a, b) (a) < (b) ? a : b
25 @@ -90,7 +95,13 @@ swapfunc(a, b, n, swaptype)
29 -med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
30 +med3(char *a, char *b, char *c,
40 @@ -101,21 +112,47 @@ __unused
41 :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
45 +#define DEPTH(x) (2 * (flsl((long)(x)) - 1))
46 +#else /* !__LP64__ */
47 +#define DEPTH(x) (2 * (fls((int)(x)) - 1))
48 +#endif /* __LP64__ */
52 -qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
53 +int __heapsort_r(void *, size_t, size_t, void *, int (*)(void *, const void *, const void *));
57 +_qsort(void *a, size_t n, size_t es,
63 -qsort(void *a, size_t n, size_t es, cmp_t *cmp)
73 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
76 int swaptype, swap_cnt;
78 -loop: SWAPINIT(a, es);
80 + if (depth_limit-- <= 0) {
82 + heapsort_b(a, n, es, cmp);
83 +#elif defined(I_AM_QSORT_R)
84 + __heapsort_r(a, n, es, thunk, cmp);
86 + heapsort(a, n, es, cmp);
93 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
94 @@ -165,25 +202,31 @@ loop: SWAPINIT(a, es);
99 + pn = (char *)a + n * es;
100 + r = min(pa - (char *)a, pb - pa);
101 + vecswap(a, pb - r, r);
102 + r = min(pd - pc, pn - pd - es);
103 + vecswap(pb, pn - r, r);
105 if (swap_cnt == 0) { /* Switch to insertion sort */
106 + r = 1 + n / 4; /* n >= 7, so r >= 2 */
107 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
109 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
113 + if (++swap_cnt > r) goto nevermind;
118 - pn = (char *)a + n * es;
119 - r = min(pa - (char *)a, pb - pa);
120 - vecswap(a, pb - r, r);
121 - r = min(pd - pc, pn - pd - es);
122 - vecswap(pb, pn - r, r);
124 if ((r = pb - pa) > es)
126 - qsort_r(a, r / es, es, thunk, cmp);
127 + _qsort(a, r / es, es, thunk, cmp, depth_limit);
129 - qsort(a, r / es, es, cmp);
130 + _qsort(a, r / es, es, cmp, depth_limit);
132 if ((r = pd - pc) > es) {
133 /* Iterate rather than recurse to save stack space */
134 @@ -193,3 +236,19 @@ loop: SWAPINIT(a, es);
136 /* qsort(pn - r, r / es, es, cmp);*/
141 +qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
142 +#elif defined(I_AM_QSORT_B)
143 +qsort_b(void *a, size_t n, size_t es, cmp_t ^cmp)
145 +qsort(void *a, size_t n, size_t es, cmp_t *cmp)