average.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef _LINUX_AVERAGE_H
  2. #define _LINUX_AVERAGE_H
  3. /* Exponentially weighted moving average (EWMA) */
  4. #define DECLARE_EWMA(name, _factor, _weight) \
  5. struct ewma_##name { \
  6. unsigned long internal; \
  7. }; \
  8. static inline void ewma_##name##_init(struct ewma_##name *e) \
  9. { \
  10. BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
  11. BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
  12. BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
  13. BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
  14. e->internal = 0; \
  15. } \
  16. static inline unsigned long \
  17. ewma_##name##_read(struct ewma_##name *e) \
  18. { \
  19. BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
  20. BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
  21. BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
  22. BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
  23. return e->internal >> ilog2(_factor); \
  24. } \
  25. static inline void ewma_##name##_add(struct ewma_##name *e, \
  26. unsigned long val) \
  27. { \
  28. unsigned long internal = ACCESS_ONCE(e->internal); \
  29. unsigned long weight = ilog2(_weight); \
  30. unsigned long factor = ilog2(_factor); \
  31. \
  32. BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
  33. BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
  34. BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
  35. BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
  36. \
  37. ACCESS_ONCE(e->internal) = internal ? \
  38. (((internal << weight) - internal) + \
  39. (val << factor)) >> weight : \
  40. (val << factor); \
  41. }
  42. #endif /* _LINUX_AVERAGE_H */