1 /* mpn_toom52_mul -- Multiply {ap,an} and {bp,bn} where an is nominally 4/3
2 times as large as bn. Or more accurately, bn < an < 2 bn.
4 Contributed to the GNU project by Marco Bodrato.
6 The idea of applying toom to unbalanced multiplication is due to Marco
7 Bodrato and Alberto Zanoni.
9 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
10 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
11 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
13 Copyright 2009 Free Software Foundation, Inc.
15 This file is part of the GNU MP Library.
17 The GNU MP Library is free software; you can redistribute it and/or modify
18 it under the terms of either:
20 * the GNU Lesser General Public License as published by the Free
21 Software Foundation; either version 3 of the License, or (at your
22 option) any later version.
26 * the GNU General Public License as published by the Free Software
27 Foundation; either version 2 of the License, or (at your option) any
30 or both in parallel, as here.
32 The GNU MP Library is distributed in the hope that it will be useful, but
33 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
34 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
37 You should have received copies of the GNU General Public License and the
38 GNU Lesser General Public License along with the GNU MP Library. If not,
39 see https://www.gnu.org/licenses/. */
45 /* Evaluate in: -2, -1, 0, +1, +2, +inf
47 <-s-><--n--><--n--><--n--><--n-->
48 ___ ______ ______ ______ ______
49 |a4_|___a3_|___a2_|___a1_|___a0_|
53 v0 = a0 * b0 # A(0)*B(0)
54 v1 = (a0+ a1+ a2+ a3+ a4)*(b0+ b1) # A(1)*B(1) ah <= 4 bh <= 1
55 vm1 = (a0- a1+ a2- a3+ a4)*(b0- b1) # A(-1)*B(-1) |ah| <= 2 bh = 0
56 v2 = (a0+2a1+4a2+8a3+16a4)*(b0+2b1) # A(2)*B(2) ah <= 30 bh <= 2
57 vm2 = (a0-2a1+4a2-8a3+16a4)*(b0-2b1) # A(-2)*B(-2) |ah| <= 20 |bh|<= 1
58 vinf= a4 * b1 # A(inf)*B(inf)
60 Some slight optimization in evaluation are taken from the paper:
61 "Towards Optimal Toom-Cook Multiplication for Univariate and
62 Multivariate Polynomials in Characteristic 2 and 0."
66 mpn_toom52_mul (mp_ptr pp
,
67 mp_srcptr ap
, mp_size_t an
,
68 mp_srcptr bp
, mp_size_t bn
, mp_ptr scratch
)
71 enum toom6_flags flags
;
75 #define a2 (ap + 2 * n)
76 #define a3 (ap + 3 * n)
77 #define a4 (ap + 4 * n)
81 n
= 1 + (2 * an
>= 5 * bn
? (an
- 1) / (size_t) 5 : (bn
- 1) >> 1);
86 ASSERT (0 < s
&& s
<= n
);
87 ASSERT (0 < t
&& t
<= n
);
89 /* Ensures that 5 values of n+1 limbs each fits in the product area.
90 Borderline cases are an = 32, bn = 8, n = 7, and an = 36, bn = 9,
94 #define v0 pp /* 2n */
95 #define vm1 (scratch) /* 2n+1 */
96 #define v1 (pp + 2 * n) /* 2n+1 */
97 #define vm2 (scratch + 2 * n + 1) /* 2n+1 */
98 #define v2 (scratch + 4 * n + 2) /* 2n+1 */
99 #define vinf (pp + 5 * n) /* s+t */
100 #define bs1 pp /* n+1 */
101 #define bsm1 (scratch + 2 * n + 2) /* n */
102 #define asm1 (scratch + 3 * n + 3) /* n+1 */
103 #define asm2 (scratch + 4 * n + 4) /* n+1 */
104 #define bsm2 (pp + n + 1) /* n+1 */
105 #define bs2 (pp + 2 * n + 2) /* n+1 */
106 #define as2 (pp + 3 * n + 3) /* n+1 */
107 #define as1 (pp + 4 * n + 4) /* n+1 */
109 /* Scratch need is 6 * n + 3 + 1. We need one extra limb, because
110 products will overwrite 2n+2 limbs. */
115 /* Compute as2 and asm2. */
116 flags
= (enum toom6_flags
) (toom6_vm2_neg
& mpn_toom_eval_pm2 (as2
, asm2
, 4, ap
, n
, s
, a1a3
));
118 /* Compute bs1 and bsm1. */
121 #if HAVE_NATIVE_mpn_add_n_sub_n
124 if (mpn_cmp (b0
, b1
, n
) < 0)
126 cy
= mpn_add_n_sub_n (bs1
, bsm1
, b1
, b0
, n
);
127 flags
= (enum toom6_flags
) (flags
^ toom6_vm1_neg
);
131 cy
= mpn_add_n_sub_n (bs1
, bsm1
, b0
, b1
, n
);
135 bs1
[n
] = mpn_add_n (bs1
, b0
, b1
, n
);
136 if (mpn_cmp (b0
, b1
, n
) < 0)
138 mpn_sub_n (bsm1
, b1
, b0
, n
);
139 flags
= (enum toom6_flags
) (flags
^ toom6_vm1_neg
);
143 mpn_sub_n (bsm1
, b0
, b1
, n
);
149 bs1
[n
] = mpn_add (bs1
, b0
, n
, b1
, t
);
150 if (mpn_zero_p (b0
+ t
, n
- t
) && mpn_cmp (b0
, b1
, t
) < 0)
152 mpn_sub_n (bsm1
, b1
, b0
, t
);
153 MPN_ZERO (bsm1
+ t
, n
- t
);
154 flags
= (enum toom6_flags
) (flags
^ toom6_vm1_neg
);
158 mpn_sub (bsm1
, b0
, n
, b1
, t
);
162 /* Compute bs2 and bsm2, recycling bs1 and bsm1. bs2=bs1+b1; bsm2=bsm1-b1 */
163 mpn_add (bs2
, bs1
, n
+1, b1
, t
);
164 if (flags
& toom6_vm1_neg
)
166 bsm2
[n
] = mpn_add (bsm2
, bsm1
, n
, b1
, t
);
167 flags
= (enum toom6_flags
) (flags
^ toom6_vm2_neg
);
174 if (mpn_cmp (bsm1
, b1
, n
) < 0)
176 mpn_sub_n (bsm2
, b1
, bsm1
, n
);
177 flags
= (enum toom6_flags
) (flags
^ toom6_vm2_neg
);
181 mpn_sub_n (bsm2
, bsm1
, b1
, n
);
186 if (mpn_zero_p (bsm1
+ t
, n
- t
) && mpn_cmp (bsm1
, b1
, t
) < 0)
188 mpn_sub_n (bsm2
, b1
, bsm1
, t
);
189 MPN_ZERO (bsm2
+ t
, n
- t
);
190 flags
= (enum toom6_flags
) (flags
^ toom6_vm2_neg
);
194 mpn_sub (bsm2
, bsm1
, n
, b1
, t
);
199 /* Compute as1 and asm1. */
200 flags
= (enum toom6_flags
) (flags
^ (toom6_vm1_neg
& mpn_toom_eval_pm1 (as1
, asm1
, 4, ap
, n
, s
, a0a2
)));
202 ASSERT (as1
[n
] <= 4);
203 ASSERT (bs1
[n
] <= 1);
204 ASSERT (asm1
[n
] <= 2);
205 /* ASSERT (bsm1[n] <= 1); */
206 ASSERT (as2
[n
] <=30);
207 ASSERT (bs2
[n
] <= 2);
208 ASSERT (asm2
[n
] <= 20);
209 ASSERT (bsm2
[n
] <= 1);
211 /* vm1, 2n+1 limbs */
212 mpn_mul (vm1
, asm1
, n
+1, bsm1
, n
); /* W4 */
214 /* vm2, 2n+1 limbs */
215 mpn_mul_n (vm2
, asm2
, bsm2
, n
+1); /* W2 */
218 mpn_mul_n (v2
, as2
, bs2
, n
+1); /* W1 */
221 mpn_mul_n (v1
, as1
, bs1
, n
+1); /* W3 */
223 /* vinf, s+t limbs */ /* W0 */
224 if (s
> t
) mpn_mul (vinf
, a4
, s
, b1
, t
);
225 else mpn_mul (vinf
, b1
, t
, a4
, s
);
228 mpn_mul_n (v0
, ap
, bp
, n
); /* W5 */
230 mpn_toom_interpolate_6pts (pp
, n
, flags
, vm1
, vm2
, v2
, t
+ s
);