X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;ds=sidebyside;f=lib%2Frand.h;h=95d4f6d4966d88a60700a09fba7a6487c032abd5;hb=04ba61dfa67784d4dfcc22a2b3de7ede28e22e40;hp=49773b0d9b6bec616744fe7bc91c548f7b80aa42;hpb=e0a04ac15f6164cfceb3e1090cc1055e3c823f7d;p=fio.git diff --git a/lib/rand.h b/lib/rand.h index 49773b0d..95d4f6d4 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -2,7 +2,8 @@ #define FIO_RAND_H #include -#include "../arch/arch.h" +#include +#include "types.h" #define FRAND32_MAX (-1U) #define FRAND64_MAX (-1ULL) @@ -23,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) @@ -117,22 +114,45 @@ 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 *, int); -extern void init_rand_seed(struct frand_state *, unsigned int seed, int); -extern void __fill_random_buf(void *buf, unsigned int len, unsigned long seed); -extern unsigned long fill_random_buf(struct frand_state *, void *buf, unsigned int len); -extern void __fill_random_buf_percentage(unsigned long, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int); -extern unsigned long fill_random_buf_percentage(struct frand_state *, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int); +extern void init_rand(struct frand_state *, bool); +extern void init_rand_seed(struct frand_state *, unsigned int seed, bool); +extern void __fill_random_buf(void *buf, unsigned int len, uint64_t seed); +extern uint64_t fill_random_buf(struct frand_state *, void *buf, unsigned int len); +extern void __fill_random_buf_percentage(uint64_t, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int); +extern uint64_t fill_random_buf_percentage(struct frand_state *, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int); #endif