rational.c: optimize Rational#fdiv

* rational.c (nurat_fdiv): optimize Rational#fdiv.
  Author: Tadashi Saito <tad.a.digger@gmail.com>

* rational.c (f_to_f, id_to_f): removed.

* rational.c (f_expt): only used when FLT_RADIX is not 2.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2016-11-12 07:28:47 +00:00
parent 05ff2fe4f3
commit af070554e8

View File

@ -34,7 +34,7 @@
VALUE rb_cRational;
static ID id_abs, id_cmp, id_eqeq_p, id_expt, id_fdiv,
id_idiv, id_integer_p, id_negate, id_to_f,
id_idiv, id_integer_p, id_negate,
id_to_i, id_truncate, id_i_num, id_i_den;
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
@ -136,13 +136,6 @@ f_to_i(VALUE x)
return rb_str_to_inum(x, 10, 0);
return rb_funcall(x, id_to_i, 0);
}
inline static VALUE
f_to_f(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
return DBL2NUM(rb_str_to_dbl(x, 0));
return rb_funcall(x, id_to_f, 0);
}
inline static VALUE
f_eqeq_p(VALUE x, VALUE y)
@ -152,7 +145,10 @@ f_eqeq_p(VALUE x, VALUE y)
return RTEST(rb_funcall(x, id_eqeq_p, 1, y));
}
#if FLT_RADIX != 2
fun2(expt)
#endif
fun2(fdiv)
fun2(idiv)
@ -938,6 +934,8 @@ nurat_div(VALUE self, VALUE other)
}
}
static VALUE nurat_to_f(VALUE self);
/*
* call-seq:
* rat.fdiv(numeric) -> float
@ -951,9 +949,17 @@ nurat_div(VALUE self, VALUE other)
static VALUE
nurat_fdiv(VALUE self, VALUE other)
{
VALUE div;
if (f_zero_p(other))
return f_div(self, f_to_f(other));
return f_to_f(f_div(self, other));
return DBL2NUM(nurat_to_double(self) / 0.0);
if (FIXNUM_P(other) && other == LONG2FIX(1))
return nurat_to_f(self);
div = nurat_div(self, other);
if (RB_TYPE_P(div, T_RATIONAL))
return nurat_to_f(div);
if (RB_FLOAT_TYPE_P(div))
return div;
return rb_funcall(div, rb_intern("to_f"), 0);
}
inline static VALUE
@ -965,8 +971,6 @@ f_odd_p(VALUE integer)
return Qfalse;
}
static VALUE nurat_to_f(VALUE self);
/*
* call-seq:
* rat ** numeric -> numeric
@ -2572,7 +2576,6 @@ Init_Rational(void)
id_idiv = rb_intern("div");
id_integer_p = rb_intern("integer?");
id_negate = rb_intern("-@");
id_to_f = rb_intern("to_f");
id_to_i = rb_intern("to_i");
id_truncate = rb_intern("truncate");
id_i_num = rb_intern("@numerator");