/src/gmp-6.2.1/mpn/toom_eval_pm2.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpn_toom_eval_pm2 -- Evaluate a polynomial in +2 and -2  | 
2  |  |  | 
3  |  |    Contributed to the GNU project by Niels Möller and Marco Bodrato  | 
4  |  |  | 
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.  | 
8  |  |  | 
9  |  | Copyright 2009 Free Software Foundation, Inc.  | 
10  |  |  | 
11  |  | This file is part of the GNU MP Library.  | 
12  |  |  | 
13  |  | The GNU MP Library is free software; you can redistribute it and/or modify  | 
14  |  | it under the terms of either:  | 
15  |  |  | 
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.  | 
19  |  |  | 
20  |  | or  | 
21  |  |  | 
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.  | 
25  |  |  | 
26  |  | or both in parallel, as here.  | 
27  |  |  | 
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.  | 
32  |  |  | 
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/.  */  | 
36  |  |  | 
37  |  | #include "gmp-impl.h"  | 
38  |  |  | 
39  |  | /* DO_addlsh2(d,a,b,n,cy) computes cy,{d,n} <- {a,n} + 4*(cy,{b,n}), it | 
40  |  |    can be used as DO_addlsh2(d,a,d,n,d[n]), for accumulation on {d,n+1}. */ | 
41  |  | #if HAVE_NATIVE_mpn_addlsh2_n  | 
42  | 0  | #define DO_addlsh2(d, a, b, n, cy)  \  | 
43  | 0  | do {         \ | 
44  | 0  |   (cy) <<= 2;       \  | 
45  | 0  |   (cy) += mpn_addlsh2_n(d, a, b, n); \  | 
46  | 0  | } while (0)  | 
47  |  | #else  | 
48  |  | #if HAVE_NATIVE_mpn_addlsh_n  | 
49  |  | #define DO_addlsh2(d, a, b, n, cy)  \  | 
50  |  | do {          \ | 
51  |  |   (cy) <<= 2;       \  | 
52  |  |   (cy) += mpn_addlsh_n(d, a, b, n, 2);  \  | 
53  |  | } while (0)  | 
54  |  | #else  | 
55  |  | /* The following is not a general substitute for addlsh2.  | 
56  |  |    It is correct if d == b, but it is not if d == a.  */  | 
57  |  | #define DO_addlsh2(d, a, b, n, cy)  \  | 
58  |  | do {          \ | 
59  |  |   (cy) <<= 2;       \  | 
60  |  |   (cy) += mpn_lshift(d, b, n, 2); \  | 
61  |  |   (cy) += mpn_add_n(d, d, a, n);  \  | 
62  |  | } while (0)  | 
63  |  | #endif  | 
64  |  | #endif  | 
65  |  |  | 
66  |  | /* Evaluates a polynomial of degree 2 < k < GMP_NUMB_BITS, in the  | 
67  |  |    points +2 and -2. */  | 
68  |  | int  | 
69  |  | mpn_toom_eval_pm2 (mp_ptr xp2, mp_ptr xm2, unsigned k,  | 
70  |  |        mp_srcptr xp, mp_size_t n, mp_size_t hn, mp_ptr tp)  | 
71  | 0  | { | 
72  | 0  |   int i;  | 
73  | 0  |   int neg;  | 
74  | 0  |   mp_limb_t cy;  | 
75  |  | 
  | 
76  | 0  |   ASSERT (k >= 3);  | 
77  | 0  |   ASSERT (k < GMP_NUMB_BITS);  | 
78  |  |  | 
79  | 0  |   ASSERT (hn > 0);  | 
80  | 0  |   ASSERT (hn <= n);  | 
81  |  |  | 
82  |  |   /* The degree k is also the number of full-size coefficients, so  | 
83  |  |    * that last coefficient, of size hn, starts at xp + k*n. */  | 
84  |  |  | 
85  | 0  |   cy = 0;  | 
86  | 0  |   DO_addlsh2 (xp2, xp + (k-2) * n, xp + k * n, hn, cy);  | 
87  | 0  |   if (hn != n)  | 
88  | 0  |     cy = mpn_add_1 (xp2 + hn, xp + (k-2) * n + hn, n - hn, cy);  | 
89  | 0  |   for (i = k - 4; i >= 0; i -= 2)  | 
90  | 0  |     DO_addlsh2 (xp2, xp + i * n, xp2, n, cy);  | 
91  | 0  |   xp2[n] = cy;  | 
92  |  | 
  | 
93  | 0  |   k--;  | 
94  |  | 
  | 
95  | 0  |   cy = 0;  | 
96  | 0  |   DO_addlsh2 (tp, xp + (k-2) * n, xp + k * n, n, cy);  | 
97  | 0  |   for (i = k - 4; i >= 0; i -= 2)  | 
98  | 0  |     DO_addlsh2 (tp, xp + i * n, tp, n, cy);  | 
99  | 0  |   tp[n] = cy;  | 
100  |  | 
  | 
101  | 0  |   if (k & 1)  | 
102  | 0  |     ASSERT_NOCARRY(mpn_lshift (tp , tp , n + 1, 1));  | 
103  | 0  |   else  | 
104  | 0  |     ASSERT_NOCARRY(mpn_lshift (xp2, xp2, n + 1, 1));  | 
105  |  |  | 
106  | 0  |   neg = (mpn_cmp (xp2, tp, n + 1) < 0) ? ~0 : 0;  | 
107  |  | 
  | 
108  |  | #if HAVE_NATIVE_mpn_add_n_sub_n  | 
109  |  |   if (neg)  | 
110  |  |     mpn_add_n_sub_n (xp2, xm2, tp, xp2, n + 1);  | 
111  |  |   else  | 
112  |  |     mpn_add_n_sub_n (xp2, xm2, xp2, tp, n + 1);  | 
113  |  | #else /* !HAVE_NATIVE_mpn_add_n_sub_n */  | 
114  | 0  |   if (neg)  | 
115  | 0  |     mpn_sub_n (xm2, tp, xp2, n + 1);  | 
116  | 0  |   else  | 
117  | 0  |     mpn_sub_n (xm2, xp2, tp, n + 1);  | 
118  |  | 
  | 
119  | 0  |   mpn_add_n (xp2, xp2, tp, n + 1);  | 
120  | 0  | #endif /* !HAVE_NATIVE_mpn_add_n_sub_n */  | 
121  |  | 
  | 
122  | 0  |   ASSERT (xp2[n] < (1<<(k+2))-1);  | 
123  | 0  |   ASSERT (xm2[n] < ((1<<(k+3))-1 - (1^k&1))/3);  | 
124  |  |  | 
125  | 0  |   neg ^= ((k & 1) - 1);  | 
126  |  | 
  | 
127  | 0  |   return neg;  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | #undef DO_addlsh2  |