SDL_qsort.c 21 KB

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