public inbox for linux-crypto@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/5] math.h: Add avg_array()
@ 2023-12-15 15:24 Lucas Segarra Fernandez
  2023-12-22  4:24 ` Herbert Xu
  0 siblings, 1 reply; 2+ messages in thread
From: Lucas Segarra Fernandez @ 2023-12-15 15:24 UTC (permalink / raw)
  To: herbert
  Cc: linux-kernel, linux-crypto, qat-linux, Lucas Segarra Fernandez,
	Giovanni Cabiddu, Damian Muszynski

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@intel.com>
Reviewed-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Reviewed-by: Damian Muszynski <damian.muszynski@intel.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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-22  4:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-15 15:24 [PATCH 1/5] math.h: Add avg_array() Lucas Segarra Fernandez
2023-12-22  4:24 ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox