Make __rand_0_1() compatible with clang
authorBart Van Assche <bvanassche@acm.org>
Mon, 22 Jun 2020 02:01:00 +0000 (19:01 -0700)
committerBart Van Assche <bvanassche@acm.org>
Mon, 22 Jun 2020 02:19:19 +0000 (19:19 -0700)
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 <bvanassche@acm.org>
lib/rand.h

index 2ccc1b3723c42f4f90ab821bf2824611037d24dc..46c1c5e023a132513ded05d885bd5769dbf7ceb9 100644 (file)
@@ -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));
 }
 
 /*