X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/dd22938ef0d9b0131dad9171a8a95866ceec9607..1589affab225db500965e2cb869c534d6860e6bd:/mp-arith.c diff --git a/mp-arith.c b/mp-arith.c index 23137e2..53584e7 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.12 2002/10/09 00:36:03 mdw Exp $ + * $Id: mp-arith.c,v 1.17 2003/10/12 15:03:35 mdw Exp $ * * Basic arithmetic on multiprecision integers * @@ -30,6 +30,24 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp-arith.c,v $ + * Revision 1.17 2003/10/12 15:03:35 mdw + * Merge fix from other branch. + * + * Revision 1.16.2.1 2003/06/10 13:21:10 mdw + * Fix bug dividing small things by large ones. + * + * Revision 1.16 2003/05/16 09:09:24 mdw + * Fix @mp_lsl2c@. Turns out to be surprisingly tricky. + * + * Revision 1.15 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * + * Revision 1.14 2002/10/15 19:18:31 mdw + * New operation to negate numbers. + * + * Revision 1.13 2002/10/15 00:19:40 mdw + * Bit setting and clearing functions. + * * Revision 1.12 2002/10/09 00:36:03 mdw * Fix bounds on workspace for Karatsuba operations. * @@ -82,13 +100,18 @@ /*----- Main code ---------------------------------------------------------*/ -/* --- @mp_lsl@, @mp_lsr@ --- * +/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- * * * Arguments: @mp *d@ = destination * @mp *a@ = source * @size_t n@ = number of bits to move * * Returns: Result, @a@ shifted left or right by @n@. + * + * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced + * on the right with ones instead of zeroes: it's used + * internally by @mp_lsl2c@, though it may be useful on its + * own. */ mp *mp_lsl(mp *d, mp *a, size_t n) @@ -100,6 +123,15 @@ mp *mp_lsl(mp *d, mp *a, size_t n) return (d); } +mp *mp_lslc(mp *d, mp *a, size_t n) +{ + MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); + mpx_lslc(d->v, d->vl, a->v, a->vl, n); + d->f = a->f & (MP_NEG | MP_BURN); + MP_SHRINK(d); + return (d); +} + mp *mp_lsr(mp *d, mp *a, size_t n) { MP_DEST(d, MP_LEN(a), a->f); @@ -124,7 +156,7 @@ mp *mp_lsl2c(mp *d, mp *a, size_t n) if (!(a->f & MP_NEG)) return (mp_lsl(d, a, n)); d = mp_not2c(d, a); - d = mp_lsl(d, d, n); + d = mp_lslc(d, d, n); d = mp_not2c(d, d); return (d); } @@ -142,34 +174,31 @@ mp *mp_lsr2c(mp *d, mp *a, size_t n) /* --- @mp_testbit@ --- * * * Arguments: @mp *x@ = a large integer - * @size_t n@ = which bit to test + * @unsigned long n@ = which bit to test * * Returns: Nonzero if the bit is set, zero if not. */ -int mp_testbit(mp *x, size_t n) +int mp_testbit(mp *x, unsigned long n) { - size_t o; if (n > MPW_BITS * MP_LEN(x)) return (0); - o = n / MPW_BITS; - n %= MPW_BITS; - return ((x->v[o] >> n) & 1); + return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u); } /* --- @mp_testbit2c@ --- * * * Arguments: @mp *x@ = a large integer - * @size_t n@ = which bit to test + * @unsigned long n@ = which bit to test * * Returns: Nonzero if the bit is set, zero if not. Fakes up two's * complement representation. */ -int mp_testbit2c(mp *x, size_t n) +int mp_testbit2c(mp *x, unsigned long n) { int r; - if (x->f & MP_NEG) + if (!(x->f & MP_NEG)) return (mp_testbit(x, n)); x = mp_not2c(MP_NEW, x); r = !mp_testbit(x, n); @@ -177,6 +206,73 @@ int mp_testbit2c(mp *x, size_t n) return (r); } +/* --- @mp_setbit@, @mp_clearbit@ --- * + * + * Arguments: @mp *d@ = a destination + * @mp *x@ = a large integer + * @unsigned long n@ = which bit to modify + * + * Returns: The argument @x@, with the appropriate bit set or cleared. + */ + +mp *mp_setbit(mp *d, mp *x, unsigned long n) +{ + size_t rq; + + rq = n + MPW_BITS; rq -= rq % MPW_BITS; + if (d != x) { + if (d) MP_DROP(d); + d = MP_COPY(x); + } + MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); + d->v[n/MPW_BITS] |= 1 << n%MPW_BITS; + return (d); +} + +mp *mp_clearbit(mp *d, mp *x, unsigned long n) +{ + size_t rq; + + rq = n + MPW_BITS; rq -= rq % MPW_BITS; + if (d != x) { + if (d) MP_DROP(d); + d = MP_COPY(x); + } + MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); + d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS); + return (d); +} + +/* --- @mp_setbit2c@, @mp_clearbit2c@ --- * + * + * Arguments: @mp *d@ = a destination + * @mp *x@ = a large integer + * @unsigned long n@ = which bit to modify + * + * Returns: The argument @x@, with the appropriate bit set or cleared. + * Fakes up two's complement representation. + */ + +mp *mp_setbit2c(mp *d, mp *x, unsigned long n) +{ + if (!(x->f & MP_NEG)) + return mp_setbit(d, x, n); + d = mp_not2c(d, x); + d = mp_clearbit(d, d, n); + d = mp_not2c(d, d); + return (d); +} + +mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) +{ + if (!(x->f & MP_NEG)) + return mp_clearbit(d, x, n); + d = mp_not2c(d, x); + d = mp_setbit(d, d, n); + d = mp_not2c(d, d); + return (d); +} + /* --- @mp_eq@ --- * * * Arguments: @const mp *a, *b@ = two numbers @@ -204,6 +300,31 @@ int mp_cmp(const mp *a, const mp *b) return (+1); } +/* --- @mp_neg@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *a@ = argument + * + * Returns: The negation of the argument. + * + * Use: Negates its argument. + */ + +mp *mp_neg(mp *d, mp *a) +{ + /* --- Surprising amounts of messing about required --- */ + + MP_SHRINK(a); + MP_COPY(a); + if (d) + MP_DROP(d); + if (a->v == a->vl) + return (a); + MP_DEST(a, MP_LEN(a), a->f); + a->f ^= MP_NEG; + return (a); +} + /* --- @mp_bitop@ --- * * * Arguments: @mp *d@ = destination @@ -223,7 +344,7 @@ int mp_cmp(const mp *a, const mp *b) \ mp *mp_bit##string(mp *d, mp *a, mp *b) \ { \ - MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \ + MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ d->f = (a->f | b->f) & MP_BURN; \ MP_SHRINK(d); \ @@ -504,7 +625,7 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b) if (r) MP_DROP(r); r = a; - MP_DEST(r, MP_LEN(a) + 2, a->f | b->f); + MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f); /* --- Fix up the quotient too --- */ @@ -719,6 +840,100 @@ MPX_DOBIN(DO) return (ok); } +static int tset(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + unsigned long n = *(unsigned long *)v[1].buf; + mp *r = *(mp **)v[2].buf; + mp *c; + int ok = 1; + + c = mp_setbit2c(MP_NEW, a, n); + if (!MP_EQ(c, r)) { + ok = 0; + fprintf(stderr, "\n***setbit (set) failed"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); + fputc('\n', stderr); + } + if (!mp_testbit2c(r, n)) { + ok = 0; + fprintf(stderr, "\n***setbit (test) failed"); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(r); + mp_drop(c); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + +static int tclr(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + unsigned long n = *(unsigned long *)v[1].buf; + mp *r = *(mp **)v[2].buf; + mp *c; + int ok = 1; + + c = mp_clearbit2c(MP_NEW, a, n); + if (!MP_EQ(c, r)) { + ok = 0; + fprintf(stderr, "\n***clrbit (set) failed"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); + fputc('\n', stderr); + } + if (mp_testbit2c(r, n)) { + ok = 0; + fprintf(stderr, "\n***clrbit (test) failed"); + fprintf(stderr, "\n*** n = %lu", n); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(c); + mp_drop(r); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + +static int tneg(dstr *v) +{ + mp *a = *(mp **)v[0].buf; + mp *r = *(mp **)v[1].buf; + int ok = 1; + mp *n = mp_neg(MP_NEW, a); + if (!MP_EQ(r, n)) { + ok = 0; + fprintf(stderr, "\n*** neg failed\n"); + fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); + fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); + fputc('\n', stderr); + } + mp_drop(n); + n = mp_neg(a, a); + if (!MP_EQ(r, n)) { + ok = 0; + fprintf(stderr, "\n*** neg failed\n"); + fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10); + fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); + fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); + fputc('\n', stderr); + } + mp_drop(a); + mp_drop(r); + assert(mparena_count(MPARENA_GLOBAL) == 0); + return (ok); +} + static int todd(dstr *v) { mp *a = *(mp **)v[0].buf; @@ -741,6 +956,7 @@ static int todd(dstr *v) mp_drop(a); mp_drop(rt); mp_drop(t); + assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); } @@ -749,12 +965,15 @@ static test_chunk tests[] = { { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } }, { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } }, { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } }, + { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } }, + { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } }, { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, + { "neg", tneg, { &type_mp, &type_mp, 0 } }, { 0, 0, { 0 } }, };