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

ruby-changes:44671

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 16:28:51 +0900 (JST)
Subject: [ruby-changes:44671] mrkn:r56744 (trunk): rational.c: optimize Rational#fdiv

mrkn	2016-11-12 16:28:47 +0900 (Sat, 12 Nov 2016)

  New Revision: 56744

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

  Log:
    rational.c: optimize Rational#fdiv
    
    * rational.c (nurat_fdiv): optimize Rational#fdiv.
      Author: Tadashi Saito <tad.a.digger@g...>
    
    * rational.c (f_to_f, id_to_f): removed.
    
    * rational.c (f_expt): only used when FLT_RADIX is not 2.

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56743)
+++ rational.c	(revision 56744)
@@ -34,7 +34,7 @@ https://github.com/ruby/ruby/blob/trunk/rational.c#L34
 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) https://github.com/ruby/ruby/blob/trunk/rational.c#L136
 	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) https://github.com/ruby/ruby/blob/trunk/rational.c#L145
     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) https://github.com/ruby/ruby/blob/trunk/rational.c#L934
     }
 }
 
+static VALUE nurat_to_f(VALUE self);
+
 /*
  * call-seq:
  *    rat.fdiv(numeric)  ->  float
@@ -951,9 +949,17 @@ nurat_div(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L949
 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) https://github.com/ruby/ruby/blob/trunk/rational.c#L971
     return Qfalse;
 }
 
-static VALUE nurat_to_f(VALUE self);
-
 /*
  * call-seq:
  *    rat ** numeric  ->  numeric
@@ -2572,7 +2576,6 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2576
     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");

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

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