* bignum.c (bigand_int): Don't apply bitwise and for BDIGIT and long.

(bigor_int): Take xn and hibitsx arguments.  Use twocomp2abs_bang.
  (rb_big_or): Use abs2twocomp and twocomp2abs_bang.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-28 23:21:57 +00:00
parent 112df332d2
commit 88430941c8
2 changed files with 52 additions and 45 deletions

View File

@ -1,3 +1,9 @@
Sat Jun 29 08:19:58 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bigand_int): Don't apply bitwise and for BDIGIT and long.
(bigor_int): Take xn and hibitsx arguments. Use twocomp2abs_bang.
(rb_big_or): Use abs2twocomp and twocomp2abs_bang.
Fri Jun 29 01:08:00 2013 Charlie Somerville <charliesome@ruby-lang.org> Fri Jun 29 01:08:00 2013 Charlie Somerville <charliesome@ruby-lang.org>
* numeric.c (fix_mul): remove FIT_SQRT_LONG test as it was causing * numeric.c (fix_mul): remove FIT_SQRT_LONG test as it was causing

View File

@ -4662,7 +4662,7 @@ bigand_int(VALUE x, long xn, BDIGIT hibitsx, long y)
BDIGIT hibitsy; BDIGIT hibitsy;
if (y == 0) return INT2FIX(0); if (y == 0) return INT2FIX(0);
if (xn == 0) return LONG2NUM((long)(hibitsx & y)); if (xn == 0) return hibitsx ? LONG2NUM(y) : 0;
hibitsy = 0 <= y ? 0 : BDIGMAX; hibitsy = 0 <= y ? 0 : BDIGMAX;
xds = BDIGITS(x); xds = BDIGITS(x);
#if SIZEOF_BDIGITS >= SIZEOF_LONG #if SIZEOF_BDIGITS >= SIZEOF_LONG
@ -4766,22 +4766,25 @@ rb_big_and(VALUE x, VALUE y)
} }
static VALUE static VALUE
bigor_int(VALUE x, long y) bigor_int(VALUE x, long xn, BDIGIT hibitsx, long y)
{ {
VALUE z; VALUE z;
BDIGIT *xds, *zds; BDIGIT *xds, *zds;
long xn, zn; long zn;
long i; long i;
char sign; BDIGIT hibitsy;
sign = (y >= 0); if (y == -1) INT2FIX(-1);
if (xn == 0) return hibitsx ? INT2FIX(-1) : LONG2FIX(y);
hibitsy = 0 <= y ? 0 : BDIGMAX;
xds = BDIGITS(x); xds = BDIGITS(x);
zn = xn = RBIGNUM_LEN(x);
zn = RBIGNUM_LEN(x);
#if SIZEOF_BDIGITS < SIZEOF_LONG #if SIZEOF_BDIGITS < SIZEOF_LONG
if (zn < bdigit_roomof(SIZEOF_LONG)) if (zn < bdigit_roomof(SIZEOF_LONG))
zn = bdigit_roomof(SIZEOF_LONG); zn = bdigit_roomof(SIZEOF_LONG);
#endif #endif
z = bignew(zn, RBIGNUM_SIGN(x) && sign); z = bignew(zn, 0);
zds = BDIGITS(z); zds = BDIGITS(z);
#if SIZEOF_BDIGITS >= SIZEOF_LONG #if SIZEOF_BDIGITS >= SIZEOF_LONG
@ -4796,7 +4799,7 @@ bigor_int(VALUE x, long y)
zds[i] = xds[i] | BIGLO(y); zds[i] = xds[i] | BIGLO(y);
y = BIGDN(y); y = BIGDN(y);
} }
if (RBIGNUM_NEGATIVE_P(x)) if (hibitsx)
goto fill_hibits; goto fill_hibits;
for (; i < zn; i++) { for (; i < zn; i++) {
if (y == 0 || y == -1) goto y_is_fixed_point; if (y == 0 || y == -1) goto y_is_fixed_point;
@ -4807,12 +4810,12 @@ bigor_int(VALUE x, long y)
#endif #endif
y_is_fixed_point: y_is_fixed_point:
if (!sign) if (hibitsy)
goto fill_hibits; goto fill_hibits;
for (; i < xn; i++) { for (; i < xn; i++) {
zds[i] = xds[i]; zds[i] = xds[i];
} }
if (RBIGNUM_NEGATIVE_P(x)) if (hibitsx)
goto fill_hibits; goto fill_hibits;
for (; i < zn; i++) { for (; i < zn; i++) {
zds[i] = 0; zds[i] = 0;
@ -4825,7 +4828,7 @@ bigor_int(VALUE x, long y)
} }
finish: finish:
if (!RBIGNUM_SIGN(z)) get2comp(z); twocomp2abs_bang(z, hibitsx || hibitsy);
return bignorm(z); return bignorm(z);
} }
@ -4837,55 +4840,53 @@ bigor_int(VALUE x, long y)
*/ */
VALUE VALUE
rb_big_or(VALUE xx, VALUE yy) rb_big_or(VALUE x, VALUE y)
{ {
volatile VALUE x, y, z; VALUE z;
BDIGIT *ds1, *ds2, *zds; BDIGIT *ds1, *ds2, *zds;
long i, l1, l2; long i, xl, yl, l1, l2;
char sign; BDIGIT hibitsx, hibitsy;
BDIGIT hibits1, hibits2;
VALUE tmpv;
BDIGIT tmph;
long tmpl;
if (!FIXNUM_P(yy) && !RB_TYPE_P(yy, T_BIGNUM)) { if (!FIXNUM_P(y) && !RB_TYPE_P(y, T_BIGNUM)) {
return rb_num_coerce_bit(xx, yy, '|'); return rb_num_coerce_bit(x, y, '|');
} }
x = xx; hibitsx = abs2twocomp(&x, &xl);
y = yy;
if (!RBIGNUM_SIGN(x)) {
x = rb_big_clone(x);
get2comp(x);
}
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
return bigor_int(x, FIX2LONG(y)); return bigor_int(x, xl, hibitsx, FIX2LONG(y));
} }
if (!RBIGNUM_SIGN(y)) { hibitsy = abs2twocomp(&y, &yl);
y = rb_big_clone(y); if (xl > yl) {
get2comp(y); tmpv = x; x = y; y = tmpv;
tmpl = xl; xl = yl; yl = tmpl;
tmph = hibitsx; hibitsx = hibitsy; hibitsy = tmph;
} }
if (RBIGNUM_LEN(x) > RBIGNUM_LEN(y)) { l1 = xl;
l1 = RBIGNUM_LEN(y); l2 = yl;
l2 = RBIGNUM_LEN(x); ds1 = BDIGITS(x);
ds1 = BDIGITS(y); ds2 = BDIGITS(y);
ds2 = BDIGITS(x); hibits1 = hibitsx;
sign = RBIGNUM_SIGN(y); hibits2 = hibitsy;
}
else { if (hibits1)
l1 = RBIGNUM_LEN(x); l2 = l1;
l2 = RBIGNUM_LEN(y);
ds1 = BDIGITS(x); z = bignew(l2, 0);
ds2 = BDIGITS(y);
sign = RBIGNUM_SIGN(x);
}
z = bignew(l2, RBIGNUM_SIGN(x) && RBIGNUM_SIGN(y));
zds = BDIGITS(z); zds = BDIGITS(z);
for (i=0; i<l1; i++) { for (i=0; i<l1; i++) {
zds[i] = ds1[i] | ds2[i]; zds[i] = ds1[i] | ds2[i];
} }
for (; i<l2; i++) { for (; i<l2; i++) {
zds[i] = sign?ds2[i]:BDIGMAX; zds[i] = hibits1 | ds2[i];
} }
if (!RBIGNUM_SIGN(z)) get2comp(z); twocomp2abs_bang(z, hibits1 || hibits2);
RB_GC_GUARD(x);
RB_GC_GUARD(y);
return bignorm(z); return bignorm(z);
} }