From bcc17969832e8f40200231f1a925c8f2318aa706 Mon Sep 17 00:00:00 2001 From: akr Date: Tue, 26 Apr 2016 11:17:37 +0000 Subject: {Fixnum,Bignum}#bit_length is unified into Integer. * numeric.c (rb_int_bit_length): {Fixnum,Bignum}#bit_length is unified into Integer. * bignum.c (rb_big_bit_length): Don't define Bignum#bit_length. * internal.h (rb_big_bit_length): Declared. --This iine, and those below, will be ignored-- M ChangeLog M bignum.c M internal.h M numeric.c git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54778 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 +++++++++++-- bignum.c | 40 +--------------------------------------- internal.h | 1 + numeric.c | 39 ++++++++++++++++++++++++++++++++------- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index c7afccf614..3b8687fd55 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,12 +1,21 @@ +Tue Apr 26 20:09:08 2016 Tanaka Akira + + * numeric.c (rb_int_bit_length): {Fixnum,Bignum}#bit_length is + unified into Integer. + + * bignum.c (rb_big_bit_length): Don't define Bignum#bit_length. + + * internal.h (rb_big_bit_length): Declared. + Tue Apr 26 19:56:16 2016 Tanaka Akira * numeric.c (int_abs): Integer#{abs,magnitude} moved from Fixnum and Bignum. - * internal.h (rb_big_abs): Declared. - * bignum.c (rb_big_abs): Don't define Bignum#{abs,magnitude}. + * internal.h (rb_big_abs): Declared. + Mon Apr 25 14:39:11 2016 Nobuyoshi Nakada * ext/rbconfig/sizeof/extconf.rb: just check the existence of each diff --git a/bignum.c b/bignum.c index 6fcbc74783..c87fdaa0fb 100644 --- a/bignum.c +++ b/bignum.c @@ -6920,45 +6920,7 @@ rb_big_size_m(VALUE big) return SIZET2NUM(rb_big_size(big)); } -/* - * call-seq: - * int.bit_length -> integer - * - * Returns the number of bits of the value of int. - * - * "the number of bits" means that - * the bit position of the highest bit which is different to the sign bit. - * (The bit position of the bit 2**n is n+1.) - * If there is no such bit (zero or minus one), zero is returned. - * - * I.e. This method returns ceil(log2(int < 0 ? -int : int+1)). - * - * (-2**10000-1).bit_length #=> 10001 - * (-2**10000).bit_length #=> 10000 - * (-2**10000+1).bit_length #=> 10000 - * - * (-2**1000-1).bit_length #=> 1001 - * (-2**1000).bit_length #=> 1000 - * (-2**1000+1).bit_length #=> 1000 - * - * (2**1000-1).bit_length #=> 1000 - * (2**1000).bit_length #=> 1001 - * (2**1000+1).bit_length #=> 1001 - * - * (2**10000-1).bit_length #=> 10000 - * (2**10000).bit_length #=> 10001 - * (2**10000+1).bit_length #=> 10001 - * - * This method can be used to detect overflow in Array#pack as follows. - * - * if n.bit_length < 32 - * [n].pack("l") # no overflow - * else - * raise "overflow" - * end - */ - -static VALUE +VALUE rb_big_bit_length(VALUE big) { int nlz_bits; diff --git a/internal.h b/internal.h index 5f79d4eb4b..a2250d5649 100644 --- a/internal.h +++ b/internal.h @@ -779,6 +779,7 @@ VALUE rb_integer_float_cmp(VALUE x, VALUE y); VALUE rb_integer_float_eq(VALUE x, VALUE y); VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base); VALUE rb_big_abs(VALUE x); +VALUE rb_big_bit_length(VALUE big); /* class.c */ VALUE rb_class_boot(VALUE); diff --git a/numeric.c b/numeric.c index 76a64ca9cc..7d3ad34ebb 100644 --- a/numeric.c +++ b/numeric.c @@ -4121,6 +4121,15 @@ fix_size(VALUE fix) return INT2FIX(sizeof(long)); } +static VALUE +rb_fix_bit_length(VALUE fix) +{ + long v = FIX2LONG(fix); + if (v < 0) + v = ~v; + return LONG2FIX(bit_length(v)); +} + /* * call-seq: * int.bit_length -> integer @@ -4133,7 +4142,14 @@ fix_size(VALUE fix) * If there is no such bit (zero or minus one), zero is returned. * * I.e. This method returns ceil(log2(int < 0 ? -int : int+1)). - * + + + * (-2**10000-1).bit_length #=> 10001 + * (-2**10000).bit_length #=> 10000 + * (-2**10000+1).bit_length #=> 10000 + * (-2**1000-1).bit_length #=> 1001 + * (-2**1000).bit_length #=> 1000 + * (-2**1000+1).bit_length #=> 1000 * (-2**12-1).bit_length #=> 13 * (-2**12).bit_length #=> 12 * (-2**12+1).bit_length #=> 12 @@ -4149,6 +4165,12 @@ fix_size(VALUE fix) * (2**12-1).bit_length #=> 12 * (2**12).bit_length #=> 13 * (2**12+1).bit_length #=> 13 + * (2**1000-1).bit_length #=> 1000 + * (2**1000).bit_length #=> 1001 + * (2**1000+1).bit_length #=> 1001 + * (2**10000-1).bit_length #=> 10000 + * (2**10000).bit_length #=> 10001 + * (2**10000+1).bit_length #=> 10001 * * This method can be used to detect overflow in Array#pack as follows. * @@ -4160,12 +4182,15 @@ fix_size(VALUE fix) */ static VALUE -rb_fix_bit_length(VALUE fix) +rb_int_bit_length(VALUE num) { - long v = FIX2LONG(fix); - if (v < 0) - v = ~v; - return LONG2FIX(bit_length(v)); + if (FIXNUM_P(num)) { + return rb_fix_bit_length(num); + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + return rb_big_bit_length(num); + } + return Qnil; } static VALUE @@ -4645,7 +4670,7 @@ Init_Numeric(void) rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1); rb_define_method(rb_cFixnum, "size", fix_size, 0); - rb_define_method(rb_cFixnum, "bit_length", rb_fix_bit_length, 0); + rb_define_method(rb_cInteger, "bit_length", rb_int_bit_length, 0); rb_define_method(rb_cFixnum, "succ", fix_succ, 0); rb_cFloat = rb_define_class("Float", rb_cNumeric); -- cgit v1.2.3