[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 8 Aug 2022 08:34:58 +0100
From: Yassine Oudjana <yassine.oudjana@...il.com>
To: Sebastian Reichel <sre@...nel.org>,
Rob Herring <robh+dt@...nel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@...aro.org>,
Andy Gross <agross@...nel.org>,
Bjorn Andersson <bjorn.andersson@...aro.org>
Cc: Yassine Oudjana <y.oudjana@...tonmail.com>,
Yassine Oudjana <yassine.oudjana@...il.com>,
Alejandro Tafalla <atafalla@...on.com>,
Konrad Dybcio <konrad.dybcio@...ainline.org>,
linux-pm@...r.kernel.org, linux-arm-msm@...r.kernel.org,
devicetree@...r.kernel.org, phone-devel@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH 7/8] util_macros.h: Add macro to find closest smaller value in array
From: Yassine Oudjana <y.oudjana@...tonmail.com>
Add a macro to find the value closest to but smaller than a given
value in an array.
Signed-off-by: Yassine Oudjana <y.oudjana@...tonmail.com>
---
include/linux/util_macros.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
index 72299f261b25..ad0020e7932b 100644
--- a/include/linux/util_macros.h
+++ b/include/linux/util_macros.h
@@ -38,4 +38,26 @@
*/
#define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)
+/**
+ * find_closest_smaller - locate the closest smaller element in a sorted array
+ * @x: The reference value.
+ * @a: The array in which to look for the closest smaller element. Must be
+ * sorted in ascending order.
+ * @as: Size of 'a'.
+ *
+ * Returns the index of the element closest to and smaller than 'x', or -1
+ * if no element smaller than 'x' exists in the array.
+ */
+#define find_closest_smaller(x, a, as) \
+({ \
+ typeof(as) __fcs_i; \
+ typeof(x) __fcs_x = (x); \
+ typeof(*a) const *__fcs_a = (a); \
+ for (__fcs_i = 0; __fcs_i < (as); __fcs_i++) { \
+ if (__fcs_x < __fcs_a[__fcs_i]) \
+ break; \
+ } \
+ (__fcs_i - 1); \
+})
+
#endif
--
2.37.1
Powered by blists - more mailing lists