Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_BSEARCH_H |
| 3 | #define _LINUX_BSEARCH_H |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7 | static __always_inline |
| 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); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 31 | |
| 32 | #endif /* _LINUX_BSEARCH_H */ |