ruby-changes:12968
From: akr <ko1@a...>
Date: Sun, 30 Aug 2009 13:02:02 +0900 (JST)
Subject: [ruby-changes:12968] Ruby:r24710 (trunk): * time.c (add): shortcut implemented for fixnums.
akr 2009-08-30 13:01:42 +0900 (Sun, 30 Aug 2009) New Revision: 24710 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24710 Log: * time.c (add): shortcut implemented for fixnums. (sub): ditto. (mul): ditto. Modified files: trunk/ChangeLog trunk/time.c Index: time.c =================================================================== --- time.c (revision 24709) +++ time.c (revision 24710) @@ -116,28 +116,46 @@ static VALUE add(VALUE x, VALUE y) { - switch (TYPE(x)) { - case T_BIGNUM: return rb_big_plus(x, y); - default: return rb_funcall(x, '+', 1, y); + if (FIXNUM_P(x) && FIXNUM_P(y)) { + long l = FIX2LONG(x) + FIX2LONG(y); + if (FIXABLE(l)) return LONG2FIX(l); + return LONG2NUM(l); } + if (TYPE(x) == T_BIGNUM) return rb_big_plus(x, y); + return rb_funcall(x, '+', 1, y); } static VALUE sub(VALUE x, VALUE y) { - switch (TYPE(x)) { - case T_BIGNUM: return rb_big_minus(x, y); - default: return rb_funcall(x, '-', 1, y); + if (FIXNUM_P(x) && FIXNUM_P(y)) { + long l = FIX2LONG(x) - FIX2LONG(y); + if (FIXABLE(l)) return LONG2FIX(l); + return LONG2NUM(l); } + if (TYPE(x) == T_BIGNUM) return rb_big_minus(x, y); + return rb_funcall(x, '-', 1, y); } static VALUE mul(VALUE x, VALUE y) { - switch (TYPE(x)) { - case T_BIGNUM: return rb_big_mul(x, y); - default: return rb_funcall(x, '*', 1, y); + if (FIXNUM_P(x) && FIXNUM_P(y)) { +#if HAVE_LONG_LONG && SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG + LONG_LONG ll = (LONG_LONG)FIX2LONG(x) * FIX2LONG(y); + if (FIXABLE(ll)) return LONG2FIX(ll); + return LL2NUM(ll); +#else + long a, b, c; + a = FIX2LONG(x); + if (a == 0) return x; + b = FIX2LONG(y); + c = a * b; + if (c / a == b && FIXABLE(c)) return LONG2FIX(c); +#endif } + if (TYPE(x) == T_BIGNUM) return rb_big_mul(x, y); + return rb_funcall(x, '*', 1, y); } #define div(x,y) (rb_funcall((x), id_div, 1, (y))) Index: ChangeLog =================================================================== --- ChangeLog (revision 24709) +++ ChangeLog (revision 24710) @@ -1,3 +1,9 @@ +Sun Aug 30 13:00:11 2009 Tanaka Akira <akr@f...> + + * time.c (add): shortcut implemented for fixnums. + (sub): ditto. + (mul): ditto. + Sun Aug 30 10:24:43 2009 Tanaka Akira <akr@f...> * time.c (eq): apply RTEST. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/