1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

complex.c: optimize f_negate

* complex.c (f_negate): optimize for special numeric types.

* complex.c (nucomp_expt): use rb_int_uminus instead of f_negate for
  fixnum value.

* internal.h (rb_float_uminus, rb_rational_uminus): exported.

* numeric.c (rb_float_uminus): rename from flo_uminus.

* rational.c (rb_rational_uminus): rename from nurat_negate, and add
  assertion for the parameter.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56830 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2016-11-18 15:17:19 +00:00
parent 6326227317
commit c151aa88a9
4 changed files with 35 additions and 11 deletions

View file

@ -144,7 +144,27 @@ f_sub(VALUE x, VALUE y)
fun1(abs)
fun1(arg)
fun1(denominator)
fun1(negate)
static VALUE nucomp_negate(VALUE self);
inline static VALUE
f_negate(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return rb_int_uminus(x);
}
else if (RB_FLOAT_TYPE_P(x)) {
return rb_float_uminus(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
return rb_rational_uminus(x);
}
else if (RB_TYPE_P(x, T_COMPLEX)) {
return nucomp_negate(x);
}
return rb_funcall(x, id_negate, 0);
}
fun1(numerator)
fun1(real_p)
@ -917,7 +937,7 @@ nucomp_expt(VALUE self, VALUE other)
}
return z;
}
return f_expt(f_reciprocal(self), f_negate(other));
return f_expt(f_reciprocal(self), rb_int_uminus(other));
}
if (k_numeric_p(other) && f_real_p(other)) {
VALUE r, theta;

View file

@ -1168,6 +1168,7 @@ int rb_num_negative_p(VALUE);
VALUE rb_int_succ(VALUE num);
VALUE rb_int_pred(VALUE num);
VALUE rb_int_uminus(VALUE num);
VALUE rb_float_uminus(VALUE num);
VALUE rb_int_plus(VALUE x, VALUE y);
VALUE rb_int_minus(VALUE x, VALUE y);
VALUE rb_int_mul(VALUE x, VALUE y);
@ -1378,6 +1379,7 @@ rb_pid_t rb_fork_ruby(int *status);
void rb_last_status_clear(void);
/* rational.c */
VALUE rb_rational_uminus(VALUE self);
VALUE rb_rational_plus(VALUE self, VALUE other);
VALUE rb_lcm(VALUE x, VALUE y);
VALUE rb_rational_reciprocal(VALUE x);

View file

@ -1032,8 +1032,8 @@ flo_coerce(VALUE x, VALUE y)
* Returns float, negated.
*/
static VALUE
flo_uminus(VALUE flt)
VALUE
rb_float_uminus(VALUE flt)
{
return DBL2NUM(-RFLOAT_VALUE(flt));
}
@ -5391,7 +5391,7 @@ Init_Numeric(void)
rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
rb_define_alias(rb_cFloat, "inspect", "to_s");
rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
rb_define_method(rb_cFloat, "-@", rb_float_uminus, 0);
rb_define_method(rb_cFloat, "+", flo_plus, 1);
rb_define_method(rb_cFloat, "-", flo_minus, 1);
rb_define_method(rb_cFloat, "*", flo_mul, 1);

View file

@ -620,10 +620,12 @@ nurat_denominator(VALUE self)
*
* Negates +rat+.
*/
static VALUE
nurat_negate(VALUE self)
VALUE
rb_rational_uminus(VALUE self)
{
const int unused = (assert(RB_TYPE_P(self, T_RATIONAL)), 0);
get_dat1(self);
(void)unused;
return f_rational_new2(CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
}
@ -1644,7 +1646,7 @@ nurat_rationalize(int argc, VALUE *argv, VALUE self)
return self;
if (nurat_negative_p(self))
return nurat_negate(nurat_rationalize(argc, argv, nurat_negate(self)));
return rb_rational_uminus(nurat_rationalize(argc, argv, rb_rational_uminus(self)));
rb_scan_args(argc, argv, "01", &e);
e = f_abs(e);
@ -2200,7 +2202,7 @@ float_rationalize(int argc, VALUE *argv, VALUE self)
double d = RFLOAT_VALUE(self);
if (d < 0.0)
return nurat_negate(float_rationalize(argc, argv, DBL2NUM(-d)));
return rb_rational_uminus(float_rationalize(argc, argv, DBL2NUM(-d)));
rb_scan_args(argc, argv, "01", &e);
@ -2339,7 +2341,7 @@ read_num(const char **s, int numsign, int strict,
}
if (numsign == '-')
*num = nurat_negate(*num);
*num = rb_rational_uminus(*num);
if (!NIL_P(exp)) {
VALUE l = f_expt10(exp);
*num = nurat_mul(*num, l);
@ -2637,7 +2639,7 @@ Init_Rational(void)
rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
rb_define_method(rb_cRational, "-@", nurat_negate, 0);
rb_define_method(rb_cRational, "-@", rb_rational_uminus, 0);
rb_define_method(rb_cRational, "+", rb_rational_plus, 1);
rb_define_method(rb_cRational, "-", nurat_sub, 1);
rb_define_method(rb_cRational, "*", nurat_mul, 1);