X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/501da53cead3de0021d1de57d930f092a2f3e05a..2bdb833f485d3c985a0f930865452fcaafdf95dd:/mpx.c diff --git a/mpx.c b/mpx.c index b6d765d..36b424c 100644 --- a/mpx.c +++ b/mpx.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx.c,v 1.3 1999/11/13 01:57:31 mdw Exp $ + * $Id: mpx.c,v 1.7 1999/12/22 15:49:07 mdw Exp $ * * Low-level multiprecision arithmetic * @@ -30,6 +30,20 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx.c,v $ + * Revision 1.7 1999/12/22 15:49:07 mdw + * New function for division by a small integer. + * + * Revision 1.6 1999/11/20 22:43:44 mdw + * Integrate testing for MPX routines. + * + * Revision 1.5 1999/11/20 22:23:27 mdw + * Add function versions of some low-level macros with wider use. + * + * Revision 1.4 1999/11/17 18:04:09 mdw + * Add two's-complement functionality. Improve mpx_udiv a little by + * performing the multiplication of the divisor by q with the subtraction + * from r. + * * Revision 1.3 1999/11/13 01:57:31 mdw * Remove stray debugging code. * @@ -379,6 +393,30 @@ done:; /*----- Unsigned arithmetic -----------------------------------------------*/ +/* --- @mpx_2c@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector + * @const mpw *v, *vl@ = source vector + * + * Returns: --- + * + * Use: Calculates the two's complement of @v@. + */ + +void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl) +{ + mpw c = 0; + while (dv < dvl && v < vl) + *dv++ = c = MPW(~*v++); + if (dv < dvl) { + if (c > MPW_MAX / 2) + c = MPW(~0); + while (dv < dvl) + *dv++ = c; + } + MPX_UADDN(dv, dvl, 1); +} + /* --- @mpx_ucmp@ --- * * * Arguments: @const mpw *av, *avl@ = first argument vector base and limit @@ -448,6 +486,18 @@ void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, } } +/* --- @mpx_uaddn@ --- * + * + * Arguments: @mpw *dv, *dvl@ = source and destination base and limit + * @mpw n@ = other addend + * + * Returns: --- + * + * Use: Adds a small integer to a multiprecision number. + */ + +void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); } + /* --- @mpx_usub@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -490,6 +540,18 @@ void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, *dv++ = c; } +/* --- @mpx_usubn@ --- * + * + * Arguments: @mpw *dv, *dvl@ = source and destination base and limit + * @n@ = subtrahend + * + * Returns: --- + * + * Use: Subtracts a small integer from a multiprecision number. + */ + +void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); } + /* --- @mpx_umul@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -544,6 +606,41 @@ void mpx_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, } } +/* --- @mpx_umuln@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector base and limit + * @const mpw *av, *avl@ = multiplicand vector base and limit + * @mpw m@ = multiplier + * + * Returns: --- + * + * Use: Multiplies a multiprecision integer by a single-word value. + * The destination and source may be equal. The destination + * is completely cleared after use. + */ + +void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m) +{ + MPX_UMULN(dv, dvl, av, avl, m); +} + +/* --- @mpx_umlan@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit + * @const mpw *av, *avl@ = multiplicand vector base and limit + * @mpw m@ = multiplier + * + * Returns: --- + * + * Use: Multiplies a multiprecision integer by a single-word value + * and adds the result to an accumulator. + */ + +void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m) +{ + MPX_UMLAN(dv, dvl, av, avl, m); +} + /* --- @mpx_usqr@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -618,8 +715,7 @@ void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl) * requiring the dividend to be in the result position but it * does make some sense really. The remainder must have * headroom for at least two extra words. The scratch space - * must be at least two words larger than twice the size of the - * divisor. + * must be at least one word larger than the divisor. */ void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl, @@ -662,12 +758,10 @@ void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl, /* --- Normalize the dividend/remainder to match --- */ if (norm) { - mpw *svvl = sv + (dvl - dv) + 1; mpx_lsl(rv, rvl, rv, rvl, norm); - mpx_lsl(sv, svvl, dv, dvl, norm); + mpx_lsl(sv, svl, dv, dvl, norm); dv = sv; - sv = svvl; - dvl = svvl; + dvl = svl; MPX_SHRINK(dv, dvl); } @@ -753,25 +847,42 @@ void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl, { mpw *svv; const mpw *dvv; - mpw c = 0; + mpw mc = 0, sc = 0; - /* --- Calculate the size of the chunk --- */ + /* --- Calculate the size of the chunk --- * + * + * This does the whole job of calculating @r >> scale - qd@. + */ - for (svv = sv, dvv = dv; dvv < dvl; svv++, dvv++) { - mpd x = (mpd)*dvv * (mpd)q + c; + for (svv = rv + scale, dvv = dv; + dvv < dvl && svv < rvl; + svv++, dvv++) { + mpd x = (mpd)*dvv * (mpd)q + mc; + mc = x >> MPW_BITS; + x = (mpd)*svv - MPW(x) - sc; *svv = MPW(x); - c = x >> MPW_BITS; + if (x >> MPW_BITS) + sc = 1; + else + sc = 0; + } + + if (svv < rvl) { + mpd x = (mpd)*svv - mc - sc; + *svv++ = MPW(x); + if (x >> MPW_BITS) + sc = MPW_MAX; + else + sc = 0; + while (svv < rvl) + *svv++ = sc; } - if (c) - *svv++ = c; - /* --- Now make sure that we can cope with the difference --- * + /* --- Fix if the quotient was too large --- * * - * Take advantage of the fact that subtraction works two's- - * complement. + * This doesn't seem to happen very often. */ - mpx_usub(rv + scale, rvl, rv + scale, rvl, sv, svv); if (rvl[-1] > MPW_MAX / 2) { mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl); q--; @@ -792,4 +903,363 @@ void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl, mpx_lsr(rv, rvl, rv, rvl, norm); } +/* --- @mpx_udivn@ --- * + * + * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap + * dividend) + * @const mpw *rv, *rvl@ = dividend + * @mpw d@ = single-precision divisor + * + * Returns: Remainder after divison. + * + * Use: Performs a single-precision division operation. + */ + +mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d) +{ + size_t i; + size_t ql = qvl - qv; + mpd r = 0; + + i = rvl - rv; + while (i > 0) { + i--; + r = (r << MPW_BITS) | rv[i]; + if (i < ql) + qv[i] = r / d; + r %= d; + } + return (MPW(r)); +} + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#include +#include +#include +#include + +#include "mpscan.h" + +#define ALLOC(v, vl, sz) do { \ + size_t _sz = (sz); \ + mpw *_vv = xmalloc(MPWS(_sz)); \ + mpw *_vvl = _vv + _sz; \ + (v) = _vv; \ + (vl) = _vvl; \ +} while (0) + +#define LOAD(v, vl, d) do { \ + const dstr *_d = (d); \ + mpw *_v, *_vl; \ + ALLOC(_v, _vl, MPW_RQ(_d->len)); \ + mpx_loadb(_v, _vl, _d->buf, _d->len); \ + (v) = _v; \ + (vl) = _vl; \ +} while (0) + +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +static void dumpbits(const char *msg, const void *pp, size_t sz) +{ + const octet *p = pp; + fputs(msg, stderr); + for (; sz; sz--) + fprintf(stderr, " %02x", *p++); + fputc('\n', stderr); +} + +static void dumpmp(const char *msg, const mpw *v, const mpw *vl) +{ + fputs(msg, stderr); + MPX_SHRINK(v, vl); + while (v < vl) + fprintf(stderr, " %08lx", (unsigned long)*--vl); + fputc('\n', stderr); +} + +static int chkscan(const mpw *v, const mpw *vl, + const void *pp, size_t sz, int step) +{ + mpscan mps; + const octet *p = pp; + unsigned bit = 0; + int ok = 1; + + mpscan_initx(&mps, v, vl); + while (sz) { + unsigned x = *p; + int i; + p += step; + for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) { + if (MPSCAN_BIT(&mps) != (x & 1)) { + fprintf(stderr, + "\n*** error, step %i, bit %u, expected %u, found %u\n", + step, bit, x & 1, MPSCAN_BIT(&mps)); + ok = 0; + } + x >>= 1; + bit++; + } + sz--; + } + + return (ok); +} + +static int loadstore(dstr *v) +{ + dstr d = DSTR_INIT; + size_t sz = MPW_RQ(v->len) * 2, diff; + mpw *m, *ml; + int ok = 1; + + dstr_ensure(&d, v->len); + m = xmalloc(MPWS(sz)); + + for (diff = 0; diff < sz; diff += 5) { + size_t oct; + + ml = m + sz - diff; + + mpx_loadl(m, ml, v->buf, v->len); + if (!chkscan(m, ml, v->buf, v->len, +1)) + ok = 0; + MPX_OCTETS(oct, m, ml); + mpx_storel(m, ml, d.buf, d.sz); + if (memcmp(d.buf, v->buf, oct) != 0) { + dumpbits("\n*** storel failed", d.buf, d.sz); + ok = 0; + } + + mpx_loadb(m, ml, v->buf, v->len); + if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1)) + ok = 0; + MPX_OCTETS(oct, m, ml); + mpx_storeb(m, ml, d.buf, d.sz); + if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) { + dumpbits("\n*** storeb failed", d.buf, d.sz); + ok = 0; + } + } + + if (!ok) + dumpbits("input data", v->buf, v->len); + + free(m); + dstr_destroy(&d); + return (ok); +} + +static int lsl(dstr *v) +{ + mpw *a, *al; + int n = *(int *)v[1].buf; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(c, cl, &v[2]); + ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS); + + mpx_lsl(d, dl, a, al, n); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** lsl(%i) failed\n", n); + dumpmp(" a", a, al); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(c); free(d); + return (ok); +} + +static int lsr(dstr *v) +{ + mpw *a, *al; + int n = *(int *)v[1].buf; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(c, cl, &v[2]); + ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1); + + mpx_lsr(d, dl, a, al, n); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** lsr(%i) failed\n", n); + dumpmp(" a", a, al); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(c); free(d); + return (ok); +} + +static int uadd(dstr *v) +{ + mpw *a, *al; + mpw *b, *bl; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(b, bl, &v[1]); + LOAD(c, cl, &v[2]); + ALLOC(d, dl, MAX(al - a, bl - b) + 1); + + mpx_uadd(d, dl, a, al, b, bl); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** uadd failed\n"); + dumpmp(" a", a, al); + dumpmp(" b", b, bl); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(b); free(c); free(d); + return (ok); +} + +static int usub(dstr *v) +{ + mpw *a, *al; + mpw *b, *bl; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(b, bl, &v[1]); + LOAD(c, cl, &v[2]); + ALLOC(d, dl, al - a); + + mpx_usub(d, dl, a, al, b, bl); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** usub failed\n"); + dumpmp(" a", a, al); + dumpmp(" b", b, bl); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(b); free(c); free(d); + return (ok); +} + +static int umul(dstr *v) +{ + mpw *a, *al; + mpw *b, *bl; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(b, bl, &v[1]); + LOAD(c, cl, &v[2]); + ALLOC(d, dl, (al - a) + (bl - b)); + + mpx_umul(d, dl, a, al, b, bl); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** umul failed\n"); + dumpmp(" a", a, al); + dumpmp(" b", b, bl); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(b); free(c); free(d); + return (ok); +} + +static int usqr(dstr *v) +{ + mpw *a, *al; + mpw *c, *cl; + mpw *d, *dl; + int ok = 1; + + LOAD(a, al, &v[0]); + LOAD(c, cl, &v[1]); + ALLOC(d, dl, 2 * (al - a)); + + mpx_usqr(d, dl, a, al); + if (MPX_UCMP(d, dl, !=, c, cl)) { + fprintf(stderr, "\n*** usqr failed\n"); + dumpmp(" a", a, al); + dumpmp("expected", c, cl); + dumpmp(" result", d, dl); + ok = 0; + } + + free(a); free(c); free(d); + return (ok); +} + +static int udiv(dstr *v) +{ + mpw *a, *al; + mpw *b, *bl; + mpw *q, *ql; + mpw *r, *rl; + mpw *qq, *qql; + mpw *s, *sl; + int ok = 1; + + ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len); + LOAD(b, bl, &v[1]); + LOAD(q, ql, &v[2]); + LOAD(r, rl, &v[3]); + ALLOC(qq, qql, al - a); + ALLOC(s, sl, (bl - b) + 1); + + mpx_udiv(qq, qql, a, al, b, bl, s, sl); + if (MPX_UCMP(qq, qql, !=, q, ql) || + MPX_UCMP(a, al, !=, r, rl)) { + fprintf(stderr, "\n*** udiv failed\n"); + dumpmp(" divisor", b, bl); + dumpmp("expect r", r, rl); + dumpmp("result r", a, al); + dumpmp("expect q", q, ql); + dumpmp("result q", qq, qql); + ok = 0; + } + + free(a); free(b); free(r); free(q); free(s); free(qq); + return (ok); +} + +static test_chunk defs[] = { + { "load-store", loadstore, { &type_hex, 0 } }, + { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } }, + { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } }, + { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } }, + { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } }, + { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, + { "usqr", usqr, { &type_hex, &type_hex, 0 } }, + { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } }, + { 0, 0, { 0 } } +}; + +int main(int argc, char *argv[]) +{ + test_run(argc, argv, defs, SRCDIR"/tests/mpx"); + return (0); +} + + +#endif + /*----- That's all, folks -------------------------------------------------*/