* bignum.c (bary_pack): Support

INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION flag.
  Fix byte order and word order handling in code specialized for
  wordsize % SIZEOF_BDIGITS == 0.

* internal.h (INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION): Defined.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-22 09:55:27 +00:00
parent d73b9320cc
commit 8f0c3ff6e4
5 changed files with 189 additions and 139 deletions

View File

@ -1,3 +1,12 @@
Sat Jun 22 18:53:10 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bary_pack): Support
INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION flag.
Fix byte order and word order handling in code specialized for
wordsize % SIZEOF_BDIGITS == 0.
* internal.h (INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION): Defined.
Sat Jun 22 15:41:25 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (rgengc_check_shady): add new WB miss checking

View File

@ -911,7 +911,8 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
INTEGER_PACK_MSBYTE_FIRST|
INTEGER_PACK_LSBYTE_FIRST|
INTEGER_PACK_NATIVE_BYTE_ORDER|
INTEGER_PACK_2COMP);
INTEGER_PACK_2COMP|
INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION);
while (dp < de && de[-1] == 0)
de--;
@ -919,6 +920,7 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
sign = 0;
}
if (!(flags & INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION)) {
if (sign == 0) {
MEMZERO(words, unsigned char, numwords * wordsize);
return 0;
@ -1041,11 +1043,10 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
if (nails == 0 && SIZEOF_BDIGITS == sizeof(BDIGIT) &&
wordsize % SIZEOF_BDIGITS == 0 && (uintptr_t)words % ALIGNOF(BDIGIT) == 0) {
size_t buf_num_bdigits = numwords * wordsize / SIZEOF_BDIGITS;
int need_swap =
(flags & INTEGER_PACK_BYTEORDER_MASK) != INTEGER_PACK_NATIVE_BYTE_ORDER &&
((flags & INTEGER_PACK_MSBYTE_FIRST) ? !HOST_BIGENDIAN_P : HOST_BIGENDIAN_P);
size_t i;
int overflow = 0;
int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0;
int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P :
(flags & INTEGER_PACK_MSBYTE_FIRST) != 0;
if (num_bdigits <= buf_num_bdigits) {
MEMCPY(words, dp, BDIGIT, num_bdigits);
MEMZERO((BDIGIT*)words + num_bdigits, BDIGIT, buf_num_bdigits - num_bdigits);
@ -1062,13 +1063,29 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
zero_p)
overflow = 0;
}
if (need_swap) {
if (msbytefirst_p != HOST_BIGENDIAN_P) {
size_t i;
for (i = 0; i < buf_num_bdigits; i++) {
BDIGIT d = ((BDIGIT*)words)[i];
((BDIGIT*)words)[i] = swap_bdigit(d);
}
}
if (flags & INTEGER_PACK_MSWORD_FIRST) {
if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
size_t i;
BDIGIT *p = words;
for (i = 0; i < numwords; i++) {
BDIGIT *p1 = p, *p2 = p1 + wordsize/SIZEOF_BDIGITS - 1;
while (p1 < p2) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
p += wordsize/SIZEOF_BDIGITS;
}
}
if (mswordfirst_p) {
BDIGIT *p1 = words, *p2 = p1 + buf_num_bdigits - 1;
while (p1 < p2) {
BDIGIT tmp = *p1;
@ -1082,6 +1099,7 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
sign *= 2;
return sign;
}
}
buf = words;
bufend = buf + numwords * wordsize;

View File

@ -61,4 +61,5 @@ Init_pack(VALUE klass)
rb_define_const(rb_cInteger, "INTEGER_PACK_BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN));
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));
}

View File

@ -62,6 +62,7 @@ extern "C" {
#define INTEGER_PACK_LSBYTE_FIRST 0x20
#define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40
#define INTEGER_PACK_2COMP 0x80
#define INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION 0x400
/* For rb_integer_unpack: */
#define INTEGER_PACK_FORCE_BIGNUM 0x100
#define INTEGER_PACK_NEGATIVE 0x200

View File

@ -15,6 +15,7 @@ class TestBignum < Test::Unit::TestCase
LITTLE_ENDIAN = Integer::INTEGER_PACK_LITTLE_ENDIAN
BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN
NEGATIVE = Integer::INTEGER_PACK_NEGATIVE
GENERIC = Integer::INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION
def test_pack_zero
assert_equal([0, ""], 0.test_pack(0, 1, 0, BIG_ENDIAN))
@ -123,6 +124,25 @@ class TestBignum < Test::Unit::TestCase
assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10"], (-0x8070605040302010).test_pack(8, 1, 0, BIG_ENDIAN))
end
def test_pack_orders
[MSWORD_FIRST, LSWORD_FIRST].each {|word_order|
[MSBYTE_FIRST, LSBYTE_FIRST, NATIVE_BYTE_ORDER].each {|byte_order|
1.upto(16) {|wordsize|
1.upto(20) {|numwords|
w = numwords*wordsize
n = 0;
0.upto(w) {|i|
n |= ((i+1) % 256) << (i*8)
}
assert_equal(n.test_pack(numwords, wordsize, 0, word_order|byte_order|GENERIC),
n.test_pack(numwords, wordsize, 0, word_order|byte_order),
"#{'%#x' % n}.test_pack(#{numwords}, #{wordsize}, 0, #{'%#x' % (word_order|byte_order)})")
}
}
}
}
end
def test_pack2comp_zero
assert_equal([0, ""], 0.test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
end
@ -273,6 +293,7 @@ class TestBignum < Test::Unit::TestCase
def test_unpack2comp_negative_zero
0.upto(100) {|n|
assert_equal(-(256**n), Integer.test_unpack("\x00"*n, n, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
assert_equal(-(256**n), Integer.test_unpack("\x00"*n, n, 1, 0, TWOCOMP|LITTLE_ENDIAN|NEGATIVE))
}
end
end