ruby-changes:42863
From: nobu <ko1@a...>
Date: Sat, 7 May 2016 14:24:24 +0900 (JST)
Subject: [ruby-changes:42863] nobu:r54937 (trunk): random.c: split random_int32
nobu 2016-05-07 15:21:00 +0900 (Sat, 07 May 2016) New Revision: 54937 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54937 Log: random.c: split random_int32 * random.c (random_int32): split the cases of rb_random_t and other objects. Modified files: trunk/random.c Index: random.c =================================================================== --- random.c (revision 54936) +++ random.c (revision 54937) @@ -918,32 +918,47 @@ rb_genrand_ulong_limited(unsigned long l https://github.com/ruby/ruby/blob/trunk/random.c#L918 } static unsigned int -random_int32(VALUE obj, rb_random_t *rnd) +obj_random_int32(VALUE obj) { - if (!rnd) { #if SIZEOF_LONG * CHAR_BIT > 32 - VALUE lim = ULONG2NUM(0x100000000UL); + VALUE lim = ULONG2NUM(0x100000000UL); #elif defined HAVE_LONG_LONG - VALUE lim = ULL2NUM((LONG_LONG)0xffffffff+1); + VALUE lim = ULL2NUM((LONG_LONG)0xffffffff+1); #else - VALUE lim = rb_big_plus(ULONG2NUM(0xffffffff), INT2FIX(1)); + VALUE lim = rb_big_plus(ULONG2NUM(0xffffffff), INT2FIX(1)); #endif - return (unsigned int)NUM2ULONG(rb_funcall2(obj, id_rand, 1, &lim)); - } + return (unsigned int)NUM2ULONG(rb_funcall2(obj, id_rand, 1, &lim)); +} + +static unsigned int +random_int32(rb_random_t *rnd) +{ return genrand_int32(&rnd->mt); } unsigned int rb_random_int32(VALUE obj) { - return random_int32(obj, try_get_rnd(obj)); + rb_random_t *rnd = try_get_rnd(obj); + if (!rnd) { + return obj_random_int32(obj); + } + return random_int32(rnd); } static double random_real(VALUE obj, rb_random_t *rnd, int excl) { - uint32_t a = random_int32(obj, rnd); - uint32_t b = random_int32(obj, rnd); + uint32_t a, b; + + if (!rnd) { + a = obj_random_int32(obj); + b = obj_random_int32(obj); + } + else { + a = random_int32(rnd); + b = random_int32(rnd); + } if (excl) { return int_pair_to_real_exclusive(a, b); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/