1
2 #ifndef _LINUX_AVERAGE_H
3 #define _LINUX_AVERAGE_H
4
5 #include <linux/bug.h>
6 #include <linux/compiler.h>
7 #include <linux/log2.h>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
29 struct ewma_##name { \
30 unsigned long internal; \
31 }; \
32 static inline void ewma_##name##_init(struct ewma_##name *e) \
33 { \
34 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
35 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
36
37
38
39 \
40 BUILD_BUG_ON((_precision) > 30); \
41 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
42 e->internal = 0; \
43 } \
44 static inline unsigned long \
45 ewma_##name##_read(struct ewma_##name *e) \
46 { \
47 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
48 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
49 BUILD_BUG_ON((_precision) > 30); \
50 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
51 return e->internal >> (_precision); \
52 } \
53 static inline void ewma_##name##_add(struct ewma_##name *e, \
54 unsigned long val) \
55 { \
56 unsigned long internal = READ_ONCE(e->internal); \
57 unsigned long weight_rcp = ilog2(_weight_rcp); \
58 unsigned long precision = _precision; \
59 \
60 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
61 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
62 BUILD_BUG_ON((_precision) > 30); \
63 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
64 \
65 WRITE_ONCE(e->internal, internal ? \
66 (((internal << weight_rcp) - internal) + \
67 (val << precision)) >> weight_rcp : \
68 (val << precision)); \
69 }
70
71 #endif