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