2019-12-04 03:16:30 -05:00
|
|
|
#include "internal/bignum.h"
|
2013-06-07 08:41:02 -04:00
|
|
|
|
|
|
|
static VALUE
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_integer_pack_raw_m(VALUE klass, VALUE val, VALUE buf, VALUE numwords_arg, VALUE wordsize_arg, VALUE nails, VALUE flags)
|
2013-06-07 08:41:02 -04:00
|
|
|
{
|
|
|
|
int sign;
|
2013-06-11 09:15:56 -04:00
|
|
|
size_t numwords = 0;
|
2013-06-07 08:41:02 -04:00
|
|
|
size_t wordsize = NUM2SIZET(wordsize_arg);
|
|
|
|
|
2013-06-09 01:53:00 -04:00
|
|
|
StringValue(buf);
|
|
|
|
rb_str_modify(buf);
|
2013-06-10 06:37:39 -04:00
|
|
|
sign = rb_integer_pack(val,
|
2013-06-11 09:15:56 -04:00
|
|
|
RSTRING_PTR(buf), NUM2SIZET(numwords_arg),
|
|
|
|
NUM2SIZET(wordsize_arg), NUM2SIZET(nails), NUM2INT(flags));
|
|
|
|
|
|
|
|
return rb_ary_new_from_args(2, INT2NUM(sign), rb_str_new(RSTRING_PTR(buf), wordsize * numwords));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_integer_pack_m(VALUE klass, VALUE val, VALUE numwords_arg, VALUE wordsize_arg, VALUE nails, VALUE flags)
|
2013-06-11 09:15:56 -04:00
|
|
|
{
|
|
|
|
int sign;
|
|
|
|
size_t numwords = NUM2SIZET(numwords_arg);
|
|
|
|
size_t wordsize = NUM2SIZET(wordsize_arg);
|
|
|
|
VALUE buf;
|
|
|
|
|
|
|
|
if (numwords != 0 && wordsize != 0 && LONG_MAX / wordsize < numwords)
|
|
|
|
rb_raise(rb_eArgError, "too big numwords * wordsize");
|
|
|
|
buf = rb_str_new(NULL, numwords * wordsize);
|
|
|
|
sign = rb_integer_pack(val,
|
|
|
|
RSTRING_PTR(buf), numwords,
|
2013-06-07 11:02:39 -04:00
|
|
|
wordsize, NUM2SIZET(nails), NUM2INT(flags));
|
2013-06-07 08:41:02 -04:00
|
|
|
|
2013-06-11 09:15:56 -04:00
|
|
|
return rb_assoc_new(INT2NUM(sign), buf);
|
2013-06-07 08:41:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2013-06-11 17:39:55 -04:00
|
|
|
rb_integer_unpack_m(VALUE klass, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
|
2013-06-07 08:41:02 -04:00
|
|
|
{
|
|
|
|
StringValue(buf);
|
|
|
|
|
2013-06-11 17:39:55 -04:00
|
|
|
return rb_integer_unpack(RSTRING_PTR(buf),
|
2013-06-11 09:15:56 -04:00
|
|
|
NUM2SIZET(numwords), NUM2SIZET(wordsize),
|
2013-06-07 11:02:39 -04:00
|
|
|
NUM2SIZET(nails), NUM2INT(flags));
|
2013-06-07 08:41:02 -04:00
|
|
|
}
|
|
|
|
|
2013-07-27 22:14:58 -04:00
|
|
|
static VALUE
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_integer_test_numbits_2comp_without_sign(VALUE klass, VALUE val)
|
2013-07-27 22:14:58 -04:00
|
|
|
{
|
|
|
|
size_t size;
|
2014-02-15 16:17:34 -05:00
|
|
|
int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val);
|
2013-07-27 22:14:58 -04:00
|
|
|
size = rb_absint_numwords(val, 1, NULL) - (neg && rb_absint_singlebit_p(val));
|
|
|
|
return SIZET2NUM(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_integer_test_numbytes_2comp_with_sign(VALUE klass, VALUE val)
|
2013-07-27 22:14:58 -04:00
|
|
|
{
|
2014-02-15 16:17:34 -05:00
|
|
|
int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val);
|
2013-07-27 22:14:58 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-07 08:41:02 -04:00
|
|
|
void
|
2013-07-18 12:07:03 -04:00
|
|
|
Init_intpack(VALUE klass)
|
2013-06-07 08:41:02 -04:00
|
|
|
{
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_define_singleton_method(klass, "test_pack_raw", rb_integer_pack_raw_m, 6);
|
|
|
|
rb_define_singleton_method(klass, "test_pack", rb_integer_pack_m, 5);
|
|
|
|
rb_define_singleton_method(klass, "test_unpack", rb_integer_unpack_m, 5);
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_MSWORD_FIRST", INT2NUM(INTEGER_PACK_MSWORD_FIRST));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_LSWORD_FIRST", INT2NUM(INTEGER_PACK_LSWORD_FIRST));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_MSBYTE_FIRST", INT2NUM(INTEGER_PACK_MSBYTE_FIRST));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_LSBYTE_FIRST", INT2NUM(INTEGER_PACK_LSBYTE_FIRST));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_NATIVE_BYTE_ORDER", INT2NUM(INTEGER_PACK_NATIVE_BYTE_ORDER));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_2COMP", INT2NUM(INTEGER_PACK_2COMP));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_LITTLE_ENDIAN", INT2NUM(INTEGER_PACK_LITTLE_ENDIAN));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_FORCE_BIGNUM", INT2NUM(INTEGER_PACK_FORCE_BIGNUM));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE));
|
|
|
|
rb_define_const(klass, "INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION", INT2NUM(INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION));
|
2013-07-27 22:14:58 -04:00
|
|
|
|
2021-01-08 05:07:16 -05:00
|
|
|
rb_define_singleton_method(klass, "test_numbits_2comp_without_sign", rb_integer_test_numbits_2comp_without_sign, 1);
|
|
|
|
rb_define_singleton_method(klass, "test_numbytes_2comp_with_sign", rb_integer_test_numbytes_2comp_with_sign, 1);
|
2013-06-07 08:41:02 -04:00
|
|
|
}
|