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 
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 
38 #include "gmp.h"
39 #include "gmp-impl.h"
40 
41 #if HAVE_NATIVE_mpn_addlsh_n
42 #define DO_mpn_addlsh_n(dst,src,n,s,ws) mpn_addlsh_n(dst,dst,src,n,s)
43 #else
44 static mp_limb_t
DO_mpn_addlsh_n(mp_ptr dst,mp_srcptr src,mp_size_t n,unsigned int s,mp_ptr ws)45 DO_mpn_addlsh_n(mp_ptr dst, mp_srcptr src, mp_size_t n, unsigned int s, mp_ptr ws)
46 {
47 #if USE_MUL_1 && 0
48   return mpn_addmul_1(dst,src,n,CNST_LIMB(1) <<(s));
49 #else
50   mp_limb_t __cy;
51   __cy = mpn_lshift(ws,src,n,s);
52   return    __cy + mpn_add_n(dst,dst,ws,n);
53 #endif
54 }
55 #endif
56 
57 /* Evaluates a polynomial of degree k >= 3. */
58 int
mpn_toom_eval_pm2rexp(mp_ptr rp,mp_ptr rm,unsigned int q,mp_srcptr ap,mp_size_t n,mp_size_t t,unsigned int s,mp_ptr ws)59 mpn_toom_eval_pm2rexp (mp_ptr rp, mp_ptr rm,
60 		      unsigned int q, mp_srcptr ap, mp_size_t n, mp_size_t t,
61 		      unsigned int s, mp_ptr ws)
62 {
63   unsigned int i;
64   int neg;
65   /* {ap,q*n+t} -> {rp,n+1} {rm,n+1} , with {ws, n+1}*/
66   ASSERT (n >= t);
67   ASSERT (s != 0); /* or _eval_pm1 should be used */
68   ASSERT (q > 1);
69   ASSERT (s*q < GMP_NUMB_BITS);
70   rp[n] = mpn_lshift(rp, ap, n, s*q);
71   ws[n] = mpn_lshift(ws, ap+n, n, s*(q-1));
72   if( (q & 1) != 0) {
73     ASSERT_NOCARRY(mpn_add(ws,ws,n+1,ap+n*q,t));
74     rp[n] += DO_mpn_addlsh_n(rp, ap+n*(q-1), n, s, rm);
75   } else {
76     ASSERT_NOCARRY(mpn_add(rp,rp,n+1,ap+n*q,t));
77   }
78   for(i=2; i<q-1; i++)
79   {
80     rp[n] += DO_mpn_addlsh_n(rp, ap+n*i, n, s*(q-i), rm);
81     i++;
82     ws[n] += DO_mpn_addlsh_n(ws, ap+n*i, n, s*(q-i), rm);
83   };
84 
85   neg = (mpn_cmp (rp, ws, n + 1) < 0) ? ~0 : 0;
86 
87 #if HAVE_NATIVE_mpn_add_n_sub_n
88   if (neg)
89     mpn_add_n_sub_n (rp, rm, ws, rp, n + 1);
90   else
91     mpn_add_n_sub_n (rp, rm, rp, ws, n + 1);
92 #else /* !HAVE_NATIVE_mpn_add_n_sub_n */
93   if (neg)
94     mpn_sub_n (rm, ws, rp, n + 1);
95   else
96     mpn_sub_n (rm, rp, ws, n + 1);
97 
98   ASSERT_NOCARRY (mpn_add_n (rp, rp, ws, n + 1));
99 #endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
100 
101   return neg;
102 }
103