1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* random.c (limited_big_rand): didn't work if SIZEOF_BDIGITS == 2.

[ruby-dev:25408]

* random.c (random_seed): refined.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7730 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2005-01-05 11:31:41 +00:00
parent 09736c1621
commit 8728b3fde4
2 changed files with 26 additions and 15 deletions

View file

@ -1,3 +1,10 @@
Wed Jan 5 20:16:32 2005 Tanaka Akira <akr@m17n.org>
* random.c (limited_big_rand): didn't work if SIZEOF_BDIGITS == 2.
[ruby-dev:25408]
* random.c (random_seed): refined.
Wed Jan 5 16:39:54 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp> Wed Jan 5 16:39:54 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
* parse.y (BITSTACK_POP): workaround for bcc32 compiler's bug. * parse.y (BITSTACK_POP): workaround for bcc32 compiler's bug.

View file

@ -215,7 +215,7 @@ rand_init(vseed)
rb_raise(rb_eTypeError, "failed to convert %s into Integer", rb_raise(rb_eTypeError, "failed to convert %s into Integer",
rb_obj_classname(vseed)); rb_obj_classname(vseed));
} }
len = ((len + 3) / 4); /* round up to 32bits */ len = (len + 3) / 4; /* number of 32bit words */
buf = ALLOCA_N(long, len); /* allocate longs for init_by_array */ buf = ALLOCA_N(long, len); /* allocate longs for init_by_array */
memset(buf, 0, len * sizeof(long)); memset(buf, 0, len * sizeof(long));
if (FIXNUM_P(seed)) { if (FIXNUM_P(seed)) {
@ -228,8 +228,8 @@ rand_init(vseed)
int i, j; int i, j;
for (i = RBIGNUM(seed)->len-1; 0 <= i; i--) { for (i = RBIGNUM(seed)->len-1; 0 <= i; i--) {
j = i * SIZEOF_BDIGITS / 4; j = i * SIZEOF_BDIGITS / 4;
#if SIZEOF_BDIGITS < SIZEOF_LONG #if SIZEOF_BDIGITS < 4
buf[j] = buf[j] << (SIZEOF_BDIGITS * 8); buf[j] <<= SIZEOF_BDIGITS * 8;
#endif #endif
buf[j] |= ((BDIGIT *)RBIGNUM(seed)->digits)[i]; buf[j] |= ((BDIGIT *)RBIGNUM(seed)->digits)[i];
} }
@ -259,23 +259,21 @@ random_seed()
struct timeval tv; struct timeval tv;
int fd; int fd;
struct stat statbuf; struct stat statbuf;
int seed_len;
BDIGIT *digits; BDIGIT *digits;
unsigned long *seed; unsigned long *seed;
NEWOBJ(big, struct RBignum); NEWOBJ(big, struct RBignum);
OBJSETUP(big, rb_cBignum, T_BIGNUM); OBJSETUP(big, rb_cBignum, T_BIGNUM);
seed_len = 4 * sizeof(long);
big->sign = 1; big->sign = 1;
big->len = sizeof(long) * 8 / SIZEOF_BDIGITS + 1; big->len = seed_len / SIZEOF_BDIGITS + 1;
digits = big->digits = ALLOC_N(BDIGIT, big->len); digits = big->digits = ALLOC_N(BDIGIT, big->len);
seed = (unsigned long *)big->digits; seed = (unsigned long *)big->digits;
memset(digits, 0, big->len * SIZEOF_BDIGITS); memset(digits, 0, big->len * SIZEOF_BDIGITS);
gettimeofday(&tv, 0);
seed[0] = tv.tv_usec;
seed[1] = tv.tv_sec;
seed[2] = getpid() ^ (n++ << 16);
seed[3] = (unsigned long)&seed;
#ifdef S_ISCHR #ifdef S_ISCHR
if ((fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK if ((fd = open("/dev/urandom", O_RDONLY|O_NONBLOCK
#ifdef O_NOCTTY #ifdef O_NOCTTY
@ -286,12 +284,18 @@ random_seed()
#endif #endif
)) >= 0) { )) >= 0) {
if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) { if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
read(fd, &seed[4], 4 * sizeof(*seed)); read(fd, seed, seed_len);
} }
close(fd); close(fd);
} }
#endif #endif
gettimeofday(&tv, 0);
seed[0] ^= tv.tv_usec;
seed[1] ^= tv.tv_sec;
seed[2] ^= getpid() ^ (n++ << 16);
seed[3] ^= (unsigned long)&seed;
/* set leading-zero-guard if need. */ /* set leading-zero-guard if need. */
digits[big->len-1] = digits[big->len-2] <= 1 ? 1 : 0; digits[big->len-1] = digits[big->len-2] <= 1 ? 1 : 0;
@ -374,11 +378,11 @@ limited_big_rand(struct RBignum *limit)
val = (struct RBignum *)rb_big_clone((VALUE)limit); val = (struct RBignum *)rb_big_clone((VALUE)limit);
val->sign = 1; val->sign = 1;
#if SIZEOF_BDIGITS == 2 #if SIZEOF_BDIGITS == 2
# define BIG_GET32(big,i) (((BDIGIT *)(big)->digits)[(i)/2] | \ # define BIG_GET32(big,i) (((BDIGIT *)(big)->digits)[(i)*2] | \
((i)/2+1 < (big)->len ? (((BDIGIT *)(big)->digits)[(i)/2+1] << 16) ((i)*2+1 < (big)->len ? (((BDIGIT *)(big)->digits)[(i)*2+1] << 16) \
: 0)) : 0))
# define BIG_SET32(big,i,d) ((((BDIGIT *)(big)->digits)[(i)/2] = (d) & 0xffff), \ # define BIG_SET32(big,i,d) ((((BDIGIT *)(big)->digits)[(i)*2] = (d) & 0xffff), \
((i)/2+1 < (big)->len ? (((BDIGIT *)(big)->digits)[(i)/2+1] = (d) >> 16) \ ((i)*2+1 < (big)->len ? (((BDIGIT *)(big)->digits)[(i)*2+1] = (d) >> 16) \
: 0)) : 0))
#else #else
/* SIZEOF_BDIGITS == 4 */ /* SIZEOF_BDIGITS == 4 */