Skip to content
Browse files

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
  • Loading branch information...
1 parent f96afe1 commit 50aab6d401b1dc1416558c08190068bb43f7c673 @nobu nobu committed Jun 20, 2012
Showing with 20 additions and 0 deletions.
  1. +20 −0 numeric.c
View
20 numeric.c
@@ -150,17 +150,37 @@ 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)));
}
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)));
}

0 comments on commit 50aab6d

Please sign in to comment.
Something went wrong with that request. Please try again.