1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

{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
This commit is contained in:
akr 2016-04-26 11:17:37 +00:00
parent 9368d7515b
commit bcc1796983
4 changed files with 45 additions and 48 deletions

View file

@ -1,12 +1,21 @@
Tue Apr 26 20:09:08 2016 Tanaka Akira <akr@fsij.org>
* 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 <akr@fsij.org>
* 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 <nobu@ruby-lang.org>
* ext/rbconfig/sizeof/extconf.rb: just check the existence of each

View file

@ -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 <i>int</i>.
*
* "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;

View file

@ -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);

View file

@ -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);