1: /*	@(#)madd.c	4.1	12/25/82	*/
   2: 
   3: #include <mp.h>
   4: m_add(a,b,c) struct mint *a,*b,*c;
   5: {   int carry,i;
   6:     int x;
   7:     short *cval;
   8:     cval=xalloc(a->len+1,"m_add");
   9:     carry=0;
  10:     for(i=0;i<b->len;i++)
  11:     {   x=carry+a->val[i]+b->val[i];
  12:         if(x&0100000)
  13:         {   carry=1;
  14:             cval[i]=x&077777;
  15:         }
  16:         else
  17:         {   carry=0;
  18:             cval[i]=x;
  19:         }
  20:     }
  21:     for(;i<a->len;i++)
  22:     {   x=carry+a->val[i];
  23:         if(x&0100000) cval[i]=x&077777;
  24:         else
  25:         {   carry=0;
  26:             cval[i]=x;
  27:         }
  28: 
  29:     }
  30:     if(carry==1)
  31:     {   cval[i]=1;
  32:         c->len=i+1;
  33:     }
  34:     else c->len=a->len;
  35:     c->val=cval;
  36:     if(c->len==0) shfree(cval);
  37:     return;
  38: }
  39: madd(a,b,c) struct mint *a,*b,*c;
  40: {   struct mint x,y,z;
  41:     int sign;
  42:     x.len=a->len;
  43:     x.val=a->val;
  44:     y.len=b->len;
  45:     y.val=b->val;
  46:     z.len=0;
  47:     sign=1;
  48:     if(x.len>=0)
  49:         if(y.len>=0)
  50:             if(x.len>=y.len) m_add(&x,&y,&z);
  51:             else m_add(&y,&x,&z);
  52:         else
  53:         {   y.len= -y.len;
  54:             msub(&x,&y,&z);
  55:         }
  56:     else    if(y.len<=0)
  57:         {   x.len = -x.len;
  58:             y.len= -y.len;
  59:             sign= -1;
  60:             madd(&x,&y,&z);
  61:         }
  62:         else
  63:         {   x.len= -x.len;
  64:             msub(&y,&x,&z);
  65:         }
  66:      xfree(c);
  67:     c->val=z.val;
  68:     c->len=sign*z.len;
  69:     return;
  70: }
  71: m_sub(a,b,c) struct mint *a,*b,*c;
  72: {   int x,i;
  73:     int borrow;
  74:     short one;
  75:     struct mint mone;
  76:     one=1; mone.len= 1; mone.val= &one;
  77:     c->val=xalloc(a->len,"m_sub");
  78:     borrow=0;
  79:     for(i=0;i<b->len;i++)
  80:     {   x=borrow+a->val[i]-b->val[i];
  81:         if(x&0100000)
  82:         {   borrow= -1;
  83:             c->val[i]=x&077777;
  84:         }
  85:         else
  86:         {   borrow=0;
  87:             c->val[i]=x;
  88:         }
  89:     }
  90:     for(;i<a->len;i++)
  91:     {   x=borrow+a->val[i];
  92:         if(x&0100000) c->val[i]=x&077777;
  93:         else
  94:         {   borrow=0;
  95:             c->val[i]=x;
  96:         }
  97:     }
  98:     if(borrow<0)
  99:     {   for(i=0;i<a->len;i++) c->val[i] ^= 077777;
 100:         c->len=a->len;
 101:         madd(c,&mone,c);
 102:     }
 103:     for(i=a->len-1;i>=0;--i) if(c->val[i]>0)
 104:                 {   if(borrow==0) c->len=i+1;
 105:                     else c->len= -i-1;
 106:                     return;
 107:                 }
 108:     shfree(c->val);
 109:     return;
 110: }
 111: msub(a,b,c) struct mint *a,*b,*c;
 112: {   struct mint x,y,z;
 113:     int sign;
 114:     x.len=a->len;
 115:     y.len=b->len;
 116:     x.val=a->val;
 117:     y.val=b->val;
 118:     z.len=0;
 119:     sign=1;
 120:     if(x.len>=0)
 121:         if(y.len>=0)
 122:             if(x.len>=y.len) m_sub(&x,&y,&z);
 123:             else
 124:             {   sign= -1;
 125:                 msub(&y,&x,&z);
 126:             }
 127:         else
 128:         {   y.len= -y.len;
 129:             madd(&x,&y,&z);
 130:         }
 131:     else    if(y.len<=0)
 132:         {   sign= -1;
 133:             x.len= -x.len;
 134:             y.len= -y.len;
 135:             msub(&y,&x,&z);
 136:         }
 137:         else
 138:         {   x.len= -x.len;
 139:             madd(&x,&y,&z);
 140:             sign= -1;
 141:         }
 142:     if(a==c && x.len!=0) xfree(a);
 143:     else if(b==c && y.len!=0) xfree(b);
 144:     else xfree(c);
 145:     c->val=z.val;
 146:     c->len=sign*z.len;
 147:     return;
 148: }

Defined functions

m_add defined in line 4; used 2 times
m_sub defined in line 71; used 1 times
madd defined in line 39; used 4 times
msub defined in line 111; used 4 times
Last modified: 1983-07-26
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 818
Valid CSS Valid XHTML 1.0 Strict