[前][次][番号順一覧][スレッド一覧]

ruby-changes:29513

From: akr <ko1@a...>
Date: Sat, 22 Jun 2013 18:55:40 +0900 (JST)
Subject: [ruby-changes:29513] akr:r41565 (trunk): * bignum.c (bary_pack): Support

akr	2013-06-22 18:55:27 +0900 (Sat, 22 Jun 2013)

  New Revision: 41565

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41565

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/ext/-test-/bignum/pack.c
    trunk/internal.h
    trunk/test/-ext-/bignum/test_pack.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41564)
+++ ChangeLog	(revision 41565)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jun 22 18:53:10 2013  Tanaka Akira  <akr@f...>
+
+	* 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@a...>
 
 	* gc.c (rgengc_check_shady): add new WB miss checking
Index: ext/-test-/bignum/pack.c
===================================================================
--- ext/-test-/bignum/pack.c	(revision 41564)
+++ ext/-test-/bignum/pack.c	(revision 41565)
@@ -61,4 +61,5 @@ Init_pack(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L61
     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));
 }
Index: internal.h
===================================================================
--- internal.h	(revision 41564)
+++ internal.h	(revision 41565)
@@ -62,6 +62,7 @@ extern "C" { https://github.com/ruby/ruby/blob/trunk/internal.h#L62
 #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
Index: bignum.c
===================================================================
--- bignum.c	(revision 41564)
+++ bignum.c	(revision 41565)
@@ -911,7 +911,8 @@ bary_pack(int sign, BDIGIT *ds, size_t n https://github.com/ruby/ruby/blob/trunk/bignum.c#L911
             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 n https://github.com/ruby/ruby/blob/trunk/bignum.c#L920
         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++;
+                if (p < e) {
+                    *p = 1 + (unsigned char)~*p;
                     p++;
+                    while (p < e) {
+                        *p = (unsigned char)~*p;
+                        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)
+                            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;
+        }
+#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 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);
+            }
+            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 (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 (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 (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--;
+            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;
Index: test/-ext-/bignum/test_pack.rb
===================================================================
--- test/-ext-/bignum/test_pack.rb	(revision 41564)
+++ test/-ext-/bignum/test_pack.rb	(revision 41565)
@@ -15,6 +15,7 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L15
     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 https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L124
       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 https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L293
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]