#ifndef MEAN_AND_VARIANCE_H_
#define MEAN_AND_VARIANCE_H_
#include <linux/types.h>
#include <linux/limits.h>
#include <linux/math.h>
#include <linux/math64.h>
#define SQRT_U64_MAX …
#if defined(__SIZEOF_INT128__) && defined(__KERNEL__) && !defined(CONFIG_PARISC)
u128_u;
static inline u128_u u64_to_u128(u64 a)
{ … }
static inline u64 u128_lo(u128_u a)
{ … }
static inline u64 u128_hi(u128_u a)
{ … }
static inline u128_u u128_add(u128_u a, u128_u b)
{ … }
static inline u128_u u128_sub(u128_u a, u128_u b)
{ … }
static inline u128_u u128_shl(u128_u a, s8 shift)
{ … }
static inline u128_u u128_square(u64 a)
{ … }
#else
typedef struct {
u64 hi, lo;
} __aligned(16) u128_u;
static inline u128_u u64_to_u128(u64 a)
{
return (u128_u) { .lo = a };
}
static inline u64 u128_lo(u128_u a)
{
return a.lo;
}
static inline u64 u128_hi(u128_u a)
{
return a.hi;
}
static inline u128_u u128_add(u128_u a, u128_u b)
{
u128_u c;
c.lo = a.lo + b.lo;
c.hi = a.hi + b.hi + (c.lo < a.lo);
return c;
}
static inline u128_u u128_sub(u128_u a, u128_u b)
{
u128_u c;
c.lo = a.lo - b.lo;
c.hi = a.hi - b.hi - (c.lo > a.lo);
return c;
}
static inline u128_u u128_shl(u128_u i, s8 shift)
{
u128_u r;
r.lo = i.lo << (shift & 63);
if (shift < 64)
r.hi = (i.hi << (shift & 63)) | (i.lo >> (-shift & 63));
else {
r.hi = i.lo << (-shift & 63);
r.lo = 0;
}
return r;
}
static inline u128_u u128_square(u64 i)
{
u128_u r;
u64 h = i >> 32, l = i & U32_MAX;
r = u128_shl(u64_to_u128(h*h), 64);
r = u128_add(r, u128_shl(u64_to_u128(h*l), 32));
r = u128_add(r, u128_shl(u64_to_u128(l*h), 32));
r = u128_add(r, u64_to_u128(l*l));
return r;
}
#endif
static inline u128_u u64s_to_u128(u64 hi, u64 lo)
{ … }
u128_u u128_div(u128_u n, u64 d);
struct mean_and_variance { … };
struct mean_and_variance_weighted { … };
static inline s64 fast_divpow2(s64 n, u8 d)
{ … }
static inline void
mean_and_variance_update(struct mean_and_variance *s, s64 v)
{ … }
s64 mean_and_variance_get_mean(struct mean_and_variance s);
u64 mean_and_variance_get_variance(struct mean_and_variance s1);
u32 mean_and_variance_get_stddev(struct mean_and_variance s);
void mean_and_variance_weighted_update(struct mean_and_variance_weighted *s,
s64 v, bool initted, u8 weight);
s64 mean_and_variance_weighted_get_mean(struct mean_and_variance_weighted s,
u8 weight);
u64 mean_and_variance_weighted_get_variance(struct mean_and_variance_weighted s,
u8 weight);
u32 mean_and_variance_weighted_get_stddev(struct mean_and_variance_weighted s,
u8 weight);
#endif