From be05c5798fd73c951228eeac95b0e164a2aed142 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 20 Mar 2016 09:53:50 +0000 Subject: [PATCH] numeric.c: optimize Fixnum<->Bignum comparisons * numeric.c (fix_gt, fix_ge, fix_lt, fix_le): optimize comparisons Fixnum against Bignum by rb_big_cmp in inversed order without new Bignum instance. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54201 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ numeric.c | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 25cd562c99..b14b9d6c78 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun Mar 20 18:53:49 2016 Nobuyoshi Nakada + + * numeric.c (fix_gt, fix_ge, fix_lt, fix_le): optimize comparisons + Fixnum against Bignum by rb_big_cmp in inversed order without + new Bignum instance. + Sun Mar 20 18:44:52 2016 NARUSE, Yui * time.c (add): remove FIXABLE() which is in LONG2NUM(). diff --git a/numeric.c b/numeric.c index 38e05b6292..4293374a64 100644 --- a/numeric.c +++ b/numeric.c @@ -3482,7 +3482,7 @@ fix_gt(VALUE x, VALUE y) return Qfalse; } else if (RB_TYPE_P(y, T_BIGNUM)) { - return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) > 0 ? Qtrue : Qfalse; + return rb_big_cmp(y, x) == INT2FIX(-1) ? Qtrue : Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse; @@ -3508,7 +3508,7 @@ fix_ge(VALUE x, VALUE y) return Qfalse; } else if (RB_TYPE_P(y, T_BIGNUM)) { - return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) >= 0 ? Qtrue : Qfalse; + return rb_big_cmp(y, x) != INT2FIX(+1) ? Qtrue : Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { VALUE rel = rb_integer_float_cmp(x, y); @@ -3534,7 +3534,7 @@ fix_lt(VALUE x, VALUE y) return Qfalse; } else if (RB_TYPE_P(y, T_BIGNUM)) { - return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) < 0 ? Qtrue : Qfalse; + return rb_big_cmp(y, x) == INT2FIX(+1) ? Qtrue : Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse; @@ -3560,7 +3560,7 @@ fix_le(VALUE x, VALUE y) return Qfalse; } else if (RB_TYPE_P(y, T_BIGNUM)) { - return FIX2INT(rb_big_cmp(rb_int2big(FIX2LONG(x)), y)) <= 0 ? Qtrue : Qfalse; + return rb_big_cmp(y, x) != INT2FIX(-1) ? Qtrue : Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { VALUE rel = rb_integer_float_cmp(x, y);