Re: [PATCH v3 1/4] util_macros.h: add find_closest() macro

From: Bartosz Golaszewski
Date: Thu Mar 19 2015 - 12:21:27 EST


2015-03-19 16:15 GMT+01:00 Joe Perches <joe@xxxxxxxxxxx>:
> On Thu, 2015-03-19 at 15:30 +0100, Bartosz Golaszewski wrote:
>> Searching for the member of an array closest to 'x' is
>> duplicated in several places.
> []
>> diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
> []
>> @@ -0,0 +1,39 @@
>> +#ifndef _LINUX_HELPER_MACROS_H_
>> +#define _LINUX_HELPER_MACROS_H_
>> +
>> +#define __find_closest(x, a, as, op)( \
>> +{ \
>> + typeof(as) _i, _as = (as) - 1; \
>> + typeof(x) _x = (x); \
>> + typeof(*a) *_a = (a); \
>> + for (_i = 0; _i < _as; _i++) { \
>> + if (_x op DIV_ROUND_CLOSEST(_a[_i] + _a[_i + 1], 2)) \
>> + break; \
>> + } \
>> + (_i); \
>> +})
>
> Please use consistent statement expression start/top whitespace.
>
>> +
>> +/*
>
> This should be /** for proper kernel-doc style
>
>> + * find_closest - locate the closest element in a sorted array
>> + * @x: The reference value.
>> + * @a: The array in which to look for the closest element. Must be sorted
>> + * in ascending order.
>> + * @as: Size of 'a'.
>> + *
>> + * Returns the index of the element closest to 'x'.
>> + */
>> +#define find_closest(x, a, as) __find_closest(x, a, as, <=)
>> +
>> +/*
>> + * find_closest_descending - locate the closest element in a sorted array
>> + * @x: The reference value.
>> + * @a: The array in which to look for the closest element. Must be sorted
>> + * in descending order.
>> + * @as: Size of 'a'.
>> + *
>> + * Similar to get_closest() but 'a' is expected to be sorted in descending
>> + * order.
>> + */
>> +#define find_closest_descending(x, a, as) __find_closest(x, a, as, >)
>
> Again, why is this > not >= ?

Hi Joe,

sorry I missed your comment last time. You're right of course. My
reasoning was this: the only caller of find_closest_descending()
(w83795) used '>' previously, so let's stay compatible. After giving
it some testing thought, it turned out that when using '>' the value
closest to 2 in pwm_freq_cksel0 is... 1. I'll fix that in the next
iteration.

Best regards,
Bartosz Golaszewski
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/