43 #include <sys/types.h> 52 #define MAX(a,b) (((a)>(b))?(a):(b)) 54 #if defined(_OPENMP) && (_OPENMP >= 201106) && !defined(KMROMP30) 56 #elif defined(_OPENMP) 57 #pragma message ("Compiler seems not support OMP 3.1") 59 #pragma message ("Compiler seems not support OMP") 70 static inline ptrdiff_t
71 kmr_min(ptrdiff_t a, intptr_t b) {
return ((a < b) ? a : b);}
76 kmr_swaptype(
void *a,
size_t es)
78 return ((((
size_t)(intptr_t)a %
sizeof(
long) != 0)
79 || (es %
sizeof(
long) != 0))
80 ? 2 : (es ==
sizeof(
long) ? 0 : 1));
84 kmr_swapbytype(
void *a,
void *b,
size_t sz,
int swaptype)
89 size_t i = (sz /
sizeof(long));
91 long t = *p; *p = *q; *q = t;
97 size_t i = (sz /
sizeof(char));
99 char t = *p; *p = *q; *q = t;
106 kmr_vecswap(
void *a,
void *b,
size_t sz,
int swaptype)
109 kmr_swapbytype(a, b, sz, swaptype);
114 kmr_swap(
void *a,
void *b,
size_t ES,
int swaptype)
119 long t = *p; *p = *q; *q = t;
121 kmr_swapbytype(a, b, ES, swaptype);
129 KMR_CMP2P(
void *A,
void *B)
134 return ((a->v < b->v) ? -1 : 1);
141 KMR_CMP2N(
void *A,
void *B)
146 return ((a->v <= b->v) ? -1 : 1);
150 kmr_meddleof(
void *a,
void *b,
void *c)
152 return (KMR_CMP2P(a, b) < 0
153 ? (KMR_CMP2P(b, c) < 0 ? b : (KMR_CMP2P(a, c) < 0 ? c : a))
154 : (KMR_CMP2P(b, c) > 0 ? b : (KMR_CMP2P(a, c) < 0 ? a : c)));
158 kmr_medianof(
char *A,
const size_t N,
const size_t ES)
160 char *pm = A + (N / 2) * ES;
163 char *pn = A + (N - 1) * ES;
165 size_t d = (N / 8) * ES;
166 pl = kmr_meddleof(pl, (pl + d), (pl + 2 * d));
167 pm = kmr_meddleof((pm - d), pm, (pm + d));
168 pn = kmr_meddleof((pn - 2 * d), (pn - d), pn);
170 pm = kmr_meddleof(pl, pm, pn);
176 kmr_subsort(
char *A,
size_t N,
const size_t ES,
int swaptype)
178 for (
char *p0 = A + ES; p0 < A + N * ES; p0 += ES) {
179 for (
char *p1 = p0; p1 > A && KMR_CMP2N((p1 - ES), p1) > 0; p1 -= ES) {
180 kmr_swap(p1, (p1 - ES), ES, swaptype);
186 kmr_isort0(
void *A0,
size_t N,
const size_t ES,
int depth)
191 int swaptype = kmr_swaptype(A, ES);
194 kmr_subsort(A, N, ES, swaptype);
197 char *pm = kmr_medianof(A, N, ES);
198 kmr_swap(A, pm, ES, swaptype);
202 char *pc = A + (N - 1) * ES;
205 while (pb <= pc && KMR_CMP2P(pb, A) <= 0) {
209 kmr_swap(pa, pb, ES, swaptype);
214 while (pb <= pc && KMR_CMP2N(pc, A) >= 0) {
218 kmr_swap(pc, pd, ES, swaptype);
226 kmr_swap(pb, pc, ES, swaptype);
231 if (swapoccur == 0) {
232 kmr_subsort(A, N, ES, swaptype);
236 char *pn = A + N * ES;
240 size_t r0 = (size_t)kmr_min(pa - A, pb - pa);
241 kmr_vecswap(A, (pb - r0), r0, swaptype);
242 size_t r1 = (size_t)kmr_min((pd - pc), (pn - pd - (ptrdiff_t)ES));
243 kmr_vecswap(pb, (pn - r1), r1, swaptype);
247 kmr_swap(A, (pb - ES), ES, swaptype);
250 size_t r2 = (size_t)(pb - pa);
252 #if defined(_OPENMP) && (_OPENMP >= 201106) && !defined(KMROMP30) 253 KMR_OMP_TASK_FINAL_(depth == 0)
254 kmr_isort0(A, (r2 / ES), ES, MAX((depth - 1), 0));
258 kmr_isort0(A, (r2 / ES), ES, MAX((depth - 1), 0));
260 kmr_isort0(A, (r2 / ES), ES, MAX((depth - 1), 0));
264 size_t r3 = (size_t)(pd - pc);
266 #if defined(_OPENMP) && (_OPENMP >= 201106) && !defined(KMROMP30) 267 KMR_OMP_TASK_FINAL_(depth == 0)
268 kmr_isort0((pn - r3), (r3 / ES), ES, MAX((depth - 1), 0));
272 kmr_isort0((pn - r3), (r3 / ES), ES, MAX((depth - 1), 0));
274 kmr_isort0((pn - r3), (r3 / ES), ES, MAX((depth - 1), 0));
278 #if defined(_OPENMP) && (_OPENMP >= 201106) && !defined(KMROMP30) 292 kmr_isort(
void *A,
const size_t N,
const size_t ES,
int depth)
296 KMR_OMP_SINGLE_NOWAIT_
298 kmr_isort0(A, N, ES, depth);
Utilities Private Part (do not include from applications).
void kmr_isort(void *A, const size_t N, const size_t ES, int depth)
Sorts by comparator on long integers.