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