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
This commit is contained in:
mrkn 2016-11-12 06:07:24 +00:00
parent 2e9357e7ea
commit e5aa590637
3 changed files with 15 additions and 33 deletions

View File

@ -1137,6 +1137,7 @@ void Init_newline(void);
/* numeric.c */ /* 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_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0)
#define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0)) #define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0))

View File

@ -260,8 +260,6 @@ compare_with_zero(VALUE num, ID mid)
return r; return r;
} }
#define FIXNUM_POSITIVE_P(num) ((SIGNED_VALUE)(num) > (SIGNED_VALUE)INT2FIX(0))
static inline int static inline int
int_pos_p(VALUE num) int_pos_p(VALUE num)
{ {

View File

@ -27,6 +27,7 @@
#define GMP_GCD_DIGITS 1 #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_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)) #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
@ -71,20 +72,6 @@ f_add(VALUE x, VALUE y)
return rb_funcall(x, '+', 1, y); return rb_funcall(x, '+', 1, 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 inline static VALUE
f_div(VALUE x, VALUE y) f_div(VALUE x, VALUE y)
{ {
@ -437,15 +424,13 @@ nurat_s_new_bang(int argc, VALUE *argv, VALUE klass)
if (!k_integer_p(den)) if (!k_integer_p(den))
den = f_to_i(den); den = f_to_i(den);
switch (FIX2INT(f_cmp(den, ZERO))) { if (INT_NEGATIVE_P(den)) {
case -1:
num = f_negate(num); num = f_negate(num);
den = f_negate(den); den = f_negate(den);
break; }
case 0: else if (INT_ZERO_P(den)) {
rb_raise_zerodiv(); rb_raise_zerodiv();
break; }
}
break; break;
} }
@ -1014,11 +999,11 @@ nurat_expt(VALUE self, VALUE other)
if (f_one_p(dat->num)) { if (f_one_p(dat->num)) {
return f_rational_new_bang1(CLASS_OF(self), ONE); 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)); return f_rational_new_bang1(CLASS_OF(self), INT2FIX(f_odd_p(other) ? -1 : 1));
} }
else if (f_zero_p(dat->num)) { else if (INT_ZERO_P(dat->num)) {
if (FIX2INT(f_cmp(other, ZERO)) == -1) { if (f_negative_p(other)) {
rb_raise_zerodiv(); rb_raise_zerodiv();
} }
else { else {
@ -1029,25 +1014,23 @@ nurat_expt(VALUE self, VALUE other)
} }
/* General case */ /* General case */
if (RB_TYPE_P(other, T_FIXNUM)) { if (FIXNUM_P(other)) {
{ {
VALUE num, den; VALUE num, den;
get_dat1(self); get_dat1(self);
switch (FIX2INT(f_cmp(other, ZERO))) { if (INT_POSITIVE_P(other)) {
case 1:
num = rb_int_pow(dat->num, other); num = rb_int_pow(dat->num, other);
den = rb_int_pow(dat->den, 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)); num = rb_int_pow(dat->den, rb_int_uminus(other));
den = rb_int_pow(dat->num, rb_int_uminus(other)); den = rb_int_pow(dat->num, rb_int_uminus(other));
break; }
default: else {
num = ONE; num = ONE;
den = ONE; den = ONE;
break;
} }
return f_rational_new2(CLASS_OF(self), num, den); return f_rational_new2(CLASS_OF(self), num, den);
} }