blob: b463569815d514e22f3619c59b4c12717c4b2d4e [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"
Charles Manning3796e1f2012-05-09 16:55:17 +00008/* #include <linux/string.h> */
William Juulc051bbe2007-11-15 11:13:05 +01009
10/*
11 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
12 */
Charles Manning3796e1f2012-05-09 16:55:17 +000013#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 { \
William Juulc051bbe2007-11-15 11:13:05 +010018 register TYPE t = *pi; \
19 *pi++ = *pj; \
20 *pj++ = t; \
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020021 } while (--i > 0); \
Charles Manning3796e1f2012-05-09 16:55:17 +000022} while (0)
William Juulc051bbe2007-11-15 11:13:05 +010023
24#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
Charles Manning3796e1f2012-05-09 16:55:17 +000025 es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
William Juulc051bbe2007-11-15 11:13:05 +010026
Charles Manning3796e1f2012-05-09 16:55:17 +000027static inline void
William Juulc051bbe2007-11-15 11:13:05 +010028swapfunc(char *a, char *b, int n, int swaptype)
29{
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020030 if (swaptype <= 1)
Charles Manning3796e1f2012-05-09 16:55:17 +000031 swapcode(long, a, b, n);
William Juulc051bbe2007-11-15 11:13:05 +010032 else
Charles Manning3796e1f2012-05-09 16:55:17 +000033 swapcode(char, a, b, n);
William Juulc051bbe2007-11-15 11:13:05 +010034}
35
Charles Manning3796e1f2012-05-09 16:55:17 +000036#define yswap(a, b) do { \
William Juulc051bbe2007-11-15 11:13:05 +010037 if (swaptype == 0) { \
38 long t = *(long *)(a); \
39 *(long *)(a) = *(long *)(b); \
40 *(long *)(b) = t; \
41 } else \
Charles Manning3796e1f2012-05-09 16:55:17 +000042 swapfunc(a, b, es, swaptype); \
43} while (0)
William Juulc051bbe2007-11-15 11:13:05 +010044
Charles Manning3796e1f2012-05-09 16:55:17 +000045#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
William Juulc051bbe2007-11-15 11:13:05 +010046
Charles Manning3796e1f2012-05-09 16:55:17 +000047static inline char *
William Juulc051bbe2007-11-15 11:13:05 +010048med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
49{
50 return cmp(a, b) < 0 ?
Charles Manning3796e1f2012-05-09 16:55:17 +000051 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
52 : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
William Juulc051bbe2007-11-15 11:13:05 +010053}
54
55#ifndef min
Charles Manning3796e1f2012-05-09 16:55:17 +000056#define min(a, b) (((a) < (b)) ? (a) : (b))
William Juulc051bbe2007-11-15 11:13:05 +010057#endif
58
59void
60yaffs_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
67loop: 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)
Charles Manning3796e1f2012-05-09 16:55:17 +000073 yswap(pl, pl - es);
William Juulc051bbe2007-11-15 11:13:05 +010074 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 }
Charles Manning3796e1f2012-05-09 16:55:17 +000088 yswap(a, pm);
William Juulc051bbe2007-11-15 11:13:05 +010089 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;
Charles Manning3796e1f2012-05-09 16:55:17 +000096 yswap(pa, pb);
William Juulc051bbe2007-11-15 11:13:05 +010097 pa += es;
98 }
99 pb += es;
100 }
101 while (pb <= pc && (r = cmp(pc, a)) >= 0) {
102 if (r == 0) {
103 swap_cnt = 1;
Charles Manning3796e1f2012-05-09 16:55:17 +0000104 yswap(pc, pd);
William Juulc051bbe2007-11-15 11:13:05 +0100105 pd -= es;
106 }
107 pc -= es;
108 }
109 if (pb > pc)
110 break;
Charles Manning3796e1f2012-05-09 16:55:17 +0000111 yswap(pb, pc);
William Juulc051bbe2007-11-15 11:13:05 +0100112 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)
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200118 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
William Juulc051bbe2007-11-15 11:13:05 +0100119 pl -= es)
Charles Manning3796e1f2012-05-09 16:55:17 +0000120 yswap(pl, pl - es);
William Juulc051bbe2007-11-15 11:13:05 +0100121 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);
Charles Manning3796e1f2012-05-09 16:55:17 +0000129 r = pb - pa;
130 if (r > es)
William Juulc051bbe2007-11-15 11:13:05 +0100131 yaffs_qsort(a, r / es, es, cmp);
Charles Manning3796e1f2012-05-09 16:55:17 +0000132 r = pd - pc;
133 if (r > es) {
William Juulc051bbe2007-11-15 11:13:05 +0100134 /* 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}