mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
8a1609040e
commit
d35616e694
3 changed files with 54 additions and 35 deletions
|
@ -1,3 +1,10 @@
|
|||
Sat Jun 8 16:38:02 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* 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.
|
||||
|
||||
Sat Jun 8 15:58:18 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* random.c (rand_init): Add a cast to fix clang compile error:
|
||||
|
|
37
bignum.c
37
bignum.c
|
@ -852,7 +852,6 @@ integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in
|
|||
VALUE
|
||||
rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
|
||||
{
|
||||
VALUE num_bits, num_bdigits;
|
||||
VALUE result;
|
||||
const unsigned char *buf = words;
|
||||
|
||||
|
@ -877,20 +876,34 @@ rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize,
|
|||
if (sign != 1 && sign != 0 && sign != -1)
|
||||
rb_raise(rb_eArgError, "unexpected sign: %d", sign);
|
||||
|
||||
/* num_bits = (wordsize * CHAR_BIT - nails) * count */
|
||||
num_bits = SIZET2NUM(wordsize);
|
||||
num_bits = rb_funcall(num_bits, '*', 1, LONG2FIX(CHAR_BIT));
|
||||
num_bits = rb_funcall(num_bits, '-', 1, SIZET2NUM(nails));
|
||||
num_bits = rb_funcall(num_bits, '*', 1, SIZET2NUM(numwords));
|
||||
if (numwords <= (SIZE_MAX - (SIZEOF_BDIGITS*CHAR_BIT-1)) / CHAR_BIT / wordsize) {
|
||||
size_t num_bits, num_bdigits;
|
||||
num_bits = (wordsize * CHAR_BIT - nails) * numwords;
|
||||
if (num_bits == 0)
|
||||
return LONG2FIX(0);
|
||||
num_bdigits = (num_bits + SIZEOF_BDIGITS*CHAR_BIT - 1) / (SIZEOF_BDIGITS*CHAR_BIT);
|
||||
if (LONG_MAX < num_bdigits)
|
||||
rb_raise(rb_eArgError, "too big to unpack as an integer");
|
||||
result = bignew((long)num_bdigits, 0 <= sign);
|
||||
}
|
||||
else {
|
||||
VALUE num_bits, num_bdigits;
|
||||
|
||||
if (num_bits == LONG2FIX(0))
|
||||
return LONG2FIX(0);
|
||||
/* num_bits = (wordsize * CHAR_BIT - nails) * numwords */
|
||||
num_bits = SIZET2NUM(wordsize);
|
||||
num_bits = rb_funcall(num_bits, '*', 1, LONG2FIX(CHAR_BIT));
|
||||
num_bits = rb_funcall(num_bits, '-', 1, SIZET2NUM(nails));
|
||||
num_bits = rb_funcall(num_bits, '*', 1, SIZET2NUM(numwords));
|
||||
|
||||
/* num_bdigits = (num_bits + SIZEOF_BDIGITS*CHAR_BIT - 1) / (SIZEOF_BDIGITS*CHAR_BIT) */
|
||||
num_bdigits = rb_funcall(num_bits, '+', 1, LONG2FIX(SIZEOF_BDIGITS*CHAR_BIT-1));
|
||||
num_bdigits = rb_funcall(num_bdigits, '/', 1, LONG2FIX(SIZEOF_BDIGITS*CHAR_BIT));
|
||||
if (num_bits == LONG2FIX(0))
|
||||
return LONG2FIX(0);
|
||||
|
||||
result = bignew(NUM2LONG(num_bdigits), 0 <= sign);
|
||||
/* num_bdigits = (num_bits + SIZEOF_BDIGITS*CHAR_BIT - 1) / (SIZEOF_BDIGITS*CHAR_BIT) */
|
||||
num_bdigits = rb_funcall(num_bits, '+', 1, LONG2FIX(SIZEOF_BDIGITS*CHAR_BIT-1));
|
||||
num_bdigits = rb_funcall(num_bdigits, '/', 1, LONG2FIX(SIZEOF_BDIGITS*CHAR_BIT));
|
||||
|
||||
result = bignew(NUM2LONG(num_bdigits), 0 <= sign);
|
||||
}
|
||||
|
||||
dp = BDIGITS(result);
|
||||
de = dp + RBIGNUM_LEN(result);
|
||||
|
|
45
random.c
45
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));
|
||||
VALUE seed;
|
||||
size_t len;
|
||||
|
||||
RBIGNUM_SET_SIGN(big, 1);
|
||||
rb_big_resize((VALUE)big, len + 1);
|
||||
digits = RBIGNUM_DIGITS(big);
|
||||
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;
|
||||
}
|
||||
|
||||
MEMCPY(digits, ptr, char, DEFAULT_SEED_LEN);
|
||||
seed = rb_integer_unpack(+1, ptr, DEFAULT_SEED_CNT, sizeof(uint32_t), 0,
|
||||
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||
|
||||
/* 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;
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue