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" |
Simon Glass | 16ff570 | 2019-11-14 12:57:19 -0700 | [diff] [blame] | 8 | #include <sort.h> |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 9 | /* #include <linux/string.h> */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". |
| 13 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 14 | #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 Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 19 | register TYPE t = *pi; \ |
| 20 | *pi++ = *pj; \ |
| 21 | *pj++ = t; \ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 22 | } while (--i > 0); \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 23 | } while (0) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 24 | |
| 25 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 26 | es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 27 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 28 | static inline void |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 29 | swapfunc(char *a, char *b, int n, int swaptype) |
| 30 | { |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 31 | if (swaptype <= 1) |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 32 | swapcode(long, a, b, n); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 33 | else |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 34 | swapcode(char, a, b, n); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 37 | #define yswap(a, b) do { \ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 38 | if (swaptype == 0) { \ |
| 39 | long t = *(long *)(a); \ |
| 40 | *(long *)(a) = *(long *)(b); \ |
| 41 | *(long *)(b) = t; \ |
| 42 | } else \ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 43 | swapfunc(a, b, es, swaptype); \ |
| 44 | } while (0) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 45 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 46 | #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] | 47 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 48 | static inline char * |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 49 | med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) |
| 50 | { |
| 51 | return cmp(a, b) < 0 ? |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 52 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a)) |
| 53 | : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c)); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #ifndef min |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 57 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | void |
| 61 | yaffs_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 | |
| 68 | loop: 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 Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 74 | yswap(pl, pl - es); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 75 | 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 Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 89 | yswap(a, pm); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 90 | 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 Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 97 | yswap(pa, pb); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 98 | 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 Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 105 | yswap(pc, pd); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 106 | pd -= es; |
| 107 | } |
| 108 | pc -= es; |
| 109 | } |
| 110 | if (pb > pc) |
| 111 | break; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 112 | yswap(pb, pc); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 113 | 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 Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 119 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 120 | pl -= es) |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 121 | yswap(pl, pl - es); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 122 | 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 Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 130 | r = pb - pa; |
| 131 | if (r > es) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 132 | yaffs_qsort(a, r / es, es, cmp); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 133 | r = pd - pc; |
| 134 | if (r > es) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 135 | /* 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 | } |