xref: /openbmc/u-boot/fs/yaffs2/yaffs_qsort.c (revision e91610da)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  */
6 
7 #include "yportenv.h"
8 /* #include <linux/string.h> */
9 
10 /*
11  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
12  */
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 {						\
18 		register TYPE	t = *pi;		\
19 		*pi++ = *pj;				\
20 		*pj++ = t;				\
21 	} while (--i > 0);				\
22 } while (0)
23 
24 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
25 	es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
26 
27 static inline void
28 swapfunc(char *a, char *b, int n, int swaptype)
29 {
30 	if (swaptype <= 1)
31 		swapcode(long, a, b, n);
32 	else
33 		swapcode(char, a, b, n);
34 }
35 
36 #define yswap(a, b) do {					\
37 	if (swaptype == 0) {				\
38 		long t = *(long *)(a);			\
39 		*(long *)(a) = *(long *)(b);		\
40 		*(long *)(b) = t;			\
41 	} else						\
42 		swapfunc(a, b, es, swaptype);		\
43 } while (0)
44 
45 #define vecswap(a, b, n)	if ((n) > 0) swapfunc(a, b, n, swaptype)
46 
47 static inline char *
48 med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
49 {
50 	return cmp(a, b) < 0 ?
51 		(cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
52 		: (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
53 }
54 
55 #ifndef min
56 #define min(a, b) (((a) < (b)) ? (a) : (b))
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)
73 				yswap(pl, pl - es);
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 	}
88 	yswap(a, pm);
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;
96 				yswap(pa, pb);
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;
104 				yswap(pc, pd);
105 				pd -= es;
106 			}
107 			pc -= es;
108 		}
109 		if (pb > pc)
110 			break;
111 		yswap(pb, pc);
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)
118 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
119 			     pl -= es)
120 				yswap(pl, pl - es);
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);
129 	r = pb - pa;
130 	if (r > es)
131 		yaffs_qsort(a, r / es, es, cmp);
132 	r = pd - pc;
133 	if (r > es) {
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 }
141