X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=lib%2Frand.h;h=1676cf98ed567653cded97eaf00a1beabe808b58;hb=b61a5f46da1283f4544f6ace3f19e64b1cfcc800;hp=24fac23cfec0856b409405e69a313dc7e873be33;hpb=de4096e8682a064ed9125af7ac30a3fe4021167b;p=fio.git diff --git a/lib/rand.h b/lib/rand.h index 24fac23c..1676cf98 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -2,8 +2,8 @@ #define FIO_RAND_H #include +#include #include "types.h" -#include "../arch/arch.h" #define FRAND32_MAX (-1U) #define FRAND64_MAX (-1ULL) @@ -24,10 +24,6 @@ struct frand_state { }; }; -struct frand64_state { - uint64_t s1, s2, s3, s4, s5; -}; - static inline uint64_t rand_max(struct frand_state *state) { if (state->use64) @@ -118,15 +114,38 @@ static inline double __rand_0_1(struct frand_state *state) } } +static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) +{ + uint32_t r; + + assert(!state->use64); + + r = __rand32(&state->state32); + end++; + return (int) ((double)end * (r / (FRAND32_MAX + 1.0))); +} + +static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) +{ + uint64_t r; + + assert(state->use64); + + r = __rand64(&state->state64); + end++; + return (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); +} + /* * Generate a random value between 'start' and 'end', both inclusive */ -static inline int rand_between(struct frand_state *state, int start, int end) +static inline uint64_t rand_between(struct frand_state *state, uint64_t start, + uint64_t end) { - uint64_t r; - - r = __rand(state); - return start + (int) ((double)end * (r / (rand_max(state) + 1.0))); + if (state->use64) + return start + rand64_upto(state, end - start); + else + return start + rand32_upto(state, end - start); } extern void init_rand(struct frand_state *, bool);