[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231215152440.34537-1-lucas.segarra.fernandez@intel.com>
Date: Fri, 15 Dec 2023 16:24:40 +0100
From: Lucas Segarra Fernandez <lucas.segarra.fernandez@...el.com>
To: herbert@...dor.apana.org.au
Cc: linux-kernel@...r.kernel.org,
linux-crypto@...r.kernel.org,
qat-linux@...el.com,
Lucas Segarra Fernandez <lucas.segarra.fernandez@...el.com>,
Giovanni Cabiddu <giovanni.cabiddu@...el.com>,
Damian Muszynski <damian.muszynski@...el.com>
Subject: [PATCH 1/5] math.h: Add avg_array()
Add macro to compute average of values within an array.
This patch is based on earlier work done by Wojciech Ziemba.
Signed-off-by: Lucas Segarra Fernandez <lucas.segarra.fernandez@...el.com>
Reviewed-by: Giovanni Cabiddu <giovanni.cabiddu@...el.com>
Reviewed-by: Damian Muszynski <damian.muszynski@...el.com>
---
include/linux/math.h | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/include/linux/math.h b/include/linux/math.h
index dd4152711de7..012416c92e89 100644
--- a/include/linux/math.h
+++ b/include/linux/math.h
@@ -205,4 +205,37 @@ static inline u32 int_sqrt64(u64 x)
}
#endif
+/**
+ * avg_array() - Return average of values within an array.
+ * @array: Array of values.
+ * @len: Number of elements.
+ *
+ * This algorithm computes average of an array without running into overflow.
+ *
+ * Return: average of values.
+ */
+#define avg_array(array, len) ( \
+{ \
+ typeof(&(array)[0]) _array = (array); \
+ __unqual_scalar_typeof(_array[0]) _x = 0; \
+ __unqual_scalar_typeof(_array[0]) _y = 0; \
+ __unqual_scalar_typeof(_array[0]) _a, _b; \
+ typeof(len) _len = (len); \
+ size_t _i; \
+ \
+ for (_i = 0; _i < _len; _i++) { \
+ _a = _array[_i]; \
+ _b = do_div(_a, _len); \
+ _x += _a; \
+ if (_y >= _len - _b) { \
+ _x++; \
+ _y -= _len - _b; \
+ } else { \
+ _y += _b; \
+ } \
+ } \
+ do_div(_y, _len); \
+ (_x + _y); \
+})
+
#endif /* _LINUX_MATH_H */
--
2.41.0
Powered by blists - more mailing lists