1 /* mpn_toom_eval_pm1 -- Evaluate a polynomial in +1 and -1
2 
3    Contributed to the GNU project by Niels M�ller
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 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.
17 
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.
22 
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/.  */
25 
26 
27 #include "gmp.h"
28 #include "gmp-impl.h"
29 
30 /* Evaluates a polynomial of degree k > 3, in the points +1 and -1. */
31 int
32 mpn_toom_eval_pm1 (mp_ptr xp1, mp_ptr xm1, unsigned k,
33 		   mp_srcptr xp, mp_size_t n, mp_size_t hn, mp_ptr tp)
34 {
35   unsigned i;
36   int neg;
37 
38   ASSERT (k >= 4);
39 
40   ASSERT (hn > 0);
41   ASSERT (hn <= n);
42 
43   /* The degree k is also the number of full-size coefficients, so
44    * that last coefficient, of size hn, starts at xp + k*n. */
45 
46   xp1[n] = mpn_add_n (xp1, xp, xp + 2*n, n);
47   for (i = 4; i < k; i += 2)
48     ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+i*n, n));
49 
50   tp[n] = mpn_add_n (tp, xp + n, xp + 3*n, n);
51   for (i = 5; i < k; i += 2)
52     ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+i*n, n));
53 
54   if (k & 1)
55     ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+k*n, hn));
56   else
57     ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+k*n, hn));
58 
59   neg = (mpn_cmp (xp1, tp, n + 1) < 0) ? ~0 : 0;
60 
61 #if HAVE_NATIVE_mpn_add_n_sub_n
62   if (neg)
63     mpn_add_n_sub_n (xp1, xm1, tp, xp1, n + 1);
64   else
65     mpn_add_n_sub_n (xp1, xm1, xp1, tp, n + 1);
66 #else
67   if (neg)
68     mpn_sub_n (xm1, tp, xp1, n + 1);
69   else
70     mpn_sub_n (xm1, xp1, tp, n + 1);
71 
72   mpn_add_n (xp1, xp1, tp, n + 1);
73 #endif
74 
75   ASSERT (xp1[n] <= k);
76   ASSERT (xm1[n] <= k/2 + 1);
77 
78   return neg;
79 }
80