1/* mpn_toom_eval_dgr3_pm2 -- Evaluate a degree 3 polynomial in +2 and -2
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
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 the GNU Lesser General Public License as published by
15the Free Software Foundation; either version 3 of the License, or (at your
16option) any later version.
17
18The GNU MP Library is distributed in the hope that it will be useful, but
19WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21License for more details.
22
23You should have received a copy of the GNU Lesser General Public License
24along 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/* Needs n+1 limbs of temporary storage. */
31int
32mpn_toom_eval_dgr3_pm2 (mp_ptr xp2, mp_ptr xm2,
33			mp_srcptr xp, mp_size_t n, mp_size_t x3n, mp_ptr tp)
34{
35  mp_limb_t cy;
36  int neg;
37
38  ASSERT (x3n > 0);
39  ASSERT (x3n <= n);
40
41  /* (x0 + 4 * x2) +/- (2 x1 + 8 x_3) */
42#if HAVE_NATIVE_mpn_addlsh_n || HAVE_NATIVE_mpn_addlsh2_n
43#if HAVE_NATIVE_mpn_addlsh2_n
44  xp2[n] = mpn_addlsh2_n (xp2, xp, xp + 2*n, n);
45
46  cy = mpn_addlsh2_n (tp, xp + n, xp + 3*n, x3n);
47#else /* HAVE_NATIVE_mpn_addlsh_n */
48  xp2[n] = mpn_addlsh_n (xp2, xp, xp + 2*n, n, 2);
49
50  cy = mpn_addlsh_n (tp, xp + n, xp + 3*n, x3n, 2);
51#endif
52  if (x3n < n)
53    cy = mpn_add_1 (tp + x3n, xp + n + x3n, n - x3n, cy);
54  tp[n] = cy;
55#else
56  cy = mpn_lshift (tp, xp + 2*n, n, 2);
57  xp2[n] = cy + mpn_add_n (xp2, tp, xp, n);
58
59  tp[x3n] = mpn_lshift (tp, xp + 3*n, x3n, 2);
60  if (x3n < n)
61    tp[n] = mpn_add (tp, xp + n, n, tp, x3n + 1);
62  else
63    tp[n] += mpn_add_n (tp, xp + n, tp, n);
64#endif
65  mpn_lshift (tp, tp, n+1, 1);
66
67  neg = (mpn_cmp (xp2, tp, n + 1) < 0) ? ~0 : 0;
68
69#if HAVE_NATIVE_mpn_add_n_sub_n
70  if (neg)
71    mpn_add_n_sub_n (xp2, xm2, tp, xp2, n + 1);
72  else
73    mpn_add_n_sub_n (xp2, xm2, xp2, tp, n + 1);
74#else
75  if (neg)
76    mpn_sub_n (xm2, tp, xp2, n + 1);
77  else
78    mpn_sub_n (xm2, xp2, tp, n + 1);
79
80  mpn_add_n (xp2, xp2, tp, n + 1);
81#endif
82
83  ASSERT (xp2[n] < 15);
84  ASSERT (xm2[n] < 10);
85
86  return neg;
87}
88