blob: 32c767f3599f1a018a5453e4234cac2f5062592c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
William Juulc051bbe2007-11-15 11:13:05 +01002/*
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
William Juulc051bbe2007-11-15 11:13:05 +01005 */
6
William Juulc051bbe2007-11-15 11:13:05 +01007#include "yportenv.h"
Simon Glass16ff5702019-11-14 12:57:19 -07008#include <sort.h>
Charles Manning3796e1f2012-05-09 16:55:17 +00009/* #include <linux/string.h> */
William Juulc051bbe2007-11-15 11:13:05 +010010
11/*
12 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
13 */
Charles Manning3796e1f2012-05-09 16:55:17 +000014#define swapcode(TYPE, parmi, parmj, n) do { \
15 long i = (n) / sizeof(TYPE); \
16 register TYPE *pi = (TYPE *) (parmi); \
17 register TYPE *pj = (TYPE *) (parmj); \
18 do { \
William Juulc051bbe2007-11-15 11:13:05 +010019 register TYPE t = *pi; \
20 *pi++ = *pj; \
21 *pj++ = t; \
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020022 } while (--i > 0); \
Charles Manning3796e1f2012-05-09 16:55:17 +000023} while (0)
William Juulc051bbe2007-11-15 11:13:05 +010024
25#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
Charles Manning3796e1f2012-05-09 16:55:17 +000026 es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
William Juulc051bbe2007-11-15 11:13:05 +010027
Charles Manning3796e1f2012-05-09 16:55:17 +000028static inline void
William Juulc051bbe2007-11-15 11:13:05 +010029swapfunc(char *a, char *b, int n, int swaptype)
30{
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020031 if (swaptype <= 1)
Charles Manning3796e1f2012-05-09 16:55:17 +000032 swapcode(long, a, b, n);
William Juulc051bbe2007-11-15 11:13:05 +010033 else
Charles Manning3796e1f2012-05-09 16:55:17 +000034 swapcode(char, a, b, n);
William Juulc051bbe2007-11-15 11:13:05 +010035}
36
Charles Manning3796e1f2012-05-09 16:55:17 +000037#define yswap(a, b) do { \
William Juulc051bbe2007-11-15 11:13:05 +010038 if (swaptype == 0) { \
39 long t = *(long *)(a); \
40 *(long *)(a) = *(long *)(b); \
41 *(long *)(b) = t; \
42 } else \
Charles Manning3796e1f2012-05-09 16:55:17 +000043 swapfunc(a, b, es, swaptype); \
44} while (0)
William Juulc051bbe2007-11-15 11:13:05 +010045
Charles Manning3796e1f2012-05-09 16:55:17 +000046#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
William Juulc051bbe2007-11-15 11:13:05 +010047
Charles Manning3796e1f2012-05-09 16:55:17 +000048static inline char *
William Juulc051bbe2007-11-15 11:13:05 +010049med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
50{
51 return cmp(a, b) < 0 ?
Charles Manning3796e1f2012-05-09 16:55:17 +000052 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
53 : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
William Juulc051bbe2007-11-15 11:13:05 +010054}
55
56#ifndef min
Charles Manning3796e1f2012-05-09 16:55:17 +000057#define min(a, b) (((a) < (b)) ? (a) : (b))
William Juulc051bbe2007-11-15 11:13:05 +010058#endif
59
60void
61yaffs_qsort(void *aa, size_t n, size_t es,
62 int (*cmp)(const void *, const void *))
63{
64 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
65 int d, r, swaptype, swap_cnt;
66 register char *a = aa;
67
68loop: SWAPINIT(a, es);
69 swap_cnt = 0;
70 if (n < 7) {
71 for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
72 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
73 pl -= es)
Charles Manning3796e1f2012-05-09 16:55:17 +000074 yswap(pl, pl - es);
William Juulc051bbe2007-11-15 11:13:05 +010075 return;
76 }
77 pm = (char *)a + (n / 2) * es;
78 if (n > 7) {
79 pl = (char *)a;
80 pn = (char *)a + (n - 1) * es;
81 if (n > 40) {
82 d = (n / 8) * es;
83 pl = med3(pl, pl + d, pl + 2 * d, cmp);
84 pm = med3(pm - d, pm, pm + d, cmp);
85 pn = med3(pn - 2 * d, pn - d, pn, cmp);
86 }
87 pm = med3(pl, pm, pn, cmp);
88 }
Charles Manning3796e1f2012-05-09 16:55:17 +000089 yswap(a, pm);
William Juulc051bbe2007-11-15 11:13:05 +010090 pa = pb = (char *)a + es;
91
92 pc = pd = (char *)a + (n - 1) * es;
93 for (;;) {
94 while (pb <= pc && (r = cmp(pb, a)) <= 0) {
95 if (r == 0) {
96 swap_cnt = 1;
Charles Manning3796e1f2012-05-09 16:55:17 +000097 yswap(pa, pb);
William Juulc051bbe2007-11-15 11:13:05 +010098 pa += es;
99 }
100 pb += es;
101 }
102 while (pb <= pc && (r = cmp(pc, a)) >= 0) {
103 if (r == 0) {
104 swap_cnt = 1;
Charles Manning3796e1f2012-05-09 16:55:17 +0000105 yswap(pc, pd);
William Juulc051bbe2007-11-15 11:13:05 +0100106 pd -= es;
107 }
108 pc -= es;
109 }
110 if (pb > pc)
111 break;
Charles Manning3796e1f2012-05-09 16:55:17 +0000112 yswap(pb, pc);
William Juulc051bbe2007-11-15 11:13:05 +0100113 swap_cnt = 1;
114 pb += es;
115 pc -= es;
116 }
117 if (swap_cnt == 0) { /* Switch to insertion sort */
118 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200119 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
William Juulc051bbe2007-11-15 11:13:05 +0100120 pl -= es)
Charles Manning3796e1f2012-05-09 16:55:17 +0000121 yswap(pl, pl - es);
William Juulc051bbe2007-11-15 11:13:05 +0100122 return;
123 }
124
125 pn = (char *)a + n * es;
126 r = min(pa - (char *)a, pb - pa);
127 vecswap(a, pb - r, r);
128 r = min((long)(pd - pc), (long)(pn - pd - es));
129 vecswap(pb, pn - r, r);
Charles Manning3796e1f2012-05-09 16:55:17 +0000130 r = pb - pa;
131 if (r > es)
William Juulc051bbe2007-11-15 11:13:05 +0100132 yaffs_qsort(a, r / es, es, cmp);
Charles Manning3796e1f2012-05-09 16:55:17 +0000133 r = pd - pc;
134 if (r > es) {
William Juulc051bbe2007-11-15 11:13:05 +0100135 /* Iterate rather than recurse to save stack space */
136 a = pn - r;
137 n = r / es;
138 goto loop;
139 }
140/* yaffs_qsort(pn - r, r / es, es, cmp);*/
141}