Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 1992, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 7 | #include "yportenv.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 8 | /* #include <linux/string.h> */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". |
| 12 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 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 { \ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 18 | register TYPE t = *pi; \ |
| 19 | *pi++ = *pj; \ |
| 20 | *pj++ = t; \ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 21 | } while (--i > 0); \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 22 | } while (0) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 23 | |
| 24 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 25 | es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 26 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 27 | static inline void |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 28 | swapfunc(char *a, char *b, int n, int swaptype) |
| 29 | { |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 30 | if (swaptype <= 1) |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 31 | swapcode(long, a, b, n); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 32 | else |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 33 | swapcode(char, a, b, n); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 36 | #define yswap(a, b) do { \ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 37 | if (swaptype == 0) { \ |
| 38 | long t = *(long *)(a); \ |
| 39 | *(long *)(a) = *(long *)(b); \ |
| 40 | *(long *)(b) = t; \ |
| 41 | } else \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 42 | swapfunc(a, b, es, swaptype); \ |
| 43 | } while (0) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 44 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 45 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 46 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 47 | static inline char * |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 48 | med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) |
| 49 | { |
| 50 | return cmp(a, b) < 0 ? |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 51 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a)) |
| 52 | : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c)); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | #ifndef min |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 56 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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) |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 73 | yswap(pl, pl - es); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 88 | yswap(a, pm); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 96 | yswap(pa, pb); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 104 | yswap(pc, pd); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 105 | pd -= es; |
| 106 | } |
| 107 | pc -= es; |
| 108 | } |
| 109 | if (pb > pc) |
| 110 | break; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 111 | yswap(pb, pc); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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) |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 118 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 119 | pl -= es) |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 120 | yswap(pl, pl - es); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 129 | r = pb - pa; |
| 130 | if (r > es) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 131 | yaffs_qsort(a, r / es, es, cmp); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 132 | r = pd - pc; |
| 133 | if (r > es) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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 | } |