beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / toom54_mul.c
blob939bb53ab68682f1da63e4526446425fc98789ff
1 /* Implementation of the algorithm for Toom-Cook 4.5-way.
3 Contributed to the GNU project by Marco Bodrato.
5 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
6 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
7 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
9 Copyright 2009, 2012 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 either:
16 * the GNU Lesser General Public License as published by the Free
17 Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
22 * the GNU General Public License as published by the Free Software
23 Foundation; either version 2 of the License, or (at your option) any
24 later version.
26 or both in parallel, as here.
28 The GNU MP Library is distributed in the hope that it will be useful, but
29 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 for more details.
33 You should have received copies of the GNU General Public License and the
34 GNU Lesser General Public License along with the GNU MP Library. If not,
35 see https://www.gnu.org/licenses/. */
37 #include "gmp.h"
38 #include "gmp-impl.h"
41 /* Toom-4.5, the splitting 5x4 unbalanced version.
42 Evaluate in: infinity, +4, -4, +2, -2, +1, -1, 0.
44 <--s-><--n--><--n--><--n--><--n-->
45 ____ ______ ______ ______ ______
46 |_a4_|__a3__|__a2__|__a1__|__a0__|
47 |b3_|__b2__|__b1__|__b0__|
48 <-t-><--n--><--n--><--n-->
51 #define TOOM_54_MUL_N_REC(p, a, b, n, ws) \
52 do { mpn_mul_n (p, a, b, n); \
53 } while (0)
55 #define TOOM_54_MUL_REC(p, a, na, b, nb, ws) \
56 do { mpn_mul (p, a, na, b, nb); \
57 } while (0)
59 void
60 mpn_toom54_mul (mp_ptr pp,
61 mp_srcptr ap, mp_size_t an,
62 mp_srcptr bp, mp_size_t bn, mp_ptr scratch)
64 mp_size_t n, s, t;
65 int sign;
67 /***************************** decomposition *******************************/
68 #define a4 (ap + 4 * n)
69 #define b3 (bp + 3 * n)
71 ASSERT (an >= bn);
72 n = 1 + (4 * an >= 5 * bn ? (an - 1) / (size_t) 5 : (bn - 1) / (size_t) 4);
74 s = an - 4 * n;
75 t = bn - 3 * n;
77 ASSERT (0 < s && s <= n);
78 ASSERT (0 < t && t <= n);
79 /* Required by mpn_toom_interpolate_8pts. */
80 ASSERT ( s + t >= n );
81 ASSERT ( s + t > 4);
82 ASSERT ( n > 2);
84 #define r8 pp /* 2n */
85 #define r7 scratch /* 3n+1 */
86 #define r5 (pp + 3*n) /* 3n+1 */
87 #define v0 (pp + 3*n) /* n+1 */
88 #define v1 (pp + 4*n+1) /* n+1 */
89 #define v2 (pp + 5*n+2) /* n+1 */
90 #define v3 (pp + 6*n+3) /* n+1 */
91 #define r3 (scratch + 3 * n + 1) /* 3n+1 */
92 #define r1 (pp + 7*n) /* s+t <= 2*n */
93 #define ws (scratch + 6 * n + 2) /* ??? */
95 /* Alloc also 3n+1 limbs for ws... mpn_toom_interpolate_8pts may
96 need all of them, when DO_mpn_sublsh_n usea a scratch */
97 /********************** evaluation and recursive calls *********************/
98 /* $\pm4$ */
99 sign = mpn_toom_eval_pm2exp (v2, v0, 4, ap, n, s, 2, pp)
100 ^ mpn_toom_eval_pm2exp (v3, v1, 3, bp, n, t, 2, pp);
101 TOOM_54_MUL_N_REC(pp, v0, v1, n + 1, ws); /* A(-4)*B(-4) */
102 TOOM_54_MUL_N_REC(r3, v2, v3, n + 1, ws); /* A(+4)*B(+4) */
103 mpn_toom_couple_handling (r3, 2*n+1, pp, sign, n, 2, 4);
105 /* $\pm1$ */
106 sign = mpn_toom_eval_pm1 (v2, v0, 4, ap, n, s, pp)
107 ^ mpn_toom_eval_dgr3_pm1 (v3, v1, bp, n, t, pp);
108 TOOM_54_MUL_N_REC(pp, v0, v1, n + 1, ws); /* A(-1)*B(-1) */
109 TOOM_54_MUL_N_REC(r7, v2, v3, n + 1, ws); /* A(1)*B(1) */
110 mpn_toom_couple_handling (r7, 2*n+1, pp, sign, n, 0, 0);
112 /* $\pm2$ */
113 sign = mpn_toom_eval_pm2 (v2, v0, 4, ap, n, s, pp)
114 ^ mpn_toom_eval_dgr3_pm2 (v3, v1, bp, n, t, pp);
115 TOOM_54_MUL_N_REC(pp, v0, v1, n + 1, ws); /* A(-2)*B(-2) */
116 TOOM_54_MUL_N_REC(r5, v2, v3, n + 1, ws); /* A(+2)*B(+2) */
117 mpn_toom_couple_handling (r5, 2*n+1, pp, sign, n, 1, 2);
119 /* A(0)*B(0) */
120 TOOM_54_MUL_N_REC(pp, ap, bp, n, ws);
122 /* Infinity */
123 if (s > t) {
124 TOOM_54_MUL_REC(r1, a4, s, b3, t, ws);
125 } else {
126 TOOM_54_MUL_REC(r1, b3, t, a4, s, ws);
129 mpn_toom_interpolate_8pts (pp, n, r3, r7, s + t, ws);
131 #undef a4
132 #undef b3
133 #undef r1
134 #undef r3
135 #undef r5
136 #undef v0
137 #undef v1
138 #undef v2
139 #undef v3
140 #undef r7
141 #undef r8
142 #undef ws