diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 06:07:24 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-12 06:07:24 +0000 |
commit | 8f14cf69d7bba56f40604f567640dcb2bed498aa (patch) | |
tree | b04f265ac787ac9db415d64892e896c8647ee307 | |
parent | e4c68cee8b5f97a1047bc96b88c0f8e2fc83c3b7 (diff) | |
download | ruby-8f14cf69d7bba56f40604f567640dcb2bed498aa.tar.gz |
rational.c: purge f_cmp
* rational.c (f_cmp, nurat_expt): purge f_cmp.
Author: Tadashi Saito <tad.a.digger@gmail.com>
* rational.c (INT_POSITIVE_P): added.
* numeric.c (FIXNUM_POSITIVE_P): move the definition into internal.h.
* internal.h (FIXNUM_POSITIVE_P): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 2 | ||||
-rw-r--r-- | rational.c | 45 |
3 files changed, 15 insertions, 33 deletions
diff --git a/internal.h b/internal.h index 48dc266166..5b17c734db 100644 --- a/internal.h +++ b/internal.h @@ -1137,6 +1137,7 @@ void Init_newline(void); /* numeric.c */ +#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0)) #define FIXNUM_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0) #define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0)) @@ -260,8 +260,6 @@ compare_with_zero(VALUE num, ID mid) return r; } -#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0)) - static inline int int_pos_p(VALUE num) { diff --git a/rational.c b/rational.c index 1d569ce55a..724b44383f 100644 --- a/rational.c +++ b/rational.c @@ -27,6 +27,7 @@ #define GMP_GCD_DIGITS 1 +#define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x)) #define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? FIXNUM_NEGATIVE_P(x) : BIGNUM_NEGATIVE_P(x)) #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x)) @@ -72,20 +73,6 @@ f_add(VALUE x, VALUE y) } inline static VALUE -f_cmp(VALUE x, VALUE y) -{ - if (FIXNUM_P(x) && FIXNUM_P(y)) { - long c = FIX2LONG(x) - FIX2LONG(y); - if (c > 0) - c = 1; - else if (c < 0) - c = -1; - return INT2FIX(c); - } - return rb_funcall(x, id_cmp, 1, y); -} - -inline static VALUE f_div(VALUE x, VALUE y) { if (FIXNUM_P(y) && FIX2LONG(y) == 1) @@ -437,15 +424,13 @@ nurat_s_new_bang(int argc, VALUE *argv, VALUE klass) if (!k_integer_p(den)) den = f_to_i(den); - switch (FIX2INT(f_cmp(den, ZERO))) { - case -1: + if (INT_NEGATIVE_P(den)) { num = f_negate(num); den = f_negate(den); - break; - case 0: + } + else if (INT_ZERO_P(den)) { rb_raise_zerodiv(); - break; - } + } break; } @@ -1014,11 +999,11 @@ nurat_expt(VALUE self, VALUE other) if (f_one_p(dat->num)) { return f_rational_new_bang1(CLASS_OF(self), ONE); } - else if (f_minus_one_p(dat->num) && k_integer_p(other)) { + else if (f_minus_one_p(dat->num) && RB_INTEGER_TYPE_P(other)) { return f_rational_new_bang1(CLASS_OF(self), INT2FIX(f_odd_p(other) ? -1 : 1)); } - else if (f_zero_p(dat->num)) { - if (FIX2INT(f_cmp(other, ZERO)) == -1) { + else if (INT_ZERO_P(dat->num)) { + if (f_negative_p(other)) { rb_raise_zerodiv(); } else { @@ -1029,25 +1014,23 @@ nurat_expt(VALUE self, VALUE other) } /* General case */ - if (RB_TYPE_P(other, T_FIXNUM)) { + if (FIXNUM_P(other)) { { VALUE num, den; get_dat1(self); - switch (FIX2INT(f_cmp(other, ZERO))) { - case 1: + if (INT_POSITIVE_P(other)) { num = rb_int_pow(dat->num, other); den = rb_int_pow(dat->den, other); - break; - case -1: + } + else if (INT_NEGATIVE_P(other)) { num = rb_int_pow(dat->den, rb_int_uminus(other)); den = rb_int_pow(dat->num, rb_int_uminus(other)); - break; - default: + } + else { num = ONE; den = ONE; - break; } return f_rational_new2(CLASS_OF(self), num, den); } |