]> Git Repo - J-linux.git/blob - include/linux/util_macros.h
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / include / linux / util_macros.h
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  * find_closest - locate the closest element in a sorted array
9  * @x: The reference value.
10  * @a: The array in which to look for the closest element. Must be sorted
11  *  in ascending order.
12  * @as: Size of 'a'.
13  *
14  * Returns the index of the element closest to 'x'.
15  * Note: If using an array of negative numbers (or mixed positive numbers),
16  *       then be sure that 'x' is of a signed-type to get good results.
17  */
18 #define find_closest(x, a, as)                                          \
19 ({                                                                      \
20         typeof(as) __fc_i, __fc_as = (as) - 1;                          \
21         long __fc_mid_x, __fc_x = (x);                                  \
22         long __fc_left, __fc_right;                                     \
23         typeof(*a) const *__fc_a = (a);                                 \
24         for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) {                  \
25                 __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \
26                 if (__fc_x <= __fc_mid_x) {                             \
27                         __fc_left = __fc_x - __fc_a[__fc_i];            \
28                         __fc_right = __fc_a[__fc_i + 1] - __fc_x;       \
29                         if (__fc_right < __fc_left)                     \
30                                 __fc_i++;                               \
31                         break;                                          \
32                 }                                                       \
33         }                                                               \
34         (__fc_i);                                                       \
35 })
36
37 /**
38  * find_closest_descending - locate the closest element in a sorted array
39  * @x: The reference value.
40  * @a: The array in which to look for the closest element. Must be sorted
41  *  in descending order.
42  * @as: Size of 'a'.
43  *
44  * Similar to find_closest() but 'a' is expected to be sorted in descending
45  * order. The iteration is done in reverse order, so that the comparison
46  * of '__fc_right' & '__fc_left' also works for unsigned numbers.
47  */
48 #define find_closest_descending(x, a, as)                               \
49 ({                                                                      \
50         typeof(as) __fc_i, __fc_as = (as) - 1;                          \
51         long __fc_mid_x, __fc_x = (x);                                  \
52         long __fc_left, __fc_right;                                     \
53         typeof(*a) const *__fc_a = (a);                                 \
54         for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) {                 \
55                 __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2; \
56                 if (__fc_x <= __fc_mid_x) {                             \
57                         __fc_left = __fc_x - __fc_a[__fc_i];            \
58                         __fc_right = __fc_a[__fc_i - 1] - __fc_x;       \
59                         if (__fc_right < __fc_left)                     \
60                                 __fc_i--;                               \
61                         break;                                          \
62                 }                                                       \
63         }                                                               \
64         (__fc_i);                                                       \
65 })
66
67 /**
68  * is_insidevar - check if the @ptr points inside the @var memory range.
69  * @ptr:        the pointer to a memory address.
70  * @var:        the variable which address and size identify the memory range.
71  *
72  * Evaluates to true if the address in @ptr lies within the memory
73  * range allocated to @var.
74  */
75 #define is_insidevar(ptr, var)                                          \
76         ((uintptr_t)(ptr) >= (uintptr_t)(var) &&                        \
77          (uintptr_t)(ptr) <  (uintptr_t)(var) + sizeof(var))
78
79 #endif
This page took 0.032522 seconds and 4 git commands to generate.