186d7f5d3SJohn Marino /* mpn_toom_eval_pm1 -- Evaluate a polynomial in +1 and -1
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Contributed to the GNU project by Niels M�ller
486d7f5d3SJohn Marino
586d7f5d3SJohn Marino THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
686d7f5d3SJohn Marino SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
786d7f5d3SJohn Marino GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
886d7f5d3SJohn Marino
986d7f5d3SJohn Marino Copyright 2009 Free Software Foundation, Inc.
1086d7f5d3SJohn Marino
1186d7f5d3SJohn Marino This file is part of the GNU MP Library.
1286d7f5d3SJohn Marino
1386d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1486d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1586d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1686d7f5d3SJohn Marino option) any later version.
1786d7f5d3SJohn Marino
1886d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1986d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2086d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
2186d7f5d3SJohn Marino License for more details.
2286d7f5d3SJohn Marino
2386d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2486d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
2586d7f5d3SJohn Marino
2686d7f5d3SJohn Marino
2786d7f5d3SJohn Marino #include "gmp.h"
2886d7f5d3SJohn Marino #include "gmp-impl.h"
2986d7f5d3SJohn Marino
3086d7f5d3SJohn Marino /* Evaluates a polynomial of degree k > 3, in the points +1 and -1. */
3186d7f5d3SJohn Marino int
mpn_toom_eval_pm1(mp_ptr xp1,mp_ptr xm1,unsigned k,mp_srcptr xp,mp_size_t n,mp_size_t hn,mp_ptr tp)3286d7f5d3SJohn Marino mpn_toom_eval_pm1 (mp_ptr xp1, mp_ptr xm1, unsigned k,
3386d7f5d3SJohn Marino mp_srcptr xp, mp_size_t n, mp_size_t hn, mp_ptr tp)
3486d7f5d3SJohn Marino {
3586d7f5d3SJohn Marino unsigned i;
3686d7f5d3SJohn Marino int neg;
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino ASSERT (k >= 4);
3986d7f5d3SJohn Marino
4086d7f5d3SJohn Marino ASSERT (hn > 0);
4186d7f5d3SJohn Marino ASSERT (hn <= n);
4286d7f5d3SJohn Marino
4386d7f5d3SJohn Marino /* The degree k is also the number of full-size coefficients, so
4486d7f5d3SJohn Marino * that last coefficient, of size hn, starts at xp + k*n. */
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino xp1[n] = mpn_add_n (xp1, xp, xp + 2*n, n);
4786d7f5d3SJohn Marino for (i = 4; i < k; i += 2)
4886d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+i*n, n));
4986d7f5d3SJohn Marino
5086d7f5d3SJohn Marino tp[n] = mpn_add_n (tp, xp + n, xp + 3*n, n);
5186d7f5d3SJohn Marino for (i = 5; i < k; i += 2)
5286d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+i*n, n));
5386d7f5d3SJohn Marino
5486d7f5d3SJohn Marino if (k & 1)
5586d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+k*n, hn));
5686d7f5d3SJohn Marino else
5786d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+k*n, hn));
5886d7f5d3SJohn Marino
5986d7f5d3SJohn Marino neg = (mpn_cmp (xp1, tp, n + 1) < 0) ? ~0 : 0;
6086d7f5d3SJohn Marino
6186d7f5d3SJohn Marino #if HAVE_NATIVE_mpn_add_n_sub_n
6286d7f5d3SJohn Marino if (neg)
6386d7f5d3SJohn Marino mpn_add_n_sub_n (xp1, xm1, tp, xp1, n + 1);
6486d7f5d3SJohn Marino else
6586d7f5d3SJohn Marino mpn_add_n_sub_n (xp1, xm1, xp1, tp, n + 1);
6686d7f5d3SJohn Marino #else
6786d7f5d3SJohn Marino if (neg)
6886d7f5d3SJohn Marino mpn_sub_n (xm1, tp, xp1, n + 1);
6986d7f5d3SJohn Marino else
7086d7f5d3SJohn Marino mpn_sub_n (xm1, xp1, tp, n + 1);
7186d7f5d3SJohn Marino
7286d7f5d3SJohn Marino mpn_add_n (xp1, xp1, tp, n + 1);
7386d7f5d3SJohn Marino #endif
7486d7f5d3SJohn Marino
7586d7f5d3SJohn Marino ASSERT (xp1[n] <= k);
7686d7f5d3SJohn Marino ASSERT (xm1[n] <= k/2 + 1);
7786d7f5d3SJohn Marino
7886d7f5d3SJohn Marino return neg;
7986d7f5d3SJohn Marino }
80