From: mdw Date: Tue, 3 Apr 2001 19:36:05 +0000 (+0000) Subject: Add some simple bitwise operations so that Perl can use them. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/0f32e0f83177bd620a1deee0e0641f8323742b6c Add some simple bitwise operations so that Perl can use them. --- diff --git a/mp-arith.c b/mp-arith.c index 82fda15..d6d892b 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.9 2000/10/08 15:48:35 mdw Exp $ + * $Id: mp-arith.c,v 1.10 2001/04/03 19:36:05 mdw Exp $ * * Basic arithmetic on multiprecision integers * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp-arith.c,v $ + * Revision 1.10 2001/04/03 19:36:05 mdw + * Add some simple bitwise operations so that Perl can use them. + * * Revision 1.9 2000/10/08 15:48:35 mdw * Rename Karatsuba constants now that we have @gfx_kmul@ too. * @@ -176,6 +179,38 @@ int mp_cmp(const mp *a, const mp *b) return (+1); } +/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *a, *b@ = sources + * + * Returns: The result of the obvious bitwise operation. + */ + +#define MP_BITBINOP(name) \ + \ +mp *mp_##name(mp *d, mp *a, mp *b) \ +{ \ + MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \ + mpx_##name(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ + d->f = (a->f | b->f) & MP_BURN; \ + MP_SHRINK(d); \ + return (d); \ +} + +MP_BITBINOP(and) +MP_BITBINOP(or) +MP_BITBINOP(xor) + +mp *mp_not(mp *d, mp *a) +{ + MP_DEST(d, MP_LEN(a), a->f); + mpx_not(d->v, d->vl, a->v, a->vl); + d->f = a->f & MP_BURN; + MP_SHRINK(d); + return (d); +} + /* --- @mp_add@ --- * * * Arguments: @mp *d@ = destination diff --git a/mp.h b/mp.h index 5b54513..dcf4349 100644 --- a/mp.h +++ b/mp.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp.h,v 1.10 2000/10/08 12:03:16 mdw Exp $ + * $Id: mp.h,v 1.11 2001/04/03 19:36:05 mdw Exp $ * * Simple multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp.h,v $ + * Revision 1.11 2001/04/03 19:36:05 mdw + * Add some simple bitwise operations so that Perl can use them. + * * Revision 1.10 2000/10/08 12:03:16 mdw * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two * integers. @@ -625,6 +628,19 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/); #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0) +/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *a, *b@ = sources + * + * Returns: The result of the obvious bitwise operation. + */ + +extern mp *mp_and(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_or(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_xor(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_not(mp */*d*/, mp */*a*/); + /* --- @mp_add@ --- * * * Arguments: @mp *d@ = destination diff --git a/mpx.c b/mpx.c index 13b6fa0..f89a89b 100644 --- a/mpx.c +++ b/mpx.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx.c,v 1.10 2000/10/08 12:06:12 mdw Exp $ + * $Id: mpx.c,v 1.11 2001/04/03 19:36:05 mdw Exp $ * * Low-level multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx.c,v $ + * Revision 1.11 2001/04/03 19:36:05 mdw + * Add some simple bitwise operations so that Perl can use them. + * * Revision 1.10 2000/10/08 12:06:12 mdw * Provide @mpx_ueq@ for rapidly testing equality of two integers. * @@ -400,6 +403,50 @@ void mpx_lsr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, size_t n) done:; } +/*----- Bitwise operations ------------------------------------------------*/ + +/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector + * @const mpw *av, *avl@ = first source vector + * @const mpw *bv, *bvl@ = second source vector + * + * Returns: --- + * + * Use; Does the obvious bitwise operations. + */ + +#define MPX_BITBINOP(name, op) \ + \ +void mpx_##name(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \ + const mpw *bv, const mpw *bvl) \ +{ \ + MPX_SHRINK(av, avl); \ + MPX_SHRINK(bv, bvl); \ + \ + while (dv < dvl) { \ + mpw a, b; \ + a = (av < avl) ? *av++ : 0; \ + b = (bv < bvl) ? *bv++ : 0; \ + *dv++ = a op b; \ + } \ +} + +MPX_BITBINOP(and, &) +MPX_BITBINOP(or, |) +MPX_BITBINOP(xor, ^) + +void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl) +{ + MPX_SHRINK(av, avl); + + while (dv < dvl) { + mpw a; + a = (av < avl) ? *av++ : 0; + *dv++ = ~a; + } +} + /*----- Unsigned arithmetic -----------------------------------------------*/ /* --- @mpx_2c@ --- * diff --git a/mpx.h b/mpx.h index dc86cc9..b633e43 100644 --- a/mpx.h +++ b/mpx.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx.h,v 1.11 2000/10/08 15:48:35 mdw Exp $ + * $Id: mpx.h,v 1.12 2001/04/03 19:36:05 mdw Exp $ * * Low level multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx.h,v $ + * Revision 1.12 2001/04/03 19:36:05 mdw + * Add some simple bitwise operations so that Perl can use them. + * * Revision 1.11 2000/10/08 15:48:35 mdw * Rename Karatsuba constants now that we have @gfx_kmul@ too. * @@ -304,6 +307,34 @@ extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/, const mpw */*av*/, const mpw */*avl*/, size_t /*n*/); +/*----- Bitwise operations ------------------------------------------------*/ + +/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector + * @const mpw *av, *avl@ = first source vector + * @const mpw *bv, *bvl@ = second source vector + * + * Returns: --- + * + * Use; Does the obvious bitwise operations. + */ + +extern void mpx_and(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/, + const mpw */*bv*/, const mpw */*bvl*/); + +extern void mpx_or(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/, + const mpw */*bv*/, const mpw */*bvl*/); + +extern void mpx_xor(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/, + const mpw */*bv*/, const mpw */*bvl*/); + +extern void mpx_not(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/); + /*----- Unsigned arithmetic -----------------------------------------------*/ /* --- @mpx_2c@ --- *