Atlas - SDL_qsort.c
Home / ext / SDL / src / stdlib Lines: 8 | Size: 21389 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 * [email protected] 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;test>=(char*)base&&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 if (mid>=last) break; 422 Pivot(SWAP_nonaligned,size); 423 memcpy(pivot,mid,size); 424 } 425 /* Partition. */ 426 Partition(SWAP_nonaligned,size); 427 /* Prepare to recurse/iterate. */ 428 Recurse(trunc) 429 } 430 } 431 PreInsertion(SWAP_nonaligned,TRUNC_nonaligned,size); 432 Insertion(SWAP_nonaligned); 433 free(pivot); 434} 435 436static void qsort_r_aligned(void *base, size_t nmemb, size_t size, 437 int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) { 438 439 stack_entry stack[STACK_SIZE]; 440 int stacktop=0; 441 char *first,*last; 442 char *pivot=malloc(size); 443 size_t trunc=TRUNC_aligned*size; 444 assert(pivot != NULL); 445 446 first=(char*)base; last=first+(nmemb-1)*size; 447 448 if ((size_t)(last-first)>=trunc) { 449 char *ffirst=first,*llast=last; 450 while (1) { 451 /* Select pivot */ 452 { char * mid=first+size*((last-first)/size >> 1); 453 if (mid>=last) break; 454 Pivot(SWAP_aligned,size); 455 memcpy(pivot,mid,size); 456 } 457 /* Partition. */ 458 Partition(SWAP_aligned,size); 459 /* Prepare to recurse/iterate. */ 460 Recurse(trunc) 461 } 462 } 463 PreInsertion(SWAP_aligned,TRUNC_aligned,size); 464 Insertion(SWAP_aligned); 465 free(pivot); 466} 467 468static void qsort_r_words(void *base, size_t nmemb, 469 int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) { 470 471 stack_entry stack[STACK_SIZE]; 472 int stacktop=0; 473 char *first,*last; 474 char *pivot=malloc(WORD_BYTES); 475 assert(pivot != NULL); 476 477 first=(char*)base; last=first+(nmemb-1)*WORD_BYTES; 478 479 if (last-first>=TRUNC_words) { 480 char *ffirst=first, *llast=last; 481 while (1) { 482#ifdef DEBUG_QSORT 483fprintf(stderr,"Doing %d:%d: ", 484 (first-(char*)base)/WORD_BYTES, 485 (last-(char*)base)/WORD_BYTES); 486#endif 487 /* Select pivot */ 488 { char * mid=first+WORD_BYTES*((last-first) / (2*WORD_BYTES)); 489 if (mid>=last) break; 490 Pivot(SWAP_words,WORD_BYTES); 491 *(int*)pivot=*(int*)mid; 492#ifdef DEBUG_QSORT 493fprintf(stderr,"pivot = %p = #%lu = %d\n", mid, (unsigned long)(((int*)mid)-((int*)base)), *(int*)mid); 494#endif 495 } 496 /* Partition. */ 497 Partition(SWAP_words,WORD_BYTES); 498#ifdef DEBUG_QSORT 499fprintf(stderr, "after partitioning first=#%lu last=#%lu\n", (first-(char*)base)/4lu, (last-(char*)base)/4lu); 500#endif 501 /* Prepare to recurse/iterate. */ 502 Recurse(TRUNC_words) 503 } 504 } 505 PreInsertion(SWAP_words,TRUNC_words/WORD_BYTES,WORD_BYTES); 506 /* Now do insertion sort. */ 507 last=((char*)base)+nmemb*WORD_BYTES; 508 for (first=((char*)base)+WORD_BYTES;first!=last;first+=WORD_BYTES) { 509 /* Find the right place for |first|. My apologies for var reuse */ 510 int *pl=(int*)(first-WORD_BYTES),*pr=(int*)first; 511 *(int*)pivot=*(int*)first; 512 for (;pl>=(int*)base&&compare(userdata,pl,pivot)>0;pr=pl,--pl) { 513 *pr=*pl; } 514 if (pr!=(int*)first) *pr=*(int*)pivot; 515 } 516 free(pivot); 517} 518 519/* ---------------------------------------------------------------------- */ 520 521void SDL_qsort_r(void *base, size_t nmemb, size_t size, 522 SDL_CompareCallback_r compare, void *userdata) { 523 524 if (nmemb<=1) return; 525 if (((uintptr_t)base|size)&(WORD_BYTES-1)) 526 qsort_r_nonaligned(base,nmemb,size,compare,userdata); 527 else if (size!=WORD_BYTES) 528 qsort_r_aligned(base,nmemb,size,compare,userdata); 529 else 530 qsort_r_words(base,nmemb,compare,userdata); 531} 532 533static int SDLCALL qsort_non_r_bridge(void *userdata, const void *a, const void *b) 534{ 535 int (SDLCALL *compare)(const void *, const void *) = (int (SDLCALL *)(const void *, const void *)) userdata; 536 return compare(a, b); 537} 538 539void SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare) 540{ 541 SDL_qsort_r(base, nmemb, size, qsort_non_r_bridge, compare); 542} 543 544// Don't use the C runtime for such a simple function, since we want to allow SDLCALL callbacks and userdata. 545// SDL's replacement: Taken from the Public Domain C Library (PDCLib): 546// Permission is granted to use, modify, and / or redistribute at will. 547void *SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata) 548{ 549 const void *pivot; 550 size_t corr; 551 int rc; 552 553 while (nmemb) { 554 /* algorithm needs -1 correction if remaining elements are an even number. */ 555 corr = nmemb % 2; 556 nmemb /= 2; 557 pivot = (const char *)base + (nmemb * size); 558 rc = compare(userdata, key, pivot); 559 560 if (rc > 0) { 561 base = (const char *)pivot + size; 562 /* applying correction */ 563 nmemb -= (1 - corr); 564 } else if (rc == 0) { 565 return (void *)pivot; 566 } 567 } 568 569 return NULL; 570} 571 572void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare) 573{ 574 // qsort_non_r_bridge just happens to match calling conventions, so reuse it. 575 return SDL_bsearch_r(key, base, nmemb, size, qsort_non_r_bridge, compare); 576} 577[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.