+/* --- @pgen_muladd@ --- *
+ *
+ * Arguments: @pgen *p@ = destination prime generation context
+ * @const pgen *q@ = source prime generation context
+ * @mpw m@ = number to multiply by
+ * @mpw a@ = number to add
+ *
+ * Returns: One of the @PGEN@ constants above.
+ *
+ * Use: Multiplies the number in a prime generation context by a
+ * small value and then adds a small value. The destination
+ * should either be uninitialized or the same as the source.
+ *
+ * Common things to do include multiplying by 2 and adding 0 to
+ * turn a prime into a jump for finding other primes with @q@ as
+ * a factor of @p - 1@, or multiplying by 2 and adding 1.
+ */
+
+int pgen_muladd(pgen *p, const pgen *q, mpw m, mpw a)
+{
+ int rc = PGEN_MAYBE;
+ int i;
+
+ /* --- Multiply the big number --- */
+
+ {
+ mp *d = mp_create(MP_LEN(q->m) + 2);
+ mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
+ mpx_uaddn(d->v, d->vl, a);
+ d->f = q->m->f;
+ if (p == q)
+ mp_drop(p->m);
+ mp_shrink(d);
+ p->m = d;
+ }
+
+ /* --- Gallivant through the residue table --- */
+
+ for (i = 0; i < NPRIME; i++) {
+ p->r[i] = (q->r[i] * m + a) % ptab[i];
+ if (!p->r[i] && rc == PGEN_MAYBE) {
+ if (MP_LEN(p->m) == 1 && p->m->v[0] == ptab[i])
+ rc = PGEN_PRIME;
+ else
+ rc = PGEN_COMPOSITE;
+ }
+ }
+
+ /* --- Small numbers must be prime --- */
+
+ if (rc == PGEN_MAYBE && MP_LEN(p->m) == 1 &&
+ p->m->v[0] < MAXPRIME * MAXPRIME)
+ rc = PGEN_PRIME;
+
+ /* --- Finished --- */
+
+ return (rc);
+}
+
+