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:
parent
05ff2fe4f3
commit
af070554e8
29
rational.c
29
rational.c
@ -34,7 +34,7 @@
|
|||||||
VALUE rb_cRational;
|
VALUE rb_cRational;
|
||||||
|
|
||||||
static ID id_abs, id_cmp, id_eqeq_p, id_expt, id_fdiv,
|
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;
|
id_to_i, id_truncate, id_i_num, id_i_den;
|
||||||
|
|
||||||
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
|
#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_str_to_inum(x, 10, 0);
|
||||||
return rb_funcall(x, id_to_i, 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
|
inline static VALUE
|
||||||
f_eqeq_p(VALUE x, VALUE y)
|
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));
|
return RTEST(rb_funcall(x, id_eqeq_p, 1, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if FLT_RADIX != 2
|
||||||
fun2(expt)
|
fun2(expt)
|
||||||
|
#endif
|
||||||
|
|
||||||
fun2(fdiv)
|
fun2(fdiv)
|
||||||
fun2(idiv)
|
fun2(idiv)
|
||||||
|
|
||||||
@ -938,6 +934,8 @@ nurat_div(VALUE self, VALUE other)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE nurat_to_f(VALUE self);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* rat.fdiv(numeric) -> float
|
* rat.fdiv(numeric) -> float
|
||||||
@ -951,9 +949,17 @@ nurat_div(VALUE self, VALUE other)
|
|||||||
static VALUE
|
static VALUE
|
||||||
nurat_fdiv(VALUE self, VALUE other)
|
nurat_fdiv(VALUE self, VALUE other)
|
||||||
{
|
{
|
||||||
|
VALUE div;
|
||||||
if (f_zero_p(other))
|
if (f_zero_p(other))
|
||||||
return f_div(self, f_to_f(other));
|
return DBL2NUM(nurat_to_double(self) / 0.0);
|
||||||
return f_to_f(f_div(self, other));
|
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
|
inline static VALUE
|
||||||
@ -965,8 +971,6 @@ f_odd_p(VALUE integer)
|
|||||||
return Qfalse;
|
return Qfalse;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE nurat_to_f(VALUE self);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* rat ** numeric -> numeric
|
* rat ** numeric -> numeric
|
||||||
@ -2572,7 +2576,6 @@ Init_Rational(void)
|
|||||||
id_idiv = rb_intern("div");
|
id_idiv = rb_intern("div");
|
||||||
id_integer_p = rb_intern("integer?");
|
id_integer_p = rb_intern("integer?");
|
||||||
id_negate = rb_intern("-@");
|
id_negate = rb_intern("-@");
|
||||||
id_to_f = rb_intern("to_f");
|
|
||||||
id_to_i = rb_intern("to_i");
|
id_to_i = rb_intern("to_i");
|
||||||
id_truncate = rb_intern("truncate");
|
id_truncate = rb_intern("truncate");
|
||||||
id_i_num = rb_intern("@numerator");
|
id_i_num = rb_intern("@numerator");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user