Sync qsort.c with userland r318515.

(Note that MIN macro is removed in favor of sys/param.h's version).

PR:		213922
This commit is contained in:
Xin LI 2017-05-19 06:37:16 +00:00
parent c9bcbf3800
commit da31cbbca0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318517

View File

@ -33,51 +33,57 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h> #include <sys/param.h>
#include <sys/libkern.h> #include <sys/libkern.h>
#ifdef I_AM_QSORT_R #ifdef I_AM_QSORT_R
typedef int cmp_t(void *, const void *, const void *); typedef int cmp_t(void *, const void *, const void *);
#else #else
typedef int cmp_t(const void *, const void *); typedef int cmp_t(const void *, const void *);
#endif #endif
static __inline char *med3(char *, char *, char *, cmp_t *, void *); static inline char *med3(char *, char *, char *, cmp_t *, void *);
static __inline void swapfunc(char *, char *, int, int); static inline void swapfunc(char *, char *, size_t, int, int);
#define min(a, b) (a) < (b) ? (a) : (b)
/* /*
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/ */
#define swapcode(TYPE, parmi, parmj, n) { \ #define swapcode(TYPE, parmi, parmj, n) { \
long i = (n) / sizeof (TYPE); \ size_t i = (n) / sizeof (TYPE); \
TYPE *pi = (TYPE *) (parmi); \ TYPE *pi = (TYPE *) (parmi); \
TYPE *pj = (TYPE *) (parmj); \ TYPE *pj = (TYPE *) (parmj); \
do { \ do { \
TYPE t = *pi; \ TYPE t = *pi; \
*pi++ = *pj; \ *pi++ = *pj; \
*pj++ = t; \ *pj++ = t; \
} while (--i > 0); \ } while (--i > 0); \
} }
#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ #define SWAPINIT(TYPE, a, es) swaptype_ ## TYPE = \
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; ((char *)a - (char *)0) % sizeof(TYPE) || \
es % sizeof(TYPE) ? 2 : es == sizeof(TYPE) ? 0 : 1;
static __inline void static inline void
swapfunc(char *a, char *b, int n, int swaptype) swapfunc(char *a, char *b, size_t n, int swaptype_long, int swaptype_int)
{ {
if(swaptype <= 1) if (swaptype_long <= 1)
swapcode(long, a, b, n) swapcode(long, a, b, n)
else if (swaptype_int <= 1)
swapcode(int, a, b, n)
else else
swapcode(char, a, b, n) swapcode(char, a, b, n)
} }
#define swap(a, b) \ #define swap(a, b) \
if (swaptype == 0) { \ if (swaptype_long == 0) { \
long t = *(long *)(a); \ long t = *(long *)(a); \
*(long *)(a) = *(long *)(b); \ *(long *)(a) = *(long *)(b); \
*(long *)(b) = t; \ *(long *)(b) = t; \
} else if (swaptype_int == 0) { \
int t = *(int *)(a); \
*(int *)(a) = *(int *)(b); \
*(int *)(b) = t; \
} else \ } else \
swapfunc(a, b, es, swaptype) swapfunc(a, b, es, swaptype_long, swaptype_int)
#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) #define vecswap(a, b, n) \
if ((n) > 0) swapfunc(a, b, n, swaptype_long, swaptype_int)
#ifdef I_AM_QSORT_R #ifdef I_AM_QSORT_R
#define CMP(t, x, y) (cmp((t), (x), (y))) #define CMP(t, x, y) (cmp((t), (x), (y)))
@ -85,16 +91,16 @@ swapfunc(char *a, char *b, int n, int swaptype)
#define CMP(t, x, y) (cmp((x), (y))) #define CMP(t, x, y) (cmp((x), (y)))
#endif #endif
static __inline char * static inline char *
med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
#ifndef I_AM_QSORT_R #ifndef I_AM_QSORT_R
__unused __unused
#endif #endif
) )
{ {
return CMP(thunk, a, b) < 0 ? return CMP(thunk, a, b) < 0 ?
(CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
:(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
} }
#ifdef I_AM_QSORT_R #ifdef I_AM_QSORT_R
@ -107,13 +113,17 @@ qsort(void *a, size_t n, size_t es, cmp_t *cmp)
#endif #endif
{ {
char *pa, *pb, *pc, *pd, *pl, *pm, *pn; char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int d, r, swaptype, swap_cnt; size_t d1, d2;
int cmp_result;
int swaptype_long, swaptype_int, swap_cnt;
loop: SWAPINIT(a, es); loop: SWAPINIT(long, a, es);
SWAPINIT(int, a, es);
swap_cnt = 0; swap_cnt = 0;
if (n < 7) { if (n < 7) {
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0; for (pl = pm;
pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es) pl -= es)
swap(pl, pl - es); swap(pl, pl - es);
return; return;
@ -123,7 +133,8 @@ loop: SWAPINIT(a, es);
pl = a; pl = a;
pn = (char *)a + (n - 1) * es; pn = (char *)a + (n - 1) * es;
if (n > 40) { if (n > 40) {
d = (n / 8) * es; size_t d = (n / 8) * es;
pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
pm = med3(pm - d, pm, pm + d, cmp, thunk); pm = med3(pm - d, pm, pm + d, cmp, thunk);
pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
@ -135,16 +146,16 @@ loop: SWAPINIT(a, es);
pc = pd = (char *)a + (n - 1) * es; pc = pd = (char *)a + (n - 1) * es;
for (;;) { for (;;) {
while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) { while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
if (r == 0) { if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pa, pb); swap(pa, pb);
pa += es; pa += es;
} }
pb += es; pb += es;
} }
while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) { while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
if (r == 0) { if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pc, pd); swap(pc, pd);
pd -= es; pd -= es;
@ -160,27 +171,51 @@ loop: SWAPINIT(a, es);
} }
if (swap_cnt == 0) { /* Switch to insertion sort */ if (swap_cnt == 0) { /* Switch to insertion sort */
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0; for (pl = pm;
pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es) pl -= es)
swap(pl, pl - es); swap(pl, pl - es);
return; return;
} }
pn = (char *)a + n * es; pn = (char *)a + n * es;
r = min(pa - (char *)a, pb - pa); d1 = MIN(pa - (char *)a, pb - pa);
vecswap(a, pb - r, r); vecswap(a, pb - d1, d1);
r = min(pd - pc, pn - pd - es); d1 = MIN(pd - pc, pn - pd - es);
vecswap(pb, pn - r, r); vecswap(pb, pn - d1, d1);
if ((r = pb - pa) > es)
#ifdef I_AM_QSORT_R d1 = pb - pa;
qsort_r(a, r / es, es, thunk, cmp); d2 = pd - pc;
if (d1 <= d2) {
/* Recurse on left partition, then iterate on right partition */
if (d1 > es) {
#ifdef I_AM_QSORT_R
qsort_r(a, d1 / es, es, thunk, cmp);
#else #else
qsort(a, r / es, es, cmp); qsort(a, d1 / es, es, cmp);
#endif #endif
if ((r = pd - pc) > es) { }
/* Iterate rather than recurse to save stack space */ if (d2 > es) {
a = pn - r; /* Iterate rather than recurse to save stack space */
n = r / es; /* qsort(pn - d2, d2 / es, es, cmp); */
goto loop; a = pn - d2;
n = d2 / es;
goto loop;
}
} else {
/* Recurse on right partition, then iterate on left partition */
if (d2 > es) {
#ifdef I_AM_QSORT_R
qsort_r(pn - d2, d2 / es, es, thunk, cmp);
#else
qsort(pn - d2, d2 / es, es, cmp);
#endif
}
if (d1 > es) {
/* Iterate rather than recurse to save stack space */
/* qsort(a, d1 / es, es, cmp); */
n = d1 / es;
goto loop;
}
} }
} }