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

From: Steven Rostedt
Date: Thu Mar 19 2015 - 12:03:32 EST



BTW, it's best to email this account and not my Red Hat one.


On Thu, 19 Mar 2015 11:53:06 -0400
Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> wrote:

> Searching for the member of an array closest to 'x' is
> duplicated in several places.
>
> Add a new include - util_macros.h - and two macros that
> implement this algorithm for arrays sorted both in ascending
> and descending order.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
> ---
> include/linux/util_macros.h | 39 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 39 insertions(+)
> create mode 100644 include/linux/util_macros.h
>
> diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
> new file mode 100644
> index 0000000..f2097ce
> --- /dev/null
> +++ 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); \

The above variables are not very unique. I've been thinking about all
the variables that are defined in macros, and we may want to establish
some kind of naming convention to keep from having name space
collisions.

Maybe something like...

typeof(as) __fc_i_, __fc_a_ = (as) -1;
typeof(__fc_x_) = (x);
typeof(*a) *__fc_a_ = (a);

-- Steve


> + for (_i = 0; _i < _as; _i++) { \
> + if (_x op DIV_ROUND_CLOSEST(_a[_i] + _a[_i + 1], 2)) \
> + break; \
> + } \
> + (_i); \
> +})
--
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/