From af3e7a8f487ae347001d1bd229ab274d1d4ab92d Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sun, 21 Jun 2020 19:01:00 -0700 Subject: Make __rand_0_1() compatible with clang This patch fixes the following clang compiler error: crc/../arch/../lib/rand.h:109:25: error: implicit conversion from 'unsigned long long' to 'double' changes value from 18446744073709551615 to 18446744073709551616 [-Werror,-Wimplicit-int-float-conversion] return (val + 1.0) / (FRAND64_MAX + 1.0); ^~~~~~~~~~~ ~ crc/../arch/../lib/rand.h:9:22: note: expanded from macro 'FRAND64_MAX' #define FRAND64_MAX (-1ULL) ^~~~~ Fixes: e7b240474543 ("Fixups for poisson rate") Signed-off-by: Bart Van Assche --- lib/rand.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/rand.h b/lib/rand.h index 2ccc1b37..46c1c5e0 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -6,7 +6,9 @@ #include "types.h" #define FRAND32_MAX (-1U) +#define FRAND32_MAX_PLUS_ONE (1.0 * (1ULL << 32)) #define FRAND64_MAX (-1ULL) +#define FRAND64_MAX_PLUS_ONE (1.0 * (1ULL << 32) * (1ULL << 32)) struct taus88_state { unsigned int s1, s2, s3; @@ -106,11 +108,11 @@ 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); + return (val + 1.0) / FRAND64_MAX_PLUS_ONE; } else { uint32_t val = __rand32(&state->state32); - return (val + 1.0) / (FRAND32_MAX + 1.0); + return (val + 1.0) / FRAND32_MAX_PLUS_ONE; } } @@ -122,7 +124,7 @@ static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) r = __rand32(&state->state32); end++; - return (int) ((double)end * (r / (FRAND32_MAX + 1.0))); + return (int) ((double)end * (r / FRAND32_MAX_PLUS_ONE)); } static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) @@ -133,7 +135,7 @@ static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) r = __rand64(&state->state64); end++; - return (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); + return (uint64_t) ((double)end * (r / FRAND64_MAX_PLUS_ONE)); } /* -- cgit v1.2.3