Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
average.c
Go to the documentation of this file.
1 /*
2  * lib/average.c
3  *
4  * This source code is licensed under the GNU General Public License,
5  * Version 2. See the file COPYING for more details.
6  */
7 
8 #include <linux/export.h>
9 #include <linux/average.h>
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/log2.h>
13 
37 void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight)
38 {
39  WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor));
40 
41  avg->weight = ilog2(weight);
42  avg->factor = ilog2(factor);
43  avg->internal = 0;
44 }
46 
54 struct ewma *ewma_add(struct ewma *avg, unsigned long val)
55 {
56  avg->internal = avg->internal ?
57  (((avg->internal << avg->weight) - avg->internal) +
58  (val << avg->factor)) >> avg->weight :
59  (val << avg->factor);
60  return avg;
61 }