* bignum.c (rb_integer_pack): Returns sign instead of words.

(absint_numwords_generic): Follow the above change.
  (big2str_base_powerof2): Follow the above change.

* internal.h: Ditto.

* hash.c (rb_hash): Ditto.

* pack.c (pack_pack): Ditto.

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

* time.c (v2w_bignum): Ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-10 10:37:39 +00:00
parent a8aaf13392
commit 0e8caa7d0f
8 changed files with 54 additions and 33 deletions

View File

@ -1,3 +1,22 @@
Mon Jun 10 19:34:39 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (rb_integer_pack): Returns sign instead of words.
(absint_numwords_generic): Follow the above change.
(big2str_base_powerof2): Follow the above change.
* internal.h: Ditto.
* hash.c (rb_hash): Ditto.
* pack.c (pack_pack): Ditto.
* random.c (int_pair_to_real_inclusive): Ditto.
(rand_init): Ditto.
(random_load): Ditto.
(limited_big_rand): Ditto.
* time.c (v2w_bignum): Ditto.
Mon Jun 10 17:20:01 2013 Koichi Sasada <ko1@atdot.net> Mon Jun 10 17:20:01 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (rgengc_remember): permit promoted object. * gc.c (rgengc_remember): permit promoted object.

View File

@ -454,7 +454,7 @@ rb_big_unpack(unsigned long *buf, long num_longs)
/* number of bytes of abs(val). additionaly number of leading zeros can be returned. */ /* number of bytes of abs(val). additionaly number of leading zeros can be returned. */
/* /*
* Calculate a number of bytes to be required to represent * Calculate the number of bytes to be required to represent
* the absolute value of the integer given as _val_. * the absolute value of the integer given as _val_.
* *
* [val] an integer. * [val] an integer.
@ -610,7 +610,7 @@ absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_num
div = rb_funcall(div, '+', 1, LONG2FIX(1)); div = rb_funcall(div, '+', 1, LONG2FIX(1));
nlz_bits = word_numbits - NUM2SIZET(mod); nlz_bits = word_numbits - NUM2SIZET(mod);
} }
rb_integer_pack(div, &sign, &numwords, 1, sizeof(numwords), 0, sign = rb_integer_pack(div, &numwords, 1, sizeof(numwords), 0,
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign == 2) if (sign == 2)
return (size_t)-1; return (size_t)-1;
@ -619,7 +619,7 @@ absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_num
} }
/* /*
* Calculate a number of words to be required to represent * Calculate the number of words to be required to represent
* the absolute value of the integer given as _val_. * the absolute value of the integer given as _val_.
* *
* [val] an integer. * [val] an integer.
@ -843,23 +843,25 @@ integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
/* /*
* Export an integer into a buffer. * Export an integer into a buffer.
* *
* [val] Fixnum, Bignum or another object which has to_int. * [val] Fixnum, Bignum or another integer like object which has to_int method.
* [signp] signedness is returned in *signp if it is not NULL.
* 0 for zero.
* -1 for negative without overflow. 1 for positive without overflow.
* -2 for negative overflow. 2 for positive overflow.
* [words] buffer to export abs(val). * [words] buffer to export abs(val).
* [numwords] the size of given buffer as number of words. * [numwords] the size of given buffer as number of words.
* [wordsize] the size of word as number of bytes. * [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. * [nails] number of padding bits in a word.
* [flags] bitwise or of constants which name starts "INTEGER_PACK_". It specifies word order and byte order. * Most significant nails bits of each word are filled by zero.
* * [flags] bitwise or of constants which name starts "INTEGER_PACK_".
* This function returns words or the allocated buffer if words is NULL. * It specifies word order and byte order.
* *
* This function returns the signedness and overflow condition as follows:
* -2 : negative overflow.
* -1 : negative without overflow.
* 0 : zero.
* 1 : positive without overflow.
* 2 : positive overflow.
*/ */
void * int
rb_integer_pack(VALUE val, int *signp, void *words, size_t numwords, size_t wordsize, size_t nails, int flags) rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
{ {
int sign; int sign;
BDIGIT *dp; BDIGIT *dp;
@ -974,10 +976,7 @@ rb_integer_pack(VALUE val, int *signp, void *words, size_t numwords, size_t word
sign *= 2; /* overflow */ sign *= 2; /* overflow */
} }
if (signp) return sign;
*signp = sign;
return buf;
#undef FILL_DD #undef FILL_DD
#undef TAKE_LOWBITS #undef TAKE_LOWBITS
} }
@ -1055,8 +1054,12 @@ integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in
* [words] buffer to import. * [words] buffer to import.
* [numwords] the size of given buffer as number of words. * [numwords] the size of given buffer as number of words.
* [wordsize] the size of word as number of bytes. * [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 ignored. * [nails] number of padding bits in a word.
* [flags] bitwise or of constants which name starts "INTEGER_PACK_". It specifies word order and byte order. * Most significant nails bits of each word are ignored.
* [flags] bitwise or of constants which name starts "INTEGER_PACK_".
* It specifies word order and byte order.
* Also, INTEGER_PACK_FORCE_BIGNUM specifies that the result will be a Bignum
* even if it is representable as a Fixnum.
* *
* This function returns the imported integer as Fixnum or Bignum. * This function returns the imported integer as Fixnum or Bignum.
*/ */
@ -1882,7 +1885,7 @@ big2str_base_powerof2(VALUE x, size_t len, int base, int trim)
result = rb_usascii_str_new(0, numwords); result = rb_usascii_str_new(0, numwords);
ptr = RSTRING_PTR(result); ptr = RSTRING_PTR(result);
} }
rb_integer_pack(x, NULL, ptr, numwords, 1, CHAR_BIT-word_numbits, rb_integer_pack(x, ptr, numwords, 1, CHAR_BIT-word_numbits,
INTEGER_PACK_BIG_ENDIAN); INTEGER_PACK_BIG_ENDIAN);
while (0 < numwords) { while (0 < numwords) {
*ptr = ruby_digitmap[*(unsigned char *)ptr]; *ptr = ruby_digitmap[*(unsigned char *)ptr];

View File

@ -6,17 +6,16 @@ rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordsize_arg, VALUE nails, VALUE f
{ {
int sign; int sign;
size_t count = 0; size_t count = 0;
void *ret;
size_t wordsize = NUM2SIZET(wordsize_arg); size_t wordsize = NUM2SIZET(wordsize_arg);
StringValue(buf); StringValue(buf);
rb_str_modify(buf); rb_str_modify(buf);
count = wordsize == 0 ? 0 : RSTRING_LEN(buf) / wordsize; count = wordsize == 0 ? 0 : RSTRING_LEN(buf) / wordsize;
ret = rb_integer_pack(val, sign = rb_integer_pack(val,
&sign, RSTRING_PTR(buf), count, RSTRING_PTR(buf), count,
wordsize, NUM2SIZET(nails), NUM2INT(flags)); wordsize, NUM2SIZET(nails), NUM2INT(flags));
return rb_ary_new_from_args(3, INT2NUM(sign), ret ? rb_str_new(ret, wordsize * count) : Qnil, SIZET2NUM(count)); return rb_ary_new_from_args(3, INT2NUM(sign), rb_str_new(RSTRING_PTR(buf), wordsize * count), SIZET2NUM(count));
} }
static VALUE static VALUE

2
hash.c
View File

@ -92,7 +92,7 @@ rb_hash(VALUE obj)
{ {
int sign; int sign;
unsigned long ul; unsigned long ul;
rb_integer_pack(hval, &sign, &ul, 1, sizeof(ul), 0, sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1; ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1;
if (sign < 0) 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); VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd);
/* bignum.c */ /* bignum.c */
void *rb_integer_pack(VALUE val, int *signp, void *words, size_t numwords, size_t wordsize, size_t nails, int flags); int rb_integer_pack(VALUE val, 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); VALUE rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
/* io.c */ /* io.c */

2
pack.c
View File

@ -1023,7 +1023,7 @@ pack_pack(VALUE ary, VALUE fmt)
numbytes = 1; numbytes = 1;
buf = rb_str_new(NULL, numbytes); buf = rb_str_new(NULL, numbytes);
rb_integer_pack(from, &sign, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN); sign = rb_integer_pack(from, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
if (sign < 0) if (sign < 0)
rb_raise(rb_eArgError, "can't compress negative numbers"); 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 { else {
uint32_t uary[4]; uint32_t uary[4];
rb_integer_pack(x, NULL, uary, numberof(uary), sizeof(uint32_t), 0, rb_integer_pack(x, uary, numberof(uary), sizeof(uint32_t), 0,
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
/* r = x >> 64 */ /* r = x >> 64 */
r = (double)uary[0] * (0x10000 * (double)0x10000) + (double)uary[1]; 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; len = MT_MAX_STATE;
if (len > numberof(buf0)) if (len > numberof(buf0))
buf = ALLOC_N(unsigned int, len); buf = ALLOC_N(unsigned int, len);
rb_integer_pack(seed, &sign, buf, len, sizeof(uint32_t), 0, sign = rb_integer_pack(seed, buf, len, sizeof(uint32_t), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign < 0) if (sign < 0)
sign = -sign; sign = -sign;
@ -644,7 +644,7 @@ random_load(VALUE obj, VALUE dump)
default: default:
rb_raise(rb_eArgError, "wrong dump data"); rb_raise(rb_eArgError, "wrong dump data");
} }
rb_integer_pack(state, NULL, mt->state, numberof(mt->state), rb_integer_pack(state, mt->state, numberof(mt->state),
sizeof(*mt->state), 0, sizeof(*mt->state), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
x = NUM2ULONG(left); x = NUM2ULONG(left);
@ -754,7 +754,7 @@ limited_big_rand(struct MT *mt, VALUE limit)
tmp = ALLOCV_N(uint32_t, vtmp, len*2); tmp = ALLOCV_N(uint32_t, vtmp, len*2);
lim_array = tmp; lim_array = tmp;
rnd_array = tmp + len; rnd_array = tmp + len;
rb_integer_pack(limit, NULL, lim_array, len, sizeof(uint32_t), 0, rb_integer_pack(limit, lim_array, len, sizeof(uint32_t), 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
retry: retry:

2
time.c
View File

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