xref: /csrg-svn/lib/libmp/mult.c (revision 61321)
148370Sbostic /*-
248370Sbostic  * %sccs.include.proprietary.c%
348370Sbostic  */
448370Sbostic 
519819Sdist #ifndef lint
6*61321Sbostic static char sccsid[] = "@(#)mult.c	8.1 (Berkeley) 06/04/93";
748370Sbostic #endif /* not lint */
819819Sdist 
99949Ssam #include <mp.h>
109949Ssam mult(a,b,c) struct mint *a,*b,*c;
119949Ssam {	struct mint x,y,z;
129949Ssam 	int sign;
139949Ssam 	sign = 1;
149949Ssam 	x.val=a->val;
159949Ssam 	y.val=b->val;
169949Ssam 	z.len=0;
179949Ssam 	if(a->len<0)
189949Ssam 	{	x.len= -a->len;
199949Ssam 		sign= -sign;
209949Ssam 	}
219949Ssam 	else	x.len=a->len;
229949Ssam 	if(b->len<0)
239949Ssam 	{	y.len= -b->len;
249949Ssam 		sign= -sign;
259949Ssam 	}
269949Ssam 	else	y.len=b->len;
279949Ssam 	if(x.len<y.len) m_mult(&y,&x,&z);
289949Ssam 	else m_mult(&x,&y,&z);
299949Ssam 	xfree(c);
309949Ssam 	if(sign<0) c->len= -z.len;
319949Ssam 	else c->len=z.len;
329949Ssam 	if(c->len==0) shfree(z.val);
339949Ssam 	else c->val=z.val;
349949Ssam 	return;
359949Ssam }
369949Ssam #define S2 x=a->val[j];
379949Ssam #define S3 x=x*b->val[i-j];
389949Ssam #define S4 tradd(&carry,&sum,x);
399949Ssam #define S5 c->val[i]=sum.yy.low&077777;
409949Ssam #define S6 sum.xx=sum.xx>>15;
419949Ssam #define S7 sum.yy.high=carry;
429949Ssam m_mult(a,b,c) struct mint *a,*b,*c;
439949Ssam {	long x;
449949Ssam 	union {long xx; struct half yy;} sum;
459949Ssam 	int carry;
469949Ssam 	int i,j;
479949Ssam 	c->val=xalloc(a->len+b->len,"m_mult");
489949Ssam 	sum.xx=0;
499949Ssam 	for(i=0;i<b->len;i++)
509949Ssam 	{	carry=0;
519949Ssam 		for(j=0;j<i+1;j++)
529949Ssam 		{	S2
539949Ssam 			S3
549949Ssam 			S4
559949Ssam 		}
569949Ssam 		S5
579949Ssam 		S6
589949Ssam 		S7
599949Ssam 	}
609949Ssam 	for(;i<a->len;i++)
619949Ssam 	{	carry=0;
629949Ssam 		for(j=i-b->len+1;j<i+1;j++)
639949Ssam 		{	S2
649949Ssam 			S3
659949Ssam 			S4
669949Ssam 		}
679949Ssam 		S5
689949Ssam 		S6
699949Ssam 		S7
709949Ssam 	}
719949Ssam 	for(;i<a->len+b->len;i++)
729949Ssam 	{	carry=0;
739949Ssam 		for(j=i-b->len+1;j<a->len;j++)
749949Ssam 		{	S2
759949Ssam 			S3
769949Ssam 			S4
779949Ssam 		}
789949Ssam 		S5
799949Ssam 		S6
809949Ssam 		S7
819949Ssam 	}
829949Ssam 	if(c->val[i-1]!=0)
839949Ssam 		c->len=a->len+b->len;
849949Ssam 	else	c->len=a->len+b->len-1;
859949Ssam 	return;
869949Ssam }
8758566Sralph union g {long xx; struct half yy;};
tradd(a,b,c)8858566Sralph tradd(a,b,c) long c; int *a; union g *b;
899949Ssam {
909949Ssam 	b->xx= b->xx+c;
919949Ssam 	if(b->yy.high&0100000)
929949Ssam 	{	b->yy.high= b->yy.high&077777;
939949Ssam 		*a += 1;
949949Ssam 	}
959949Ssam 	return;
969949Ssam }
97