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

ruby-changes:37368

From: nobu <ko1@a...>
Date: Fri, 30 Jan 2015 17:28:42 +0900 (JST)
Subject: [ruby-changes:37368] nobu:r49449 (trunk): math.c: optimization for Bignum

nobu	2015-01-30 17:28:32 +0900 (Fri, 30 Jan 2015)

  New Revision: 49449

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

  Log:
    math.c: optimization for Bignum
    
    * math.c (num2dbl_with_to_f): make faster when Bignum passed by
      direct conversion using rb_big2dbl().  [Feature #10800]

  Modified files:
    trunk/ChangeLog
    trunk/math.c
Index: math.c
===================================================================
--- math.c	(revision 49448)
+++ math.c	(revision 49449)
@@ -26,13 +26,38 @@ static ID id_to_f; https://github.com/ruby/ruby/blob/trunk/math.c#L26
 VALUE rb_mMath;
 VALUE rb_eMathDomainError;
 
-#define fix_to_f_optimizable() rb_method_basic_definition_p(rb_cFixnum, id_to_f)
-#define Get_Float(x) \
-    ((RB_TYPE_P((x), T_FLOAT) ? (void)0 : ((x) = rb_to_float(x))), RFLOAT_VALUE(x))
-#define Get_Double(x) \
-    (FIXNUM_P(x) && fix_to_f_optimizable() ? \
-     (double)FIX2LONG(x) : \
-     Get_Float(x))
+static inline int
+basic_to_f_p(VALUE klass)
+{
+    return rb_method_basic_definition_p(klass, id_to_f);
+}
+
+static inline double
+num2dbl_with_to_f(VALUE num)
+{
+    if (SPECIAL_CONST_P(num)) {
+	if (FIXNUM_P(num)) {
+	    if (basic_to_f_p(rb_cFixnum))
+		return (double)FIX2LONG(num);
+	}
+	else if (FLONUM_P(num)) {
+	    return RFLOAT_VALUE(num);
+	}
+    }
+    else {
+	switch (BUILTIN_TYPE(num)) {
+	  case T_FLOAT:
+	    return RFLOAT_VALUE(num);
+	  case T_BIGNUM:
+	    if (basic_to_f_p(rb_cBignum))
+		return rb_big2dbl(num);
+	    break;
+	}
+    }
+    return RFLOAT_VALUE(rb_to_float(num));
+}
+
+#define Get_Double(x) num2dbl_with_to_f(x)
 
 #define domain_error(msg) \
     rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49448)
+++ ChangeLog	(revision 49449)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jan 30 17:28:29 2015  gogotanaka  <mail@t...>
+
+	* math.c (num2dbl_with_to_f): make faster when Bignum passed by
+	  direct conversion using rb_big2dbl().  [Feature #10800]
+
 Thu Jan 29 23:30:00 2015  Kenta Murata  <mrkn@m...>
 
 	* ext/bigdecimal/bigdecimal.c (rb_rational_num): add fallback function

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

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