chiark / gitweb /
preload-hacks: Some patches to make it work.
[termux-packages] / disabled-packages / swi-prolog / qsortr.patch
1 diff -u -r ../swipl-7.3.6/src/pl-dict.c ./src/pl-dict.c
2 --- ../swipl-7.3.6/src/pl-dict.c        2015-08-25 05:55:13.000000000 -0400
3 +++ ./src/pl-dict.c     2015-09-16 19:10:55.212457701 -0400
4 @@ -113,10 +113,148 @@
5  
6  #if (defined _GNU_SOURCE || defined __GNU__ || defined __linux__)
7  
8 +#ifdef __ANDROID__
9 +#include <stdlib.h>
10 +
11 +typedef int              cmp_t(void *, const void *, const void *);
12 +static inline char      *med3(char *, char *, char *, cmp_t *, void *);
13 +static inline void       swapfunc(char *, char *, int, int);
14 +
15 +#define min(a, b)       (a) < (b) ? a : b
16 +
17 +/*
18 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
19 + */
20 +#define swapcode(TYPE, parmi, parmj, n) {               \
21 +       long i = (n) / sizeof (TYPE);                   \
22 +       TYPE *pi = (TYPE *) (parmi);            \
23 +       TYPE *pj = (TYPE *) (parmj);            \
24 +       do {                                            \
25 +               TYPE    t = *pi;                \
26 +               *pi++ = *pj;                            \
27 +               *pj++ = t;                              \
28 +       } while (--i > 0);                              \
29 +}
30 +
31 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
32 +                                  es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
33 +
34 +       static inline void
35 +swapfunc(char *a, char *b, int n, int swaptype)
36 +{
37 +       if(swaptype <= 1)
38 +               swapcode(long, a, b, n)
39 +       else
40 +               swapcode(char, a, b, n)
41 +}
42 +
43 +#define swap(a, b)                                      \
44 +       if (swaptype == 0) {                            \
45 +               long t = *(long *)(a);                  \
46 +               *(long *)(a) = *(long *)(b);            \
47 +               *(long *)(b) = t;                       \
48 +       } else                                          \
49 +swapfunc(a, b, es, swaptype)
50 +
51 +#define vecswap(a, b, n)        if ((n) > 0) swapfunc(a, b, n, swaptype)
52 +
53 +#define CMP(t, x, y) (cmp((t), (x), (y)))
54 +
55 +       static inline char *
56 +med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
57 +{
58 +       return CMP(thunk, a, b) < 0 ?
59 +               (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
60 +               :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
61 +}
62 +
63 +void qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
64 +{
65 +       char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
66 +       size_t d, r;
67 +       int cmp_result;
68 +       int swaptype, swap_cnt;
69 +
70 +loop:   SWAPINIT(a, es);
71 +       swap_cnt = 0;
72 +       if (n < 7) {
73 +               for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
74 +                       for (pl = pm;
75 +                                       pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
76 +                                       pl -= es)
77 +                               swap(pl, pl - es);
78 +               return;
79 +       }
80 +       pm = (char *)a + (n / 2) * es;
81 +       if (n > 7) {
82 +               pl = a;
83 +               pn = (char *)a + (n - 1) * es;
84 +               if (n > 40) {
85 +                       d = (n / 8) * es;
86 +                       pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
87 +                       pm = med3(pm - d, pm, pm + d, cmp, thunk);
88 +                       pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
89 +               }
90 +               pm = med3(pl, pm, pn, cmp, thunk);
91 +       }
92 +       swap(a, pm);
93 +       pa = pb = (char *)a + es;
94 +
95 +       pc = pd = (char *)a + (n - 1) * es;
96 +       for (;;) {
97 +               while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
98 +                       if (cmp_result == 0) {
99 +                               swap_cnt = 1;
100 +                               swap(pa, pb);
101 +                               pa += es;
102 +                       }
103 +                       pb += es;
104 +               }
105 +               while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
106 +                       if (cmp_result == 0) {
107 +                               swap_cnt = 1;
108 +                               swap(pc, pd);
109 +                               pd -= es;
110 +                       }
111 +                       pc -= es;
112 +               }
113 +               if (pb > pc)
114 +                       break;
115 +               swap(pb, pc);
116 +               swap_cnt = 1;
117 +               pb += es;
118 +               pc -= es;
119 +       }
120 +       if (swap_cnt == 0) {  /* Switch to insertion sort */
121 +               for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
122 +                       for (pl = pm;
123 +                                       pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
124 +                                       pl -= es)
125 +                               swap(pl, pl - es);
126 +               return;
127 +       }
128 +
129 +       pn = (char *)a + n * es;
130 +       r = min(pa - (char *)a, pb - pa);
131 +       vecswap(a, pb - r, r);
132 +       r = min(pd - pc, pn - pd - es);
133 +       vecswap(pb, pn - r, r);
134 +       if ((r = pb - pa) > es)
135 +               qsort_r(a, r / es, es, thunk, cmp);
136 +       if ((r = pd - pc) > es) {
137 +               /* Iterate rather than recurse to save stack space */
138 +               a = pn - r;
139 +               n = r / es;
140 +               goto loop;
141 +       }
142 +}
143 +
144 +# else
145  typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
146  extern void qsort_r (void *__base, size_t __nmemb, size_t __size,
147                       __compar_d_fn_t __compar, void *__arg)
148    __nonnull ((1, 4));
149 +#endif
150  
151  #endif
152