зеркало из https://github.com/github/ruby.git
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
0cd2058bb3
Коммит
782e379f29
13
ChangeLog
13
ChangeLog
|
@ -1,3 +1,16 @@
|
||||||
|
Sun Apr 4 22:18:32 2010 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Sun Apr 4 10:04:28 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* include/ruby/ruby.h: replace snprintf and vsnprintf by
|
* include/ruby/ruby.h: replace snprintf and vsnprintf by
|
||||||
|
|
92
time.c
92
time.c
|
@ -31,8 +31,6 @@ static ID id_eq, id_ne, id_quo, id_div, id_cmp, id_lshift;
|
||||||
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
|
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
|
||||||
#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
|
#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
|
static int
|
||||||
eq(VALUE x, VALUE y)
|
eq(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
|
@ -43,18 +41,23 @@ eq(VALUE x, VALUE y)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lt(VALUE x, VALUE y)
|
cmp(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(x) && FIXNUM_P(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 ne(x,y) (!eq((x),(y)))
|
||||||
#define gt(x,y) (lt(y,x))
|
#define lt(x,y) (cmp((x),(y)) < 0)
|
||||||
#define le(x,y) (!gt(x,y))
|
#define gt(x,y) (cmp((x),(y)) > 0)
|
||||||
#define ge(x,y) (!lt(x,y))
|
#define le(x,y) (cmp((x),(y)) <= 0)
|
||||||
|
#define ge(x,y) (cmp((x),(y)) >= 0)
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
add(VALUE x, VALUE y)
|
add(VALUE x, VALUE y)
|
||||||
|
@ -163,15 +166,14 @@ quo(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
VALUE ret;
|
VALUE ret;
|
||||||
if (FIXNUM_P(x) && FIXNUM_P(y)) {
|
if (FIXNUM_P(x) && FIXNUM_P(y)) {
|
||||||
long a, b, c;
|
long a, b, c;
|
||||||
a = FIX2LONG(x);
|
a = FIX2LONG(x);
|
||||||
b = FIX2LONG(y);
|
b = FIX2LONG(y);
|
||||||
if (b == 0) rb_num_zerodiv();
|
if (b == 0) rb_num_zerodiv();
|
||||||
c = a / b;
|
c = a / b;
|
||||||
if (c * b == a) {
|
if (c * b == a) {
|
||||||
return LONG2NUM(c);
|
return LONG2NUM(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
ret = rb_funcall(x, id_quo, 1, y);
|
ret = rb_funcall(x, id_quo, 1, y);
|
||||||
if (TYPE(ret) == T_RATIONAL &&
|
if (TYPE(ret) == T_RATIONAL &&
|
||||||
|
@ -391,7 +393,7 @@ v2w(VALUE v)
|
||||||
return WIDEVAL_WRAP(v);
|
return WIDEVAL_WRAP(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int
|
static int
|
||||||
weq(wideval_t wx, wideval_t wy)
|
weq(wideval_t wx, wideval_t wy)
|
||||||
{
|
{
|
||||||
#if WIDEVALUE_IS_WIDER
|
#if WIDEVALUE_IS_WIDER
|
||||||
|
@ -404,23 +406,32 @@ weq(wideval_t wx, wideval_t wy)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int
|
static int
|
||||||
wlt(wideval_t wx, wideval_t wy)
|
wcmp(wideval_t wx, wideval_t wy)
|
||||||
{
|
{
|
||||||
|
VALUE x, y;
|
||||||
#if WIDEVALUE_IS_WIDER
|
#if WIDEVALUE_IS_WIDER
|
||||||
if (FIXWV_P(wx) && FIXWV_P(wy)) {
|
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
|
#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 wne(x,y) (!weq((x),(y)))
|
||||||
#define wgt(x,y) (wlt(y,x))
|
#define wlt(x,y) (wcmp((x),(y)) < 0)
|
||||||
#define wle(x,y) (!wgt(x,y))
|
#define wgt(x,y) (wcmp((x),(y)) > 0)
|
||||||
#define wge(x,y) (!wlt(x,y))
|
#define wle(x,y) (wcmp((x),(y)) <= 0)
|
||||||
|
#define wge(x,y) (wcmp((x),(y)) >= 0)
|
||||||
|
|
||||||
static wideval_t
|
static wideval_t
|
||||||
wadd(wideval_t wx, wideval_t wy)
|
wadd(wideval_t wx, wideval_t wy)
|
||||||
|
@ -516,27 +527,6 @@ wmul(wideval_t wx, wideval_t wy)
|
||||||
return v2w(z);
|
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
|
static wideval_t
|
||||||
wquo(wideval_t wx, wideval_t wy)
|
wquo(wideval_t wx, wideval_t wy)
|
||||||
{
|
{
|
||||||
|
@ -3176,7 +3166,7 @@ time_subsec(VALUE time)
|
||||||
struct time_object *tobj;
|
struct time_object *tobj;
|
||||||
|
|
||||||
GetTimeval(time, 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Загрузка…
Ссылка в новой задаче