Annotation of coherent/d/usr/lib/libmp/sdiv.c, revision 1.1.1.1

1.1       root        1: #include "mprec.h"
                      2: #include <assert.h>
                      3: 
                      4: 
                      5: /*
                      6:  *     Sdiv divides the mint pointed to by "a" by the int "n".  It sets
                      7:  *     the mint pointed to by "q" to the quotient and the int pointed to
                      8:  *     by "r" to the remainder.
                      9:  *     Note that it is assumed that 1 <= "n" <= BASE.
                     10:  *     The division is performed such that the following two properties
                     11:  *     hold :
                     12:  *             1. r + q * n = a.
                     13:  *             2. the sign of r = the sign of q.
                     14:  *             3. the abs. value of r < the abs. value of b.
                     15:  *     Note that "a" == "q" is permissable.
                     16:  */
                     17: 
                     18: void
                     19: sdiv(a, n, q, r)
                     20: mint *a, *q;
                     21: register int n;
                     22: int *r;
                     23: {
                     24:        register char   *qp, *ap;
                     25:        char    *res;
                     26:        int     npfl;   /* numerator positive flag */
                     27:        char    dmfl;   /* denominator minus flag */
                     28:        mint    al;
                     29:        register int    rem;
                     30: 
                     31:        if (n == 0)
                     32:                mperr("division by zero attempted");
                     33:        if (dmfl = n < 0)
                     34:                n = -n;
                     35:        assert(1 <= n && n <= BASE);
                     36: 
                     37:        /* set npfl and negate a if necessary */
                     38:        npfl = ispos(a);
                     39:        if (!npfl) {
                     40:                minit(&al);
                     41:                mneg(a, &al);
                     42:                a = &al;
                     43:        }
                     44: 
                     45:        /* allocate space for result */
                     46:        res = (char *)mpalc(a->len);
                     47: 
                     48:        /* compute quotient, most sig. digit first */
                     49:        qp = res + a->len;
                     50:        ap = a->val + a->len;
                     51:        rem = 0;
                     52:        while (qp > res) {
                     53:                rem = BASE * rem + *--ap;
                     54:                *--qp = rem / n;
                     55:                rem %= n;
                     56:        }
                     57:        mpfree(q->val);
                     58:        q->val = res;
                     59:        q->len = a->len;
                     60:        norm(q);
                     61: 
                     62:        /* fix up sign if a was negative */
                     63:        if (npfl ? dmfl : !dmfl) {
                     64:                rem = -rem;
                     65:                mneg(q, q);
                     66:        }
                     67:        if (!npfl)
                     68:                mpfree(al.val);
                     69:        *r = rem;
                     70: }

unix.superglobalmegacorp.com

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