1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23// SDL3 always uses its own internal qsort implementation, below, so
24// it can guarantee stable sorts across platforms and not have to
25// tapdance to support the various qsort_r interfaces, or bridge from
26// the C runtime's non-SDLCALL compare functions.
27
28#ifdef assert
29#undef assert
30#endif
31#define assert SDL_assert
32#ifdef malloc
33#undef malloc
34#endif
35#define malloc SDL_malloc
36#ifdef free
37#undef free
38#endif
39#define free SDL_free
40#ifdef memcpy
41#undef memcpy
42#endif
43#define memcpy SDL_memcpy
44#ifdef memmove
45#undef memmove
46#endif
47#define memmove SDL_memmove
48
49/*
50This code came from Gareth McCaughan, under the zlib license.
51Specifically this: https://www.mccaughan.org.uk/software/qsort.c-1.16
52
53Everything below this comment until the HAVE_QSORT #endif was from Gareth
54(any minor changes will be noted inline).
55
56Thank you to Gareth for relicensing this code under the zlib license for our
57benefit!
58
59Update for SDL3: we have modified this from a qsort function to qsort_r.
60
61--ryan.
62*/
63
64/* This is a drop-in replacement for the C library's |qsort()| routine.
65 *
66 * It is intended for use where you know or suspect that your
67 * platform's qsort is bad. If that isn't the case, then you
68 * should probably use the qsort your system gives you in preference
69 * to mine -- it will likely have been tested and tuned better.
70 *
71 * Features:
72 * - Median-of-three pivoting (and more)
73 * - Truncation and final polishing by a single insertion sort
74 * - Early truncation when no swaps needed in pivoting step
75 * - Explicit recursion, guaranteed not to overflow
76 * - A few little wrinkles stolen from the GNU |qsort()|.
77 * (For the avoidance of doubt, no code was stolen, only
78 * broad ideas.)
79 * - separate code for non-aligned / aligned / word-size objects
80 *
81 * Earlier releases of this code used an idiosyncratic licence
82 * I wrote myself, because I'm an idiot. The code is now released
83 * under the "zlib/libpng licence"; you will find the actual
84 * terms in the next comment. I request (but do not require)
85 * that if you make any changes beyond the name of the exported
86 * routine and reasonable tweaks to the TRUNC_* and
87 * PIVOT_THRESHOLD values, you modify the _ID string so as
88 * to make it clear that you have changed the code.
89 *
90 * If you find problems with this code, or find ways of
91 * making it significantly faster, please let me know!
92 * My e-mail address, valid as of early 2016 and for the
93 * foreseeable future, is
94 * gareth.mccaughan@pobox.com
95 * Thanks!
96 *
97 * Gareth McCaughan
98 */
99
100/* Copyright (c) 1998-2021 Gareth McCaughan
101 *
102 * This software is provided 'as-is', without any express or implied
103 * warranty. In no event will the authors be held liable for any
104 * damages arising from the use of this software.
105 *
106 * Permission is granted to anyone to use this software for any purpose,
107 * including commercial applications, and to alter it and redistribute it
108 * freely, subject to the following restrictions:
109 *
110 * 1. The origin of this software must not be misrepresented;
111 * you must not claim that you wrote the original software.
112 * If you use this software in a product, an acknowledgment
113 * in the product documentation would be appreciated but
114 * is not required.
115 *
116 * 2. Altered source versions must be plainly marked as such,
117 * and must not be misrepresented as being the original software.
118 *
119 * 3. This notice may not be removed or altered from any source
120 * distribution.
121 */
122
123/* Revision history since release:
124 * 1998-03-19 v1.12 First release I have any records of.
125 * 2007-09-02 v1.13 Fix bug kindly reported by Dan Bodoh
126 * (premature termination of recursion).
127 * Add a few clarifying comments.
128 * Minor improvements to debug output.
129 * 2016-02-21 v1.14 Replace licence with 2-clause BSD,
130 * and clarify a couple of things in
131 * comments. No code changes.
132 * 2016-03-10 v1.15 Fix bug kindly reported by Ryan Gordon
133 * (pre-insertion-sort messed up).
134 * Disable DEBUG_QSORT by default.
135 * Tweak comments very slightly.
136 * 2021-02-20 v1.16 Fix bug kindly reported by Ray Gardner
137 * (error in recursion leading to possible
138 * stack overflow).
139 * When checking alignment, avoid casting
140 * pointer to possibly-smaller integer.
141 */
142
143/* BEGIN SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
144#if 0
145#include <assert.h>
146#include <stdint.h>
147#include <stdlib.h>
148#include <string.h>
149
150#undef DEBUG_QSORT
151
152static char _ID[]="<qsort.c gjm WITH CHANGES FOR SDL3 1.16 2021-02-20>";
153#endif
154/* END SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
155
156/* How many bytes are there per word? (Must be a power of 2,
157 * and must in fact equal sizeof(int).)
158 */
159#define WORD_BYTES sizeof(int)
160
161/* How big does our stack need to be? Answer: one entry per
162 * bit in a |size_t|. (Actually, a bit less because we don't
163 * recurse all the way down to size-1 subarrays.)
164 */
165#define STACK_SIZE (8*sizeof(size_t))
166
167/* Different situations have slightly different requirements,
168 * and we make life epsilon easier by using different truncation
169 * points for the three different cases.
170 * So far, I have tuned TRUNC_words and guessed that the same
171 * value might work well for the other two cases. Of course
172 * what works well on my machine might work badly on yours.
173 */
174#define TRUNC_nonaligned 12
175#define TRUNC_aligned 12
176#define TRUNC_words 12*WORD_BYTES /* nb different meaning */
177
178/* We use a simple pivoting algorithm for shortish sub-arrays
179 * and a more complicated one for larger ones. The threshold
180 * is PIVOT_THRESHOLD.
181 */
182#define PIVOT_THRESHOLD 40
183
184typedef struct { char * first; char * last; } stack_entry;
185#define pushLeft {stack[stacktop].first=ffirst;stack[stacktop++].last=last;}
186#define pushRight {stack[stacktop].first=first;stack[stacktop++].last=llast;}
187#define doLeft {first=ffirst;llast=last;continue;}
188#define doRight {ffirst=first;last=llast;continue;}
189#define pop {if (--stacktop<0) break;\
190 first=ffirst=stack[stacktop].first;\
191 last=llast=stack[stacktop].last;\
192 continue;}
193
194/* Some comments on the implementation.
195 * 1. When we finish partitioning the array into "low"
196 * and "high", we forget entirely about short subarrays,
197 * because they'll be done later by insertion sort.
198 * Doing lots of little insertion sorts might be a win
199 * on large datasets for locality-of-reference reasons,
200 * but it makes the code much nastier and increases
201 * bookkeeping overhead.
202 * 2. We always save the longer and get to work on the
203 * shorter. This guarantees that whenever we push
204 * a k'th entry onto the stack we are about to get
205 * working on something of size <= N/2^k where N is
206 * the original array size; so the stack can't need
207 * more than log_2(max-array-size) entries.
208 * 3. We choose a pivot by looking at the first, last
209 * and middle elements. We arrange them into order
210 * because it's easy to do that in conjunction with
211 * choosing the pivot, and it makes things a little
212 * easier in the partitioning step. Anyway, the pivot
213 * is the middle of these three. It's still possible
214 * to construct datasets where the algorithm takes
215 * time of order n^2, but it simply never happens in
216 * practice.
217 * 3' Newsflash: On further investigation I find that
218 * it's easy to construct datasets where median-of-3
219 * simply isn't good enough. So on large-ish subarrays
220 * we do a more sophisticated pivoting: we take three
221 * sets of 3 elements, find their medians, and then
222 * take the median of those.
223 * 4. We copy the pivot element to a separate place
224 * because that way we can always do our comparisons
225 * directly against a pointer to that separate place,
226 * and don't have to wonder "did we move the pivot
227 * element?". This makes the inner loop better.
228 * 5. It's possible to make the pivoting even more
229 * reliable by looking at more candidates when n
230 * is larger. (Taking this to its logical conclusion
231 * results in a variant of quicksort that doesn't
232 * have that n^2 worst case.) However, the overhead
233 * from the extra bookkeeping means that it's just
234 * not worth while.
235 * 6. This is pretty clean and portable code. Here are
236 * all the potential portability pitfalls and problems
237 * I know of:
238 * - In one place (the insertion sort) I construct
239 * a pointer that points just past the end of the
240 * supplied array, and assume that (a) it won't
241 * compare equal to any pointer within the array,
242 * and (b) it will compare equal to a pointer
243 * obtained by stepping off the end of the array.
244 * These might fail on some segmented architectures.
245 * - I assume that there are 8 bits in a |char| when
246 * computing the size of stack needed. This would
247 * fail on machines with 9-bit or 16-bit bytes.
248 * - I assume that if |((int)base&(sizeof(int)-1))==0|
249 * and |(size&(sizeof(int)-1))==0| then it's safe to
250 * get at array elements via |int*|s, and that if
251 * actually |size==sizeof(int)| as well then it's
252 * safe to treat the elements as |int|s. This might
253 * fail on systems that convert pointers to integers
254 * in non-standard ways.
255 * - I assume that |8*sizeof(size_t)<=INT_MAX|. This
256 * would be false on a machine with 8-bit |char|s,
257 * 16-bit |int|s and 4096-bit |size_t|s. :-)
258 */
259
260/* The recursion logic is the same in each case.
261 * We keep chopping up until we reach subarrays of size
262 * strictly less than Trunc; we leave these unsorted. */
263#define Recurse(Trunc) \
264 { size_t l=last-ffirst,r=llast-first; \
265 if (l<Trunc) { \
266 if (r>=Trunc) doRight \
267 else pop \
268 } \
269 else if (l<=r) { pushRight; doLeft } \
270 else if (r>=Trunc) { pushLeft; doRight }\
271 else doLeft \
272 }
273
274/* and so is the pivoting logic (note: last is inclusive): */
275#define Pivot(swapper,sz) \
276 if ((size_t)(last-first)>PIVOT_THRESHOLD*sz) mid=pivot_big(first,mid,last,sz,compare,userdata);\
277 else { \
278 if (compare(userdata,first,mid)<0) { \
279 if (compare(userdata,mid,last)>0) { \
280 swapper(mid,last); \
281 if (compare(userdata,first,mid)>0) swapper(first,mid);\
282 } \
283 } \
284 else { \
285 if (compare(userdata,mid,last)>0) swapper(first,last)\
286 else { \
287 swapper(first,mid); \
288 if (compare(userdata,mid,last)>0) swapper(mid,last);\
289 } \
290 } \
291 first+=sz; last-=sz; \
292 }
293
294#ifdef DEBUG_QSORT
295#include <stdio.h>
296#endif
297
298/* and so is the partitioning logic: */
299#define Partition(swapper,sz) { \
300 do { \
301 while (compare(userdata,first,pivot)<0) first+=sz; \
302 while (compare(userdata,pivot,last)<0) last-=sz; \
303 if (first<last) { \
304 swapper(first,last); \
305 first+=sz; last-=sz; } \
306 else if (first==last) { first+=sz; last-=sz; break; }\
307 } while (first<=last); \
308}
309
310/* and so is the pre-insertion-sort operation of putting
311 * the smallest element into place as a sentinel.
312 * Doing this makes the inner loop nicer. I got this
313 * idea from the GNU implementation of qsort().
314 * We find the smallest element from the first |nmemb|,
315 * or the first |limit|, whichever is smaller;
316 * therefore we must have ensured that the globally smallest
317 * element is in the first |limit| (because our
318 * quicksort recursion bottoms out only once we
319 * reach subarrays smaller than |limit|).
320 */
321#define PreInsertion(swapper,limit,sz) \
322 first=base; \
323 last=first + ((nmemb>limit ? limit : nmemb)-1)*sz;\
324 while (last!=base) { \
325 if (compare(userdata,first,last)>0) first=last; \
326 last-=sz; } \
327 if (first!=base) swapper(first,(char*)base);
328
329/* and so is the insertion sort, in the first two cases: */
330#define Insertion(swapper) \
331 last=((char*)base)+nmemb*size; \
332 for (first=((char*)base)+size;first!=last;first+=size) { \
333 char *test; \
334 /* Find the right place for |first|. \
335 * My apologies for var reuse. */ \
336 for (test=first-size;compare(userdata,test,first)>0;test-=size) ; \
337 test+=size; \
338 if (test!=first) { \
339 /* Shift everything in [test,first) \
340 * up by one, and place |first| \
341 * where |test| is. */ \
342 memcpy(pivot,first,size); \
343 memmove(test+size,test,first-test); \
344 memcpy(test,pivot,size); \
345 } \
346 }
347
348#define SWAP_nonaligned(a,b) { \
349 register char *aa=(a),*bb=(b); \
350 register size_t sz=size; \
351 do { register char t=*aa; *aa++=*bb; *bb++=t; } while (--sz); }
352
353#define SWAP_aligned(a,b) { \
354 register int *aa=(int*)(a),*bb=(int*)(b); \
355 register size_t sz=size; \
356 do { register int t=*aa;*aa++=*bb; *bb++=t; } while (sz-=WORD_BYTES); }
357
358#define SWAP_words(a,b) { \
359 register int t=*((int*)a); *((int*)a)=*((int*)b); *((int*)b)=t; }
360
361/* ---------------------------------------------------------------------- */
362
363static char * pivot_big(char *first, char *mid, char *last, size_t size,
364 int (SDLCALL *compare)(void *, const void *, const void *), void *userdata) {
365 size_t d=(((last-first)/size)>>3)*size;
366#ifdef DEBUG_QSORT
367fprintf(stderr, "pivot_big: first=%p last=%p size=%lu n=%lu\n", first, (unsigned long)last, size, (unsigned long)((last-first+1)/size));
368#endif
369 char *m1,*m2,*m3;
370 { char *a=first, *b=first+d, *c=first+2*d;
371#ifdef DEBUG_QSORT
372fprintf(stderr,"< %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
373#endif
374 m1 = compare(userdata,a,b)<0 ?
375 (compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
376 : (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
377 }
378 { char *a=mid-d, *b=mid, *c=mid+d;
379#ifdef DEBUG_QSORT
380fprintf(stderr,". %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
381#endif
382 m2 = compare(userdata,a,b)<0 ?
383 (compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
384 : (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
385 }
386 { char *a=last-2*d, *b=last-d, *c=last;
387#ifdef DEBUG_QSORT
388fprintf(stderr,"> %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
389#endif
390 m3 = compare(userdata,a,b)<0 ?
391 (compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
392 : (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
393 }
394#ifdef DEBUG_QSORT
395fprintf(stderr,"-> %d %d %d @ %p %p %p\n",*(int*)m1,*(int*)m2,*(int*)m3, m1,m2,m3);
396#endif
397 return compare(userdata,m1,m2)<0 ?
398 (compare(userdata,m2,m3)<0 ? m2 : (compare(userdata,m1,m3)<0 ? m3 : m1))
399 : (compare(userdata,m1,m3)<0 ? m1 : (compare(userdata,m2,m3)<0 ? m3 : m2));
400}
401
402/* ---------------------------------------------------------------------- */
403
404static void qsort_r_nonaligned(void *base, size_t nmemb, size_t size,
405 int (SDLCALL *compare)(void *, const void *, const void *), void *userdata) {
406
407 stack_entry stack[STACK_SIZE];
408 int stacktop=0;
409 char *first,*last;
410 char *pivot=malloc(size);
411 size_t trunc=TRUNC_nonaligned*size;
412 assert(pivot != NULL);
413
414 first=(char*)base; last=first+(nmemb-1)*size;
415
416 if ((size_t)(last-first)>=trunc) {
417 char *ffirst=first, *llast=last;
418 while (1) {
419 /* Select pivot */
420 { char * mid=first+size*((last-first)/size >> 1);
421 Pivot(SWAP_nonaligned,size);
422 memcpy(pivot,mid,size);
423 }
424 /* Partition. */
425 Partition(SWAP_nonaligned,size);
426 /* Prepare to recurse/iterate. */
427 Recurse(trunc)
428 }
429 }
430 PreInsertion(SWAP_nonaligned,TRUNC_nonaligned,size);
431 Insertion(SWAP_nonaligned);
432 free(pivot);
433}
434
435static void qsort_r_aligned(void *base, size_t nmemb, size_t size,
436 int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) {
437
438 stack_entry stack[STACK_SIZE];
439 int stacktop=0;
440 char *first,*last;
441 char *pivot=malloc(size);
442 size_t trunc=TRUNC_aligned*size;
443 assert(pivot != NULL);
444
445 first=(char*)base; last=first+(nmemb-1)*size;
446
447 if ((size_t)(last-first)>=trunc) {
448 char *ffirst=first,*llast=last;
449 while (1) {
450 /* Select pivot */
451 { char * mid=first+size*((last-first)/size >> 1);
452 Pivot(SWAP_aligned,size);
453 memcpy(pivot,mid,size);
454 }
455 /* Partition. */
456 Partition(SWAP_aligned,size);
457 /* Prepare to recurse/iterate. */
458 Recurse(trunc)
459 }
460 }
461 PreInsertion(SWAP_aligned,TRUNC_aligned,size);
462 Insertion(SWAP_aligned);
463 free(pivot);
464}
465
466static void qsort_r_words(void *base, size_t nmemb,
467 int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) {
468
469 stack_entry stack[STACK_SIZE];
470 int stacktop=0;
471 char *first,*last;
472 char *pivot=malloc(WORD_BYTES);
473 assert(pivot != NULL);
474
475 first=(char*)base; last=first+(nmemb-1)*WORD_BYTES;
476
477 if (last-first>=TRUNC_words) {
478 char *ffirst=first, *llast=last;
479 while (1) {
480#ifdef DEBUG_QSORT
481fprintf(stderr,"Doing %d:%d: ",
482 (first-(char*)base)/WORD_BYTES,
483 (last-(char*)base)/WORD_BYTES);
484#endif
485 /* Select pivot */
486 { char * mid=first+WORD_BYTES*((last-first) / (2*WORD_BYTES));
487 Pivot(SWAP_words,WORD_BYTES);
488 *(int*)pivot=*(int*)mid;
489#ifdef DEBUG_QSORT
490fprintf(stderr,"pivot = %p = #%lu = %d\n", mid, (unsigned long)(((int*)mid)-((int*)base)), *(int*)mid);
491#endif
492 }
493 /* Partition. */
494 Partition(SWAP_words,WORD_BYTES);
495#ifdef DEBUG_QSORT
496fprintf(stderr, "after partitioning first=#%lu last=#%lu\n", (first-(char*)base)/4lu, (last-(char*)base)/4lu);
497#endif
498 /* Prepare to recurse/iterate. */
499 Recurse(TRUNC_words)
500 }
501 }
502 PreInsertion(SWAP_words,TRUNC_words/WORD_BYTES,WORD_BYTES);
503 /* Now do insertion sort. */
504 last=((char*)base)+nmemb*WORD_BYTES;
505 for (first=((char*)base)+WORD_BYTES;first!=last;first+=WORD_BYTES) {
506 /* Find the right place for |first|. My apologies for var reuse */
507 int *pl=(int*)(first-WORD_BYTES),*pr=(int*)first;
508 *(int*)pivot=*(int*)first;
509 for (;compare(userdata,pl,pivot)>0;pr=pl,--pl) {
510 *pr=*pl; }
511 if (pr!=(int*)first) *pr=*(int*)pivot;
512 }
513 free(pivot);
514}
515
516/* ---------------------------------------------------------------------- */
517
518void SDL_qsort_r(void *base, size_t nmemb, size_t size,
519 SDL_CompareCallback_r compare, void *userdata) {
520
521 if (nmemb<=1) return;
522 if (((uintptr_t)base|size)&(WORD_BYTES-1))
523 qsort_r_nonaligned(base,nmemb,size,compare,userdata);
524 else if (size!=WORD_BYTES)
525 qsort_r_aligned(base,nmemb,size,compare,userdata);
526 else
527 qsort_r_words(base,nmemb,compare,userdata);
528}
529
530static int SDLCALL qsort_non_r_bridge(void *userdata, const void *a, const void *b)
531{
532 int (SDLCALL *compare)(const void *, const void *) = (int (SDLCALL *)(const void *, const void *)) userdata;
533 return compare(a, b);
534}
535
536void SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
537{
538 SDL_qsort_r(base, nmemb, size, qsort_non_r_bridge, compare);
539}
540
541// Don't use the C runtime for such a simple function, since we want to allow SDLCALL callbacks and userdata.
542// SDL's replacement: Taken from the Public Domain C Library (PDCLib):
543// Permission is granted to use, modify, and / or redistribute at will.
544void *SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata)
545{
546 const void *pivot;
547 size_t corr;
548 int rc;
549
550 while (nmemb) {
551 /* algorithm needs -1 correction if remaining elements are an even number. */
552 corr = nmemb % 2;
553 nmemb /= 2;
554 pivot = (const char *)base + (nmemb * size);
555 rc = compare(userdata, key, pivot);
556
557 if (rc > 0) {
558 base = (const char *)pivot + size;
559 /* applying correction */
560 nmemb -= (1 - corr);
561 } else if (rc == 0) {
562 return (void *)pivot;
563 }
564 }
565
566 return NULL;
567}
568
569void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
570{
571 // qsort_non_r_bridge just happens to match calling conventions, so reuse it.
572 return SDL_bsearch_r(key, base, nmemb, size, qsort_non_r_bridge, compare);
573}
574
575