aboutsummaryrefslogtreecommitdiff
path: root/bits/stdlib-bsearch.h
diff options
context:
space:
mode:
Diffstat (limited to 'bits/stdlib-bsearch.h')
-rw-r--r--bits/stdlib-bsearch.h22
1 files changed, 10 insertions, 12 deletions
diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
index 540d718..5ca6785 100644
--- a/bits/stdlib-bsearch.h
+++ b/bits/stdlib-bsearch.h
@@ -1,5 +1,5 @@
/* Perform binary search - inline version.
- Copyright (C) 1991-2024 Free Software Foundation, Inc.
+ Copyright (C) 1991-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -20,22 +20,14 @@ __extern_inline void *
bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
__compar_fn_t __compar)
{
- size_t __l, __u, __idx;
const void *__p;
int __comparison;
- __l = 0;
- __u = __nmemb;
- while (__l < __u)
+ while (__nmemb)
{
- __idx = (__l + __u) / 2;
- __p = (const void *) (((const char *) __base) + (__idx * __size));
+ __p = (const void *) (((const char *) __base) + ((__nmemb >> 1) * __size));
__comparison = (*__compar) (__key, __p);
- if (__comparison < 0)
- __u = __idx;
- else if (__comparison > 0)
- __l = __idx + 1;
- else
+ if (__comparison == 0)
{
#if __GNUC_PREREQ(4, 6)
# pragma GCC diagnostic push
@@ -46,6 +38,12 @@ bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
# pragma GCC diagnostic pop
#endif
}
+ if (__comparison > 0)
+ {
+ __base = ((const char *) __p) + __size;
+ --__nmemb;
+ }
+ __nmemb >>= 1;
}
return NULL;