diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-28 02:14:58 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-28 02:14:58 +0000 |
commit | e5ff9d58effb093653bcb12ed2865e1509e2b265 (patch) | |
tree | cd87442e70cdbebe9e13f3e7d7f82781dffa20ac /ext | |
parent | 56150eba928407b5b1410219292caf095db5ee63 (diff) | |
download | ruby-e5ff9d58effb093653bcb12ed2865e1509e2b265.tar.gz |
* include/ruby/intern.h (rb_absint_size): Declaration moved from
internal.h to calculate required buffer size to pack integers.
(rb_absint_numwords): Ditto.
(rb_absint_singlebit_p): Ditto.
[ruby-core:42813] [Feature #6065]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42208 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/-test-/bignum/intpack.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/ext/-test-/bignum/intpack.c b/ext/-test-/bignum/intpack.c index 909294d88c..03023196e6 100644 --- a/ext/-test-/bignum/intpack.c +++ b/ext/-test-/bignum/intpack.c @@ -45,6 +45,26 @@ rb_integer_unpack_m(VALUE klass, VALUE buf, VALUE numwords, VALUE wordsize, VALU NUM2SIZET(nails), NUM2INT(flags)); } +static VALUE +rb_integer_test_numbits_2comp_without_sign(VALUE val) +{ + size_t size; + int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : RBIGNUM_NEGATIVE_P(val); + size = rb_absint_numwords(val, 1, NULL) - (neg && rb_absint_singlebit_p(val)); + return SIZET2NUM(size); +} + +static VALUE +rb_integer_test_numbytes_2comp_with_sign(VALUE val) +{ + int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : RBIGNUM_NEGATIVE_P(val); + int nlz_bits; + size_t size = rb_absint_size(val, &nlz_bits); + if (nlz_bits == 0 && !(neg && rb_absint_singlebit_p(val))) + size++; + return SIZET2NUM(size); +} + void Init_intpack(VALUE klass) { @@ -62,4 +82,7 @@ Init_intpack(VALUE klass) rb_define_const(rb_cInteger, "INTEGER_PACK_FORCE_BIGNUM", INT2NUM(INTEGER_PACK_FORCE_BIGNUM)); rb_define_const(rb_cInteger, "INTEGER_PACK_NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE)); rb_define_const(rb_cInteger, "INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION", INT2NUM(INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION)); + + rb_define_method(rb_cInteger, "test_numbits_2comp_without_sign", rb_integer_test_numbits_2comp_without_sign, 0); + rb_define_method(rb_cInteger, "test_numbytes_2comp_with_sign", rb_integer_test_numbytes_2comp_with_sign, 0); } |