1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* bignum.c (bary_swap): New function.

(bary_pack): Use bary_swap.
  (bary_unpack_internal): Ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41571 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-22 14:51:48 +00:00
parent 963d678953
commit 9ab8ab38d8
2 changed files with 25 additions and 30 deletions

View file

@ -1,3 +1,9 @@
Sat Jun 22 23:45:22 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bary_swap): New function.
(bary_pack): Use bary_swap.
(bary_unpack_internal): Ditto.
Sat Jun 22 23:18:39 2013 Tanaka Akira <akr@fsij.org> Sat Jun 22 23:18:39 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bytes_2comp): Renamed from quad_buf_complement. * bignum.c (bytes_2comp): Renamed from quad_buf_complement.

View file

@ -781,6 +781,18 @@ rb_absint_singlebit_p(VALUE val)
return d == 0; return d == 0;
} }
static void
bary_swap(BDIGIT *ds, size_t num_bdigits)
{
BDIGIT *p1 = ds;
BDIGIT *p2 = ds + num_bdigits - 1;
for (; p1 < p2; p1++, p2--) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
#define INTEGER_PACK_WORDORDER_MASK \ #define INTEGER_PACK_WORDORDER_MASK \
(INTEGER_PACK_MSWORD_FIRST | \ (INTEGER_PACK_MSWORD_FIRST | \
INTEGER_PACK_LSWORD_FIRST) INTEGER_PACK_LSWORD_FIRST)
@ -1046,7 +1058,8 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
#endif #endif
if (nails == 0 && SIZEOF_BDIGITS == sizeof(BDIGIT) && if (nails == 0 && SIZEOF_BDIGITS == sizeof(BDIGIT) &&
wordsize % SIZEOF_BDIGITS == 0 && (uintptr_t)words % ALIGNOF(BDIGIT) == 0) { wordsize % SIZEOF_BDIGITS == 0 && (uintptr_t)words % ALIGNOF(BDIGIT) == 0) {
size_t buf_num_bdigits = numwords * wordsize / SIZEOF_BDIGITS; size_t bdigits_per_word = wordsize / SIZEOF_BDIGITS;
size_t buf_num_bdigits = numwords * bdigits_per_word;
int overflow = 0; int overflow = 0;
int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0; int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0;
int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P : int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P :
@ -1078,26 +1091,12 @@ bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords
size_t i; size_t i;
BDIGIT *p = words; BDIGIT *p = words;
for (i = 0; i < numwords; i++) { for (i = 0; i < numwords; i++) {
BDIGIT *p1 = p, *p2 = p1 + wordsize/SIZEOF_BDIGITS - 1; bary_swap(p, bdigits_per_word);
while (p1 < p2) { p += bdigits_per_word;
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
p += wordsize/SIZEOF_BDIGITS;
} }
} }
if (mswordfirst_p) { if (mswordfirst_p) {
BDIGIT *p1 = words, *p2 = p1 + buf_num_bdigits - 1; bary_swap(words, buf_num_bdigits);
while (p1 < p2) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
} }
if (overflow) if (overflow)
sign *= 2; sign *= 2;
@ -1574,23 +1573,13 @@ bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, siz
(flags & INTEGER_PACK_MSBYTE_FIRST) != 0; (flags & INTEGER_PACK_MSBYTE_FIRST) != 0;
MEMCPY(dp, words, BDIGIT, numwords*bdigits_per_word); MEMCPY(dp, words, BDIGIT, numwords*bdigits_per_word);
if (mswordfirst_p) { if (mswordfirst_p) {
BDIGIT *p1 = dp, *p2 = de - 1; bary_swap(dp, num_bdigits);
for (; p1 < p2; p1++, p2--) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
} }
if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) { if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
size_t i; size_t i;
BDIGIT *p = dp; BDIGIT *p = dp;
for (i = 0; i < numwords; i++) { for (i = 0; i < numwords; i++) {
BDIGIT *p1 = p, *p2 = p1 + bdigits_per_word - 1; bary_swap(p, bdigits_per_word);
for (; p1 < p2; p1++, p2--) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
p += bdigits_per_word; p += bdigits_per_word;
} }
} }