summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-09 07:12:00 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-09 07:12:00 +0000
commit525cb66467ff22a50f2e6bf307924459d38cd592 (patch)
treecdf539d08cb2ac619a0376e7241f2b19eccd06b5 /random.c
parentfdbd3716781817c840544796d04a7d41b856d9f4 (diff)
downloadruby-525cb66467ff22a50f2e6bf307924459d38cd592.tar.gz
siphash
* random.c (rb_memhash): use siphash. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37585 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/random.c b/random.c
index a137551772..196fd14f1e 100644
--- a/random.c
+++ b/random.c
@@ -1326,7 +1326,15 @@ random_s_rand(int argc, VALUE *argv, VALUE obj)
return rand_random(argc, argv, rand_start(&default_rand));
}
+#define SIP_HASH_STREAMING 0
+#define sip_hash24 ruby_sip_hash24
+#include "siphash.c"
+
static st_index_t hashseed;
+static union {
+ uint8_t key[16];
+ uint32_t u32[(16 * sizeof(uint8_t) - 1) / sizeof(uint32_t)];
+} sipseed;
static VALUE
init_randomseed(struct MT *mt, unsigned int initial[DEFAULT_SEED_CNT])
@@ -1346,6 +1354,7 @@ Init_RandomSeed(void)
unsigned int initial[DEFAULT_SEED_CNT];
struct MT *mt = &r->mt;
VALUE seed = init_randomseed(mt, initial);
+ int i;
hashseed = genrand_int32(mt);
#if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
@@ -1361,6 +1370,9 @@ Init_RandomSeed(void)
hashseed |= genrand_int32(mt);
#endif
+ for (i = 0; i < numberof(sipseed.u32); ++i)
+ sipseed.u32[i] = genrand_int32(mt);
+
rb_global_variable(&r->seed);
r->seed = seed;
}
@@ -1371,6 +1383,17 @@ rb_hash_start(st_index_t h)
return st_hash_start(hashseed + h);
}
+st_index_t
+rb_memhash(const void *ptr, long len)
+{
+ sip_uint64_t h = sip_hash24(sipseed.key, ptr, len);
+#ifdef HAVE_UINT64_T
+ return (st_index_t)h;
+#else
+ return (st_index_t)(h.u32[0] ^ h.u32[1]);
+#endif
+}
+
static void
Init_RandomSeed2(void)
{