X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fzipf.c;h=321a4fb9645e8995e1145758ec75530aeea75a16;hb=HEAD;hp=8b581fafff6e9e0dd8b1423d7e7d0b9e5e2d94de;hpb=8348daf915d4b2c3ec95a16ad803e9e42eb2bfda;p=fio.git diff --git a/lib/zipf.c b/lib/zipf.c index 8b581faf..14d7928f 100644 --- a/lib/zipf.c +++ b/lib/zipf.c @@ -1,12 +1,5 @@ #include #include -#include -#include -#include -#include -#include -#include "ieee754.h" -#include "../log.h" #include "zipf.h" #include "../minmax.h" #include "../hash.h" @@ -15,7 +8,7 @@ static void zipf_update(struct zipf_state *zs) { - unsigned long to_gen; + uint64_t to_gen; unsigned int i; /* @@ -29,20 +22,22 @@ static void zipf_update(struct zipf_state *zs) zs->zetan += pow(1.0 / (double) (i + 1), zs->theta); } -static void shared_rand_init(struct zipf_state *zs, unsigned long nranges, - unsigned int seed) +static void shared_rand_init(struct zipf_state *zs, uint64_t nranges, + double center, unsigned int seed) { memset(zs, 0, sizeof(*zs)); zs->nranges = nranges; init_rand_seed(&zs->rand, seed, 0); zs->rand_off = __rand(&zs->rand); + if (center != -1) + zs->rand_off = nranges * center; } -void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta, - unsigned int seed) +void zipf_init(struct zipf_state *zs, uint64_t nranges, double theta, + double center, unsigned int seed) { - shared_rand_init(zs, nranges, seed); + shared_rand_init(zs, nranges, center, seed); zs->theta = theta; zs->zeta2 = pow(1.0, zs->theta) + pow(0.5, zs->theta); @@ -50,7 +45,7 @@ void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta, zipf_update(zs); } -unsigned long long zipf_next(struct zipf_state *zs) +uint64_t zipf_next(struct zipf_state *zs) { double alpha, eta, rand_uni, rand_z; unsigned long long n = zs->nranges; @@ -69,28 +64,32 @@ unsigned long long zipf_next(struct zipf_state *zs) else val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha)); + val--; + if (!zs->disable_hash) - return (__hash_u64(val - 1) + zs->rand_off) % zs->nranges; + val = __hash_u64(val); - return (val - 1 + zs->rand_off) % zs->nranges; + return (val + zs->rand_off) % zs->nranges; } -void pareto_init(struct zipf_state *zs, unsigned long nranges, double h, - unsigned int seed) +void pareto_init(struct zipf_state *zs, uint64_t nranges, double h, + double center, unsigned int seed) { - shared_rand_init(zs, nranges, seed); + shared_rand_init(zs, nranges, center, seed); zs->pareto_pow = log(h) / log(1.0 - h); } -unsigned long long pareto_next(struct zipf_state *zs) +uint64_t pareto_next(struct zipf_state *zs) { double rand = (double) __rand(&zs->rand) / (double) FRAND32_MAX; - unsigned long long n = zs->nranges - 1; + unsigned long long n; + + n = (zs->nranges - 1) * pow(rand, zs->pareto_pow); if (!zs->disable_hash) - return (__hash_u64(n * pow(rand, zs->pareto_pow)) + zs->rand_off) % zs->nranges; + n = __hash_u64(n); - return (unsigned long long) (n * pow(rand, zs->pareto_pow) + zs->rand_off) % zs->nranges; + return (n + zs->rand_off) % zs->nranges; } void zipf_disable_hash(struct zipf_state *zs)