=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/engine/Z.c,v retrieving revision 1.2 retrieving revision 1.8 diff -u -p -r1.2 -r1.8 --- OpenXM_contrib2/asir2000/engine/Z.c 2003/09/30 02:23:02 1.2 +++ OpenXM_contrib2/asir2000/engine/Z.c 2006/10/15 23:44:30 1.8 @@ -1,59 +1,149 @@ -#if 0 #include "ca.h" +#include "base.h" #include "inline.h" + +#if defined(__GNUC__) +#define INLINE inline +#elif defined(VISUAL) +#define INLINE __inline +#else +#define INLINE #endif -typedef struct oZ { - int p; - unsigned int b[1]; -} *Z; +INLINE void _addz(Z n1,Z n2,Z nr); +INLINE void _subz(Z n1,Z n2,Z nr); +INLINE void _mulz(Z n1,Z n2,Z nr); +int _addz_main(unsigned int *m1,int d1,unsigned int *m2,int d2,unsigned int *mr); +int _subz_main(unsigned int *m1,int d1,unsigned int *m2,int d2,unsigned int *mr); -#define ZALLOC(d) ((Z)MALLOC_ATOMIC(TRUESIZE(oZ,(d)-1,int))) -#define SL(n) ((n)->p) +/* immediate int -> Z */ +#define UTOZ(c,n) (n)=(!((unsigned int)(c))?0:(((unsigned int)(c))<=IMM_MAX?((Z)((((unsigned int)(c))<<1)|1)):utoz((unsigned int)(c)))) +#define STOZ(c,n) (n)=(!((int)(c))?0:(((int)(c))>=IMM_MIN&&((int)(c))<=IMM_MAX?((Z)((((int)(c))<<1)|1)):stoz((int)(c)))) +/* immediate Z ? */ +#define IS_IMM(c) (((unsigned int)c)&1) +/* Z can be conver to immediate ? */ +#define IS_SZ(n) (((SL(n) == 1)||(SL(n)==-1))&&BD(n)[0]<=IMM_MAX) +/* Z -> immediate Z */ +#define SZTOZ(n,z) (z)=(Z)(SL(n)<0?(((-BD(n)[0])<<1)|1):(((BD(n)[0])<<1)|1)) +/* Z -> immediate int */ +#define ZTOS(c) (((int)(c))>>1) -Z qtoz(Q n); -Q ztoq(Z n); -Z chsgnz(Z n); -Z dupz(Z n); -Z addz(Z n1,Z n2); -Z subz(Z n1,Z n2); -Z mulz(Z n1,Z n2); -Z divsz(Z n1,Z n2); -Z divz(Z n1,Z n2,Z *rem); -Z gcdz(Z n1,Z n2); -Z gcdz_cofactor(Z n1,Z n2,Z *c1,Z *c2); -Z estimate_array_gcdz(Z *a,int n); -Z array_gcdz(Z *a,int n); -void mkwcz(int k,int l,Z *t); -int remzi(Z n,int m); -inline void _addz(Z n1,Z n2,Z nr); -inline void _subz(Z n1,Z n2,Z nr); -inline void _mulz(Z n1,Z n2,Z nr); -inline int _addz_main(unsigned int *m1,int d1,unsigned int *m2,int d2,unsigned int *mr); -inline int _subz_main(unsigned int *m1,int d1,unsigned int *m2,int d2,unsigned int *mr); +int uniz(Z a) +{ + if ( IS_IMM(a) && ZTOS(a) == 1 ) return 1; + else return 0; +} -sgnz(Z n) +int cmpz(Z a,Z b) { + int *ma,*mb; + int sa,sb,da,db,ca,cb,i; + + if ( !a ) + return -sgnz(b); + else if ( !b ) + return sgnz(a); + else { + sa = sgnz(a); sb = sgnz(b); + if ( sa > sb ) return 1; + else if ( sa < sb ) return -1; + else if ( IS_IMM(a) ) + if ( IS_IMM(b) ) { + ca = ZTOS(a); cb = ZTOS(b); + if ( ca > cb ) return sa; + else if ( ca < cb ) return -sa; + else return 0; + } else + return -sa; + else if ( IS_IMM(b) ) + return sa; + else { + da = SL(a)*sa; db = SL(b)*sa; + if ( da > db ) return sa; + else if ( da < db ) return -sa; + else { + for ( i = da-1, ma = BD(a)+i, mb = BD(b)+i; i >= 0; i-- ) + if ( *ma > *mb ) return sa; + else if ( *ma < *mb ) return -sa; + return 0; + } + } + } +} + +Z stoz(int c) +{ + Z z; + + z = ZALLOC(1); + if ( c < 0 ) { + SL(z) = -1; BD(z)[0] = -c; + } else { + SL(z) = 1; BD(z)[0] = c; + } + return z; +} + +Z utoz(unsigned int c) +{ + Z z; + + z = ZALLOC(1); + SL(z) = 1; BD(z)[0] = c; + return z; +} + +Z simpz(Z n) +{ + Z n1; + if ( !n ) return 0; + else if ( IS_IMM(n) ) return n; + else if ( IS_SZ(n) ) { + SZTOZ(n,n1); return n1; + } else + return n; +} + +int sgnz(Z n) +{ + if ( !n ) return 0; + else if ( IS_IMM(n) ) return ZTOS(n)>0?1:-1; else if ( SL(n) < 0 ) return -1; else return 1; } z_mag(Z n) { - return n_bits((N)n); + int c,i; + + if ( !n ) return 0; + else if ( IS_IMM(n) ) { + c = ZTOS(n); + if ( c < 0 ) c = -c; + for ( i = 0; c; c >>= 1, i++ ); + return i; + } + else return n_bits((N)n); } Z qtoz(Q n) { - Z r; + Z r,t; + int c; if ( !n ) return 0; else if ( !INT(n) ) error("qtoz : invalid input"); else { - r = dupz((Z)NM(n)); - if ( SGN(n) < 0 ) SL(r) = -SL(r); + t = (Z)NM(n); + if ( IS_SZ(t) ) { + c = SGN(n) < 0 ? -BD(t)[0] : BD(t)[0]; + STOZ(c,r); + } else { + r = dupz((Z)t); + if ( SGN(n) < 0 ) SL(r) = -SL(r); + } return r; } } @@ -62,10 +152,14 @@ Q ztoq(Z n) { Q r; Z nm; - int sgn; + int sgn,c; if ( !n ) return 0; - else { + else if ( IS_IMM(n) ) { + c = ZTOS(n); + STOQ(c,r); + return r; + } else { nm = dupz(n); if ( SL(nm) < 0 ) { sgn = -1; @@ -79,213 +173,363 @@ Q ztoq(Z n) Z dupz(Z n) { - Z nr; + Z r; int sd,i; if ( !n ) return 0; - if ( (sd = SL(n)) < 0 ) sd = -sd; - nr = ZALLOC(sd); - SL(nr) = SL(n); - for ( i = 0; i < sd; i++ ) BD(nr)[i] = BD(n)[i]; - return nr; + else if ( IS_IMM(n) ) return n; + else { + if ( (sd = SL(n)) < 0 ) sd = -sd; + r = ZALLOC(sd); + SL(r) = SL(n); + for ( i = 0; i < sd; i++ ) BD(r)[i] = BD(n)[i]; + return r; + } } Z chsgnz(Z n) { Z r; + int c; if ( !n ) return 0; - else { + else if ( IS_IMM(n) ) { + c = -ZTOS(n); + STOZ(c,r); + return r; + } else { r = dupz(n); SL(r) = -SL(r); return r; } } +Z absz(Z n) +{ + Z r; + int c; + + if ( !n ) return 0; + else if ( sgnz(n) > 0 ) + return n; + else + return chsgnz(n); +} + Z addz(Z n1,Z n2) { - unsigned int u1,u2,t; - int sd1,sd2,d,d1,d2; - Z nr; + int d1,d2,d,c; + Z r,r1; + struct oZ t; if ( !n1 ) return dupz(n2); else if ( !n2 ) return dupz(n1); - else { - d1 = ((sd1 = SL(n1))< 0)?-sd1:sd1; - d2 = ((sd2 = SL(n2))< 0)?-sd2:sd2; - if ( d1 == 1 && d2 == 1 ) { - u1 = BD(n1)[0]; u2 = BD(n2)[0]; - if ( sd1 > 0 ) { - if ( sd2 > 0 ) { - t = u1+u2; - if ( t < u1 ) { - nr=ZALLOC(2); SL(nr) = 2; BD(nr)[1] = 1; - } else { - nr=ZALLOC(1); SL(nr) = 1; - } - BD(nr)[0] = t; - } else { - if ( u1 == u2 ) nr = 0; - else if ( u1 > u2 ) { - nr=ZALLOC(1); SL(nr) = 1; BD(nr)[0] = u1-u2; - } else { - nr=ZALLOC(1); SL(nr) = -1; BD(nr)[0] = u2-u1; - } - } + else if ( IS_IMM(n1) ) { + if ( IS_IMM(n2) ) { + c = ZTOS(n1)+ZTOS(n2); + STOZ(c,r); + } else { + c = ZTOS(n1); + if ( c < 0 ) { + t.p = -1; t.b[0] = -c; } else { - if ( sd2 > 0 ) { - if ( u2 == u1 ) nr = 0; - else if ( u2 > u1 ) { - nr=ZALLOC(1); SL(nr) = 1; BD(nr)[0] = u2-u1; - } else { - nr=ZALLOC(1); SL(nr) = -1; BD(nr)[0] = u1-u2; - } - } else { - t = u1+u2; - if ( t < u1 ) { - nr=ZALLOC(2); SL(nr) = -2; BD(nr)[1] = 1; - } else { - nr=ZALLOC(1); SL(nr) = -1; - } - BD(nr)[0] = t; - } + t.p = 1; t.b[0] = c; } + if ( (d2 = SL(n2)) < 0 ) d2 = -d2; + r = ZALLOC(d2+1); + _addz(&t,n2,r); + if ( !SL(r) ) r = 0; + } + } else if ( IS_IMM(n2) ) { + c = ZTOS(n2); + if ( c < 0 ) { + t.p = -1; t.b[0] = -c; } else { - d = MAX(d1,d2)+1; - nr = ZALLOC(d); - _addz(n1,n2,nr); - if ( !SL(nr) ) nr = 0; + t.p = 1; t.b[0] = c; } - return nr; + if ( (d1 = SL(n1)) < 0 ) d1 = -d1; + r = ZALLOC(d1+1); + _addz(n1,&t,r); + if ( !SL(r) ) r = 0; + } else { + if ( (d1 = SL(n1)) < 0 ) d1 = -d1; + if ( (d2 = SL(n2)) < 0 ) d2 = -d2; + d = MAX(d1,d2)+1; + r = ZALLOC(d); + _addz(n1,n2,r); + if ( !SL(r) ) r = 0; } + if ( r && !((int)r&1) && IS_SZ(r) ) { + SZTOZ(r,r1); r = r1; + } + return r; } Z subz(Z n1,Z n2) { - int sd1,sd2,d; - Z nr; + int d1,d2,d,c; + Z r,r1; + struct oZ t; if ( !n1 ) - if ( !n2 ) return 0; - else { - nr = dupz(n2); - SL(nr) = -SL(nr); + return chsgnz(n2); + else if ( !n2 ) return n1; + else if ( IS_IMM(n1) ) { + if ( IS_IMM(n2) ) { + c = ZTOS(n1)-ZTOS(n2); + STOZ(c,r); + } else { + c = ZTOS(n1); + if ( c < 0 ) { + t.p = -1; t.b[0] = -c; + } else { + t.p = 1; t.b[0] = c; + } + if ( (d2 = SL(n2)) < 0 ) d2 = -d2; + r = ZALLOC(d2+1); + _subz(&t,n2,r); + if ( !SL(r) ) r = 0; } - else if ( !n2 ) return dupz(n1); - else { - if ( (sd1 = SL(n1)) < 0 ) sd1 = -sd1; - if ( (sd2 = SL(n2)) < 0 ) sd2 = -sd2; - d = MAX(sd1,sd2)+1; - nr = ZALLOC(d); - _subz(n1,n2,nr); - if ( !SL(nr) ) nr = 0; - return nr; + } else if ( IS_IMM(n2) ) { + c = ZTOS(n2); + if ( c < 0 ) { + t.p = -1; t.b[0] = -c; + } else { + t.p = 1; t.b[0] = c; + } + if ( (d1 = SL(n1)) < 0 ) d1 = -d1; + r = ZALLOC(d1+1); + _subz(n1,&t,r); + if ( !SL(r) ) r = 0; + } else { + if ( (d1 = SL(n1)) < 0 ) d1 = -d1; + if ( (d2 = SL(n2)) < 0 ) d2 = -d2; + d = MAX(d1,d2)+1; + r = ZALLOC(d); + _subz(n1,n2,r); + if ( !SL(r) ) r = 0; } + if ( r && !((int)r&1) && IS_SZ(r) ) { + SZTOZ(r,r1); r = r1; + } + return r; } Z mulz(Z n1,Z n2) { - int sd1,sd2,d1,d2; + int d1,d2,sgn,i; + int c1,c2; unsigned int u1,u2,u,l; - Z nr; + Z r; if ( !n1 || !n2 ) return 0; - else { - d1 = ((sd1 = SL(n1))< 0)?-sd1:sd1; - d2 = ((sd2 = SL(n2))< 0)?-sd2:sd2; - if ( d1 == 1 && d2 == 1 ) { - u1 = BD(n1)[0]; u2 = BD(n2)[0]; - DM(u1,u2,u,l); - if ( u ) { - nr = ZALLOC(2); SL(nr) = sd1*sd2*2; BD(nr)[1] = u; - } else { - nr = ZALLOC(1); SL(nr) = sd1*sd2; + + if ( IS_IMM(n1) ) { + c1 = ZTOS(n1); + if ( IS_IMM(n2) ) { + c2 = ZTOS(n2); + if ( c1 == 1 ) + return n2; + else if ( c1 == -1 ) + return chsgnz(n2); + else if ( c2 == 1 ) + return n1; + else if ( c2 == -1 ) + return chsgnz(n1); + else { + sgn = 1; + if ( c1 < 0 ) { c1 = -c1; sgn = -sgn; } + if ( c2 < 0 ) { c2 = -c2; sgn = -sgn; } + u1 = (unsigned int)c1; u2 = (unsigned int)c2; + DM(u1,u2,u,l); + if ( !u ) { + UTOZ(l,r); + } else { + r = ZALLOC(2); SL(r) = 2; BD(r)[1] = u; BD(r)[0] = l; + } } - BD(nr)[0] = l; } else { - nr = ZALLOC(d1+d2); - _mulz(n1,n2,nr); + sgn = 1; + if ( c1 < 0 ) { c1 = -c1; sgn = -sgn; } + u1 = (unsigned int)c1; + if ( (d2 = SL(n2)) < 0 ) { sgn = -sgn; d2 = -d2; } + r = ZALLOC(d2+1); + for ( i = d2; i >= 0; i-- ) BD(r)[i] = 0; + muln_1(BD(n2),d2,u1,BD(r)); + SL(r) = BD(r)[d2]?d2+1:d2; } - return nr; + } else if ( IS_IMM(n2) ) { + c2 = ZTOS(n2); + if ( c2 == 1 ) + return n1; + else if ( c2 == -1 ) + return chsgnz(n1); + + sgn = 1; + if ( c2 < 0 ) { sgn = -sgn; c2 = -c2; } + u2 = (unsigned int)c2; + if ( (d1 = SL(n1)) < 0 ) { sgn = -sgn; d1 = -d1; } + r = ZALLOC(d1+1); + for ( i = d1; i >= 0; i-- ) BD(r)[i] = 0; + muln_1(BD(n1),d1,u2,BD(r)); + SL(r) = BD(r)[d1]?d1+1:d1; + } else { + sgn = 1; + if ( (d1 = SL(n1)) < 0 ) d1 = -d1; + if ( (d2 = SL(n2)) < 0 ) d2 = -d2; + r = ZALLOC(d1+d2); + _mulz(n1,n2,r); } + if ( sgn < 0 ) r = chsgnz(r); + return r; } -/* XXX */ -Z divz(Z n1,Z n2,Z *rem) +/* kokokara */ +#if 0 +Z divsz(Z n1,Z n2) { - int sgn,sd1,sd2; - N q,r; + int sgn,d1,d2; + Z q; - if ( !n2 ) error("divz : division by 0"); - else if ( !n1 ) { - *rem = 0; return 0; - } else { - sd2 = SL(n2); - if ( sd2 == 1 && BD(n2)[0] == 1 ) { - *rem = 0; return n1; - } else if ( sd2 == -1 && BD(n2)[0] == 1 ) { - *rem = 0; return chsgnz(n1); - } + if ( !n2 ) error("divsz : division by 0"); + if ( !n1 ) return 0; - sd1 = SL(n1); - n1 = dupz(n1); - if ( sd1 < 0 ) SL(n1) = -sd1; - if ( sd2 < 0 ) SL(n2) = -sd2; - divn((N)n1,(N)n2,&q,&r); - if ( q && ((sd1>0&&sd2<0)||(sd1<0&&sd2>0)) ) SL((Z)q) = -SL((Z)q); - if ( r && sd1 < 0 ) SL((Z)r) = -SL((Z)r); - SL(n2) = sd2; - *rem = (Z)r; - return (Z)q; + if ( IS_IMM(n1) ) { + if ( !IS_IMM(n2) ) + error("divsz : cannot happen"); + c = ZTOS(n1)/ZTOS(n2); + STOZ(c,q); + return q; } + if ( IS_IMM(n2) ) { + sgn = 1; + u2 = ZTOS(n2); if ( u2 < 0 ) { sgn = -sgn; u2 = -u2; } + diviz(n1,u2,&q); + if ( sgn < 0 ) SL(q) = -SL(q); + return q; + } + + sgn = 1; + if ( (d2 = SL(n2)) < 0 ) { sgn = -sgn; d2 = -d2; } + if ( d2 == 1 ) { + diviz(n1,BD(u2)[0],&q); + if ( sgn < 0 ) SL(q) = -SL(q); + return q; + } + if ( (d1 = SL(n1)) < 0 ) { sgn = -sgn; d1 = -d1; } + if ( d1 < d2 ) error("divsz : cannot happen"); + return q; } +#endif +/* XXX */ +Z divz(Z n1,Z n2,Z *r) +{ + int s1,s2; + Q t1,t2,qq,rq; + N qn,rn; + + if ( !n1 ) { + *r = 0; return 0; + } + if ( !n2 ) + error("divz : division by 0"); + t1 = ztoq(n1); t2 = ztoq(n2); + s1 = sgnz(n1); s2 = sgnz(n2); + /* n1 = qn*SGN(n1)*SGN(n2)*n2+SGN(n1)*rn */ + divn(NM(t1),NM(t2),&qn,&rn); + NTOQ(qn,s1*s2,qq); + NTOQ(rn,s1,rq); + *r = qtoz(rq); + return qtoz(qq); +} + Z divsz(Z n1,Z n2) { - int sgn,sd1,sd2; - N q; + int s1,s2; + Q t1,t2,qq; + N qn; - if ( !n2 ) error("divsz : division by 0"); - else if ( !n1 ) return 0; - else { - sd2 = SL(n2); - if ( sd2 == 1 && BD(n2)[0] == 1 ) return n1; - else if ( sd2 == -1 && BD(n2)[0] == 1 ) return chsgnz(n1); - - sd1 = SL(n1); - n1 = dupz(n1); - if ( sd1 < 0 ) SL(n1) = -sd1; - if ( sd2 < 0 ) SL(n2) = -sd2; - divsn((N)n1,(N)n2,&q); - if ( q && ((sd1>0&&sd2<0)||(sd1<0&&sd2>0)) ) SL((Z)q) = -SL((Z)q); - SL(n2) = sd2; - return (Z)q; + if ( !n1 ) + return 0; + if ( !n2 ) + error("divsz : division by 0"); + t1 = ztoq(n1); t2 = ztoq(n2); + s1 = sgnz(n1); s2 = sgnz(n2); + /* n1 = qn*SGN(n1)*SGN(n2)*n2 */ + divsn(NM(t1),NM(t2),&qn); + NTOQ(qn,s1*s2,qq); + return qtoz(qq); +} + +int gcdimm(int c1,int c2) +{ + int r; + + if ( !c1 ) return c2; + else if ( !c2 ) return c1; + while ( 1 ) { + r = c1%c2; + if ( !r ) return c2; + c1 = c2; c2 = r; } } Z gcdz(Z n1,Z n2) { - int sd1,sd2; - N gcd; + int c1,c2,g; + Z gcd,r; + N gn; if ( !n1 ) return n2; else if ( !n2 ) return n1; - n1 = dupz(n1); - if ( SL(n1) < 0 ) SL(n1) = -SL(n1); - n2 = dupz(n2); - if ( SL(n2) < 0 ) SL(n2) = -SL(n2); - gcdn((N)n1,(N)n2,&gcd); - return (Z)gcd; + if ( IS_IMM(n1) ) { + c1 = ZTOS(n1); + if ( c1 < 0 ) c1 = -c1; + if ( IS_IMM(n2) ) + c2 = ZTOS(n2); + else + c2 = remzi(n2,c1>0?c1:-c1); + if ( c2 < 0 ) c2 = -c2; + g = gcdimm(c1,c2); + STOZ(g,gcd); + return gcd; + } else if ( IS_IMM(n2) ) { + c2 = ZTOS(n2); + if ( c2 < 0 ) c2 = -c2; + c1 = remzi(n1,c2>0?c2:-c2); + if ( c1 < 0 ) c1 = -c1; + g = gcdimm(c1,c2); + STOZ(g,gcd); + return gcd; + } else { + n1 = dupz(n1); + if ( SL(n1) < 0 ) SL(n1) = -SL(n1); + n2 = dupz(n2); + if ( SL(n2) < 0 ) SL(n2) = -SL(n2); + gcdn((N)n1,(N)n2,&gn); + gcd = (Z)gn; + if ( IS_SZ(gcd) ) { + SZTOZ(gcd,r); gcd = r; + } + return gcd; + } } int remzi(Z n,int m) { unsigned int *x; unsigned int t,r; - int i; + int i,c; if ( !n ) return 0; + if ( IS_IMM(n) ) { + c = ZTOS(n)%m; + if ( c < 0 ) c += m; + return c; + } + i = SL(n); if ( i < 0 ) i = -i; for ( i--, x = BD(n)+i, r = 0; i >= 0; i--, x-- ) { @@ -295,6 +539,8 @@ int remzi(Z n,int m) DSAB(m,r,*x,t,r); #endif } + if ( r && SL(n) < 0 ) + r = m-r; return r; } @@ -308,6 +554,7 @@ Z gcdz_cofactor(Z n1,Z n2,Z *c1,Z *c2) return gcd; } +#if 0 Z estimate_array_gcdz(Z *b,int n) { int m,i,j,sd; @@ -348,6 +595,7 @@ Z array_gcdz(Z *b,int n) gcd = gcdz(gcd,a[i]); return gcd; } +#endif void _copyz(Z n1,Z n2) { @@ -363,43 +611,43 @@ void _copyz(Z n1,Z n2) void _addz(Z n1,Z n2,Z nr) { - int sd1,sd2; + int d1,d2; if ( !n1 || !SL(n1) ) _copyz(n2,nr); else if ( !n2 || !SL(n2) ) _copyz(n1,nr); - else if ( (sd1=SL(n1)) > 0 ) - if ( (sd2=SL(n2)) > 0 ) - SL(nr) = _addz_main(BD(n1),sd1,BD(n2),sd2,BD(nr)); + else if ( (d1=SL(n1)) > 0 ) + if ( (d2=SL(n2)) > 0 ) + SL(nr) = _addz_main(BD(n1),d1,BD(n2),d2,BD(nr)); else - SL(nr) = _subz_main(BD(n1),sd1,BD(n2),-sd2,BD(nr)); - else if ( (sd2=SL(n2)) > 0 ) - SL(nr) = _subz_main(BD(n2),sd2,BD(n1),-sd1,BD(nr)); + SL(nr) = _subz_main(BD(n1),d1,BD(n2),-d2,BD(nr)); + else if ( (d2=SL(n2)) > 0 ) + SL(nr) = _subz_main(BD(n2),d2,BD(n1),-d1,BD(nr)); else - SL(nr) = -_addz_main(BD(n1),-sd1,BD(n2),-sd2,BD(nr)); + SL(nr) = -_addz_main(BD(n1),-d1,BD(n2),-d2,BD(nr)); } void _subz(Z n1,Z n2,Z nr) { - int sd1,sd2; + int d1,d2; if ( !n1 || !SL(n1) ) _copyz(n2,nr); else if ( !n2 || !SL(n2) ) { _copyz(n1,nr); SL(nr) = -SL(nr); - } else if ( (sd1=SL(n1)) > 0 ) - if ( (sd2=SL(n2)) > 0 ) - SL(nr) = _subz_main(BD(n1),sd1,BD(n2),sd2,BD(nr)); + } else if ( (d1=SL(n1)) > 0 ) + if ( (d2=SL(n2)) > 0 ) + SL(nr) = _subz_main(BD(n1),d1,BD(n2),d2,BD(nr)); else - SL(nr) = _addz_main(BD(n1),sd1,BD(n2),-sd2,BD(nr)); - else if ( (sd2=SL(n2)) > 0 ) - SL(nr) = -_addz_main(BD(n1),-sd1,BD(n2),sd2,BD(nr)); + SL(nr) = _addz_main(BD(n1),d1,BD(n2),-d2,BD(nr)); + else if ( (d2=SL(n2)) > 0 ) + SL(nr) = -_addz_main(BD(n1),-d1,BD(n2),d2,BD(nr)); else - SL(nr) = -_subz_main(BD(n1),-sd1,BD(n2),-sd2,BD(nr)); + SL(nr) = -_subz_main(BD(n1),-d1,BD(n2),-d2,BD(nr)); } void _mulz(Z n1,Z n2,Z nr) { - int sd1,sd2; + int d1,d2; int i,d,sgn; unsigned int mul; unsigned int *m1,*m2; @@ -407,14 +655,14 @@ void _mulz(Z n1,Z n2,Z nr) if ( !n1 || !SL(n1) || !n2 || !SL(n2) ) SL(nr) = 0; else { - sd1 = SL(n1); sd2 = SL(n2); + d1 = SL(n1); d2 = SL(n2); sgn = 1; - if ( sd1 < 0 ) { sgn = -sgn; sd1 = -sd1; } - if ( sd2 < 0 ) { sgn = -sgn; sd2 = -sd2; } - d = sd1+sd2; + if ( d1 < 0 ) { sgn = -sgn; d1 = -d1; } + if ( d2 < 0 ) { sgn = -sgn; d2 = -d2; } + d = d1+d2; for ( i = d-1, m1 = BD(nr); i >= 0; i-- ) *m1++ = 0; - for ( i = 0, m1 = BD(n1), m2 = BD(n2); i < sd2; i++, m2++ ) - if ( mul = *m2 ) muln_1(m1,sd1,mul,BD(nr)+i); + for ( i = 0, m1 = BD(n1), m2 = BD(n2); i < d2; i++, m2++ ) + if ( mul = *m2 ) muln_1(m1,d1,mul,BD(nr)+i); SL(nr) = sgn*(BD(nr)[d-1]?d:d-1); } } @@ -478,7 +726,7 @@ int _addz_main(unsigned int *m1,int d1,unsigned int *m :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ :"eax","ebx","ecx","edx","esi","edi"); #else - for ( i = 0, c = 0, mr = BD(nr); i < d2; i++, m1++, m2++, mr++ ) { + for ( i = 0, c = 0; i < d2; i++, m1++, m2++, mr++ ) { tmp = *m1 + *m2; if ( tmp < *m1 ) { tmp += c; @@ -568,7 +816,7 @@ int _subz_main(unsigned int *m1,int d1,unsigned int *m :"m"(m1),"m"(m2),"m"(mr),"m"(d2)\ :"eax","ebx","ecx","edx","esi","edi"); #else - for ( i = 0, br = 0, mr = BD(nr); i < d2; i++, mr++ ) { + for ( i = 0, br = 0; i < d2; i++, mr++ ) { t = *m1++; tmp = *m2++ + br; if ( br > 0 && !tmp ) { @@ -596,11 +844,14 @@ int _subz_main(unsigned int *m1,int d1,unsigned int *m void printz(Z n) { - int sd; + int sd,u; if ( !n ) fprintf(asir_out,"0"); - else { + else if ( IS_IMM(n) ) { + u = ZTOS(n); + fprintf(asir_out,"%d",u); + } else { if ( (sd = SL(n)) < 0 ) { SL(n) = -SL(n); fprintf(asir_out,"-"); } printn((N)n); if ( sd < 0 ) SL(n) = -SL(n);