xref: /original-bsd/lib/libmp/util.c (revision 57124d5e)
1 #ifndef lint
2 static char sccsid[] = "@(#)util.c	5.2 (Berkeley) 03/02/87";
3 #endif not lint
4 
5 char *malloc();
6 #ifdef lint
7 int xv_oid;
8 #endif
9 #include <stdio.h>
10 #include <mp.h>
11 move(a,b) MINT *a,*b;
12 {	int i,j;
13 	xfree(b);
14 	b->len=a->len;
15 	if((i=a->len)<0) i = -i;
16 	if(i==0) return;
17 	b->val=xalloc(i,"move");
18 	for(j=0;j<i;j++)
19 		b->val[j]=a->val[j];
20 	return;
21 }
22 dummy(){}
23 short *xalloc(nint,s) char *s;
24 {	short *i;
25 	i=(short *)malloc(2*(unsigned)nint+4);
26 #ifdef DBG
27 	if(dbg) fprintf(stderr, "%s: %o\n",s,i);
28 #endif
29 	if(i!=NULL) return(i);
30 	fatal("mp: no free space");
31 	return(0);
32 }
33 fatal(s) char *s;
34 {
35 	fprintf(stderr,"%s\n",s);
36 	VOID fflush(stdout);
37 	sleep(2);
38 	abort();
39 }
40 xfree(c) MINT *c;
41 {
42 #ifdef DBG
43 	if(dbg) fprintf(stderr, "xfree ");
44 #endif
45 	if(c->len==0) return;
46 	shfree(c->val);
47 	c->len=0;
48 	return;
49 }
50 mcan(a) MINT *a;
51 {	int i,j;
52 	if((i=a->len)==0) return;
53 	else if(i<0) i= -i;
54 	for(j=i;j>0 && a->val[j-1]==0;j--);
55 	if(j==i) return;
56 	if(j==0)
57 	{	xfree(a);
58 		return;
59 	}
60 	if(a->len > 0) a->len=j;
61 	else a->len = -j;
62 }
63 MINT *itom(n)
64 {	MINT *a;
65 	a=(MINT *)xalloc(2,"itom");
66 	if(n>0)
67 	{	a->len=1;
68 		a->val=xalloc(1,"itom1");
69 		*a->val=n;
70 		return(a);
71 	}
72 	else if(n<0)
73 	{	a->len = -1;
74 		a->val=xalloc(1,"itom2");
75 		*a->val= -n;
76 		return(a);
77 	}
78 	else
79 	{	a->len=0;
80 		return(a);
81 	}
82 }
83 mcmp(a,b) MINT *a,*b;
84 {	MINT c;
85 	int res;
86 	if(a->len!=b->len) return(a->len-b->len);
87 	c.len=0;
88 	msub(a,b,&c);
89 	res=c.len;
90 	xfree(&c);
91 	return(res);
92 }
93