1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HELPER_MACROS_H_
3 #define _LINUX_HELPER_MACROS_H_
4 
5 #include <linux/math.h>
6 
7 /**
8  * for_each_if - helper for handling conditionals in various for_each macros
9  * @condition: The condition to check
10  *
11  * Typical use::
12  *
13  *	#define for_each_foo_bar(x, y) \'
14  *		list_for_each_entry(x, y->list, head) \'
15  *			for_each_if(x->something == SOMETHING)
16  *
17  * The for_each_if() macro makes the use of for_each_foo_bar() less error
18  * prone.
19  */
20 #define for_each_if(condition) if (!(condition)) {} else
21 
22 /**
23  * find_closest - locate the closest element in a sorted array
24  * @x: The reference value.
25  * @a: The array in which to look for the closest element. Must be sorted
26  *  in ascending order.
27  * @as: Size of 'a'.
28  *
29  * Returns the index of the element closest to 'x'.
30  * Note: If using an array of negative numbers (or mixed positive numbers),
31  *       then be sure that 'x' is of a signed-type to get good results.
32  */
33 #define find_closest(x, a, as)						\
34 ({									\
35 	typeof(as) __fc_i, __fc_as = (as) - 1;				\
36 	long __fc_mid_x, __fc_x = (x);					\
37 	long __fc_left, __fc_right;					\
38 	typeof(*a) const *__fc_a = (a);					\
39 	for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) {			\
40 		__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2;	\
41 		if (__fc_x <= __fc_mid_x) {				\
42 			__fc_left = __fc_x - __fc_a[__fc_i];		\
43 			__fc_right = __fc_a[__fc_i + 1] - __fc_x;	\
44 			if (__fc_right < __fc_left)			\
45 				__fc_i++;				\
46 			break;						\
47 		}							\
48 	}								\
49 	(__fc_i);							\
50 })
51 
52 /**
53  * find_closest_descending - locate the closest element in a sorted array
54  * @x: The reference value.
55  * @a: The array in which to look for the closest element. Must be sorted
56  *  in descending order.
57  * @as: Size of 'a'.
58  *
59  * Similar to find_closest() but 'a' is expected to be sorted in descending
60  * order. The iteration is done in reverse order, so that the comparison
61  * of '__fc_right' & '__fc_left' also works for unsigned numbers.
62  */
63 #define find_closest_descending(x, a, as)				\
64 ({									\
65 	typeof(as) __fc_i, __fc_as = (as) - 1;				\
66 	long __fc_mid_x, __fc_x = (x);					\
67 	long __fc_left, __fc_right;					\
68 	typeof(*a) const *__fc_a = (a);					\
69 	for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) {			\
70 		__fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2;	\
71 		if (__fc_x <= __fc_mid_x) {				\
72 			__fc_left = __fc_x - __fc_a[__fc_i];		\
73 			__fc_right = __fc_a[__fc_i - 1] - __fc_x;	\
74 			if (__fc_right < __fc_left)			\
75 				__fc_i--;				\
76 			break;						\
77 		}							\
78 	}								\
79 	(__fc_i);							\
80 })
81 
82 /**
83  * is_insidevar - check if the @ptr points inside the @var memory range.
84  * @ptr:	the pointer to a memory address.
85  * @var:	the variable which address and size identify the memory range.
86  *
87  * Evaluates to true if the address in @ptr lies within the memory
88  * range allocated to @var.
89  */
90 #define is_insidevar(ptr, var)						\
91 	((uintptr_t)(ptr) >= (uintptr_t)(var) &&			\
92 	 (uintptr_t)(ptr) <  (uintptr_t)(var) + sizeof(var))
93 
94 #endif
95