prob_distr: Implement type-safe downcasting functions.

This commit is contained in:
George Kadianakis 2019-03-05 17:49:44 +02:00
parent 58fd864a85
commit 8d9f81bc9c

View File

@ -46,26 +46,27 @@
#include "lib/crypt_ops/crypto_rand.h" #include "lib/crypt_ops/crypto_rand.h"
#include "lib/cc/ctassert.h" #include "lib/cc/ctassert.h"
#include "lib/log/util_bug.h"
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <stddef.h> #include <stddef.h>
/** Validators for downcasting macros below */ /** Declare a function that downcasts from a generic dist struct to the actual
#define validate_container_of(PTR, TYPE, FIELD) \ * subtype probablity distribution it represents. */
(0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \ #define DECLARE_PROB_DISTR_DOWNCAST_FN(name) \
offsetof(TYPE, FIELD)))->FIELD)) static inline \
#define validate_const_container_of(PTR, TYPE, FIELD) \ const struct name * \
(0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) - \ dist_to_const_##name(const struct dist *obj) { \
offsetof(TYPE, FIELD)))->FIELD)) tor_assert(obj->ops == &name##_ops); \
/** Downcasting macro */ return SUBTYPE_P(obj, struct name, base); \
#define container_of(PTR, TYPE, FIELD) \ }
((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \ DECLARE_PROB_DISTR_DOWNCAST_FN(uniform)
+ validate_container_of(PTR, TYPE, FIELD)) DECLARE_PROB_DISTR_DOWNCAST_FN(geometric)
/** Constified downcasting macro */ DECLARE_PROB_DISTR_DOWNCAST_FN(logistic)
#define const_container_of(PTR, TYPE, FIELD) \ DECLARE_PROB_DISTR_DOWNCAST_FN(log_logistic)
((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD)) \ DECLARE_PROB_DISTR_DOWNCAST_FN(genpareto)
+ validate_const_container_of(PTR, TYPE, FIELD)) DECLARE_PROB_DISTR_DOWNCAST_FN(weibull)
/** /**
* Count number of one bits in 32-bit word. * Count number of one bits in 32-bit word.
@ -1360,8 +1361,7 @@ dist_isf(const struct dist *dist, double p)
static double static double
uniform_sample(const struct dist *dist) uniform_sample(const struct dist *dist)
{ {
const struct uniform *U = const_container_of(dist, struct uniform, const struct uniform *U = dist_to_const_uniform(dist);
base);
double p0 = random_uniform_01(); double p0 = random_uniform_01();
return sample_uniform_interval(p0, U->a, U->b); return sample_uniform_interval(p0, U->a, U->b);
@ -1370,9 +1370,7 @@ uniform_sample(const struct dist *dist)
static double static double
uniform_cdf(const struct dist *dist, double x) uniform_cdf(const struct dist *dist, double x)
{ {
const struct uniform *U = const_container_of(dist, struct uniform, const struct uniform *U = dist_to_const_uniform(dist);
base);
if (x < U->a) if (x < U->a)
return 0; return 0;
else if (x < U->b) else if (x < U->b)
@ -1384,8 +1382,7 @@ uniform_cdf(const struct dist *dist, double x)
static double static double
uniform_sf(const struct dist *dist, double x) uniform_sf(const struct dist *dist, double x)
{ {
const struct uniform *U = const_container_of(dist, struct uniform, const struct uniform *U = dist_to_const_uniform(dist);
base);
if (x > U->b) if (x > U->b)
return 0; return 0;
@ -1398,8 +1395,7 @@ uniform_sf(const struct dist *dist, double x)
static double static double
uniform_icdf(const struct dist *dist, double p) uniform_icdf(const struct dist *dist, double p)
{ {
const struct uniform *U = const_container_of(dist, struct uniform, const struct uniform *U = dist_to_const_uniform(dist);
base);
double w = U->b - U->a; double w = U->b - U->a;
return (p < 0.5 ? (U->a + w*p) : (U->b - w*(1 - p))); return (p < 0.5 ? (U->a + w*p) : (U->b - w*(1 - p)));
@ -1408,8 +1404,7 @@ uniform_icdf(const struct dist *dist, double p)
static double static double
uniform_isf(const struct dist *dist, double p) uniform_isf(const struct dist *dist, double p)
{ {
const struct uniform *U = const_container_of(dist, struct uniform, const struct uniform *U = dist_to_const_uniform(dist);
base);
double w = U->b - U->a; double w = U->b - U->a;
return (p < 0.5 ? (U->b - w*p) : (U->a + w*(1 - p))); return (p < 0.5 ? (U->b - w*p) : (U->a + w*(1 - p)));
@ -1429,8 +1424,7 @@ const struct dist_ops uniform_ops = {
static double static double
logistic_sample(const struct dist *dist) logistic_sample(const struct dist *dist)
{ {
const struct logistic *L = const_container_of(dist, struct logistic, const struct logistic *L = dist_to_const_logistic(dist);
base);
uint32_t s = crypto_rand_u32(); uint32_t s = crypto_rand_u32();
double t = random_uniform_01(); double t = random_uniform_01();
double p0 = random_uniform_01(); double p0 = random_uniform_01();
@ -1441,36 +1435,28 @@ logistic_sample(const struct dist *dist)
static double static double
logistic_cdf(const struct dist *dist, double x) logistic_cdf(const struct dist *dist, double x)
{ {
const struct logistic *L = const_container_of(dist, struct logistic, const struct logistic *L = dist_to_const_logistic(dist);
base);
return cdf_logistic(x, L->mu, L->sigma); return cdf_logistic(x, L->mu, L->sigma);
} }
static double static double
logistic_sf(const struct dist *dist, double x) logistic_sf(const struct dist *dist, double x)
{ {
const struct logistic *L = const_container_of(dist, struct logistic, const struct logistic *L = dist_to_const_logistic(dist);
base);
return sf_logistic(x, L->mu, L->sigma); return sf_logistic(x, L->mu, L->sigma);
} }
static double static double
logistic_icdf(const struct dist *dist, double p) logistic_icdf(const struct dist *dist, double p)
{ {
const struct logistic *L = const_container_of(dist, struct logistic, const struct logistic *L = dist_to_const_logistic(dist);
base);
return icdf_logistic(p, L->mu, L->sigma); return icdf_logistic(p, L->mu, L->sigma);
} }
static double static double
logistic_isf(const struct dist *dist, double p) logistic_isf(const struct dist *dist, double p)
{ {
const struct logistic *L = const_container_of(dist, struct logistic, const struct logistic *L = dist_to_const_logistic(dist);
base);
return isf_logistic(p, L->mu, L->sigma); return isf_logistic(p, L->mu, L->sigma);
} }
@ -1488,8 +1474,7 @@ const struct dist_ops logistic_ops = {
static double static double
log_logistic_sample(const struct dist *dist) log_logistic_sample(const struct dist *dist)
{ {
const struct log_logistic *LL = const_container_of(dist, struct const struct log_logistic *LL = dist_to_const_log_logistic(dist);
log_logistic, base);
uint32_t s = crypto_rand_u32(); uint32_t s = crypto_rand_u32();
double p0 = random_uniform_01(); double p0 = random_uniform_01();
@ -1499,36 +1484,28 @@ log_logistic_sample(const struct dist *dist)
static double static double
log_logistic_cdf(const struct dist *dist, double x) log_logistic_cdf(const struct dist *dist, double x)
{ {
const struct log_logistic *LL = const_container_of(dist, const struct log_logistic *LL = dist_to_const_log_logistic(dist);
struct log_logistic, base);
return cdf_log_logistic(x, LL->alpha, LL->beta); return cdf_log_logistic(x, LL->alpha, LL->beta);
} }
static double static double
log_logistic_sf(const struct dist *dist, double x) log_logistic_sf(const struct dist *dist, double x)
{ {
const struct log_logistic *LL = const_container_of(dist, const struct log_logistic *LL = dist_to_const_log_logistic(dist);
struct log_logistic, base);
return sf_log_logistic(x, LL->alpha, LL->beta); return sf_log_logistic(x, LL->alpha, LL->beta);
} }
static double static double
log_logistic_icdf(const struct dist *dist, double p) log_logistic_icdf(const struct dist *dist, double p)
{ {
const struct log_logistic *LL = const_container_of(dist, const struct log_logistic *LL = dist_to_const_log_logistic(dist);
struct log_logistic, base);
return icdf_log_logistic(p, LL->alpha, LL->beta); return icdf_log_logistic(p, LL->alpha, LL->beta);
} }
static double static double
log_logistic_isf(const struct dist *dist, double p) log_logistic_isf(const struct dist *dist, double p)
{ {
const struct log_logistic *LL = const_container_of(dist, const struct log_logistic *LL = dist_to_const_log_logistic(dist);
struct log_logistic, base);
return isf_log_logistic(p, LL->alpha, LL->beta); return isf_log_logistic(p, LL->alpha, LL->beta);
} }
@ -1546,8 +1523,7 @@ const struct dist_ops log_logistic_ops = {
static double static double
weibull_sample(const struct dist *dist) weibull_sample(const struct dist *dist)
{ {
const struct weibull *W = const_container_of(dist, struct weibull, const struct weibull *W = dist_to_const_weibull(dist);
base);
uint32_t s = crypto_rand_u32(); uint32_t s = crypto_rand_u32();
double p0 = random_uniform_01(); double p0 = random_uniform_01();
@ -1557,36 +1533,28 @@ weibull_sample(const struct dist *dist)
static double static double
weibull_cdf(const struct dist *dist, double x) weibull_cdf(const struct dist *dist, double x)
{ {
const struct weibull *W = const_container_of(dist, struct weibull, const struct weibull *W = dist_to_const_weibull(dist);
base);
return cdf_weibull(x, W->lambda, W->k); return cdf_weibull(x, W->lambda, W->k);
} }
static double static double
weibull_sf(const struct dist *dist, double x) weibull_sf(const struct dist *dist, double x)
{ {
const struct weibull *W = const_container_of(dist, struct weibull, const struct weibull *W = dist_to_const_weibull(dist);
base);
return sf_weibull(x, W->lambda, W->k); return sf_weibull(x, W->lambda, W->k);
} }
static double static double
weibull_icdf(const struct dist *dist, double p) weibull_icdf(const struct dist *dist, double p)
{ {
const struct weibull *W = const_container_of(dist, struct weibull, const struct weibull *W = dist_to_const_weibull(dist);
base);
return icdf_weibull(p, W->lambda, W->k); return icdf_weibull(p, W->lambda, W->k);
} }
static double static double
weibull_isf(const struct dist *dist, double p) weibull_isf(const struct dist *dist, double p)
{ {
const struct weibull *W = const_container_of(dist, struct weibull, const struct weibull *W = dist_to_const_weibull(dist);
base);
return isf_weibull(p, W->lambda, W->k); return isf_weibull(p, W->lambda, W->k);
} }
@ -1604,8 +1572,7 @@ const struct dist_ops weibull_ops = {
static double static double
genpareto_sample(const struct dist *dist) genpareto_sample(const struct dist *dist)
{ {
const struct genpareto *GP = const_container_of(dist, struct genpareto, const struct genpareto *GP = dist_to_const_genpareto(dist);
base);
uint32_t s = crypto_rand_u32(); uint32_t s = crypto_rand_u32();
double p0 = random_uniform_01(); double p0 = random_uniform_01();
@ -1615,36 +1582,28 @@ genpareto_sample(const struct dist *dist)
static double static double
genpareto_cdf(const struct dist *dist, double x) genpareto_cdf(const struct dist *dist, double x)
{ {
const struct genpareto *GP = const_container_of(dist, struct genpareto, const struct genpareto *GP = dist_to_const_genpareto(dist);
base);
return cdf_genpareto(x, GP->mu, GP->sigma, GP->xi); return cdf_genpareto(x, GP->mu, GP->sigma, GP->xi);
} }
static double static double
genpareto_sf(const struct dist *dist, double x) genpareto_sf(const struct dist *dist, double x)
{ {
const struct genpareto *GP = const_container_of(dist, struct genpareto, const struct genpareto *GP = dist_to_const_genpareto(dist);
base);
return sf_genpareto(x, GP->mu, GP->sigma, GP->xi); return sf_genpareto(x, GP->mu, GP->sigma, GP->xi);
} }
static double static double
genpareto_icdf(const struct dist *dist, double p) genpareto_icdf(const struct dist *dist, double p)
{ {
const struct genpareto *GP = const_container_of(dist, struct genpareto, const struct genpareto *GP = dist_to_const_genpareto(dist);
base);
return icdf_genpareto(p, GP->mu, GP->sigma, GP->xi); return icdf_genpareto(p, GP->mu, GP->sigma, GP->xi);
} }
static double static double
genpareto_isf(const struct dist *dist, double p) genpareto_isf(const struct dist *dist, double p)
{ {
const struct genpareto *GP = const_container_of(dist, struct genpareto, const struct genpareto *GP = dist_to_const_genpareto(dist);
base);
return isf_genpareto(p, GP->mu, GP->sigma, GP->xi); return isf_genpareto(p, GP->mu, GP->sigma, GP->xi);
} }
@ -1662,7 +1621,7 @@ const struct dist_ops genpareto_ops = {
static double static double
geometric_sample(const struct dist *dist) geometric_sample(const struct dist *dist)
{ {
const struct geometric *G = const_container_of(dist, struct geometric, base); const struct geometric *G = dist_to_const_geometric(dist);
uint32_t s = crypto_rand_u32(); uint32_t s = crypto_rand_u32();
double p0 = random_uniform_01(); double p0 = random_uniform_01();
@ -1672,7 +1631,7 @@ geometric_sample(const struct dist *dist)
static double static double
geometric_cdf(const struct dist *dist, double x) geometric_cdf(const struct dist *dist, double x)
{ {
const struct geometric *G = const_container_of(dist, struct geometric, base); const struct geometric *G = dist_to_const_geometric(dist);
if (x < 1) if (x < 1)
return 0; return 0;
@ -1683,7 +1642,7 @@ geometric_cdf(const struct dist *dist, double x)
static double static double
geometric_sf(const struct dist *dist, double x) geometric_sf(const struct dist *dist, double x)
{ {
const struct geometric *G = const_container_of(dist, struct geometric, base); const struct geometric *G = dist_to_const_geometric(dist);
if (x < 1) if (x < 1)
return 0; return 0;
@ -1694,7 +1653,7 @@ geometric_sf(const struct dist *dist, double x)
static double static double
geometric_icdf(const struct dist *dist, double p) geometric_icdf(const struct dist *dist, double p)
{ {
const struct geometric *G = const_container_of(dist, struct geometric, base); const struct geometric *G = dist_to_const_geometric(dist);
return log1p(-p)/log1p(-G->p); return log1p(-p)/log1p(-G->p);
} }
@ -1702,7 +1661,7 @@ geometric_icdf(const struct dist *dist, double p)
static double static double
geometric_isf(const struct dist *dist, double p) geometric_isf(const struct dist *dist, double p)
{ {
const struct geometric *G = const_container_of(dist, struct geometric, base); const struct geometric *G = dist_to_const_geometric(dist);
return log(p)/log1p(-G->p); return log(p)/log1p(-G->p);
} }