1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* time.c (add): shortcut implemented for fixnums.

(sub): ditto.
  (mul): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24710 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-08-30 04:01:42 +00:00
parent 570d8f55b2
commit dcf2aad3a5
2 changed files with 33 additions and 9 deletions

View file

@ -1,3 +1,9 @@
Sun Aug 30 13:00:11 2009 Tanaka Akira <akr@fsij.org>
* time.c (add): shortcut implemented for fixnums.
(sub): ditto.
(mul): ditto.
Sun Aug 30 10:24:43 2009 Tanaka Akira <akr@fsij.org> Sun Aug 30 10:24:43 2009 Tanaka Akira <akr@fsij.org>
* time.c (eq): apply RTEST. * time.c (eq): apply RTEST.

36
time.c
View file

@ -116,28 +116,46 @@ static ID id_eq, id_ne, id_quo, id_div, id_cmp, id_lshift;
static VALUE static VALUE
add(VALUE x, VALUE y) add(VALUE x, VALUE y)
{ {
switch (TYPE(x)) { if (FIXNUM_P(x) && FIXNUM_P(y)) {
case T_BIGNUM: return rb_big_plus(x, y); long l = FIX2LONG(x) + FIX2LONG(y);
default: return rb_funcall(x, '+', 1, 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 static VALUE
sub(VALUE x, VALUE y) sub(VALUE x, VALUE y)
{ {
switch (TYPE(x)) { if (FIXNUM_P(x) && FIXNUM_P(y)) {
case T_BIGNUM: return rb_big_minus(x, y); long l = FIX2LONG(x) - FIX2LONG(y);
default: return rb_funcall(x, '-', 1, 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 static VALUE
mul(VALUE x, VALUE y) mul(VALUE x, VALUE y)
{ {
switch (TYPE(x)) { if (FIXNUM_P(x) && FIXNUM_P(y)) {
case T_BIGNUM: return rb_big_mul(x, y); #if HAVE_LONG_LONG && SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
default: return rb_funcall(x, '*', 1, y); 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))) #define div(x,y) (rb_funcall((x), id_div, 1, (y)))