summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-08 07:40:03 (GMT)
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-08 07:40:03 (GMT)
commitd35616e69456305c78fd72c35aaf6a1beefc49f1 (patch)
tree38fe2102379f43b0a946d96235db19e07b27a638 /random.c
parent8a1609040e959ffb370c121840cf1fdfb144616a (diff)
* bignum.c (rb_integer_unpack): Don't use rb_funcall if possible.
* random.c: Use uint32_t for elements of seed. (make_seed_value): Use rb_integer_unpack. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c45
1 files changed, 22 insertions, 23 deletions
diff --git a/random.c b/random.c
index 38aee08..03b8235 100644
--- a/random.c
+++ b/random.c
@@ -440,7 +440,7 @@ random_init(int argc, VALUE *argv, VALUE obj)
return obj;
}
-#define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * (int)sizeof(int))
+#define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * (int)sizeof(int32_t))
#if defined(S_ISCHR) && !defined(DOSISH)
# define USE_DEV_URANDOM 1
@@ -449,7 +449,7 @@ random_init(int argc, VALUE *argv, VALUE obj)
#endif
static void
-fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
+fill_random_seed(uint32_t seed[DEFAULT_SEED_CNT])
{
static int n = 0;
struct timeval tv;
@@ -500,28 +500,27 @@ fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
}
static VALUE
-make_seed_value(const void *ptr)
+make_seed_value(const uint32_t *ptr)
{
- const long len = DEFAULT_SEED_LEN/SIZEOF_BDIGITS;
- BDIGIT *digits;
- NEWOBJ_OF(big, struct RBignum, rb_cBignum, T_BIGNUM | (RGENGC_WB_PROTECTED_ARRAY ? FL_WB_PROTECTED : 0));
-
- RBIGNUM_SET_SIGN(big, 1);
- rb_big_resize((VALUE)big, len + 1);
- digits = RBIGNUM_DIGITS(big);
+ VALUE seed;
+ size_t len;
- MEMCPY(digits, ptr, char, DEFAULT_SEED_LEN);
+ if (ptr[DEFAULT_SEED_CNT-1] <= 1) {
+ /* set leading-zero-guard */
+ uint32_t buf[DEFAULT_SEED_CNT+1];
+ MEMCPY(buf, ptr, uint32_t, DEFAULT_SEED_CNT);
+ buf[DEFAULT_SEED_CNT] = 1;
+ ptr = buf;
+ len = DEFAULT_SEED_CNT+1;
+ }
+ else {
+ len = DEFAULT_SEED_CNT;
+ }
- /* set leading-zero-guard if need. */
- digits[len] =
-#if SIZEOF_INT32 / SIZEOF_BDIGITS > 1
- digits[len-2] <= 1 && digits[len-1] == 0
-#else
- digits[len-1] <= 1
-#endif
- ? 1 : 0;
+ seed = rb_integer_unpack(+1, ptr, DEFAULT_SEED_CNT, sizeof(uint32_t), 0,
+ INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
- return rb_big_norm((VALUE)big);
+ return seed;
}
/*
@@ -535,7 +534,7 @@ make_seed_value(const void *ptr)
static VALUE
random_seed(void)
{
- unsigned int buf[DEFAULT_SEED_CNT];
+ uint32_t buf[DEFAULT_SEED_CNT];
fill_random_seed(buf);
return make_seed_value(buf);
}
@@ -1271,7 +1270,7 @@ static union {
} sipseed;
static VALUE
-init_randomseed(struct MT *mt, unsigned int initial[DEFAULT_SEED_CNT])
+init_randomseed(struct MT *mt, uint32_t initial[DEFAULT_SEED_CNT])
{
VALUE seed;
fill_random_seed(initial);
@@ -1285,7 +1284,7 @@ void
Init_RandomSeed(void)
{
rb_random_t *r = &default_rand;
- unsigned int initial[DEFAULT_SEED_CNT];
+ uint32_t initial[DEFAULT_SEED_CNT];
struct MT *mt = &r->mt;
VALUE seed = init_randomseed(mt, initial);
int i;