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

ruby-changes:15328

From: akr <ko1@a...>
Date: Sun, 4 Apr 2010 22:20:51 +0900 (JST)
Subject: [ruby-changes:15328] Ruby:r27216 (trunk): * time.c (cmp): optimize for fixnums.

akr	2010-04-04 22:20:29 +0900 (Sun, 04 Apr 2010)

  New Revision: 27216

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

  Log:
    * time.c (cmp): optimize for fixnums.
      (lt): use cmp.
      (gt): ditto.
      (le): ditto.
      (ge): ditto.
      (wlt): use wcmp.
      (wgt): ditto.
      (wle): ditto.
      (wge): ditto.
      (time_subsec): use wmod.

  Modified files:
    trunk/ChangeLog
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 27215)
+++ time.c	(revision 27216)
@@ -31,8 +31,6 @@
 #define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
 
-#define cmp(x,y) (rb_cmpint(rb_funcall((x), id_cmp, 1, (y)), (x), (y)))
-
 static int
 eq(VALUE x, VALUE y)
 {
@@ -43,18 +41,23 @@
 }
 
 static int
-lt(VALUE x, VALUE y)
+cmp(VALUE x, VALUE y)
 {
     if (FIXNUM_P(x) && FIXNUM_P(y)) {
-        return (long)x < (long)y;
+        if ((long)x < (long)y)
+            return -1;
+        if ((long)x > (long)y)
+            return 1;
+        return 0;
     }
-    return cmp(x,y) < 0;
+    return rb_cmpint(rb_funcall(x, id_cmp, 1, y), x, y);
 }
 
-#define ne(x,y) (!eq(x,y))
-#define gt(x,y) (lt(y,x))
-#define le(x,y) (!gt(x,y))
-#define ge(x,y) (!lt(x,y))
+#define ne(x,y) (!eq((x),(y)))
+#define lt(x,y) (cmp((x),(y)) < 0)
+#define gt(x,y) (cmp((x),(y)) > 0)
+#define le(x,y) (cmp((x),(y)) <= 0)
+#define ge(x,y) (cmp((x),(y)) >= 0)
 
 static VALUE
 add(VALUE x, VALUE y)
@@ -163,15 +166,14 @@
 {
     VALUE ret;
     if (FIXNUM_P(x) && FIXNUM_P(y)) {
-      long a, b, c;
-      a = FIX2LONG(x);
-      b = FIX2LONG(y);
-      if (b == 0) rb_num_zerodiv();
-      c = a / b;
-      if (c * b == a) {
-          return LONG2NUM(c);
-      }
-
+        long a, b, c;
+        a = FIX2LONG(x);
+        b = FIX2LONG(y);
+        if (b == 0) rb_num_zerodiv();
+        c = a / b;
+        if (c * b == a) {
+            return LONG2NUM(c);
+        }
     }
     ret = rb_funcall(x, id_quo, 1, y);
     if (TYPE(ret) == T_RATIONAL &&
@@ -391,7 +393,7 @@
     return WIDEVAL_WRAP(v);
 }
 
-static inline int
+static int
 weq(wideval_t wx, wideval_t wy)
 {
 #if WIDEVALUE_IS_WIDER
@@ -404,23 +406,32 @@
 #endif
 }
 
-static inline int
-wlt(wideval_t wx, wideval_t wy)
+static int
+wcmp(wideval_t wx, wideval_t wy)
 {
+    VALUE x, y;
 #if WIDEVALUE_IS_WIDER
     if (FIXWV_P(wx) && FIXWV_P(wy)) {
-        return (SIGNED_WIDEVALUE)WIDEVAL_GET(wx) < (SIGNED_WIDEVALUE)WIDEVAL_GET(wy);
+        wideint_t a, b;
+        a = FIXWV2WINT(wx);
+        b = FIXWV2WINT(wy);
+        if (a < b)
+            return -1;
+        if (a > b)
+            return 1;
+        return 0;
     }
-    return RTEST(rb_funcall(w2v(wx), '<', 1,  w2v(wy)));
-#else
-    return lt(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
 #endif
+    x = w2v(wx);
+    y = w2v(wy);
+    return rb_cmpint(rb_funcall(x, id_cmp, 1, y), x, y);
 }
 
-#define wne(x,y) (!weq(x,y))
-#define wgt(x,y) (wlt(y,x))
-#define wle(x,y) (!wgt(x,y))
-#define wge(x,y) (!wlt(x,y))
+#define wne(x,y) (!weq((x),(y)))
+#define wlt(x,y) (wcmp((x),(y)) < 0)
+#define wgt(x,y) (wcmp((x),(y)) > 0)
+#define wle(x,y) (wcmp((x),(y)) <= 0)
+#define wge(x,y) (wcmp((x),(y)) >= 0)
 
 static wideval_t
 wadd(wideval_t wx, wideval_t wy)
@@ -516,27 +527,6 @@
     return v2w(z);
 }
 
-static int
-wcmp(wideval_t wx, wideval_t wy)
-{
-    VALUE x, y;
-#if WIDEVALUE_IS_WIDER
-    if (FIXWV_P(wx) && FIXWV_P(wy)) {
-        wideint_t a, b;
-        a = FIXWV2WINT(wx);
-        b = FIXWV2WINT(wy);
-        if (a < b)
-            return -1;
-        if (a > b)
-            return 1;
-        return 0;
-    }
-#endif
-    x = w2v(wx);
-    y = w2v(wy);
-    return rb_cmpint(rb_funcall(x, id_cmp, 1, y), x, y);
-}
-
 static wideval_t
 wquo(wideval_t wx, wideval_t wy)
 {
@@ -3176,7 +3166,7 @@
     struct time_object *tobj;
 
     GetTimeval(time, tobj);
-    return quo(mod(w2v(tobj->timew), INT2FIX(TIME_SCALE)), INT2FIX(TIME_SCALE));
+    return quo(w2v(wmod(tobj->timew, WINT2FIXWV(TIME_SCALE))), INT2FIX(TIME_SCALE));
 }
 
 /*
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27215)
+++ ChangeLog	(revision 27216)
@@ -1,3 +1,16 @@
+Sun Apr  4 22:18:32 2010  Tanaka Akira  <akr@f...>
+
+	* time.c (cmp): optimize for fixnums.
+	  (lt): use cmp.
+	  (gt): ditto.
+	  (le): ditto.
+	  (ge): ditto.
+	  (wlt): use wcmp.
+	  (wgt): ditto.
+	  (wle): ditto.
+	  (wge): ditto.
+	  (time_subsec): use wmod.
+
 Sun Apr  4 10:04:28 2010  NARUSE, Yui  <naruse@r...>
 
 	* include/ruby/ruby.h: replace snprintf and vsnprintf by

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

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