1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef MEAN_AND_VARIANCE_H_
3 #define MEAN_AND_VARIANCE_H_
5 #include <linux/types.h>
6 #include <linux/limits.h>
7 #include <linux/math.h>
8 #include <linux/math64.h>
10 #define SQRT_U64_MAX 4294967295ULL
13 * u128_u: u128 user mode, because not all architectures support a real int128
17 #ifdef __SIZEOF_INT128__
21 } __aligned(16) u128_u;
23 static inline u128_u u64_to_u128(u64 a)
25 return (u128_u) { .v = a };
28 static inline u64 u128_lo(u128_u a)
33 static inline u64 u128_hi(u128_u a)
38 static inline u128_u u128_add(u128_u a, u128_u b)
44 static inline u128_u u128_sub(u128_u a, u128_u b)
50 static inline u128_u u128_shl(u128_u a, s8 shift)
56 static inline u128_u u128_square(u64 a)
58 u128_u b = u64_to_u128(a);
68 } __aligned(16) u128_u;
72 static inline u128_u u64_to_u128(u64 a)
74 return (u128_u) { .lo = a };
77 static inline u64 u128_lo(u128_u a)
82 static inline u64 u128_hi(u128_u a)
89 static inline u128_u u128_add(u128_u a, u128_u b)
94 c.hi = a.hi + b.hi + (c.lo < a.lo);
98 static inline u128_u u128_sub(u128_u a, u128_u b)
103 c.hi = a.hi - b.hi - (c.lo > a.lo);
107 static inline u128_u u128_shl(u128_u i, s8 shift)
111 r.lo = i.lo << shift;
113 r.hi = (i.hi << shift) | (i.lo >> (64 - shift));
115 r.hi = i.lo << (shift - 64);
121 static inline u128_u u128_square(u64 i)
124 u64 h = i >> 32, l = i & U32_MAX;
126 r = u128_shl(u64_to_u128(h*h), 64);
127 r = u128_add(r, u128_shl(u64_to_u128(h*l), 32));
128 r = u128_add(r, u128_shl(u64_to_u128(l*h), 32));
129 r = u128_add(r, u64_to_u128(l*l));
135 static inline u128_u u64s_to_u128(u64 hi, u64 lo)
137 u128_u c = u64_to_u128(hi);
140 c = u128_add(c, u64_to_u128(lo));
144 u128_u u128_div(u128_u n, u64 d);
146 struct mean_and_variance {
152 /* expontentially weighted variant */
153 struct mean_and_variance_weighted {
155 u8 weight; /* base 2 logarithim */
161 * fast_divpow2() - fast approximation for n / (1 << d)
163 * @d: the power of 2 denominator.
165 * note: this rounds towards 0.
167 static inline s64 fast_divpow2(s64 n, u8 d)
169 return (n + ((n < 0) ? ((1 << d) - 1) : 0)) >> d;
173 * mean_and_variance_update() - update a mean_and_variance struct @s1 with a new sample @v1
175 * @s1: the mean_and_variance to update.
176 * @v1: the new sample.
178 * see linked pdf equation 12.
181 mean_and_variance_update(struct mean_and_variance *s, s64 v)
185 s->sum_squares = u128_add(s->sum_squares, u128_square(abs(v)));
188 s64 mean_and_variance_get_mean(struct mean_and_variance s);
189 u64 mean_and_variance_get_variance(struct mean_and_variance s1);
190 u32 mean_and_variance_get_stddev(struct mean_and_variance s);
192 void mean_and_variance_weighted_update(struct mean_and_variance_weighted *s, s64 v);
194 s64 mean_and_variance_weighted_get_mean(struct mean_and_variance_weighted s);
195 u64 mean_and_variance_weighted_get_variance(struct mean_and_variance_weighted s);
196 u32 mean_and_variance_weighted_get_stddev(struct mean_and_variance_weighted s);
198 #endif // MEAN_AND_VAIRANCE_H_