/* * Copyright (c) 1980 Regents of the University of California. * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ #ifndef lint static char sccsid[] = "@(#)mdiv.c 5.1 (Berkeley) 4/30/85"; #endif not lint #include mdiv(a,b,q,r) MINT *a,*b,*q,*r; { MINT x,y; int sign; sign=1; x.val=a->val; y.val=b->val; x.len=a->len; if(x.len<0) {sign= -1; x.len= -x.len;} y.len=b->len; if(y.len<0) {sign= -sign; y.len= -y.len;} xfree(q); xfree(r); m_div(&x,&y,q,r); if(sign==-1) { q->len= -q->len; r->len = - r->len; } return; } m_dsb(q,n,a,b) short *a,*b; { long int x,qx; int borrow,j; short u; qx=q; borrow=0; for(j=0;j>15; } x=borrow+b[j]; b[j]=x&077777; if(x>>15 ==0) { return(0);} borrow=0; for(j=0;jlen==0) { fatal("mdiv divide by zero"); return;} if(b->len==1) { r->val=xalloc(1,"m_div1"); sdiv(a,b->val[0],q,r->val); if(r->val[0]==0) { shfree(r->val); r->len=0; } else r->len=1; return; } if(a->len < b->len) { q->len=0; r->len=a->len; r->val=xalloc(r->len,"m_div2"); for(qq=0;qqlen;qq++) r->val[qq]=a->val[qq]; return; } x.len=1; x.val = &d; n=b->len; d=0100000L/(b->val[n-1]+1L); mult(a,&x,&u); /*subtle: relies on fact that mult allocates extra space */ mult(b,&x,&v); v1=v.val[n-1]; v2=v.val[n-2]; qval=xalloc(a->len-n+1,"m_div3"); for(j=a->len-n;j>=0;j--) { qq=m_trq(v1,v2,u.val[j+n],u.val[j+n-1],u.val[j+n-2]); if(m_dsb(qq,n,v.val,&(u.val[j]))) qq -= 1; qval[j]=qq; } x.len=n; x.val=u.val; mcan(&x); sdiv(&x,d,&w,(short *)&qq); r->len=w.len; r->val=w.val; q->val=qval; qq=a->len-n+1; if(qq>0 && qval[qq-1]==0) qq -= 1; q->len=qq; if(qq==0) shfree(qval); if(x.len!=0) xfree(&u); xfree(&v); return; }