bsearch.h 624 B

1234567891011121314151617181920212223242526272829303132
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_BSEARCH_H
  3. #define _LINUX_BSEARCH_H
  4. #include <linux/types.h>
  5. static __always_inline
  6. void *__inline_bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
  7. {
  8. const char *pivot;
  9. int result;
  10. while (num > 0) {
  11. pivot = base + (num >> 1) * size;
  12. result = cmp(key, pivot);
  13. if (result == 0)
  14. return (void *)pivot;
  15. if (result > 0) {
  16. base = pivot + size;
  17. num--;
  18. }
  19. num >>= 1;
  20. }
  21. return NULL;
  22. }
  23. extern void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp);
  24. #endif /* _LINUX_BSEARCH_H */