From e5ff9d58effb093653bcb12ed2865e1509e2b265 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 28 Jul 2013 02:14:58 +0000 Subject: * 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 --- ext/-test-/bignum/intpack.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'ext/-test-') 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); } -- cgit v1.2.3