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