~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/average.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  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  * Exponentially weighted moving average (EWMA)
 11  *
 12  * This implements a fixed-precision EWMA algorithm, with both the
 13  * precision and fall-off coefficient determined at compile-time
 14  * and built into the generated helper funtions.
 15  *
 16  * The first argument to the macro is the name that will be used
 17  * for the struct and helper functions.
 18  *
 19  * The second argument, the precision, expresses how many bits are
 20  * used for the fractional part of the fixed-precision values.
 21  *
 22  * The third argument, the weight reciprocal, determines how the
 23  * new values will be weighed vs. the old state, new values will
 24  * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
 25  * that this parameter must be a power of two for efficiency.
 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                  * Even if you want to feed it just 0/1 you should have \
 38                  * some bits for the non-fractional part...             \
 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 /* _LINUX_AVERAGE_H */
 72 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php