/* -*-c-*-
*
- * $Id: mp-arith.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ * $Id: mp-arith.c,v 1.3 1999/12/11 10:57:43 mdw Exp $
*
* Basic arithmetic on multiprecision integers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: mp-arith.c,v $
+ * Revision 1.3 1999/12/11 10:57:43 mdw
+ * Karatsuba squaring algorithm.
+ *
+ * Revision 1.2 1999/12/10 23:18:39 mdw
+ * Change interface for suggested destinations.
+ *
* Revision 1.1 1999/11/17 18:02:16 mdw
* New multiprecision integer arithmetic suite.
*
#include "mp.h"
+/*----- Macros ------------------------------------------------------------*/
+
+#define MAX(x, y) ((x) >= (y) ? (x) : (y))
+
/*----- Main code ---------------------------------------------------------*/
/* --- @mp_2c@ --- *
/* --- @mp_lsl@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
* @size_t n@ = number of bits to move
*
* Returns: Result, @a@ shifted left by @n@.
*/
-mp *mp_lsl(mp *d, const mp *a, size_t n)
+mp *mp_lsl(mp *d, mp *a, size_t n)
{
MP_MODIFY(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS);
mpx_lsl(d->v, d->vl, a->v, a->vl, n);
/* --- @mp_lsr@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
* @size_t n@ = number of bits to move
*
* Returns: Result, @a@ shifted left by @n@.
*/
-mp *mp_lsr(mp *d, const mp *a, size_t n)
+mp *mp_lsr(mp *d, mp *a, size_t n)
{
MP_MODIFY(d, MP_LEN(a));
mpx_lsr(d->v, d->vl, a->v, a->vl, n);
/* --- @mp_add@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @a@ added to @b@.
*/
-mp *mp_add(mp *d, const mp *a, const mp *b)
+mp *mp_add(mp *d, mp *a, mp *b)
{
- MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ MP_MODIFY(d, MAX(MP_LEN(a), MP_LEN(b)) + 1);
if (!((a->f ^ b->f) & MP_NEG))
mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
else {
if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
- const mp *t = a; a = b; b = t;
+ mp *t = a; a = b; b = t;
}
mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
}
/* --- @mp_sub@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @b@ subtracted from @a@.
*/
-mp *mp_sub(mp *d, const mp *a, const mp *b)
+mp *mp_sub(mp *d, mp *a, mp *b)
{
unsigned sgn = 0;
- MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ MP_MODIFY(d, MAX(MP_LEN(a), MP_LEN(b)) + 1);
if ((a->f ^ b->f) & MP_NEG)
mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
else {
if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
- const mp *t = a; a = b; b = t;
+ mp *t = a; a = b; b = t;
sgn = MP_NEG;
}
mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
/* --- @mp_mul@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Returns: Result, @a@ multiplied by @b@.
*/
-mp *mp_mul(mp *d, const mp *a, const mp *b)
+mp *mp_mul(mp *d, mp *a, mp *b)
{
- if (d == a || d == b)
- d = MP_NEW;
+ a = MP_COPY(a);
+ b = MP_COPY(b);
+
MP_MODIFY(d, MP_LEN(a) + MP_LEN(b));
- mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ if (MP_LEN(a) <= KARATSUBA_CUTOFF || MP_LEN(b) <= KARATSUBA_CUTOFF)
+ mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ else {
+ size_t m = MAX(MP_LEN(a), MP_LEN(b)) * 2 + KARATSUBA_SLOP;
+ mpw *s;
+ m += 32;
+ s = MP_ALLOC(m);
+ mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m);
+ MP_FREE(s);
+ }
+
d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
MP_SHRINK(d);
+ MP_DROP(a);
+ MP_DROP(b);
return (d);
}
/* --- @mp_sqr@ --- *
*
* Arguments: @mp *d@ = destination
- * @const mp *a@ = source
+ * @mp *a@ = source
*
* Returns: Result, @a@ squared.
*/
-mp *mp_sqr(mp *d, const mp *a)
+mp *mp_sqr(mp *d, mp *a)
{
- if (d == a)
- d = MP_NEW;
- MP_MODIFY(d, 2 * MP_LEN(a));
- mpx_usqr(d->v, d->vl, a->v, a->vl);
+ size_t m = MP_LEN(a);
+
+ a = MP_COPY(a);
+ MP_MODIFY(d, 2 * m);
+ if (m > KARATSUBA_CUTOFF) {
+ mpw *s;
+ m = 2 * (m + 1) + 32;
+ s = MP_ALLOC(m);
+ mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + m);
+ MP_FREE(s);
+ } else
+ mpx_usqr(d->v, d->vl, a->v, a->vl);
d->f = a->f & MP_BURN;
MP_SHRINK(d);
+ MP_DROP(a);
return (d);
}
/* --- @mp_div@ --- *
*
* Arguments: @mp **qq, **rr@ = destination, quotient and remainder
- * @const mp *a, *b@ = sources
+ * @mp *a, *b@ = sources
*
* Use: Calculates the quotient and remainder when @a@ is divided by
* @b@. The destinations @*qq@ and @*rr@ must be distinct.
* straightforward.
*/
-void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
+void mp_div(mp **qq, mp **rr, mp *a, mp *b)
{
mp *r = rr ? *rr : MP_NEW;
mp *q = qq ? *qq : MP_NEW;
if (MP_LEN(b) > rq)
rq = MP_LEN(b);
+ b = MP_COPY(b);
if (r == a) {
- MP_SPLIT(r);
+ MP_SPLIT(a);
+ a = r = MP_COPY(a);
MP_ENSURE(r, MP_LEN(r) + 2);
} else {
- if (r == b)
- r = MP_NEW;
+ a = MP_COPY(a);
MP_MODIFY(r, MP_LEN(a) + 2);
memcpy(r->v, a->v, MPWS(MP_LEN(a)));
memset(r->v + MP_LEN(a), 0, MPWS(2));
/* --- Fix up the quotient too --- */
- if (q == a || q == b)
- q = MP_NEW;
MP_MODIFY(q, MP_LEN(a));
/* --- Perform the calculation --- */
q->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
if (q->f & MP_NEG) {
- mpw *v = r->v;
- while (v < r->vl) {
+ mpw *v;
+ for (v = r->v; v < r->vl; v++) {
if (*v) {
MPX_UADDN(q->v, q->vl, 1);
mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
*rr = r;
}
+ MP_DROP(a);
+ MP_DROP(b);
MP_FREE(sv);
}
}
#define RIG(name, op) \
- static int t ## name(dstr *v) \
+ static int t##name(dstr *v) \
{ \
mp *a = *(mp **)v[0].buf; \
mpw n = *(int *)v[1].buf; \
mp_build(&b, &n, &n + 1); \
ok = verify(#name, r, c, a, &b); \
mp_drop(a); mp_drop(c); mp_drop(r); \
+ assert(mparena_count(MPARENA_GLOBAL) == 0); \
return (ok); \
}
#undef RIG
#define RIG(name, op) \
- static int t ## name(dstr *v) \
+ static int t##name(dstr *v) \
{ \
mp *a = *(mp **)v[0].buf; \
mp *b = *(mp **)v[1].buf; \
mp *c = op(MP_NEW, a, b); \
int ok = verify(#name, r, c, a, b); \
mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
+ assert(mparena_count(MPARENA_GLOBAL) == 0); \
return (ok); \
}
ok &= verify("div(quotient)", q, c, a, b);
ok &= verify("div(remainder)", r, d, a, b);
mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
+ assert(mparena_count(MPARENA_GLOBAL) == 0);
return (ok);
}