ruby-changes:51437
From: nobu <ko1@a...>
Date: Wed, 13 Jun 2018 10:22:04 +0900 (JST)
Subject: [ruby-changes:51437] nobu:r63643 (trunk): bignum.c: call functions directly
nobu 2018-06-13 10:21:59 +0900 (Wed, 13 Jun 2018) New Revision: 63643 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63643 Log: bignum.c: call functions directly * bignum.c (int_pow_tmp{1,2,3}): call dedicated functions directly for internal calculations, instead of method calls. Modified files: trunk/bignum.c Index: bignum.c =================================================================== --- bignum.c (revision 63642) +++ bignum.c (revision 63643) @@ -6938,7 +6938,7 @@ int_pow_tmp3(VALUE x, VALUE y, VALUE m, https://github.com/ruby/ruby/blob/trunk/bignum.c#L6938 z = bignew(zn, 1); bary_powm_gmp(BDIGITS(z), zn, BDIGITS(x), xn, BDIGITS(y), yn, BDIGITS(m), mn); if (nega_flg & BIGNUM_POSITIVE_P(z)) { - z = rb_funcall(z, '-', 1, m); + z = rb_big_minus(z, m); } RB_GC_GUARD(x); RB_GC_GUARD(y); @@ -6948,25 +6948,25 @@ int_pow_tmp3(VALUE x, VALUE y, VALUE m, https://github.com/ruby/ruby/blob/trunk/bignum.c#L6948 VALUE tmp = LONG2FIX(1L); long yy; - for (/*NOP*/; ! FIXNUM_P(y); y = rb_funcall(y, rb_intern(">>"), 1, LONG2FIX(1L))) { - if (RTEST(rb_funcall(y, rb_intern("odd?"), 0))) { - tmp = rb_funcall(tmp, '*', 1, x); + for (/*NOP*/; ! FIXNUM_P(y); y = rb_big_rshift(y, LONG2FIX(1L))) { + if (RTEST(rb_int_odd_p(y))) { + tmp = rb_int_mul(tmp, x); tmp = rb_int_modulo(tmp, m); } - x = rb_funcall(x, '*', 1, x); + x = rb_int_mul(x, x); x = rb_int_modulo(x, m); } for (yy = FIX2LONG(y); yy; yy >>= 1L) { if (yy & 1L) { - tmp = rb_funcall(tmp, '*', 1, x); + tmp = rb_int_mul(tmp, x); tmp = rb_int_modulo(tmp, m); } - x = rb_funcall(x, '*', 1, x); + x = rb_int_mul(x, x); x = rb_int_modulo(x, m); } - if (nega_flg && RTEST(rb_funcall(tmp, rb_intern("positive?"), 0))) { - tmp = rb_funcall(tmp, '-', 1, m); + if (nega_flg && rb_num_positive_int_p(tmp)) { + tmp = rb_int_minus(tmp, m); } return tmp; #endif @@ -6983,7 +6983,7 @@ int_pow_tmp1(VALUE x, VALUE y, long mm, https://github.com/ruby/ruby/blob/trunk/bignum.c#L6983 long tmp = 1L; long yy; - for (/*NOP*/; ! FIXNUM_P(y); y = rb_funcall(y, idGTGT, 1, LONG2FIX(1L))) { + for (/*NOP*/; ! FIXNUM_P(y); y = rb_big_rshift(y, LONG2FIX(1L))) { if (RTEST(rb_int_odd_p(y))) { tmp = (tmp * xx) % mm; } @@ -7019,7 +7019,7 @@ int_pow_tmp2(VALUE x, VALUE y, long mm, https://github.com/ruby/ruby/blob/trunk/bignum.c#L7019 # define MUL_MODULO(a, b, c) rb_int_modulo(rb_fix_mul_fix((a), (b)), (c)) #endif - for (/*NOP*/; ! FIXNUM_P(y); y = rb_funcall(y, idGTGT, 1, LONG2FIX(1L))) { + for (/*NOP*/; ! FIXNUM_P(y); y = rb_big_rshift(y, LONG2FIX(1L))) { if (RTEST(rb_int_odd_p(y))) { tmp2 = MUL_MODULO(tmp2, xx, m); } @@ -7078,22 +7078,23 @@ rb_int_powm(int const argc, VALUE * cons https://github.com/ruby/ruby/blob/trunk/bignum.c#L7078 } if (rb_num_negative_int_p(m)) { - m = rb_funcall(m, idUMinus, 0); + m = rb_int_uminus(m); nega_flg = 1; } - if (!rb_num_positive_int_p(m)) { - rb_num_zerodiv(); - } if (FIXNUM_P(m)) { long const half_val = (long)HALF_LONG_MSB; long const mm = FIX2LONG(m); + if (!mm) rb_num_zerodiv(); if (mm <= half_val) { return int_pow_tmp1(rb_int_modulo(a, m), b, mm, nega_flg); - } else { + } + else { return int_pow_tmp2(rb_int_modulo(a, m), b, mm, nega_flg); } - } else if (RB_TYPE_P(m, T_BIGNUM)) { + } + else { + if (rb_bigzero_p(m)) rb_num_zerodiv(); return int_pow_tmp3(rb_int_modulo(a, m), b, m, nega_flg); } } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/