lkml.org 
[lkml]   [2021]   [Oct]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[v2 01/10] utils_macro: introduce find_closest_unsorted()
    Date
    This is similar to find_closest() and find_closest_descending(), but, it
    doesn't make any assumption about the array being ordered.

    Signed-off-by: Andrea Merello <andrea.merello@iit.it>
    ---
    include/linux/util_macros.h | 26 ++++++++++++++++++++++++++
    1 file changed, 26 insertions(+)

    diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
    index 72299f261b25..b48f80ceb380 100644
    --- a/include/linux/util_macros.h
    +++ b/include/linux/util_macros.h
    @@ -2,6 +2,8 @@
    #ifndef _LINUX_HELPER_MACROS_H_
    #define _LINUX_HELPER_MACROS_H_

    +#include <linux/math.h>
    +
    #define __find_closest(x, a, as, op) \
    ({ \
    typeof(as) __fc_i, __fc_as = (as) - 1; \
    @@ -38,4 +40,28 @@
    */
    #define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)

    +/**
    + * find_closest_unsorted - locate the closest element in a unsorted array
    + * @x: The reference value.
    + * @a: The array in which to look for the closest element.
    + * @as: Size of 'a'.
    + *
    + * Similar to find_closest() but 'a' has no requirement to being sorted
    + */
    +#define find_closest_unsorted(x, a, as) \
    +({ \
    + typeof(x) __fc_best_delta, __fc_delta; \
    + typeof(as) __fc_i, __fc_best_idx; \
    + bool __fc_first = true; \
    + for (__fc_i = 0; __fc_i < (as); __fc_i++) { \
    + __fc_delta = abs(a[__fc_i] - (x)); \
    + if (__fc_first || __fc_delta < __fc_best_delta) { \
    + __fc_best_delta = __fc_delta; \
    + __fc_best_idx = __fc_i; \
    + } \
    + __fc_first = false; \
    + } \
    + (__fc_best_idx); \
    +})
    +
    #endif
    --
    2.17.1
    \
     
     \ /
      Last update: 2021-10-28 12:19    [W:2.259 / U:0.080 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site