ruby-changes:29643
From: akr <ko1@a...>
Date: Sat, 29 Jun 2013 23:38:39 +0900 (JST)
Subject: [ruby-changes:29643] akr:r41695 (trunk): * bignum.c (bary_neg): Extracted from bary_2comp.
akr 2013-06-29 23:38:26 +0900 (Sat, 29 Jun 2013) New Revision: 41695 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41695 Log: * bignum.c (bary_neg): Extracted from bary_2comp. (bary_plus_one): Extracted from bary_2comp. (bary_2comp): Use bary_neg and bary_plus_one. (big_extend_carry): Extracted from get2comp. (get2comp): Use big_extend_carry. (rb_integer_unpack): Use big_extend_carry. (rb_big_neg): Use bary_neg. Modified files: trunk/ChangeLog trunk/bignum.c Index: ChangeLog =================================================================== --- ChangeLog (revision 41694) +++ ChangeLog (revision 41695) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Jun 29 23:26:41 2013 Tanaka Akira <akr@f...> + + * bignum.c (bary_neg): Extracted from bary_2comp. + (bary_plus_one): Extracted from bary_2comp. + (bary_2comp): Use bary_neg and bary_plus_one. + (big_extend_carry): Extracted from get2comp. + (get2comp): Use big_extend_carry. + (rb_integer_unpack): Use big_extend_carry. + (rb_big_neg): Use bary_neg. + Sat Jun 29 22:31:59 2013 Tanaka Akira <akr@f...> * bignum.c (bary_2comp): Simplified. Index: bignum.c =================================================================== --- bignum.c (revision 41694) +++ bignum.c (revision 41695) @@ -267,13 +267,17 @@ bytes_2comp(unsigned char *buf, size_t l https://github.com/ruby/ruby/blob/trunk/bignum.c#L267 return 1; } +static void +bary_neg(BDIGIT *ds, size_t n) +{ + while (n--) + ds[n] = BIGLO(~ds[n]); +} + static int -bary_2comp(BDIGIT *ds, size_t n) +bary_plus_one(BDIGIT *ds, size_t n) { - size_t i = n; - if (!n) return 1; - while (i--) ds[i] = BIGLO(~ds[i]); - i = 0; + size_t i; for (i = 0; i < n; i++) { ds[i] = BIGLO(ds[i]+1); if (ds[i] != 0) @@ -282,6 +286,21 @@ bary_2comp(BDIGIT *ds, size_t n) https://github.com/ruby/ruby/blob/trunk/bignum.c#L286 return 1; } +static int +bary_2comp(BDIGIT *ds, size_t n) +{ + if (!n) return 1; + bary_neg(ds, n); + return bary_plus_one(ds, n); +} + +static void +big_extend_carry(VALUE x) +{ + rb_big_resize(x, RBIGNUM_LEN(x)+1); + BDIGITS(x)[RBIGNUM_LEN(x)-1] = 1; +} + /* modify a bignum by 2's complement */ static void get2comp(VALUE x) @@ -290,9 +309,7 @@ get2comp(VALUE x) https://github.com/ruby/ruby/blob/trunk/bignum.c#L309 BDIGIT *ds = BDIGITS(x); if (bary_2comp(ds, i)) { - rb_big_resize(x, RBIGNUM_LEN(x)+1); - ds = BDIGITS(x); - ds[RBIGNUM_LEN(x)-1] = 1; + big_extend_carry(x); } } @@ -1696,8 +1713,7 @@ rb_integer_unpack(const void *words, siz https://github.com/ruby/ruby/blob/trunk/bignum.c#L1713 if (sign == -2) { if (val) { - rb_big_resize(val, (long)num_bdigits+1); - BDIGITS(val)[num_bdigits] = 1; + big_extend_carry(val); } else if (num_bdigits == numberof(fixbuf)) { val = bignew((long)num_bdigits+1, 0); @@ -3026,9 +3042,7 @@ rb_big_neg(VALUE x) https://github.com/ruby/ruby/blob/trunk/bignum.c#L3042 ds = BDIGITS(z); i = RBIGNUM_LEN(x); if (!i) return INT2FIX(~(SIGNED_VALUE)0); - while (i--) { - ds[i] = BIGLO(~ds[i]); - } + bary_neg(ds, i); RBIGNUM_SET_SIGN(z, !RBIGNUM_SIGN(z)); if (RBIGNUM_SIGN(x)) get2comp(z); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/