From 1b49df0b3d0a08f450a475eafdb5a80c614d85bb Mon Sep 17 00:00:00 2001 From: naruse Date: Tue, 8 Mar 2016 09:15:18 +0000 Subject: * intern.h (rb_divmod): assume compilers `/` and `%` comply C99 and reduce branching. If a compiler doesn't comply, add #ifdefs. * intern.h (rb_div): added for Ruby's behavior. * intern.h (rb_mod): added for Ruby's behavior. * insns.def (opt_div): use rb_div. * insns.def (opt_mod): use rb_mod. * numeric.c (fixdivmod): removed. * numeric.c (fix_divide): use rb_div. * numeric.c (fix_mod): use rb_mod. * numeric.c (fix_divmod): use rb_divmod. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index c424203109..1e5f779ae9 100644 --- a/numeric.c +++ b/numeric.c @@ -3079,33 +3079,6 @@ fix_mul(VALUE x, VALUE y) } } -static void -fixdivmod(long x, long y, long *divp, long *modp) -{ - long div, mod; - - if (y == 0) rb_num_zerodiv(); - if (y < 0) { - if (x < 0) - div = -x / -y; - else - div = - (x / -y); - } - else { - if (x < 0) - div = - (-x / y); - else - div = x / y; - } - mod = x - div*y; - if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) { - mod += y; - div -= 1; - } - if (divp) *divp = div; - if (modp) *modp = mod; -} - /* * call-seq: * fix.fdiv(numeric) -> float @@ -3138,10 +3111,8 @@ static VALUE fix_divide(VALUE x, VALUE y, ID op) { if (FIXNUM_P(y)) { - long div; - - fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0); - return LONG2NUM(div); + if (FIX2LONG(y) == 0) rb_num_zerodiv(); + return LONG2NUM(rb_div(FIX2LONG(x), FIX2LONG(y))); } else if (RB_TYPE_P(y, T_BIGNUM)) { x = rb_int2big(FIX2LONG(x)); @@ -3212,10 +3183,8 @@ static VALUE fix_mod(VALUE x, VALUE y) { if (FIXNUM_P(y)) { - long mod; - - fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod); - return LONG2NUM(mod); + if (FIX2LONG(y) == 0) rb_num_zerodiv(); + return LONG2FIX(rb_mod(FIX2LONG(x), FIX2LONG(y))); } else if (RB_TYPE_P(y, T_BIGNUM)) { x = rb_int2big(FIX2LONG(x)); @@ -3240,10 +3209,9 @@ fix_divmod(VALUE x, VALUE y) { if (FIXNUM_P(y)) { long div, mod; - - fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod); - - return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod)); + if (FIX2LONG(y) == 0) rb_num_zerodiv(); + rb_divmod(FIX2LONG(x), FIX2LONG(y), &div, &mod); + return rb_assoc_new(LONG2NUM(div), LONG2FIX(mod)); } else if (RB_TYPE_P(y, T_BIGNUM)) { x = rb_int2big(FIX2LONG(x)); -- cgit v1.2.3