SDL_qsort.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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.15
  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-2016 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. */
  124. /* BEGIN SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
  125. #if 0
  126. #include <assert.h>
  127. #include <stdlib.h>
  128. #include <string.h>
  129. #undef DEBUG_QSORT
  130. static char _ID[]="<qsort.c gjm WITH CHANGES FOR SDL3 1.15 2016-03-10>";
  131. #endif
  132. /* END SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
  133. /* How many bytes are there per word? (Must be a power of 2,
  134. * and must in fact equal sizeof(int).)
  135. */
  136. #define WORD_BYTES sizeof(int)
  137. /* How big does our stack need to be? Answer: one entry per
  138. * bit in a |size_t|.
  139. */
  140. #define STACK_SIZE (8*sizeof(size_t))
  141. /* Different situations have slightly different requirements,
  142. * and we make life epsilon easier by using different truncation
  143. * points for the three different cases.
  144. * So far, I have tuned TRUNC_words and guessed that the same
  145. * value might work well for the other two cases. Of course
  146. * what works well on my machine might work badly on yours.
  147. */
  148. #define TRUNC_nonaligned 12
  149. #define TRUNC_aligned 12
  150. #define TRUNC_words 12*WORD_BYTES /* nb different meaning */
  151. /* We use a simple pivoting algorithm for shortish sub-arrays
  152. * and a more complicated one for larger ones. The threshold
  153. * is PIVOT_THRESHOLD.
  154. */
  155. #define PIVOT_THRESHOLD 40
  156. typedef struct { char * first; char * last; } stack_entry;
  157. #define pushLeft {stack[stacktop].first=ffirst;stack[stacktop++].last=last;}
  158. #define pushRight {stack[stacktop].first=first;stack[stacktop++].last=llast;}
  159. #define doLeft {first=ffirst;llast=last;continue;}
  160. #define doRight {ffirst=first;last=llast;continue;}
  161. #define pop {if (--stacktop<0) break;\
  162. first=ffirst=stack[stacktop].first;\
  163. last=llast=stack[stacktop].last;\
  164. continue;}
  165. /* Some comments on the implementation.
  166. * 1. When we finish partitioning the array into "low"
  167. * and "high", we forget entirely about short subarrays,
  168. * because they'll be done later by insertion sort.
  169. * Doing lots of little insertion sorts might be a win
  170. * on large datasets for locality-of-reference reasons,
  171. * but it makes the code much nastier and increases
  172. * bookkeeping overhead.
  173. * 2. We always save the shorter and get to work on the
  174. * longer. This guarantees that every time we push
  175. * an item onto the stack its size is <= 1/2 of that
  176. * of its parent; so the stack can't need more than
  177. * log_2(max-array-size) entries.
  178. * 3. We choose a pivot by looking at the first, last
  179. * and middle elements. We arrange them into order
  180. * because it's easy to do that in conjunction with
  181. * choosing the pivot, and it makes things a little
  182. * easier in the partitioning step. Anyway, the pivot
  183. * is the middle of these three. It's still possible
  184. * to construct datasets where the algorithm takes
  185. * time of order n^2, but it simply never happens in
  186. * practice.
  187. * 3' Newsflash: On further investigation I find that
  188. * it's easy to construct datasets where median-of-3
  189. * simply isn't good enough. So on large-ish subarrays
  190. * we do a more sophisticated pivoting: we take three
  191. * sets of 3 elements, find their medians, and then
  192. * take the median of those.
  193. * 4. We copy the pivot element to a separate place
  194. * because that way we can always do our comparisons
  195. * directly against a pointer to that separate place,
  196. * and don't have to wonder "did we move the pivot
  197. * element?". This makes the inner loop better.
  198. * 5. It's possible to make the pivoting even more
  199. * reliable by looking at more candidates when n
  200. * is larger. (Taking this to its logical conclusion
  201. * results in a variant of quicksort that doesn't
  202. * have that n^2 worst case.) However, the overhead
  203. * from the extra bookkeeping means that it's just
  204. * not worth while.
  205. * 6. This is pretty clean and portable code. Here are
  206. * all the potential portability pitfalls and problems
  207. * I know of:
  208. * - In one place (the insertion sort) I construct
  209. * a pointer that points just past the end of the
  210. * supplied array, and assume that (a) it won't
  211. * compare equal to any pointer within the array,
  212. * and (b) it will compare equal to a pointer
  213. * obtained by stepping off the end of the array.
  214. * These might fail on some segmented architectures.
  215. * - I assume that there are 8 bits in a |char| when
  216. * computing the size of stack needed. This would
  217. * fail on machines with 9-bit or 16-bit bytes.
  218. * - I assume that if |((int)base&(sizeof(int)-1))==0|
  219. * and |(size&(sizeof(int)-1))==0| then it's safe to
  220. * get at array elements via |int*|s, and that if
  221. * actually |size==sizeof(int)| as well then it's
  222. * safe to treat the elements as |int|s. This might
  223. * fail on systems that convert pointers to integers
  224. * in non-standard ways.
  225. * - I assume that |8*sizeof(size_t)<=INT_MAX|. This
  226. * would be false on a machine with 8-bit |char|s,
  227. * 16-bit |int|s and 4096-bit |size_t|s. :-)
  228. */
  229. /* The recursion logic is the same in each case.
  230. * We keep chopping up until we reach subarrays of size
  231. * strictly less than Trunc; we leave these unsorted. */
  232. #define Recurse(Trunc) \
  233. { size_t l=last-ffirst,r=llast-first; \
  234. if (l<Trunc) { \
  235. if (r>=Trunc) doRight \
  236. else pop \
  237. } \
  238. else if (l<=r) { pushLeft; doRight } \
  239. else if (r>=Trunc) { pushRight; doLeft }\
  240. else doLeft \
  241. }
  242. /* and so is the pivoting logic (note: last is inclusive): */
  243. #define Pivot(swapper,sz) \
  244. if ((size_t)(last-first)>PIVOT_THRESHOLD*sz) mid=pivot_big(first,mid,last,sz,compare,userdata);\
  245. else { \
  246. if (compare(userdata,first,mid)<0) { \
  247. if (compare(userdata,mid,last)>0) { \
  248. swapper(mid,last); \
  249. if (compare(userdata,first,mid)>0) swapper(first,mid);\
  250. } \
  251. } \
  252. else { \
  253. if (compare(userdata,mid,last)>0) swapper(first,last)\
  254. else { \
  255. swapper(first,mid); \
  256. if (compare(userdata,mid,last)>0) swapper(mid,last);\
  257. } \
  258. } \
  259. first+=sz; last-=sz; \
  260. }
  261. #ifdef DEBUG_QSORT
  262. #include <stdio.h>
  263. #endif
  264. /* and so is the partitioning logic: */
  265. #define Partition(swapper,sz) { \
  266. do { \
  267. while (compare(userdata,first,pivot)<0) first+=sz; \
  268. while (compare(userdata,pivot,last)<0) last-=sz; \
  269. if (first<last) { \
  270. swapper(first,last); \
  271. first+=sz; last-=sz; } \
  272. else if (first==last) { first+=sz; last-=sz; break; }\
  273. } while (first<=last); \
  274. }
  275. /* and so is the pre-insertion-sort operation of putting
  276. * the smallest element into place as a sentinel.
  277. * Doing this makes the inner loop nicer. I got this
  278. * idea from the GNU implementation of qsort().
  279. * We find the smallest element from the first |nmemb|,
  280. * or the first |limit|, whichever is smaller;
  281. * therefore we must have ensured that the globally smallest
  282. * element is in the first |limit| (because our
  283. * quicksort recursion bottoms out only once we
  284. * reach subarrays smaller than |limit|).
  285. */
  286. #define PreInsertion(swapper,limit,sz) \
  287. first=base; \
  288. last=first + ((nmemb>limit ? limit : nmemb)-1)*sz;\
  289. while (last!=base) { \
  290. if (compare(userdata,first,last)>0) first=last; \
  291. last-=sz; } \
  292. if (first!=base) swapper(first,(char*)base);
  293. /* and so is the insertion sort, in the first two cases: */
  294. #define Insertion(swapper) \
  295. last=((char*)base)+nmemb*size; \
  296. for (first=((char*)base)+size;first!=last;first+=size) { \
  297. char *test; \
  298. /* Find the right place for |first|. \
  299. * My apologies for var reuse. */ \
  300. for (test=first-size;compare(userdata,test,first)>0;test-=size) ; \
  301. test+=size; \
  302. if (test!=first) { \
  303. /* Shift everything in [test,first) \
  304. * up by one, and place |first| \
  305. * where |test| is. */ \
  306. memcpy(pivot,first,size); \
  307. memmove(test+size,test,first-test); \
  308. memcpy(test,pivot,size); \
  309. } \
  310. }
  311. #define SWAP_nonaligned(a,b) { \
  312. register char *aa=(a),*bb=(b); \
  313. register size_t sz=size; \
  314. do { register char t=*aa; *aa++=*bb; *bb++=t; } while (--sz); }
  315. #define SWAP_aligned(a,b) { \
  316. register int *aa=(int*)(a),*bb=(int*)(b); \
  317. register size_t sz=size; \
  318. do { register int t=*aa;*aa++=*bb; *bb++=t; } while (sz-=WORD_BYTES); }
  319. #define SWAP_words(a,b) { \
  320. register int t=*((int*)a); *((int*)a)=*((int*)b); *((int*)b)=t; }
  321. /* ---------------------------------------------------------------------- */
  322. static char * pivot_big(char *first, char *mid, char *last, size_t size,
  323. int (SDLCALL * compare)(void *, const void *, const void *), void *userdata) {
  324. size_t d=(((last-first)/size)>>3)*size;
  325. #ifdef DEBUG_QSORT
  326. fprintf(stderr, "pivot_big: first=%p last=%p size=%lu n=%lu\n", first, (unsigned long)last, size, (unsigned long)((last-first+1)/size));
  327. #endif
  328. char *m1,*m2,*m3;
  329. { char *a=first, *b=first+d, *c=first+2*d;
  330. #ifdef DEBUG_QSORT
  331. fprintf(stderr,"< %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
  332. #endif
  333. m1 = compare(userdata,a,b)<0 ?
  334. (compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
  335. : (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
  336. }
  337. { char *a=mid-d, *b=mid, *c=mid+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. m2 = 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=last-2*d, *b=last-d, *c=last;
  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. m3 = 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. #ifdef DEBUG_QSORT
  354. fprintf(stderr,"-> %d %d %d @ %p %p %p\n",*(int*)m1,*(int*)m2,*(int*)m3, m1,m2,m3);
  355. #endif
  356. return compare(userdata,m1,m2)<0 ?
  357. (compare(userdata,m2,m3)<0 ? m2 : (compare(userdata,m1,m3)<0 ? m3 : m1))
  358. : (compare(userdata,m1,m3)<0 ? m1 : (compare(userdata,m2,m3)<0 ? m3 : m2));
  359. }
  360. /* ---------------------------------------------------------------------- */
  361. static void qsort_r_nonaligned(void *base, size_t nmemb, size_t size,
  362. int (SDLCALL * compare)(void *, const void *, const void *), void *userdata) {
  363. stack_entry stack[STACK_SIZE];
  364. int stacktop=0;
  365. char *first,*last;
  366. char *pivot=malloc(size);
  367. size_t trunc=TRUNC_nonaligned*size;
  368. assert(pivot != NULL);
  369. first=(char*)base; last=first+(nmemb-1)*size;
  370. if ((size_t)(last-first)>=trunc) {
  371. char *ffirst=first, *llast=last;
  372. while (1) {
  373. /* Select pivot */
  374. { char * mid=first+size*((last-first)/size >> 1);
  375. Pivot(SWAP_nonaligned,size);
  376. memcpy(pivot,mid,size);
  377. }
  378. /* Partition. */
  379. Partition(SWAP_nonaligned,size);
  380. /* Prepare to recurse/iterate. */
  381. Recurse(trunc)
  382. }
  383. }
  384. PreInsertion(SWAP_nonaligned,TRUNC_nonaligned,size);
  385. Insertion(SWAP_nonaligned);
  386. free(pivot);
  387. }
  388. static void qsort_r_aligned(void *base, size_t nmemb, size_t size,
  389. int (SDLCALL * compare)(void *,const void *, const void *), void *userdata) {
  390. stack_entry stack[STACK_SIZE];
  391. int stacktop=0;
  392. char *first,*last;
  393. char *pivot=malloc(size);
  394. size_t trunc=TRUNC_aligned*size;
  395. assert(pivot != NULL);
  396. first=(char*)base; last=first+(nmemb-1)*size;
  397. if ((size_t)(last-first)>=trunc) {
  398. char *ffirst=first,*llast=last;
  399. while (1) {
  400. /* Select pivot */
  401. { char * mid=first+size*((last-first)/size >> 1);
  402. Pivot(SWAP_aligned,size);
  403. memcpy(pivot,mid,size);
  404. }
  405. /* Partition. */
  406. Partition(SWAP_aligned,size);
  407. /* Prepare to recurse/iterate. */
  408. Recurse(trunc)
  409. }
  410. }
  411. PreInsertion(SWAP_aligned,TRUNC_aligned,size);
  412. Insertion(SWAP_aligned);
  413. free(pivot);
  414. }
  415. static void qsort_r_words(void *base, size_t nmemb,
  416. int (SDLCALL * compare)(void *,const void *, const void *), void *userdata) {
  417. stack_entry stack[STACK_SIZE];
  418. int stacktop=0;
  419. char *first,*last;
  420. char *pivot=malloc(WORD_BYTES);
  421. assert(pivot != NULL);
  422. first=(char*)base; last=first+(nmemb-1)*WORD_BYTES;
  423. if (last-first>=TRUNC_words) {
  424. char *ffirst=first, *llast=last;
  425. while (1) {
  426. #ifdef DEBUG_QSORT
  427. fprintf(stderr,"Doing %d:%d: ",
  428. (first-(char*)base)/WORD_BYTES,
  429. (last-(char*)base)/WORD_BYTES);
  430. #endif
  431. /* Select pivot */
  432. { char * mid=first+WORD_BYTES*((last-first) / (2*WORD_BYTES));
  433. Pivot(SWAP_words,WORD_BYTES);
  434. *(int*)pivot=*(int*)mid;
  435. #ifdef DEBUG_QSORT
  436. fprintf(stderr,"pivot = %p = #%lu = %d\n", mid, (unsigned long)(((int*)mid)-((int*)base)), *(int*)mid);
  437. #endif
  438. }
  439. /* Partition. */
  440. Partition(SWAP_words,WORD_BYTES);
  441. #ifdef DEBUG_QSORT
  442. fprintf(stderr, "after partitioning first=#%lu last=#%lu\n", (first-(char*)base)/4lu, (last-(char*)base)/4lu);
  443. #endif
  444. /* Prepare to recurse/iterate. */
  445. Recurse(TRUNC_words)
  446. }
  447. }
  448. PreInsertion(SWAP_words,TRUNC_words/WORD_BYTES,WORD_BYTES);
  449. /* Now do insertion sort. */
  450. last=((char*)base)+nmemb*WORD_BYTES;
  451. for (first=((char*)base)+WORD_BYTES;first!=last;first+=WORD_BYTES) {
  452. /* Find the right place for |first|. My apologies for var reuse */
  453. int *pl=(int*)(first-WORD_BYTES),*pr=(int*)first;
  454. *(int*)pivot=*(int*)first;
  455. for (;compare(userdata,pl,pivot)>0;pr=pl,--pl) {
  456. *pr=*pl; }
  457. if (pr!=(int*)first) *pr=*(int*)pivot;
  458. }
  459. free(pivot);
  460. }
  461. /* ---------------------------------------------------------------------- */
  462. extern void SDL_qsort_r(void *base, size_t nmemb, size_t size,
  463. int (SDLCALL * compare)(void *, const void *, const void *), void *userdata) {
  464. if (nmemb<=1) return;
  465. if (((size_t)base|size)&(WORD_BYTES-1))
  466. qsort_r_nonaligned(base,nmemb,size,compare,userdata);
  467. else if (size!=WORD_BYTES)
  468. qsort_r_aligned(base,nmemb,size,compare,userdata);
  469. else
  470. qsort_r_words(base,nmemb,compare,userdata);
  471. }
  472. static int SDLCALL qsort_non_r_bridge(void *userdata, const void *a, const void *b)
  473. {
  474. int (SDLCALL *compare)(const void *, const void *) = (int (SDLCALL *)(const void *, const void *)) userdata;
  475. return compare(a, b);
  476. }
  477. void SDL_qsort(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *))
  478. {
  479. SDL_qsort_r(base, nmemb, size, qsort_non_r_bridge, compare);
  480. }
  481. // Don't use the C runtime for such a simple function, since we want to allow SDLCALL callbacks and userdata.
  482. // SDL's replacement: Taken from the Public Domain C Library (PDCLib):
  483. // Permission is granted to use, modify, and / or redistribute at will.
  484. void *SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare)(void *, const void *, const void *), void *userdata)
  485. {
  486. const void *pivot;
  487. size_t corr;
  488. int rc;
  489. while (nmemb) {
  490. /* algorithm needs -1 correction if remaining elements are an even number. */
  491. corr = nmemb % 2;
  492. nmemb /= 2;
  493. pivot = (const char *)base + (nmemb * size);
  494. rc = compare(userdata, key, pivot);
  495. if (rc > 0) {
  496. base = (const char *)pivot + size;
  497. /* applying correction */
  498. nmemb -= (1 - corr);
  499. } else if (rc == 0) {
  500. return (void *)pivot;
  501. }
  502. }
  503. return NULL;
  504. }
  505. void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare)(const void *, const void *))
  506. {
  507. // qsort_non_r_bridge just happens to match calling conventions, so reuse it.
  508. return SDL_bsearch_r(key, base, nmemb, size, qsort_non_r_bridge, compare);
  509. }