1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HELPER_MACROS_H_
3 #define _LINUX_HELPER_MACROS_H_
5 #define __find_closest(x, a, as, op) \
7 typeof(as) __fc_i, __fc_as = (as) - 1; \
8 typeof(x) __fc_x = (x); \
9 typeof(*a) const *__fc_a = (a); \
10 for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \
11 if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \
12 __fc_a[__fc_i + 1], 2)) \
19 * find_closest - locate the closest element in a sorted array
20 * @x: The reference value.
21 * @a: The array in which to look for the closest element. Must be sorted
25 * Returns the index of the element closest to 'x'.
27 #define find_closest(x, a, as) __find_closest(x, a, as, <=)
30 * find_closest_descending - locate the closest element in a sorted array
31 * @x: The reference value.
32 * @a: The array in which to look for the closest element. Must be sorted
33 * in descending order.
36 * Similar to find_closest() but 'a' is expected to be sorted in descending
39 #define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)
42 * is_insidevar - check if the @ptr points inside the @var memory range.
43 * @ptr: the pointer to a memory address.
44 * @var: the variable which address and size identify the memory range.
46 * Evaluates to true if the address in @ptr lies within the memory
47 * range allocated to @var.
49 #define is_insidevar(ptr, var) \
50 ((uintptr_t)(ptr) >= (uintptr_t)(var) && \
51 (uintptr_t)(ptr) < (uintptr_t)(var) + sizeof(var))