From 50aab6d401b1dc1416558c08190068bb43f7c673 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 20 Jun 2012 06:31:35 +0000 Subject: numeric.c: optimize * numeric.c (positive_int_p, negative_int_p): optimize. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36150 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index bbd64b6d1a..42e923fe5a 100644 --- a/numeric.c +++ b/numeric.c @@ -150,10 +150,21 @@ rb_num_to_uint(VALUE val, unsigned int *ret) return NUMERR_TYPE; } +#define method_basic_p(klass) rb_method_basic_definition_p(klass, mid) + static inline int positive_int_p(VALUE num) { const ID mid = '>'; + + if (FIXNUM_P(num)) { + if (method_basic_p(rb_cFixnum)) + return (SIGNED_VALUE)num > 0; + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + if (method_basic_p(rb_cBignum)) + return RBIGNUM_POSITIVE_P(num); + } return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); } @@ -161,6 +172,15 @@ static inline int negative_int_p(VALUE num) { const ID mid = '<'; + + if (FIXNUM_P(num)) { + if (method_basic_p(rb_cFixnum)) + return (SIGNED_VALUE)num < 0; + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + if (method_basic_p(rb_cBignum)) + return RBIGNUM_NEGATIVE_P(num); + } return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); } -- cgit v1.2.3