summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-22 11:38:19 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-22 11:38:19 +0000
commit972ae3e5389d4b35d6252b05e3bc0a4f22f29fc7 (patch)
tree160a5dfd5197e2d96ff7175a0a3bbaacb6344b88
parent8f0c3ff6e49fad5263f9be52946e1c13f9bcf021 (diff)
* bignum.c (bary_unpack_internal): Specialized unpacker implemented.
(bary_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION. (rb_integer_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--bignum.c171
-rw-r--r--test/-ext-/bignum/test_pack.rb29
3 files changed, 195 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index 317b447572..c3938ba9a2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sat Jun 22 20:36:50 2013 Tanaka Akira <akr@fsij.org>
+
+ * bignum.c (bary_unpack_internal): Specialized unpacker implemented.
+ (bary_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION.
+ (rb_integer_unpack): Support INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION.
+
Sat Jun 22 18:53:10 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bary_pack): Support
diff --git a/bignum.c b/bignum.c
index 828c24f0a4..409d83c076 100644
--- a/bignum.c
+++ b/bignum.c
@@ -1457,13 +1457,167 @@ static int
bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags, int nlp_bits)
{
int sign;
+ const unsigned char *buf = words;
+ BDIGIT *dp;
+ BDIGIT *de;
- if (num_bdigits != 0) {
- const unsigned char *buf = words;
+ dp = bdigits;
+ de = dp + num_bdigits;
- BDIGIT *dp;
- BDIGIT *de;
+ if (!(flags & INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION)) {
+ if (nails == 0 && numwords == 1) {
+ int need_swap = wordsize != 1 &&
+ (flags & INTEGER_PACK_BYTEORDER_MASK) != INTEGER_PACK_NATIVE_BYTE_ORDER &&
+ ((flags & INTEGER_PACK_MSBYTE_FIRST) ? !HOST_BIGENDIAN_P : HOST_BIGENDIAN_P);
+ if (wordsize == 1) {
+ BDIGIT u = *(uint8_t *)buf;
+ if (flags & INTEGER_PACK_2COMP) {
+ sign = (flags & INTEGER_PACK_NEGATIVE) ?
+ ((sizeof(uint8_t) == SIZEOF_BDIGITS && u == 0) ? -2 : -1) :
+ ((u >> (sizeof(uint8_t) * CHAR_BIT - 1)) ? -1 : 1);
+ if (sign < 0) u = -(u | LSHIFTX((~(BDIGIT)0), sizeof(uint8_t) * CHAR_BIT));
+ }
+ else
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ *dp = u;
+ return sign;
+ }
+#if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGITS
+ if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
+ BDIGIT u = *(uint16_t *)buf;
+ if (need_swap) u = swap16(u);
+ if (flags & INTEGER_PACK_2COMP) {
+ sign = (flags & INTEGER_PACK_NEGATIVE) ?
+ ((sizeof(uint16_t) == SIZEOF_BDIGITS && u == 0) ? -2 : -1) :
+ ((u >> (sizeof(uint16_t) * CHAR_BIT - 1)) ? -1 : 1);
+ if (sign < 0) u = -(u | LSHIFTX((~(BDIGIT)0), sizeof(uint16_t) * CHAR_BIT));
+ }
+ else
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ *dp = u;
+ return sign;
+ }
+#endif
+#if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGITS
+ if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
+ BDIGIT u = *(uint32_t *)buf;
+ if (need_swap) u = swap32(u);
+ if (flags & INTEGER_PACK_2COMP) {
+ sign = (flags & INTEGER_PACK_NEGATIVE) ?
+ ((sizeof(uint32_t) == SIZEOF_BDIGITS && u == 0) ? -2 : -1) :
+ ((u >> (sizeof(uint32_t) * CHAR_BIT - 1)) ? -1 : 1);
+ if (sign < 0) u = -(u | LSHIFTX((~(BDIGIT)0), sizeof(uint32_t) * CHAR_BIT));
+ }
+ else
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ *dp = u;
+ return sign;
+ }
+#endif
+#if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGITS
+ if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
+ BDIGIT u = *(uint64_t *)buf;
+ if (need_swap) u = swap64(u);
+ if (flags & INTEGER_PACK_2COMP) {
+ sign = (flags & INTEGER_PACK_NEGATIVE) ?
+ ((sizeof(uint64_t) == SIZEOF_BDIGITS && u == 0) ? -2 : -1) :
+ ((u >> (sizeof(uint64_t) * CHAR_BIT - 1)) ? -1 : 1);
+ if (sign < 0) u = -(u | LSHIFTX((~(BDIGIT)0), sizeof(uint64_t) * CHAR_BIT));
+ }
+ else
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ *dp = u;
+ return sign;
+ }
+#endif
+ }
+#if !defined(WORDS_BIGENDIAN)
+ if (nails == 0 && SIZEOF_BDIGITS == sizeof(BDIGIT) &&
+ (flags & INTEGER_PACK_WORDORDER_MASK) == INTEGER_PACK_LSWORD_FIRST &&
+ (flags & INTEGER_PACK_BYTEORDER_MASK) != INTEGER_PACK_MSBYTE_FIRST) {
+ size_t src_size = numwords * wordsize;
+ size_t dst_size = num_bdigits * SIZEOF_BDIGITS;
+ MEMCPY(dp, words, char, src_size);
+ if (flags & INTEGER_PACK_2COMP) {
+ if (flags & INTEGER_PACK_NEGATIVE) {
+ int zero_p;
+ memset((char*)dp + src_size, 0xff, dst_size - src_size);
+ zero_p = bary_2comp(dp, num_bdigits);
+ sign = zero_p ? -2 : -1;
+ }
+ else if (buf[src_size-1] >> (CHAR_BIT-1)) {
+ memset((char*)dp + src_size, 0xff, dst_size - src_size);
+ bary_2comp(dp, num_bdigits);
+ sign = -1;
+ }
+ else {
+ MEMZERO((char*)dp + src_size, char, dst_size - src_size);
+ sign = 1;
+ }
+ }
+ else {
+ MEMZERO((char*)dp + src_size, char, dst_size - src_size);
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ }
+ return sign;
+ }
+#endif
+ if (nails == 0 && SIZEOF_BDIGITS == sizeof(BDIGIT) &&
+ wordsize % SIZEOF_BDIGITS == 0) {
+ size_t bdigits_per_word = wordsize / SIZEOF_BDIGITS;
+ 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;
+ MEMCPY(dp, words, BDIGIT, numwords*bdigits_per_word);
+ if (mswordfirst_p) {
+ BDIGIT *p1 = dp, *p2 = de - 1;
+ for (; p1 < p2; p1++, p2--) {
+ BDIGIT tmp = *p1;
+ *p1 = *p2;
+ *p2 = tmp;
+ }
+ }
+ if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
+ size_t i;
+ BDIGIT *p = dp;
+ for (i = 0; i < numwords; i++) {
+ BDIGIT *p1 = p, *p2 = p1 + bdigits_per_word - 1;
+ for (; p1 < p2; p1++, p2--) {
+ BDIGIT tmp = *p1;
+ *p1 = *p2;
+ *p2 = tmp;
+ }
+ p += bdigits_per_word;
+ }
+ }
+ if (msbytefirst_p != HOST_BIGENDIAN_P) {
+ BDIGIT *p;
+ for (p = dp; p < de; p++) {
+ BDIGIT d = *p;
+ *p = swap_bdigit(d);
+ }
+ }
+ if (flags & INTEGER_PACK_2COMP) {
+ if (flags & INTEGER_PACK_NEGATIVE) {
+ int zero_p = bary_2comp(dp, num_bdigits);
+ sign = zero_p ? -2 : -1;
+ }
+ else if (de[-1] >> (BITSPERDIG-1)) {
+ bary_2comp(dp, num_bdigits);
+ sign = -1;
+ }
+ else {
+ sign = 1;
+ }
+ }
+ else {
+ sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
+ }
+ return sign;
+ }
+ }
+ if (num_bdigits != 0) {
int word_num_partialbits;
size_t word_num_fullbytes;
@@ -1476,9 +1630,6 @@ bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, siz
BDIGIT_DBL dd;
int numbits_in_dd;
- dp = bdigits;
- de = dp + num_bdigits;
-
integer_pack_loop_setup(numwords, wordsize, nails, flags,
&word_num_fullbytes, &word_num_partialbits,
&word_start, &word_step, &word_last, &byte_start, &byte_step);
@@ -1568,7 +1719,8 @@ bary_unpack(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwo
INTEGER_PACK_NATIVE_BYTE_ORDER|
INTEGER_PACK_2COMP|
INTEGER_PACK_FORCE_BIGNUM|
- INTEGER_PACK_NEGATIVE);
+ INTEGER_PACK_NEGATIVE|
+ INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION);
num_bdigits0 = integer_unpack_num_bdigits(numwords, wordsize, nails, &nlp_bits);
@@ -1644,7 +1796,8 @@ rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t na
INTEGER_PACK_NATIVE_BYTE_ORDER|
INTEGER_PACK_2COMP|
INTEGER_PACK_FORCE_BIGNUM|
- INTEGER_PACK_NEGATIVE);
+ INTEGER_PACK_NEGATIVE|
+ INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION);
num_bdigits = integer_unpack_num_bdigits(numwords, wordsize, nails, &nlp_bits);
diff --git a/test/-ext-/bignum/test_pack.rb b/test/-ext-/bignum/test_pack.rb
index fddc69b792..e5fb358ce8 100644
--- a/test/-ext-/bignum/test_pack.rb
+++ b/test/-ext-/bignum/test_pack.rb
@@ -259,6 +259,27 @@ class TestBignum < Test::Unit::TestCase
assert_equal(-0x8070605040302010, Integer.test_unpack("\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN|NEGATIVE))
end
+ def test_unpack_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
+ ary = []
+ 0.upto(w) {|i|
+ ary << ((i+1) % 256);
+ }
+ str = ary.pack("C*")
+ flags = word_order|byte_order
+ assert_equal(Integer.test_unpack(str, numwords, wordsize, 0, flags|GENERIC),
+ Integer.test_unpack(str, numwords, wordsize, 0, flags),
+ "Integer.test_unpack(#{str.dump}, #{numwords}, #{wordsize}, 0, #{'%#x' % flags})")
+ }
+ }
+ }
+ }
+ end
+
def test_unpack2comp_single_byte
assert_equal(-128, Integer.test_unpack("\x80", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
assert_equal( -2, Integer.test_unpack("\xFE", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
@@ -292,8 +313,12 @@ 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))
+ str = "\x00"*n
+ flags = TWOCOMP|BIG_ENDIAN|NEGATIVE
+ assert_equal(-(256**n), Integer.test_unpack(str, n, 1, 0, flags))
+ flags = TWOCOMP|LITTLE_ENDIAN|NEGATIVE
+ assert_equal(-(256**n), Integer.test_unpack(str, n, 1, 0, flags),
+ "Integer.test_unpack(#{str.dump}, #{n}, 1, 0, #{'%#x' % flags})")
}
end
end