zipf: use 64-bit safe hash for zipf/pareto
authorJens Axboe <axboe@kernel.dk>
Sun, 11 Nov 2012 07:27:24 +0000 (08:27 +0100)
committerJens Axboe <axboe@kernel.dk>
Sun, 11 Nov 2012 07:27:24 +0000 (08:27 +0100)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
hash.h
lib/zipf.c
lib/zipf.h

diff --git a/hash.h b/hash.h
index 93dd8318ffa11dcb4324ab5ec7622880506916fd..13600f4e5e4b3602df1e59636e3d687b1ce0dc82 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -28,6 +28,8 @@
 #error Define GOLDEN_RATIO_PRIME for your wordsize.
 #endif
 
+#define GR_PRIME_64    0x9e37fffffffc0001UL
+
 static inline unsigned long __hash_long(unsigned long val)
 {
        unsigned long hash = val;
@@ -60,6 +62,11 @@ static inline unsigned long hash_long(unsigned long val, unsigned int bits)
        /* High bits are more random, so use them. */
        return __hash_long(val) >> (BITS_PER_LONG - bits);
 }
+
+static inline uint64_t __hash_u64(uint64_t val)
+{
+       return val * GR_PRIME_64;
+}
        
 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
 {
index 41e20554b3d019885ed76c9fbe9b66183c4100cf..9b6ce6334836baa1f0f3c0c238da24f319ad4c3b 100644 (file)
@@ -69,7 +69,7 @@ unsigned long long zipf_next(struct zipf_state *zs)
        else
                val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha));
 
-       return (__hash_long(val - 1) + zs->rand_off) % zs->nranges;
+       return (__hash_u64(val - 1) + zs->rand_off) % zs->nranges;
 }
 
 void pareto_init(struct zipf_state *zs, unsigned long nranges, double h,
@@ -84,5 +84,5 @@ unsigned long long pareto_next(struct zipf_state *zs)
        double rand = (double) __rand(&zs->rand) / (double) FRAND_MAX;
        unsigned long long n = zs->nranges - 1;
 
-       return (__hash_long(n * pow(rand, zs->pareto_pow)) + zs->rand_off) % zs->nranges;
+       return (__hash_u64(n * pow(rand, zs->pareto_pow)) + zs->rand_off) % zs->nranges;
 }
index dbcaffb2453069442b2aa998267f846c2e15b47f..f98ad8182883142e5232000388126534ef3fefe6 100644 (file)
@@ -11,7 +11,7 @@ struct zipf_state {
        double zetan;
        double pareto_pow;
        struct frand_state rand;
-       unsigned long rand_off;
+       uint64_t rand_off;
 };
 
 void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta, unsigned int seed);