* bignum.c (rb_integer_pack): numwords_allocated argument removed.

* internal.h (rb_integer_pack): Follow the above change.

* hash.c (rb_hash): Ditto.

* time.c (v2w_bignum): Ditto.

* pack.c (pack_pack): Ditto.

* random.c (int_pair_to_real_inclusive): Ditto.
  (rand_init): Ditto.
  (random_load): Ditto.
  (limited_big_rand): Ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-09 05:53:00 +00:00
parent f6757f080c
commit 837392b452
9 changed files with 58 additions and 73 deletions

View File

@ -1,3 +1,20 @@
Sun Jun 9 14:41:05 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (rb_integer_pack): numwords_allocated argument removed.
* internal.h (rb_integer_pack): Follow the above change.
* hash.c (rb_hash): Ditto.
* time.c (v2w_bignum): Ditto.
* pack.c (pack_pack): Ditto.
* random.c (int_pair_to_real_inclusive): Ditto.
(rand_init): Ditto.
(random_load): Ditto.
(limited_big_rand): Ditto.
Sun Jun 9 09:34:44 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (big2str_base_powerof2): New function.

View File

@ -707,10 +707,8 @@ integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
* 0 for zero.
* -1 for negative without overflow. 1 for positive without overflow.
* -2 for negative overflow. 2 for positive overflow.
* [numwords_allocated] the number of words allocated is returned in *numwords_allocated if it is not NULL.
* It is not modified if words is not NULL.
* [words] buffer to export abs(val). allocated by xmalloc if it is NULL.
* [numwords] the size of given buffer as number of words (only meaningful when words is not NULL).
* [words] buffer to export abs(val).
* [numwords] the size of given buffer as number of words.
* [wordsize] the size of word as number of bytes.
* [nails] number of padding bits in a word. Most significant nails bits of each word are filled by zero.
* [flags] bitwise or of constants which name starts "INTEGER_PACK_". It specifies word order and byte order.
@ -720,7 +718,7 @@ integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
*/
void *
rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
rb_integer_pack(VALUE val, int *signp, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
{
int sign;
BDIGIT *dp;
@ -731,7 +729,7 @@ rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words,
val = rb_to_int(val);
validate_integer_pack_format(wordsize, nails, flags);
if (words && SIZE_MAX / wordsize < numwords)
if (SIZE_MAX / wordsize < numwords)
rb_raise(rb_eArgError, "too big count * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize);
if (FIXNUM_P(val)) {
@ -768,32 +766,8 @@ rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words,
sign = 0;
}
if (words) {
buf = words;
bufend = buf + numwords * wordsize;
}
else {
/*
* val_numbits = (de - dp) * SIZEOF_BDIGITS * CHAR_BIT - nlz(de[-1])
* word_numbits = wordsize * CHAR_BIT - nails
* numwords = (val_numbits + word_numbits - 1) / word_numbits
*/
VALUE val_numbits, word_numbits, numwordsv;
val_numbits = SIZET2NUM((de - dp) * SIZEOF_BDIGITS);
val_numbits = rb_funcall(val_numbits, '*', 1, LONG2FIX(CHAR_BIT));
if (dp != de)
val_numbits = rb_funcall(val_numbits, '-', 1, LONG2FIX(nlz(de[-1])));
word_numbits = SIZET2NUM(wordsize);
word_numbits = rb_funcall(word_numbits, '*', 1, LONG2FIX(CHAR_BIT));
if (nails != 0)
word_numbits = rb_funcall(word_numbits, '-', 1, SIZET2NUM(nails));
numwordsv = rb_funcall(val_numbits, '+', 1, word_numbits);
numwordsv = rb_funcall(numwordsv, '-', 1, LONG2FIX(1));
numwordsv = rb_funcall(numwordsv, rb_intern("div"), 1, word_numbits);
numwords = NUM2SIZET(numwordsv);
buf = xmalloc(numwords * wordsize);
bufend = buf + numwords * wordsize;
}
buf = words;
bufend = buf + numwords * wordsize;
if (buf == bufend) {
sign *= 2; /* overflow if non-zero*/
@ -862,9 +836,6 @@ rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words,
if (signp)
*signp = sign;
if (!words && numwords_allocated)
*numwords_allocated = numwords;
return buf;
#undef FILL_DD
#undef TAKE_LOWBITS
@ -1705,7 +1676,7 @@ big2str_base_powerof2(VALUE x, size_t len, int base, int trim)
result = rb_usascii_str_new(0, numwords);
ptr = RSTRING_PTR(result);
}
rb_integer_pack(x, NULL, NULL, ptr, numwords, 1, CHAR_BIT-word_numbits,
rb_integer_pack(x, NULL, ptr, numwords, 1, CHAR_BIT-word_numbits,
INTEGER_PACK_BIG_ENDIAN);
while (0 < numwords) {
*ptr = ruby_digitmap[*(unsigned char *)ptr];

View File

@ -9,14 +9,11 @@ rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordsize_arg, VALUE nails, VALUE f
void *ret;
size_t wordsize = NUM2SIZET(wordsize_arg);
if (!NIL_P(buf)) {
StringValue(buf);
rb_str_modify(buf);
count = RSTRING_LEN(buf) / wordsize;
}
StringValue(buf);
rb_str_modify(buf);
count = wordsize == 0 ? 0 : RSTRING_LEN(buf) / wordsize;
ret = rb_integer_pack(val,
&sign, &count, NIL_P(buf) ? NULL : RSTRING_PTR(buf), count,
&sign, RSTRING_PTR(buf), count,
wordsize, NUM2SIZET(nails), NUM2INT(flags));
return rb_ary_new_from_args(3, INT2NUM(sign), ret ? rb_str_new(ret, wordsize * count) : Qnil, SIZET2NUM(count));

2
hash.c
View File

@ -92,7 +92,7 @@ rb_hash(VALUE obj)
{
int sign;
unsigned long ul;
rb_integer_pack(hval, &sign, NULL, &ul, 1, sizeof(ul), 0,
rb_integer_pack(hval, &sign, &ul, 1, sizeof(ul), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1;
if (sign < 0)

View File

@ -447,7 +447,7 @@ const char *rb_objspace_data_type_name(VALUE obj);
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd);
/* bignum.c */
void *rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
void *rb_integer_pack(VALUE val, int *signp, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
VALUE rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
/* io.c */

2
pack.c
View File

@ -1023,7 +1023,7 @@ pack_pack(VALUE ary, VALUE fmt)
numbytes = 1;
buf = rb_str_new(NULL, numbytes);
rb_integer_pack(from, &sign, NULL, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
rb_integer_pack(from, &sign, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
if (sign < 0)
rb_raise(rb_eArgError, "can't compress negative numbers");

View File

@ -295,7 +295,7 @@ int_pair_to_real_inclusive(uint32_t a, uint32_t b)
}
else {
uint32_t uary[4];
rb_integer_pack(x, NULL, NULL, uary, numberof(uary), sizeof(uint32_t), 0,
rb_integer_pack(x, NULL, uary, numberof(uary), sizeof(uint32_t), 0,
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
/* r = x >> 64 */
r = (double)uary[0] * (0x10000 * (double)0x10000) + (double)uary[1];
@ -380,7 +380,7 @@ rand_init(struct MT *mt, VALUE vseed)
len = MT_MAX_STATE;
if (len > numberof(buf0))
buf = ALLOC_N(unsigned int, len);
rb_integer_pack(seed, &sign, NULL, buf, len, sizeof(uint32_t), 0,
rb_integer_pack(seed, &sign, buf, len, sizeof(uint32_t), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign < 0)
sign = -sign;
@ -644,7 +644,7 @@ random_load(VALUE obj, VALUE dump)
default:
rb_raise(rb_eArgError, "wrong dump data");
}
rb_integer_pack(state, NULL, NULL, mt->state, numberof(mt->state),
rb_integer_pack(state, NULL, mt->state, numberof(mt->state),
sizeof(*mt->state), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
x = NUM2ULONG(left);
@ -754,7 +754,7 @@ limited_big_rand(struct MT *mt, VALUE limit)
tmp = ALLOCV_N(uint32_t, vtmp, len*2);
lim_array = tmp;
rnd_array = tmp + len;
rb_integer_pack(limit, NULL, NULL, lim_array, len, sizeof(uint32_t), 0,
rb_integer_pack(limit, NULL, lim_array, len, sizeof(uint32_t), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
retry:

View File

@ -15,26 +15,26 @@ class TestBignum < Test::Unit::TestCase
BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN
def test_pack_zero
assert_equal([0, "", 0], 0.test_pack(nil, 1, 0, BIG_ENDIAN))
assert_equal([0, "", 0], 0.test_pack("", 1, 0, BIG_ENDIAN))
end
def test_pack_argument_check
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, MSBYTE_FIRST) }
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, MSWORD_FIRST) }
assert_raise(ArgumentError) { 0.test_pack(nil, 0, 0, BIG_ENDIAN) }
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 8, BIG_ENDIAN) }
assert_raise(ArgumentError) { 0.test_pack("", 1, 0, MSBYTE_FIRST) }
assert_raise(ArgumentError) { 0.test_pack("", 1, 0, MSWORD_FIRST) }
assert_raise(ArgumentError) { 0.test_pack("", 0, 0, BIG_ENDIAN) }
assert_raise(ArgumentError) { 0.test_pack("", 1, 8, BIG_ENDIAN) }
# assume sizeof(ssize_t) == sizeof(intptr_t)
assert_raise(ArgumentError) { 0.test_pack(nil, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
assert_raise(ArgumentError) { 0.test_pack("", 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
end
def test_pack_wordsize
assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 0, BIG_ENDIAN))
assert_equal([1, "\x00\x01", 1], 1.test_pack(nil, 2, 0, BIG_ENDIAN))
assert_equal([1, "\x00\x00\x01", 1], 1.test_pack(nil, 3, 0, BIG_ENDIAN))
assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 0, LITTLE_ENDIAN))
assert_equal([1, "\x01\x00", 1], 1.test_pack(nil, 2, 0, LITTLE_ENDIAN))
assert_equal([1, "\x01\x00\x00", 1], 1.test_pack(nil, 3, 0, LITTLE_ENDIAN))
assert_equal([1, "\x01", 1], 1.test_pack("x", 1, 0, BIG_ENDIAN))
assert_equal([1, "\x00\x01", 1], 1.test_pack("xx", 2, 0, BIG_ENDIAN))
assert_equal([1, "\x00\x00\x01", 1], 1.test_pack("xxx", 3, 0, BIG_ENDIAN))
assert_equal([1, "\x01", 1], 1.test_pack("x", 1, 0, LITTLE_ENDIAN))
assert_equal([1, "\x01\x00", 1], 1.test_pack("xx", 2, 0, LITTLE_ENDIAN))
assert_equal([1, "\x01\x00\x00", 1], 1.test_pack("xxx", 3, 0, LITTLE_ENDIAN))
end
def test_pack_fixed_buffer
@ -51,25 +51,25 @@ class TestBignum < Test::Unit::TestCase
end
def test_pack_wordorder_and_endian
assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack(nil, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack(nil, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack(nil, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack(nil, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack("xxxx", 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack("xxxx", 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack("xxxx", 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack("xxxx", 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
end
def test_pack_native_endian
assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack(nil, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack("xx", 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
end
def test_pack_nail
assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_pack(nil, 1, 7, BIG_ENDIAN))
assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack(nil, 1, 4, BIG_ENDIAN))
assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack(nil, 2, 8, BIG_ENDIAN))
assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_pack("xxxxxx", 1, 7, BIG_ENDIAN))
assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack("xxxxxxxx", 1, 4, BIG_ENDIAN))
assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack("xxxxxxxx", 2, 8, BIG_ENDIAN))
end
def test_pack_sign
assert_equal([-1, "\x01", 1], (-1).test_pack(nil, 1, 0, BIG_ENDIAN))
assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack(nil, 1, 0, BIG_ENDIAN))
assert_equal([-1, "\x01", 1], (-1).test_pack("x", 1, 0, BIG_ENDIAN))
assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack("xxxxxxxx", 1, 0, BIG_ENDIAN))
end
def test_unpack_zero

2
time.c
View File

@ -302,7 +302,7 @@ v2w_bignum(VALUE v)
int sign;
uwideint_t u;
wideint_t i;
rb_integer_pack(v, &sign, NULL, &u, 1, sizeof(i), 0,
rb_integer_pack(v, &sign, &u, 1, sizeof(i), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign == 0)
return WINT2FIXWV(0);