bsearch: Fix an IAR compilation problem

This commit is contained in:
Kha Vo
2016-04-03 13:56:59 -06:00
committed by Gregory Nutt
parent 4fbd79d1a8
commit 51facd904d
+11 -10
View File
@@ -95,7 +95,7 @@
* look at item 2, so we change 'lim' to 2 so that we will look at items * look at item 2, so we change 'lim' to 2 so that we will look at items
* 0 & 1. If 'lim' is even, the same applies. If 'lim' is odd, moving * 0 & 1. If 'lim' is even, the same applies. If 'lim' is odd, moving
* right again involes halving 'lim', this time moving the base up one * right again involes halving 'lim', this time moving the base up one
* item past 'ptr': e.g., when 'lim' is 5 we change base to item 3 and * item past 'middle': e.g., when 'lim' is 5 we change base to item 3 and
* make 'lim' 2 so that we will look at items 3 and 4. If 'lim' is * make 'lim' 2 so that we will look at items 3 and 4. If 'lim' is
* even, however, we have to shrink it by one before halving: e.g., * even, however, we have to shrink it by one before halving: e.g.,
* when 'lim' is 4, we still looked at item 2, so we have to make 'lim' * when 'lim' is 4, we still looked at item 2, so we have to make 'lim'
@@ -107,29 +107,30 @@ FAR void *bsearch(FAR const void *key, FAR const void *base, size_t nel,
size_t width, CODE int (*compar)(FAR const void *, size_t width, CODE int (*compar)(FAR const void *,
FAR const void *)) FAR const void *))
{ {
FAR const void *ptr; FAR const void *middle; /* Current entry being tested */
size_t lim; FAR const char *lower; /* The lower limit of the search region */
int cmp; size_t lim; /* The number of elements in the region */
int cmp; /* Boolean comparison result */
DEBUGASSERT(key != NULL); DEBUGASSERT(key != NULL);
DEBUGASSERT(base != NULL || nel == 0); DEBUGASSERT(base != NULL || nel == 0);
DEBUGASSERT(compar != NULL); DEBUGASSERT(compar != NULL);
for (lim = nel; lim != 0; lim >>= 1) for (lim = nel, lower = (const char *)base; lim != 0; lim >>= 1)
{ {
ptr = base + (lim >> 1) * width; middle = lower + (lim >> 1) * width;
cmp = (*compar)(key, ptr); cmp = (*compar)(key, middle);
if (cmp == 0) if (cmp == 0)
{ {
return (FAR void *)ptr; return (FAR void *)middle;
} }
if (cmp > 0) if (cmp > 0)
{ {
/* key > ptr: move right (else move left) */ /* key > middle: move right (else move left) */
base = (FAR const char *)ptr + width; lower = (FAR const char *)middle + width;
lim--; lim--;
} }
} }