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

Int. overflow bug in multiplication fixed & VpNmlz() speed up.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shigek 2003-08-21 15:13:45 +00:00
parent 87f143f99d
commit 39503e4d0f
2 changed files with 29 additions and 31 deletions

View file

@ -1,3 +1,8 @@
Tue Aug 21 00:13:00 2003 Shigeo Kobayashi <shigek@ruby-lang.org>
* ext/bigdecimal/bigdecimal.c: Int. overflow bug in multiplication fixed,
and VpNmlz() speed up.
Wed Aug 20 16:44:49 2003 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed Aug 20 16:44:49 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/socket/socket.c (ruby_connect): many systems seem to have * ext/socket/socket.c (ruby_connect): many systems seem to have

View file

@ -2495,7 +2495,7 @@ VP_EXPORT int
VpMult(Real *c, Real *a, Real *b) VpMult(Real *c, Real *a, Real *b)
{ {
U_LONG MxIndA, MxIndB, MxIndAB, MxIndC; U_LONG MxIndA, MxIndB, MxIndAB, MxIndC;
U_LONG ind_c, i, nc; U_LONG ind_c, i, ii, nc;
U_LONG ind_as, ind_ae, ind_bs, ind_be; U_LONG ind_as, ind_ae, ind_bs, ind_be;
U_LONG Carry, s; U_LONG Carry, s;
Real *w; Real *w;
@ -2568,30 +2568,30 @@ VpMult(Real *c, Real *a, Real *b)
ind_be = 0; ind_be = 0;
} }
s = 0L; for(i = ind_as; i <= ind_ae; ++i) {
for(i = ind_as; i <= ind_ae; ++i) s +=((a->frac[i]) *(b->frac[ind_bs--])); s =((a->frac[i]) *(b->frac[ind_bs--]));
Carry = s / BASE; Carry = s / BASE;
s = s -(Carry * BASE); s = s -(Carry * BASE);
c->frac[ind_c] += s; c->frac[ind_c] += s;
if(c->frac[ind_c] >= BASE) { if(c->frac[ind_c] >= BASE) {
s = c->frac[ind_c] / BASE; s = c->frac[ind_c] / BASE;
Carry += s; Carry += s;
c->frac[ind_c] -=(s * BASE); c->frac[ind_c] -= (s * BASE);
} }
i = ind_c;
if(Carry) { if(Carry) {
while((--i) >= 0) { ii = ind_c;
c->frac[i] += Carry; while((--ii) >= 0) {
if(c->frac[i] >= BASE) { c->frac[ii] += Carry;
Carry = c->frac[i] / BASE; if(c->frac[ii] >= BASE) {
c->frac[i] -=(Carry * BASE); Carry = c->frac[ii] / BASE;
c->frac[ii] -=(Carry * BASE);
} else { } else {
break; break;
} }
} }
} }
} }
}
VpNmlz(c); /* normalize the result */ VpNmlz(c); /* normalize the result */
if(w != NULL) { /* free work variable */ if(w != NULL) { /* free work variable */
@ -2827,7 +2827,7 @@ Exit:
static int static int
VpNmlz(Real *a) VpNmlz(Real *a)
{ {
U_LONG ind_a, i, j; U_LONG ind_a, i;
if(!VpIsDef(a)) goto NoVal; if(!VpIsDef(a)) goto NoVal;
if(VpIsZero(a)) goto NoVal; if(VpIsZero(a)) goto NoVal;
@ -2836,20 +2836,13 @@ VpNmlz(Real *a)
while(ind_a--) { while(ind_a--) {
if(a->frac[ind_a]) { if(a->frac[ind_a]) {
a->Prec = ind_a + 1; a->Prec = ind_a + 1;
i = j = 0; i = 0;
while(a->frac[i] == 0) ++i; /* skip the first few zeros */ while(a->frac[i] == 0) ++i; /* skip the first few zeros */
if(i) { if(i) {
a->Prec -= i; a->Prec -= i;
if(!AddExponent(a,-((S_INT)i))) return 0; if(!AddExponent(a,-((S_INT)i))) return 0;
while(i <= ind_a) { memmove(&(a->frac[0]),&(a->frac[i]),(a->Prec)*sizeof(U_LONG));
a->frac[j] = a->frac[i];
++i;
++j;
} }
}
#ifdef _DEBUG
if(gfCheckVal) VpVarCheck(a);
#endif /* _DEBUG */
return 1; return 1;
} }
} }