[前][次][番号順一覧][スレッド一覧]

ruby-changes:44757

From: mrkn <ko1@a...>
Date: Sat, 19 Nov 2016 00:17:25 +0900 (JST)
Subject: [ruby-changes:44757] mrkn:r56830 (trunk): complex.c: optimize f_negate

mrkn	2016-11-19 00:17:19 +0900 (Sat, 19 Nov 2016)

  New Revision: 56830

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56830

  Log:
    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.

  Modified files:
    trunk/complex.c
    trunk/internal.h
    trunk/numeric.c
    trunk/rational.c
Index: internal.h
===================================================================
--- internal.h	(revision 56829)
+++ internal.h	(revision 56830)
@@ -1168,6 +1168,7 @@ int rb_num_negative_p(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L1168
 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); https://github.com/ruby/ruby/blob/trunk/internal.h#L1379
 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);
Index: numeric.c
===================================================================
--- numeric.c	(revision 56829)
+++ numeric.c	(revision 56830)
@@ -1032,8 +1032,8 @@ flo_coerce(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1032
  * 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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5391
     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);
Index: rational.c
===================================================================
--- rational.c	(revision 56829)
+++ rational.c	(revision 56830)
@@ -620,10 +620,12 @@ nurat_denominator(VALUE self) https://github.com/ruby/ruby/blob/trunk/rational.c#L620
  *
  * 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, https://github.com/ruby/ruby/blob/trunk/rational.c#L1646
 	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, https://github.com/ruby/ruby/blob/trunk/rational.c#L2202
     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, in https://github.com/ruby/ruby/blob/trunk/rational.c#L2341
     }
 
     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) https://github.com/ruby/ruby/blob/trunk/rational.c#L2639
     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);
Index: complex.c
===================================================================
--- complex.c	(revision 56829)
+++ complex.c	(revision 56830)
@@ -144,7 +144,27 @@ f_sub(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/complex.c#L144
 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) https://github.com/ruby/ruby/blob/trunk/complex.c#L937
 	    }
 	    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;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]