Annotation of researchv10no/libmp/util.c, revision 1.1.1.1

1.1       root        1: extern char *malloc();
                      2: #include "stdio.h"
                      3: #include "mp.h"
                      4: move(a,b) mint *a,*b;
                      5: {      int i,j;
                      6:        if(a == b)
                      7:                return;
                      8:        xfree(b);
                      9:        b->len=a->len;
                     10:        if((i=a->len)<0)
                     11:                i = -i;
                     12:        if(i==0)
                     13:                return;
                     14:        b->val=xalloc(i,"move");
                     15:        for(j=0;j<i;j++)
                     16:                b->val[j]=a->val[j];
                     17:        return;
                     18: }
                     19: dummy(){ }
                     20: /*ARGSUSED*/
                     21: short *xalloc(nint,s) char *s;
                     22: {      short *i;
                     23:        extern short *halloc();
                     24:        i = halloc(nint);
                     25: #ifdef DBG
                     26:        i=(short *)malloc(2*(unsigned)nint+4);
                     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:        hfree(c->val);
                     48:        c->len=0;
                     49:        return;
                     50: }
                     51: 
                     52: mfree(a)
                     53: mint *a;
                     54: {
                     55:        xfree(a);
                     56:        hfree(a);
                     57: }
                     58: mcan(a) mint *a;
                     59: {      int i,j;
                     60:        if((i=a->len)==0) return;
                     61:        else if(i<0) i= -i;
                     62:        for(j=i;j>0 && a->val[j-1]==0;j--);
                     63:        if(j==i) return;
                     64:        if(j==0) {      
                     65:                xfree(a);
                     66:                return;
                     67:        }
                     68:        if(a->len > 0) a->len=j;
                     69:        else a->len = -j;
                     70: }
                     71: mint *itom(n)
                     72: {      mint *a;
                     73:        a=(mint *)xalloc(2,"itom");
                     74:        if(n>0) {       
                     75:                a->len=1;
                     76:                a->val=xalloc(1,"itom1");
                     77:                *a->val=n;
                     78:                return(a);
                     79:        }
                     80:        else if(n<0) {  
                     81:                a->len = -1;
                     82:                a->val=xalloc(1,"itom2");
                     83:                *a->val= -n;
                     84:                return(a);
                     85:        }
                     86:        else {  
                     87:                a->len=0;
                     88:                return(a);
                     89:        }
                     90: }
                     91: mcmp(a,b) mint *a,*b;
                     92: {      mint c;
                     93:        int res;
                     94:        if(a->len < b->len)
                     95:                return(-1);
                     96:        if(a->len > b->len)
                     97:                return(1);
                     98:        c.len=0;
                     99:        msub(a,b,&c);
                    100:        res=c.len;
                    101:        xfree(&c);
                    102:        if(res < 0)
                    103:                return(-1);
                    104:        else if(res == 0)
                    105:                return(0);
                    106:        else
                    107:                return(1);
                    108: }
                    109: 
                    110: dtom(z, r)
                    111: double z;
                    112: mint *r;
                    113: {      int i, sgn;
                    114:        static mint *c;
                    115:        if(!c) {
                    116:                c = itom(16384);
                    117:                madd(c, c, c);
                    118:        }
                    119:        if(z < 0) {
                    120:                sgn = 1;
                    121:                z = -z;
                    122:        }
                    123:        else
                    124:                sgn = 0;
                    125:        for(i = 0; z >= 32768; i++)
                    126:                z /= 32768;
                    127:        move(c, r);
                    128:        r->len = 1;
                    129:        r->val[0] = z;
                    130:        while(--i >= 0) {
                    131:                z -= r->val[0];
                    132:                z *= 32768;
                    133:                mult(r, c, r);
                    134:                r->val[0] = z;
                    135:        }
                    136:        if(sgn)
                    137:                r->len = -r->len;
                    138: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.