3 Contributed by Niels Möller and Marco Bodrato.
5 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
6 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
7 GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
9 Copyright 2003, 2004, 2005, 2008, 2009 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21 License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
30 #define MUL(rp, ap, an, bp, bn) do { \
32 mpn_mul (rp, ap, an, bp, bn); \
34 mpn_mul (rp, bp, bn, ap, an); \
37 /* Inputs are unsigned. */
39 abs_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
42 MPN_CMP (c, ap, bp, n);
45 mpn_sub_n (rp, ap, bp, n);
50 mpn_sub_n (rp, bp, ap, n);
56 add_signed_n (mp_ptr rp,
57 mp_srcptr ap, int as, mp_srcptr bp, int bs, mp_size_t n)
60 return as ^ abs_sub_n (rp, ap, bp, n);
63 ASSERT_NOCARRY (mpn_add_n (rp, ap, bp, n));
69 mpn_matrix22_mul_itch (mp_size_t rn, mp_size_t mn)
71 if (BELOW_THRESHOLD (rn, MATRIX22_STRASSEN_THRESHOLD)
72 || BELOW_THRESHOLD (mn, MATRIX22_STRASSEN_THRESHOLD))
75 return 3*(rn + mn) + 5;
80 / s0 \ / 1 0 0 0 \ / r0 \
81 | s1 | | 0 1 0 1 | | r1 |
82 | s2 | | 0 0 -1 1 | | r2 |
83 | s3 | = | 0 1 -1 1 | \ r3 /
88 / t0 \ / 1 0 0 0 \ / m0 \
89 | t1 | | 0 1 0 1 | | m1 |
90 | t2 | | 0 0 -1 1 | | m2 |
91 | t3 | = | 0 1 -1 1 | \ m3 /
96 Note: the two matrices above are the same, but s_i and t_i are used
97 in the same product, only for i<4, see "A Strassen-like Matrix
98 Multiplication suited for squaring and higher power computation" by
99 M. Bodrato, in Proceedings of ISSAC 2010.
101 / r0 \ / 1 0 0 0 0 1 0 \ / s0*t0 \
102 | r1 | = | 0 0 -1 1 -1 1 0 | | s1*t1 |
103 | r2 | | 0 1 0 -1 0 -1 -1 | | s2*t2 |
104 \ r3 / \ 0 1 1 -1 0 -1 0 / | s3*t3 |
109 The scheduling uses two temporaries U0 and U1 to store products, and
110 two, S0 and T0, to store combinations of entries of the two
114 /* Computes R = R * M. Elements are numbers R = (r0, r1; r2, r3).
116 * Resulting elements are of size up to rn + mn + 1.
118 * Temporary storage: 3 rn + 3 mn + 5. */
120 mpn_matrix22_mul_strassen (mp_ptr r0, mp_ptr r1, mp_ptr r2, mp_ptr r3, mp_size_t rn,
121 mp_srcptr m0, mp_srcptr m1, mp_srcptr m2, mp_srcptr m3, mp_size_t mn,
124 mp_ptr s0, t0, u0, u1;
125 int r1s, r3s, s0s, t0s, u1s;
126 s0 = tp; tp += rn + 1;
127 t0 = tp; tp += mn + 1;
128 u0 = tp; tp += rn + mn + 1;
129 u1 = tp; /* rn + mn + 2 */
131 MUL (u0, r1, rn, m2, mn); /* u5 = s5 * t6 */
132 r3s = abs_sub_n (r3, r3, r2, rn); /* r3 - r2 */
135 r1s = abs_sub_n (r1, r1, r3, rn);
140 r1[rn] = mpn_add_n (r1, r1, r3, rn);
141 r1s = 0; /* r1 - r2 + r3 */
145 s0[rn] = mpn_add_n (s0, r1, r0, rn);
148 else if (r1[rn] != 0)
150 s0[rn] = r1[rn] - mpn_sub_n (s0, r1, r0, rn);
151 s0s = 1; /* s4 = -r0 + r1 - r2 + r3 */
156 s0s = abs_sub_n (s0, r0, r1, rn);
159 MUL (u1, r0, rn, m0, mn); /* u0 = s0 * t0 */
160 r0[rn+mn] = mpn_add_n (r0, u0, u1, rn + mn);
161 ASSERT (r0[rn+mn] < 2); /* u0 + u5 */
163 t0s = abs_sub_n (t0, m3, m2, mn);
164 u1s = r3s^t0s^1; /* Reverse sign! */
165 MUL (u1, r3, rn, t0, mn); /* u2 = s2 * t2 */
169 t0s = abs_sub_n (t0, m1, t0, mn);
174 t0[mn] = mpn_add_n (t0, t0, m1, mn);
177 /* FIXME: Could be simplified if we had space for rn + mn + 2 limbs
178 at r3. I'd expect that for matrices of random size, the high
179 words t0[mn] and r1[rn] are non-zero with a pretty small
180 probability. If that can be confirmed this should be done as an
181 unconditional rn x (mn+1) followed by an if (UNLIKELY (r1[rn]))
185 MUL (r3, r1, rn, t0, mn + 1); /* u3 = s3 * t3 */
188 mpn_add_n (r3 + rn, r3 + rn, t0, mn + 1);
192 MUL (r3, r1, rn + 1, t0, mn);
195 ASSERT (r3[rn+mn] < 4);
200 r3s = abs_sub_n (r3, u0, r3, rn + mn + 1);
204 ASSERT_NOCARRY (mpn_add_n (r3, r3, u0, rn + mn + 1));
205 r3s = 0; /* u3 + u5 */
210 t0[mn] = mpn_add_n (t0, t0, m0, mn);
212 else if (t0[mn] != 0)
214 t0[mn] -= mpn_sub_n (t0, t0, m0, mn);
218 t0s = abs_sub_n (t0, t0, m0, mn);
220 MUL (u0, r2, rn, t0, mn + 1); /* u6 = s6 * t4 */
221 ASSERT (u0[rn+mn] < 2);
224 ASSERT_NOCARRY (mpn_sub_n (r1, r2, r1, rn));
228 r1[rn] += mpn_add_n (r1, r1, r2, rn);
231 t0s = add_signed_n (r2, r3, r3s, u0, t0s, rn + mn);
233 ASSERT (r2[rn+mn-1] < 4);
234 r3s = add_signed_n (r3, r3, r3s, u1, u1s, rn + mn);
236 ASSERT (r3[rn+mn-1] < 3);
237 MUL (u0, s0, rn, m1, mn); /* u4 = s4 * t5 */
238 ASSERT (u0[rn+mn-1] < 2);
239 t0[mn] = mpn_add_n (t0, m3, m1, mn);
240 MUL (u1, r1, rn, t0, mn + 1); /* u1 = s1 * t1 */
242 ASSERT (u1[mn-1] < 4);
243 ASSERT (u1[mn] == 0);
244 ASSERT_NOCARRY (add_signed_n (r1, r3, r3s, u0, s0s, mn));
245 /* -u2 + u3 - u4 + u5 */
246 ASSERT (r1[mn-1] < 2);
249 ASSERT_NOCARRY (mpn_add_n (r3, u1, r3, mn));
253 ASSERT_NOCARRY (mpn_sub_n (r3, u1, r3, mn));
254 /* u1 + u2 - u3 - u5 */
256 ASSERT (r3[mn-1] < 2);
259 ASSERT_NOCARRY (mpn_add_n (r2, u1, r2, mn));
263 ASSERT_NOCARRY (mpn_sub_n (r2, u1, r2, mn));
264 /* u1 - u3 - u5 - u6 */
266 ASSERT (r2[mn-1] < 2);
270 mpn_matrix22_mul (mp_ptr r0, mp_ptr r1, mp_ptr r2, mp_ptr r3, mp_size_t rn,
271 mp_srcptr m0, mp_srcptr m1, mp_srcptr m2, mp_srcptr m3, mp_size_t mn,
274 if (BELOW_THRESHOLD (rn, MATRIX22_STRASSEN_THRESHOLD)
275 || BELOW_THRESHOLD (mn, MATRIX22_STRASSEN_THRESHOLD))
280 /* Temporary storage: 3 rn + 2 mn */
284 for (i = 0; i < 2; i++)
286 MPN_COPY (tp, r0, rn);
290 mpn_mul (p0, r0, rn, m0, mn);
291 mpn_mul (p1, r1, rn, m3, mn);
292 mpn_mul (r0, r1, rn, m2, mn);
293 mpn_mul (r1, tp, rn, m1, mn);
297 mpn_mul (p0, m0, mn, r0, rn);
298 mpn_mul (p1, m3, mn, r1, rn);
299 mpn_mul (r0, m2, mn, r1, rn);
300 mpn_mul (r1, m1, mn, tp, rn);
302 r0[rn+mn] = mpn_add_n (r0, r0, p0, rn + mn);
303 r1[rn+mn] = mpn_add_n (r1, r1, p1, rn + mn);
309 mpn_matrix22_mul_strassen (r0, r1, r2, r3, rn,
310 m0, m1, m2, m3, mn, tp);