From 95b0fed3714b87dcb40a16f33d9e3160f9945e38 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 20 Jun 2020 14:55:09 -0700 Subject: Make Integer#zero? a separated method and builtin (#3226) A prerequisite to fix https://bugs.ruby-lang.org/issues/15589 with JIT. This commit alone doesn't make a significant difference yet, but I thought this commit should be committed independently. This method override was discussed in [Misc #16961]. --- numeric.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 990d792245..76567f835b 100644 --- a/numeric.c +++ b/numeric.c @@ -39,6 +39,7 @@ #include "internal/variable.h" #include "ruby/encoding.h" #include "ruby/util.h" +#include "builtin.h" /* use IEEE 64bit values if not defined */ #ifndef FLT_RADIX @@ -768,21 +769,28 @@ num_abs(VALUE num) static VALUE num_zero_p(VALUE num) +{ + if (rb_equal(num, INT2FIX(0))) { + return Qtrue; + } + return Qfalse; +} + +static VALUE +int_zero_p(VALUE num) { if (FIXNUM_P(num)) { if (FIXNUM_ZERO_P(num)) { return Qtrue; } } - else if (RB_TYPE_P(num, T_BIGNUM)) { + else { + assert(RB_TYPE_P(num, T_BIGNUM)); if (rb_bigzero_p(num)) { /* this should not happen usually */ return Qtrue; } } - else if (rb_equal(num, INT2FIX(0))) { - return Qtrue; - } return Qfalse; } @@ -3307,7 +3315,7 @@ static VALUE int_anybits_p(VALUE num, VALUE mask) { mask = rb_to_int(mask); - return num_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue; + return int_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue; } /* @@ -3321,7 +3329,7 @@ static VALUE int_nobits_p(VALUE num, VALUE mask) { mask = rb_to_int(mask); - return num_zero_p(rb_int_and(num, mask)); + return int_zero_p(rb_int_and(num, mask)); } /* @@ -4722,7 +4730,7 @@ int_aref1(VALUE num, VALUE arg) if (!RTEST(num_negative_p(end))) { if (!excl) end = rb_int_plus(end, INT2FIX(1)); VALUE mask = generate_mask(end); - if (RTEST(num_zero_p(rb_int_and(num, mask)))) { + if (RTEST(int_zero_p(rb_int_and(num, mask)))) { return INT2FIX(0); } else { @@ -5844,3 +5852,5 @@ rb_float_new(double d) { return rb_float_new_inline(d); } + +#include "integer.rbinc" -- cgit v1.2.3