1 /* An alternative to qsort, with an identical interface.
2 This file is part of the GNU C Library.
3 Copyright (C) 1992,95-97,99,2000,01,02,04,07 Free Software Foundation, Inc.
4 Written by Mike Haertel, September 1988.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
36 static void msort_with_tmp (const struct msort_param
*p
, void *b
, size_t n
);
39 msort_with_tmp (const struct msort_param
*p
, void *b
, size_t n
)
50 b2
= (char *) b
+ (n1
* p
->s
);
52 msort_with_tmp (p
, b1
, n1
);
53 msort_with_tmp (p
, b2
, n2
);
56 const size_t s
= p
->s
;
57 __compar_fn_t cmp
= p
->cmp
;
61 while (n1
> 0 && n2
> 0)
63 if ((*cmp
) (b1
, b2
) <= 0)
65 *(uint32_t *) tmp
= *(uint32_t *) b1
;
66 b1
+= sizeof (uint32_t);
71 *(uint32_t *) tmp
= *(uint32_t *) b2
;
72 b2
+= sizeof (uint32_t);
75 tmp
+= sizeof (uint32_t);
79 while (n1
> 0 && n2
> 0)
81 if ((*cmp
) (b1
, b2
) <= 0)
83 *(uint64_t *) tmp
= *(uint64_t *) b1
;
84 b1
+= sizeof (uint64_t);
89 *(uint64_t *) tmp
= *(uint64_t *) b2
;
90 b2
+= sizeof (uint64_t);
93 tmp
+= sizeof (uint64_t);
97 while (n1
> 0 && n2
> 0)
99 unsigned long *tmpl
= (unsigned long *) tmp
;
103 if ((*cmp
) (b1
, b2
) <= 0)
105 bl
= (unsigned long *) b1
;
111 bl
= (unsigned long *) b2
;
115 while (tmpl
< (unsigned long *) tmp
)
120 while (n1
> 0 && n2
> 0)
122 if ((*cmp
) (*(const void **) b1
, *(const void **) b2
) <= 0)
124 *(void **) tmp
= *(void **) b1
;
125 b1
+= sizeof (void *);
130 *(void **) tmp
= *(void **) b2
;
131 b2
+= sizeof (void *);
134 tmp
+= sizeof (void *);
138 while (n1
> 0 && n2
> 0)
140 if ((*cmp
) (b1
, b2
) <= 0)
142 tmp
= (char *) __mempcpy (tmp
, b1
, s
);
148 tmp
= (char *) __mempcpy (tmp
, b2
, s
);
157 memcpy (tmp
, b1
, n1
* s
);
158 memcpy (b
, p
->t
, (n
- n2
) * s
);
162 qsort (void *b
, size_t n
, size_t s
, __compar_fn_t cmp
)
166 struct msort_param p
;
168 /* For large object sizes use indirect sorting. */
170 size
= 2 * n
* sizeof (void *) + s
;
173 /* The temporary array is small, so put it on the stack. */
174 p
.t
= __alloca (size
);
177 /* We should avoid allocating too much memory since this might
178 have to be backed up by swap space. */
179 static long int phys_pages
;
184 phys_pages
= __sysconf (_SC_PHYS_PAGES
);
186 if (phys_pages
== -1)
187 /* Error while determining the memory size. So let's
188 assume there is enough memory. Otherwise the
189 implementer should provide a complete implementation of
190 the `sysconf' function. */
191 phys_pages
= (long int) (~0ul >> 1);
193 /* The following determines that we will never use more than
194 a quarter of the physical memory. */
197 pagesize
= __sysconf (_SC_PAGESIZE
);
200 /* Just a comment here. We cannot compute
201 phys_pages * pagesize
202 and compare the needed amount of memory against this value.
203 The problem is that some systems might have more physical
204 memory then can be represented with a `size_t' value (when
205 measured in bytes. */
207 /* If the memory requirements are too high don't allocate memory. */
208 if (size
/ pagesize
> (size_t) phys_pages
)
210 _quicksort (b
, n
, s
, cmp
);
214 /* It's somewhat large, so malloc it. */
220 /* Couldn't get space, so use the slower algorithm
221 that doesn't need a temporary array. */
222 _quicksort (b
, n
, s
, cmp
);
234 /* Indirect sorting. */
235 char *ip
= (char *) b
;
236 void **tp
= (void **) (p
.t
+ n
* sizeof (void *));
238 void *tmp_storage
= (void *) (tp
+ n
);
240 while ((void *) t
< tmp_storage
)
245 p
.s
= sizeof (void *);
247 msort_with_tmp (&p
, p
.t
+ n
* sizeof (void *), n
);
249 /* tp[0] .. tp[n - 1] is now sorted, copy around entries of
250 the original array. Knuth vol. 3 (2nd ed.) exercise 5.2-10. */
253 for (i
= 0, ip
= (char *) b
; i
< n
; i
++, ip
+= s
)
254 if ((kp
= tp
[i
]) != ip
)
258 memcpy (tmp_storage
, ip
, s
);
262 size_t k
= (kp
- (char *) b
) / s
;
272 memcpy (jp
, tmp_storage
, s
);
277 if ((s
& (sizeof (uint32_t) - 1)) == 0
278 && ((char *) b
- (char *) 0) % __alignof__ (uint32_t) == 0)
280 if (s
== sizeof (uint32_t))
282 else if (s
== sizeof (uint64_t)
283 && ((char *) b
- (char *) 0) % __alignof__ (uint64_t) == 0)
285 else if ((s
& (sizeof (unsigned long) - 1)) == 0
286 && ((char *) b
- (char *) 0)
287 % __alignof__ (unsigned long) == 0)
290 msort_with_tmp (&p
, b
, n
);
294 libc_hidden_def (qsort
)