diff options
Diffstat (limited to 'random.c')
-rw-r--r-- | random.c | 42 |
1 files changed, 41 insertions, 1 deletions
@@ -2,7 +2,7 @@ random.c - - $Author: akr $ + $Author: usa $ created at: Fri Dec 24 16:39:21 JST 1993 Copyright (C) 1993-2007 Yukihiro Matsumoto @@ -73,6 +73,9 @@ The original copyright notice follows. #endif #include <math.h> #include <errno.h> +#if defined(HAVE_SYS_TIME_H) +#include <sys/time.h> +#endif #ifdef _WIN32 # if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400 @@ -1255,10 +1258,32 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj) static VALUE random_s_rand(int argc, VALUE *argv, VALUE obj) { + rand_start(&default_rand); return random_rand(argc, argv, rb_Random_DEFAULT); } +#define SIP_HASH_STREAMING 0 +#define sip_hash24 ruby_sip_hash24 +#if !defined _WIN32 && !defined BYTE_ORDER +# ifdef WORDS_BIGENDIAN +# define BYTE_ORDER BIG_ENDIAN +# else +# define BYTE_ORDER LITTLE_ENDIAN +# endif +# ifndef LITTLE_ENDIAN +# define LITTLE_ENDIAN 1234 +# endif +# ifndef BIG_ENDIAN +# define BIG_ENDIAN 4321 +# endif +#endif +#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]) @@ -1278,6 +1303,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 @@ -1293,6 +1319,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; } @@ -1303,6 +1332,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) { |