SDL_qsort.c 20 KB

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