xref: /openbsd/sys/dev/pci/drm/include/linux/average.h (revision f6aab3d8)
1 /* Public domain. */
2 
3 #ifndef _LINUX_AVERAGE_H
4 #define _LINUX_AVERAGE_H
5 
6 #include <sys/types.h>
7 #include <lib/libkern/libkern.h>
8 
9 #define DECLARE_EWMA(name, precision, recip)			\
10 struct ewma_##name {						\
11 	u_long value;						\
12 };								\
13 								\
14 static inline void						\
15 ewma_##name##_init(struct ewma_##name *p)			\
16 {								\
17 	p->value = 0;						\
18 }								\
19 								\
20 static inline void						\
21 ewma_##name##_add(struct ewma_##name *p, u_long value)		\
22 {								\
23 	u_long shift = fls(recip) - 1;				\
24 								\
25 	if (p->value == 0)					\
26 		p->value = (value << (precision));		\
27 	else							\
28 		p->value = ((((p->value << shift) - p->value) +	\
29 		    (value << (precision))) >> shift);		\
30 }								\
31 								\
32 static inline u_long						\
33 ewma_##name##_read(struct ewma_##name *p)			\
34 {								\
35 	return (p->value >> (precision));			\
36 }
37 
38 #endif
39