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

ruby-changes:44652

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 01:38:33 +0900 (JST)
Subject: [ruby-changes:44652] mrkn:r56725 (trunk): rational.c: optimize Rational#**

mrkn	2016-11-12 01:38:28 +0900 (Sat, 12 Nov 2016)

  New Revision: 56725

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

  Log:
    rational.c: optimize Rational#**
    
    * rational.c (nurat_expt): optimize Rational#**.
      Author: Tadashi Saito <tad.a.digger@g...>
    
    * numeric.c (rb_float_pow): rename flo_pow() to rb_float_pow()
      and remove static to be exporetd.
    
    * internal.h (rb_int_pow, rb_float_pow): exported.

  Modified files:
    trunk/internal.h
    trunk/numeric.c
    trunk/rational.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 56724)
+++ numeric.c	(revision 56725)
@@ -1291,8 +1291,8 @@ flo_divmod(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1291
  *    2.0**3      #=> 8.0
  */
 
-static VALUE
-flo_pow(VALUE x, VALUE y)
+VALUE
+rb_float_pow(VALUE x, VALUE y)
 {
     double dx, dy;
     if (RB_TYPE_P(y, T_FIXNUM)) {
@@ -3931,7 +3931,7 @@ fix_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3931
     }
 }
 
-static VALUE
+VALUE
 rb_int_pow(VALUE x, VALUE y)
 {
     if (FIXNUM_P(x)) {
@@ -5395,7 +5395,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5395
     rb_define_method(rb_cFloat, "%", flo_mod, 1);
     rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
     rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
-    rb_define_method(rb_cFloat, "**", flo_pow, 1);
+    rb_define_method(rb_cFloat, "**", rb_float_pow, 1);
     rb_define_method(rb_cFloat, "==", flo_eq, 1);
     rb_define_method(rb_cFloat, "===", flo_eq, 1);
     rb_define_method(rb_cFloat, "<=>", flo_cmp, 1);
Index: internal.h
===================================================================
--- internal.h	(revision 56724)
+++ internal.h	(revision 56725)
@@ -1171,6 +1171,8 @@ VALUE rb_fix_plus(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L1171
 VALUE rb_int_ge(VALUE x, VALUE y);
 enum ruby_num_rounding_mode rb_num_get_rounding_option(VALUE opts);
 double rb_int_fdiv_double(VALUE x, VALUE y);
+VALUE rb_int_pow(VALUE x, VALUE y);
+VALUE rb_float_pow(VALUE x, VALUE y);
 
 #if USE_FLONUM
 #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
Index: rational.c
===================================================================
--- rational.c	(revision 56724)
+++ rational.c	(revision 56725)
@@ -966,6 +966,8 @@ f_odd_p(VALUE integer) https://github.com/ruby/ruby/blob/trunk/rational.c#L966
     return Qfalse;
 }
 
+static VALUE nurat_to_f(VALUE self);
+
 /*
  * call-seq:
  *    rat ** numeric  ->  numeric
@@ -1022,12 +1024,12 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1024
 
 	    switch (FIX2INT(f_cmp(other, ZERO))) {
 	      case 1:
-		num = f_expt(dat->num, other);
-		den = f_expt(dat->den, other);
+		num = rb_int_pow(dat->num, other);
+		den = rb_int_pow(dat->den, other);
 		break;
 	      case -1:
-		num = f_expt(dat->den, f_negate(other));
-		den = f_expt(dat->num, f_negate(other));
+		num = rb_int_pow(dat->den, rb_int_uminus(other));
+		den = rb_int_pow(dat->num, rb_int_uminus(other));
 		break;
 	      default:
 		num = ONE;
@@ -1039,10 +1041,10 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1041
     }
     else if (RB_TYPE_P(other, T_BIGNUM)) {
 	rb_warn("in a**b, b may be too big");
-	return f_expt(f_to_f(self), other);
+	return rb_float_pow(nurat_to_f(self), other);
     }
     else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_RATIONAL)) {
-	return f_expt(f_to_f(self), other);
+	return rb_float_pow(nurat_to_f(self), other);
     }
     else {
 	return rb_num_coerce_bin(self, other, id_expt);

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

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