1/* mpn_toom_eval_pm2rexp -- Evaluate a polynomial in +2^-k and -2^-k
2
3   Contributed to the GNU project by 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
9Copyright 2009 Free Software Foundation, Inc.
10
11This file is part of the GNU MP Library.
12
13The GNU MP Library is free software; you can redistribute it and/or modify
14it 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
20or
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
26or both in parallel, as here.
27
28The GNU MP Library is distributed in the hope that it will be useful, but
29WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31for more details.
32
33You should have received copies of the GNU General Public License and the
34GNU Lesser General Public License along with the GNU MP Library.  If not,
35see https://www.gnu.org/licenses/.  */
36
37
38#include "gmp-impl.h"
39
40#if HAVE_NATIVE_mpn_addlsh_n
41#define DO_mpn_addlsh_n(dst,src,n,s,ws) mpn_addlsh_n(dst,dst,src,n,s)
42#else
43static mp_limb_t
44DO_mpn_addlsh_n(mp_ptr dst, mp_srcptr src, mp_size_t n, unsigned int s, mp_ptr ws)
45{
46#if USE_MUL_1 && 0
47  return mpn_addmul_1(dst,src,n,CNST_LIMB(1) <<(s));
48#else
49  mp_limb_t __cy;
50  __cy = mpn_lshift(ws,src,n,s);
51  return    __cy + mpn_add_n(dst,dst,ws,n);
52#endif
53}
54#endif
55
56/* Evaluates a polynomial of degree k >= 3. */
57int
58mpn_toom_eval_pm2rexp (mp_ptr rp, mp_ptr rm,
59		      unsigned int q, mp_srcptr ap, mp_size_t n, mp_size_t t,
60		      unsigned int s, mp_ptr ws)
61{
62  unsigned int i;
63  int neg;
64  /* {ap,q*n+t} -> {rp,n+1} {rm,n+1} , with {ws, n+1}*/
65  ASSERT (n >= t);
66  ASSERT (s != 0); /* or _eval_pm1 should be used */
67  ASSERT (q > 1);
68  ASSERT (s*q < GMP_NUMB_BITS);
69  rp[n] = mpn_lshift(rp, ap, n, s*q);
70  ws[n] = mpn_lshift(ws, ap+n, n, s*(q-1));
71  if( (q & 1) != 0) {
72    ASSERT_NOCARRY(mpn_add(ws,ws,n+1,ap+n*q,t));
73    rp[n] += DO_mpn_addlsh_n(rp, ap+n*(q-1), n, s, rm);
74  } else {
75    ASSERT_NOCARRY(mpn_add(rp,rp,n+1,ap+n*q,t));
76  }
77  for(i=2; i<q-1; i++)
78  {
79    rp[n] += DO_mpn_addlsh_n(rp, ap+n*i, n, s*(q-i), rm);
80    i++;
81    ws[n] += DO_mpn_addlsh_n(ws, ap+n*i, n, s*(q-i), rm);
82  };
83
84  neg = (mpn_cmp (rp, ws, n + 1) < 0) ? ~0 : 0;
85
86#if HAVE_NATIVE_mpn_add_n_sub_n
87  if (neg)
88    mpn_add_n_sub_n (rp, rm, ws, rp, n + 1);
89  else
90    mpn_add_n_sub_n (rp, rm, rp, ws, n + 1);
91#else /* !HAVE_NATIVE_mpn_add_n_sub_n */
92  if (neg)
93    mpn_sub_n (rm, ws, rp, n + 1);
94  else
95    mpn_sub_n (rm, rp, ws, n + 1);
96
97  ASSERT_NOCARRY (mpn_add_n (rp, rp, ws, n + 1));
98#endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
99
100  return neg;
101}
102