date and time created 88/10/07 16:35:45 by marc
[unix-history] / usr / src / lib / libmp / util.c
CommitLineData
051b1e55 1#ifndef lint
3bc5312c 2static char sccsid[] = "@(#)util.c 5.2 (Berkeley) %G%";
051b1e55 3#endif not lint
59fd1272
SL
4
5char *malloc();
6#ifdef lint
7int xv_oid;
8#endif
9#include <stdio.h>
10#include <mp.h>
11move(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}
22dummy(){}
23short *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}
33fatal(s) char *s;
34{
35 fprintf(stderr,"%s\n",s);
36 VOID fflush(stdout);
37 sleep(2);
38 abort();
39}
40xfree(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}
50mcan(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}
63MINT *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}
83mcmp(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}