xref: /OK3568_Linux_fs/kernel/include/linux/average.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_AVERAGE_H
3*4882a593Smuzhiyun #define _LINUX_AVERAGE_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/bug.h>
6*4882a593Smuzhiyun #include <linux/compiler.h>
7*4882a593Smuzhiyun #include <linux/log2.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Exponentially weighted moving average (EWMA)
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This implements a fixed-precision EWMA algorithm, with both the
13*4882a593Smuzhiyun  * precision and fall-off coefficient determined at compile-time
14*4882a593Smuzhiyun  * and built into the generated helper funtions.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * The first argument to the macro is the name that will be used
17*4882a593Smuzhiyun  * for the struct and helper functions.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * The second argument, the precision, expresses how many bits are
20*4882a593Smuzhiyun  * used for the fractional part of the fixed-precision values.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * The third argument, the weight reciprocal, determines how the
23*4882a593Smuzhiyun  * new values will be weighed vs. the old state, new values will
24*4882a593Smuzhiyun  * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
25*4882a593Smuzhiyun  * that this parameter must be a power of two for efficiency.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define DECLARE_EWMA(name, _precision, _weight_rcp)			\
29*4882a593Smuzhiyun 	struct ewma_##name {						\
30*4882a593Smuzhiyun 		unsigned long internal;					\
31*4882a593Smuzhiyun 	};								\
32*4882a593Smuzhiyun 	static inline void ewma_##name##_init(struct ewma_##name *e)	\
33*4882a593Smuzhiyun 	{								\
34*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_precision));	\
35*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp));	\
36*4882a593Smuzhiyun 		/*							\
37*4882a593Smuzhiyun 		 * Even if you want to feed it just 0/1 you should have	\
38*4882a593Smuzhiyun 		 * some bits for the non-fractional part...		\
39*4882a593Smuzhiyun 		 */							\
40*4882a593Smuzhiyun 		BUILD_BUG_ON((_precision) > 30);			\
41*4882a593Smuzhiyun 		BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp);		\
42*4882a593Smuzhiyun 		e->internal = 0;					\
43*4882a593Smuzhiyun 	}								\
44*4882a593Smuzhiyun 	static inline unsigned long					\
45*4882a593Smuzhiyun 	ewma_##name##_read(struct ewma_##name *e)			\
46*4882a593Smuzhiyun 	{								\
47*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_precision));	\
48*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp));	\
49*4882a593Smuzhiyun 		BUILD_BUG_ON((_precision) > 30);			\
50*4882a593Smuzhiyun 		BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp);		\
51*4882a593Smuzhiyun 		return e->internal >> (_precision);			\
52*4882a593Smuzhiyun 	}								\
53*4882a593Smuzhiyun 	static inline void ewma_##name##_add(struct ewma_##name *e,	\
54*4882a593Smuzhiyun 					     unsigned long val)		\
55*4882a593Smuzhiyun 	{								\
56*4882a593Smuzhiyun 		unsigned long internal = READ_ONCE(e->internal);	\
57*4882a593Smuzhiyun 		unsigned long weight_rcp = ilog2(_weight_rcp);		\
58*4882a593Smuzhiyun 		unsigned long precision = _precision;			\
59*4882a593Smuzhiyun 									\
60*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_precision));	\
61*4882a593Smuzhiyun 		BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp));	\
62*4882a593Smuzhiyun 		BUILD_BUG_ON((_precision) > 30);			\
63*4882a593Smuzhiyun 		BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp);		\
64*4882a593Smuzhiyun 									\
65*4882a593Smuzhiyun 		WRITE_ONCE(e->internal, internal ?			\
66*4882a593Smuzhiyun 			(((internal << weight_rcp) - internal) +	\
67*4882a593Smuzhiyun 				(val << precision)) >> weight_rcp :	\
68*4882a593Smuzhiyun 			(val << precision));				\
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #endif /* _LINUX_AVERAGE_H */
72