From 8f0c3ff6e49fad5263f9be52946e1c13f9bcf021 Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 22 Jun 2013 09:55:27 +0000 Subject: [PATCH] * 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 --- ChangeLog | 9 + bignum.c | 296 +++++++++++++++++---------------- ext/-test-/bignum/pack.c | 1 + internal.h | 1 + test/-ext-/bignum/test_pack.rb | 21 +++ 5 files changed, 189 insertions(+), 139 deletions(-) diff --git a/ChangeLog b/ChangeLog index 48ffa56ef2..317b447572 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Sat Jun 22 18:53:10 2013 Tanaka Akira + + * 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 * gc.c (rgengc_check_shady): add new WB miss checking diff --git a/bignum.c b/bignum.c index 0b2b6ef1ed..828c24f0a4 100644 --- a/bignum.c +++ b/bignum.c @@ -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,168 +920,185 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords sign = 0; } - if (sign == 0) { - MEMZERO(words, unsigned char, numwords * wordsize); - return 0; - } - 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 (0 < sign || !(flags & INTEGER_PACK_2COMP)) { - BDIGIT d; - if (wordsize == 1) { - *((unsigned char *)words) = (unsigned char)(d = dp[0]); - return ((1 < de - dp || CLEAR_LOWBITS(d, 8) != 0) ? 2 : 1) * sign; - } + if (!(flags & INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION)) { + if (sign == 0) { + MEMZERO(words, unsigned char, numwords * wordsize); + return 0; + } + 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 (0 < sign || !(flags & INTEGER_PACK_2COMP)) { + BDIGIT d; + if (wordsize == 1) { + *((unsigned char *)words) = (unsigned char)(d = dp[0]); + return ((1 < de - dp || CLEAR_LOWBITS(d, 8) != 0) ? 2 : 1) * sign; + } #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGITS - if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) { - uint16_t u = (uint16_t)(d = dp[0]); - if (need_swap) u = swap16(u); - *((uint16_t *)words) = u; - return ((1 < de - dp || CLEAR_LOWBITS(d, 16) != 0) ? 2 : 1) * sign; - } + if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) { + uint16_t u = (uint16_t)(d = dp[0]); + if (need_swap) u = swap16(u); + *((uint16_t *)words) = u; + return ((1 < de - dp || CLEAR_LOWBITS(d, 16) != 0) ? 2 : 1) * sign; + } #endif #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGITS - if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) { - uint32_t u = (uint32_t)(d = dp[0]); - if (need_swap) u = swap32(u); - *((uint32_t *)words) = u; - return ((1 < de - dp || CLEAR_LOWBITS(d, 32) != 0) ? 2 : 1) * sign; - } + if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) { + uint32_t u = (uint32_t)(d = dp[0]); + if (need_swap) u = swap32(u); + *((uint32_t *)words) = u; + return ((1 < de - dp || CLEAR_LOWBITS(d, 32) != 0) ? 2 : 1) * sign; + } #endif #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGITS - if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) { - uint64_t u = (uint64_t)(d = dp[0]); - if (need_swap) u = swap64(u); - *((uint64_t *)words) = u; - return ((1 < de - dp || CLEAR_LOWBITS(d, 64) != 0) ? 2 : 1) * sign; - } + if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) { + uint64_t u = (uint64_t)(d = dp[0]); + if (need_swap) u = swap64(u); + *((uint64_t *)words) = u; + return ((1 < de - dp || CLEAR_LOWBITS(d, 64) != 0) ? 2 : 1) * sign; + } #endif - } - else { /* sign < 0 && (flags & INTEGER_PACK_2COMP) */ - BDIGIT_DBL_SIGNED d; - if (wordsize == 1) { - *((unsigned char *)words) = (unsigned char)(d = -(BDIGIT_DBL_SIGNED)dp[0]); - return (1 < de - dp || FILL_LOWBITS(d, 8) != -1) ? -2 : -1; } + else { /* sign < 0 && (flags & INTEGER_PACK_2COMP) */ + BDIGIT_DBL_SIGNED d; + if (wordsize == 1) { + *((unsigned char *)words) = (unsigned char)(d = -(BDIGIT_DBL_SIGNED)dp[0]); + return (1 < de - dp || FILL_LOWBITS(d, 8) != -1) ? -2 : -1; + } #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGITS - if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) { - uint16_t u = (uint16_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); - if (need_swap) u = swap16(u); - *((uint16_t *)words) = u; - return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : - (1 < de - dp || FILL_LOWBITS(d, 16) != -1) ? -2 : -1; - } + if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) { + uint16_t u = (uint16_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); + if (need_swap) u = swap16(u); + *((uint16_t *)words) = u; + return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : + (1 < de - dp || FILL_LOWBITS(d, 16) != -1) ? -2 : -1; + } #endif #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGITS - if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) { - uint32_t u = (uint32_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); - if (need_swap) u = swap32(u); - *((uint32_t *)words) = u; - return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : - (1 < de - dp || FILL_LOWBITS(d, 32) != -1) ? -2 : -1; - } + if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) { + uint32_t u = (uint32_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); + if (need_swap) u = swap32(u); + *((uint32_t *)words) = u; + return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : + (1 < de - dp || FILL_LOWBITS(d, 32) != -1) ? -2 : -1; + } #endif #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGITS - if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) { - uint64_t u = (uint64_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); - if (need_swap) u = swap64(u); - *((uint64_t *)words) = u; - return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : - (1 < de - dp || FILL_LOWBITS(d, 64) != -1) ? -2 : -1; - } + if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) { + uint64_t u = (uint64_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]); + if (need_swap) u = swap64(u); + *((uint64_t *)words) = u; + return (wordsize == SIZEOF_BDIGITS && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 : + (1 < de - dp || FILL_LOWBITS(d, 64) != -1) ? -2 : -1; + } #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 = num_bdigits * SIZEOF_BDIGITS; - size_t dst_size = numwords * wordsize; - int overflow = 0; - while (0 < src_size && ((unsigned char *)ds)[src_size-1] == 0) - src_size--; - if (src_size <= dst_size) { - MEMCPY(words, dp, char, src_size); - MEMZERO((char*)words + src_size, char, dst_size - src_size); - } - else { - MEMCPY(words, dp, char, dst_size); - overflow = 1; - } - if (sign < 0 && (flags & INTEGER_PACK_2COMP)) { - unsigned char *p = words, *e = (unsigned char *)words + dst_size; - while (p < e && *p == 0) - p++; - if (p < e) { - *p = 1 + (unsigned char)~*p; - p++; - while (p < e) { - *p = (unsigned char)~*p; + 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 = num_bdigits * SIZEOF_BDIGITS; + size_t dst_size = numwords * wordsize; + int overflow = 0; + while (0 < src_size && ((unsigned char *)ds)[src_size-1] == 0) + src_size--; + if (src_size <= dst_size) { + MEMCPY(words, dp, char, src_size); + MEMZERO((char*)words + src_size, char, dst_size - src_size); + } + else { + MEMCPY(words, dp, char, dst_size); + overflow = 1; + } + if (sign < 0 && (flags & INTEGER_PACK_2COMP)) { + unsigned char *p = words, *e = (unsigned char *)words + dst_size; + while (p < e && *p == 0) p++; - } - } - else if (overflow) { - p = (unsigned char *)dp + dst_size; - e = (unsigned char *)dp + src_size; - if (p < e && *p++ == 1) { - while (p < e && *p == 0) + if (p < e) { + *p = 1 + (unsigned char)~*p; + p++; + while (p < e) { + *p = (unsigned char)~*p; p++; - if (p == e) - overflow = 0; + } + } + else if (overflow) { + p = (unsigned char *)dp + dst_size; + e = (unsigned char *)dp + src_size; + if (p < e && *p++ == 1) { + while (p < e && *p == 0) + p++; + if (p == e) + overflow = 0; + } } } + if (overflow) + sign *= 2; + return sign; } - if (overflow) - sign *= 2; - return sign; - } #endif - 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; - if (num_bdigits <= buf_num_bdigits) { - MEMCPY(words, dp, BDIGIT, num_bdigits); - MEMZERO((BDIGIT*)words + num_bdigits, BDIGIT, buf_num_bdigits - num_bdigits); - } - else { - MEMCPY(words, dp, BDIGIT, buf_num_bdigits); - overflow = 1; - } - if (sign < 0 && (flags & INTEGER_PACK_2COMP)) { - int zero_p = bary_2comp(words, buf_num_bdigits); - if (overflow && - buf_num_bdigits == num_bdigits-1 && - dp[buf_num_bdigits] == 1 && - zero_p) - overflow = 0; - } - if (need_swap) { - for (i = 0; i < buf_num_bdigits; i++) { - BDIGIT d = ((BDIGIT*)words)[i]; - ((BDIGIT*)words)[i] = swap_bdigit(d); + 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 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); } - } - if (flags & INTEGER_PACK_MSWORD_FIRST) { - BDIGIT *p1 = words, *p2 = p1 + buf_num_bdigits - 1; - while (p1 < p2) { - BDIGIT tmp = *p1; - *p1 = *p2; - *p2 = tmp; - p1++; - p2--; + else { + MEMCPY(words, dp, BDIGIT, buf_num_bdigits); + overflow = 1; } + if (sign < 0 && (flags & INTEGER_PACK_2COMP)) { + int zero_p = bary_2comp(words, buf_num_bdigits); + if (overflow && + buf_num_bdigits == num_bdigits-1 && + dp[buf_num_bdigits] == 1 && + zero_p) + overflow = 0; + } + 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 (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; + *p1 = *p2; + *p2 = tmp; + p1++; + p2--; + } + } + if (overflow) + sign *= 2; + return sign; } - if (overflow) - sign *= 2; - return sign; } buf = words; diff --git a/ext/-test-/bignum/pack.c b/ext/-test-/bignum/pack.c index f54e706884..503866938f 100644 --- a/ext/-test-/bignum/pack.c +++ b/ext/-test-/bignum/pack.c @@ -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)); } diff --git a/internal.h b/internal.h index de4403761e..9f49260d6c 100644 --- a/internal.h +++ b/internal.h @@ -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 diff --git a/test/-ext-/bignum/test_pack.rb b/test/-ext-/bignum/test_pack.rb index 202395324b..fddc69b792 100644 --- a/test/-ext-/bignum/test_pack.rb +++ b/test/-ext-/bignum/test_pack.rb @@ -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