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

ruby-changes:42147

From: naruse <ko1@a...>
Date: Tue, 22 Mar 2016 03:57:35 +0900 (JST)
Subject: [ruby-changes:42147] naruse:r54221 (trunk): * time.c (MUL_OVERFLOW_FIXWV_P): defined for FIXWV.

naruse	2016-03-22 03:57:30 +0900 (Tue, 22 Mar 2016)

  New Revision: 54221

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

  Log:
    * time.c (MUL_OVERFLOW_FIXWV_P): defined for FIXWV.
    
    * time.c (wmul): use MUL_OVERFLOW_FIXWV_P and only switch.
    
    * time.c (wmul): use mul which has Fixnum optimization.
    
    * time.c (rb_time_magnify): If WIDEVALUE_IS_WIDER, wmul() has the same
      optimized logic, else mul() has also the similar logic for Fixnum.
    
    * time.c (rb_time_unmagnify): almost ditto.

  Modified files:
    trunk/ChangeLog
    trunk/time.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54220)
+++ ChangeLog	(revision 54221)
@@ -1,3 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Mar 22 03:45:03 2016  NARUSE, Yui  <naruse@r...>
+
+	* time.c (MUL_OVERFLOW_FIXWV_P): defined for FIXWV.
+
+	* time.c (wmul): use MUL_OVERFLOW_FIXWV_P and only switch.
+
+	* time.c (wmul): use mul which has Fixnum optimization.
+
+	* time.c (rb_time_magnify): If WIDEVALUE_IS_WIDER, wmul() has the same
+	  optimized logic, else mul() has also the similar logic for Fixnum.
+
+	* time.c (rb_time_unmagnify): almost ditto.
+
 Tue Mar 22 03:10:09 2016  NARUSE, Yui  <naruse@r...>
 
 	* time.c (divmodv): add the case both arguments are Fixnum.
Index: time.c
===================================================================
--- time.c	(revision 54220)
+++ time.c	(revision 54221)
@@ -203,6 +203,7 @@ divmodv(VALUE n, VALUE d, VALUE *q, VALU https://github.com/ruby/ruby/blob/trunk/time.c#L203
 #define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
 #define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
 #define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
+#define MUL_OVERFLOW_FIXWV_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXWV_MIN, FIXWV_MAX)
 
 /* #define STRUCT_WIDEVAL */
 #ifdef STRUCT_WIDEVAL
@@ -358,65 +359,17 @@ wsub(wideval_t wx, wideval_t wy) https://github.com/ruby/ruby/blob/trunk/time.c#L359
     return v2w(rb_funcall(x, '-', 1, w2v(wy)));
 }
 
-static int
-wi_mul(wideint_t x, wideint_t y, wideint_t *z)
-{
-    uwideint_t a, b, c;
-    int s;
-    if (x == 0 || y == 0) {
-	*z = 0;
-	return 1;
-    }
-    if (x < 0) {
-	s = -1;
-	a = (uwideint_t)-x;
-    }
-    else {
-	s = 1;
-	a = (uwideint_t)x;
-    }
-    if (y < 0) {
-        s = -s;
-	b = (uwideint_t)-y;
-    }
-    else {
-	b = (uwideint_t)y;
-    }
-    if (a <= UWIDEINT_MAX / b) {
-        c = a * b;
-	if (s < 0) {
-	    if (c <= (uwideint_t)WIDEINT_MAX + 1) {
-		*z = -(wideint_t)c;
-		return 1;
-	    }
-	}
-	else {
-	    if (c <= (uwideint_t)WIDEINT_MAX) {
-		*z = (wideint_t)c;
-		return 1;
-	    }
-	}
-    }
-    return 0;
-}
-
 static wideval_t
 wmul(wideval_t wx, wideval_t wy)
 {
 #if WIDEVALUE_IS_WIDER
     if (FIXWV_P(wx) && FIXWV_P(wy)) {
 	wideint_t z;
-	if (wi_mul(FIXWV2WINT(wx), FIXWV2WINT(wy), &z))
-	    return WINT2WV(z);
+	if (MUL_OVERFLOW_FIXWV_P(FIXWV2WINT(wx), FIXWV2WINT(wy))
+	    return WINT2WV(FIXWV2WINT(wx) * FIXWV2WINT(wy));
     }
 #endif
-    x = w2v(wx);
-    if (RB_TYPE_P(x, T_BIGNUM)) return v2w(rb_big_mul(x, w2v(wy)));
-    z = rb_funcall(x, '*', 1, w2v(wy));
-    if (RB_TYPE_P(z, T_RATIONAL) && RRATIONAL(z)->den == INT2FIX(1)) {
-        z = RRATIONAL(z)->num;
-    }
-    return v2w(z);
+    return v2w(mul(w2v(wx), w2v(wy)));
 }
 
 static wideval_t
@@ -582,28 +535,12 @@ num_exact(VALUE v) https://github.com/ruby/ruby/blob/trunk/time.c#L535
 static wideval_t
 rb_time_magnify(wideval_t w)
 {
-    if (FIXWV_P(w)) {
-	wideint_t z;
-	if (wi_mul(FIXWV2WINT(w), TIME_SCALE, &z))
-	    return WINT2WV(z);
-    }
     return wmul(w, WINT2FIXWV(TIME_SCALE));
 }
 
 static wideval_t
 rb_time_unmagnify(wideval_t w)
 {
-#if WIDEVALUE_IS_WIDER
-    if (FIXWV_P(w)) {
-        wideint_t a, b, c;
-        a = FIXWV2WINT(w);
-        b = TIME_SCALE;
-        c = a / b;
-        if (c * b == a) {
-            return WINT2FIXWV(c);
-        }
-    }
-#endif
     return wquo(w, WINT2FIXWV(TIME_SCALE));
 }
 

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

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