|
2 | 2 | #ifndef _LINUX_HELPER_MACROS_H_ |
3 | 3 | #define _LINUX_HELPER_MACROS_H_ |
4 | 4 |
|
5 | | -#define __find_closest(x, a, as, op) \ |
6 | | -({ \ |
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)) \ |
13 | | - break; \ |
14 | | - } \ |
15 | | - (__fc_i); \ |
16 | | -}) |
17 | | - |
18 | 5 | /** |
19 | 6 | * find_closest - locate the closest element in a sorted array |
20 | 7 | * @x: The reference value. |
|
23 | 10 | * @as: Size of 'a'. |
24 | 11 | * |
25 | 12 | * Returns the index of the element closest to 'x'. |
| 13 | + * Note: If using an array of negative numbers (or mixed positive numbers), |
| 14 | + * then be sure that 'x' is of a signed-type to get good results. |
26 | 15 | */ |
27 | | -#define find_closest(x, a, as) __find_closest(x, a, as, <=) |
| 16 | +#define find_closest(x, a, as) \ |
| 17 | +({ \ |
| 18 | + typeof(as) __fc_i, __fc_as = (as) - 1; \ |
| 19 | + long __fc_mid_x, __fc_x = (x); \ |
| 20 | + long __fc_left, __fc_right; \ |
| 21 | + typeof(*a) const *__fc_a = (a); \ |
| 22 | + for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \ |
| 23 | + __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i + 1]) / 2; \ |
| 24 | + if (__fc_x <= __fc_mid_x) { \ |
| 25 | + __fc_left = __fc_x - __fc_a[__fc_i]; \ |
| 26 | + __fc_right = __fc_a[__fc_i + 1] - __fc_x; \ |
| 27 | + if (__fc_right < __fc_left) \ |
| 28 | + __fc_i++; \ |
| 29 | + break; \ |
| 30 | + } \ |
| 31 | + } \ |
| 32 | + (__fc_i); \ |
| 33 | +}) |
28 | 34 |
|
29 | 35 | /** |
30 | 36 | * find_closest_descending - locate the closest element in a sorted array |
|
34 | 40 | * @as: Size of 'a'. |
35 | 41 | * |
36 | 42 | * Similar to find_closest() but 'a' is expected to be sorted in descending |
37 | | - * order. |
| 43 | + * order. The iteration is done in reverse order, so that the comparison |
| 44 | + * of '__fc_right' & '__fc_left' also works for unsigned numbers. |
38 | 45 | */ |
39 | | -#define find_closest_descending(x, a, as) __find_closest(x, a, as, >=) |
| 46 | +#define find_closest_descending(x, a, as) \ |
| 47 | +({ \ |
| 48 | + typeof(as) __fc_i, __fc_as = (as) - 1; \ |
| 49 | + long __fc_mid_x, __fc_x = (x); \ |
| 50 | + long __fc_left, __fc_right; \ |
| 51 | + typeof(*a) const *__fc_a = (a); \ |
| 52 | + for (__fc_i = __fc_as; __fc_i >= 1; __fc_i--) { \ |
| 53 | + __fc_mid_x = (__fc_a[__fc_i] + __fc_a[__fc_i - 1]) / 2; \ |
| 54 | + if (__fc_x <= __fc_mid_x) { \ |
| 55 | + __fc_left = __fc_x - __fc_a[__fc_i]; \ |
| 56 | + __fc_right = __fc_a[__fc_i - 1] - __fc_x; \ |
| 57 | + if (__fc_right < __fc_left) \ |
| 58 | + __fc_i--; \ |
| 59 | + break; \ |
| 60 | + } \ |
| 61 | + } \ |
| 62 | + (__fc_i); \ |
| 63 | +}) |
40 | 64 |
|
41 | 65 | /** |
42 | 66 | * is_insidevar - check if the @ptr points inside the @var memory range. |
|
0 commit comments