From: mdw Date: Tue, 15 Oct 2002 00:19:40 +0000 (+0000) Subject: Bit setting and clearing functions. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/09d00c6bc88fe624f00ef13b0930b8cc0b6300c1 Bit setting and clearing functions. --- diff --git a/mp-arith.c b/mp-arith.c index 23137e2..e47f301 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.13 2002/10/15 00:19:40 mdw Exp $ * * Basic arithmetic on multiprecision integers * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp-arith.c,v $ + * 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. * @@ -142,34 +145,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 +177,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 @@ -719,6 +786,70 @@ 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 todd(dstr *v) { mp *a = *(mp **)v[0].buf; @@ -741,6 +872,7 @@ static int todd(dstr *v) mp_drop(a); mp_drop(rt); mp_drop(t); + assert(mparena_count(MPARENA_GLOBAL) == 0); return (ok); } @@ -749,6 +881,8 @@ 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 } }, diff --git a/mp.h b/mp.h index c405bca..6ccf523 100644 --- a/mp.h +++ b/mp.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp.h,v 1.13 2002/10/06 22:52:50 mdw Exp $ + * $Id: mp.h,v 1.14 2002/10/15 00:19:40 mdw Exp $ * * Simple multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp.h,v $ + * Revision 1.14 2002/10/15 00:19:40 mdw + * Bit setting and clearing functions. + * * Revision 1.13 2002/10/06 22:52:50 mdw * Pile of changes for supporting two's complement properly. * @@ -683,23 +686,23 @@ extern 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. */ -extern int mp_testbit(mp */*x*/, size_t /*n*/); +extern int mp_testbit(mp */*x*/, unsigned long /*n*/); /* --- @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. */ -extern int mp_testbit2c(mp */*x*/, size_t /*n*/); +extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/); /* --- @mp_eq@ --- * * @@ -726,6 +729,31 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/); #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0) +/* --- @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. + */ + +extern mp *mp_setbit(mp */*d*/, mp */*x*/, unsigned long /*n*/); +extern mp *mp_clearbit(mp */*d*/, mp */*x*/, unsigned long /*n*/); + +/* --- @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. + */ + +extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/); +extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/); + /* --- @mp_bitop@ --- * * * Arguments: @mp *d@ = destination diff --git a/tests/mp b/tests/mp index a73bc65..ea51574 100644 --- a/tests/mp +++ b/tests/mp @@ -1,6 +1,6 @@ # Test vectors for MP functions # -# $Id: mp,v 1.10 2002/10/06 22:52:50 mdw Exp $ +# $Id: mp,v 1.11 2002/10/15 00:19:40 mdw Exp $ add { 5 4 9; 5 -4 1; -5 4 -1; -5 -4 -9; @@ -35,6 +35,22 @@ lsr2c { -6 2 -2; } +setbit { + 0 40 0x10000000000; + 0x87348 40 0x10000087348; + 5 1 7; + 7 1 7; + -3 1 -1; +} + +clrbit { + 0x10000000000 40 0; + 0x87348 40 0x87348; + 5 1 5; + 7 1 5; + -1 1 -3; +} + odd { 1 0 1; 2 1 1;