X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=lib%2Frand.h;h=bff4a35167cbbfc95dc9258c54b5bbec3f1400e4;hb=4717fc5db6601707d6e4fe7ea1e383f1847f5620;hp=cab91585cac9e63fc76dd14d21a507deb179c8c6;hpb=c3546b531f48a2ff413c9508aed465e0145c8dfc;p=fio.git diff --git a/lib/rand.h b/lib/rand.h index cab91585..bff4a351 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -2,6 +2,8 @@ #define FIO_RAND_H #include +#include +#include "types.h" #include "../arch/arch.h" #define FRAND32_MAX (-1U) @@ -23,10 +25,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) @@ -104,12 +102,37 @@ static inline uint64_t __rand(struct frand_state *state) return __rand32(&state->state32); } -extern void init_rand(struct frand_state *, int); -extern void init_rand_seed(struct frand_state *, unsigned int seed, int); +static inline double __rand_0_1(struct frand_state *state) +{ + if (state->use64) { + uint64_t val = __rand64(&state->state64); + + return (val + 1.0) / (FRAND64_MAX + 1.0); + } else { + uint32_t val = __rand32(&state->state32); + + return (val + 1.0) / (FRAND32_MAX + 1.0); + } +} + +/* + * Generate a random value between 'start' and 'end', both inclusive + */ +static inline int rand32_between(struct frand_state *state, int start, int end) +{ + uint32_t r; + + assert(!state->use64); + + r = __rand32(&state->state32); + return start + (int) ((double)end * (r / (FRAND32_MAX + 1.0))); +} + +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, 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 fill_pattern(void *p, unsigned int len, char *pattern, unsigned int pattern_bytes); #endif