1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/* mpn_toom22_mul -- Multiply {ap,an} and {bp,bn} where an >= bn. Or more
accurately, bn <= an < 2bn.
Contributed to the GNU project by Torbjorn Granlund.
THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
#include "gmp.h"
#include "gmp-impl.h"
/* Evaluate in: -1, 0, +inf
<-s--><--n-->
____ ______
|_a1_|___a0_|
|b1_|___b0_|
<-t-><--n-->
v0 = a0 * b0 # A(0)*B(0)
vm1 = (a0- a1)*(b0- b1) # A(-1)*B(-1)
vinf= a1 * b1 # A(inf)*B(inf)
*/
#if TUNE_PROGRAM_BUILD
#define MAYBE_mul_toom22 1
#else
#define MAYBE_mul_toom22 \
(MUL_TOOM33_THRESHOLD >= 2 * MUL_TOOM22_THRESHOLD)
#endif
#define TOOM22_MUL_N_REC(p, a, b, n, ws) \
do { \
if (! MAYBE_mul_toom22 \
|| BELOW_THRESHOLD (n, MUL_KARATSUBA_THRESHOLD)) \
mpn_mul_basecase (p, a, n, b, n); \
else \
mpn_toom22_mul (p, a, n, b, n, ws); \
} while (0)
void
mpn_toom22_mul (mp_ptr pp,
mp_srcptr ap, mp_size_t an,
mp_srcptr bp, mp_size_t bn,
mp_ptr scratch)
{
mp_size_t n, s, t;
int vm1_neg;
mp_limb_t cy, cy2;
mp_ptr asm1;
mp_ptr bsm1;
#define a0 ap
#define a1 (ap + n)
#define b0 bp
#define b1 (bp + n)
s = an >> 1;
n = an - s;
t = bn - n;
ASSERT (an >= bn);
ASSERT (0 < s && s <= n);
ASSERT (0 < t && t <= s);
asm1 = pp;
bsm1 = pp + n;
vm1_neg = 0;
/* Compute asm1. */
if (s == n)
{
if (mpn_cmp (a0, a1, n) < 0)
{
mpn_sub_n (asm1, a1, a0, n);
vm1_neg = 1;
}
else
{
mpn_sub_n (asm1, a0, a1, n);
}
}
else
{
if (mpn_zero_p (a0 + s, n - s) && mpn_cmp (a0, a1, s) < 0)
{
mpn_sub_n (asm1, a1, a0, s);
MPN_ZERO (asm1 + s, n - s);
vm1_neg = 1;
}
else
{
mpn_sub (asm1, a0, n, a1, s);
}
}
/* Compute bsm1. */
if (t == n)
{
if (mpn_cmp (b0, b1, n) < 0)
{
mpn_sub_n (bsm1, b1, b0, n);
vm1_neg ^= 1;
}
else
{
mpn_sub_n (bsm1, b0, b1, n);
}
}
else
{
if (mpn_zero_p (b0 + t, n - t) && mpn_cmp (b0, b1, t) < 0)
{
mpn_sub_n (bsm1, b1, b0, t);
MPN_ZERO (bsm1 + t, n - t);
vm1_neg ^= 1;
}
else
{
mpn_sub (bsm1, b0, n, b1, t);
}
}
#define v0 pp /* 2n */
#define vinf (pp + 2 * n) /* s+t */
#define vm1 scratch /* 2n */
#define scratch_out scratch + 2 * n
/* vm1, 2n limbs */
TOOM22_MUL_N_REC (vm1, asm1, bsm1, n, scratch_out);
/* vinf, s+t limbs */
mpn_mul (vinf, a1, s, b1, t);
/* v0, 2n limbs */
TOOM22_MUL_N_REC (v0, ap, bp, n, scratch_out);
/* H(v0) + L(vinf) */
cy = mpn_add_n (pp + 2 * n, v0 + n, vinf, n);
/* L(v0) + H(v0) */
cy2 = cy + mpn_add_n (pp + n, pp + 2 * n, v0, n);
/* L(vinf) + H(vinf) */
cy += mpn_add (pp + 2 * n, pp + 2 * n, n, vinf + n, s + t - n);
if (vm1_neg)
cy += mpn_add_n (pp + n, pp + n, vm1, 2 * n);
else
cy -= mpn_sub_n (pp + n, pp + n, vm1, 2 * n);
ASSERT (cy + 1 <= 3);
ASSERT (cy2 <= 2);
mpn_incr_u (pp + 2 * n, cy2);
if (LIKELY (cy <= 2))
mpn_incr_u (pp + 3 * n, cy);
else
mpn_decr_u (pp + 3 * n, 1);
}
|