186d7f5d3SJohn Marino /* mpn_toom_eval_pm2rexp -- Evaluate a polynomial in +2^-k and -2^-k
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino Contributed to the GNU project by Marco Bodrato
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 #if HAVE_NATIVE_mpn_addlsh_n
3186d7f5d3SJohn Marino #define DO_mpn_addlsh_n(dst,src,n,s,ws) mpn_addlsh_n(dst,dst,src,n,s)
3286d7f5d3SJohn Marino #else
3386d7f5d3SJohn Marino static mp_limb_t
DO_mpn_addlsh_n(mp_ptr dst,mp_srcptr src,mp_size_t n,unsigned int s,mp_ptr ws)3486d7f5d3SJohn Marino DO_mpn_addlsh_n(mp_ptr dst, mp_srcptr src, mp_size_t n, unsigned int s, mp_ptr ws)
3586d7f5d3SJohn Marino {
3686d7f5d3SJohn Marino #if USE_MUL_1 && 0
3786d7f5d3SJohn Marino return mpn_addmul_1(dst,src,n,CNST_LIMB(1) <<(s));
3886d7f5d3SJohn Marino #else
3986d7f5d3SJohn Marino mp_limb_t __cy;
4086d7f5d3SJohn Marino __cy = mpn_lshift(ws,src,n,s);
4186d7f5d3SJohn Marino return __cy + mpn_add_n(dst,dst,ws,n);
4286d7f5d3SJohn Marino #endif
4386d7f5d3SJohn Marino }
4486d7f5d3SJohn Marino #endif
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino /* Evaluates a polynomial of degree k >= 3. */
4786d7f5d3SJohn Marino 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)4886d7f5d3SJohn Marino mpn_toom_eval_pm2rexp (mp_ptr rp, mp_ptr rm,
4986d7f5d3SJohn Marino unsigned int q, mp_srcptr ap, mp_size_t n, mp_size_t t,
5086d7f5d3SJohn Marino unsigned int s, mp_ptr ws)
5186d7f5d3SJohn Marino {
5286d7f5d3SJohn Marino unsigned int i;
5386d7f5d3SJohn Marino int neg;
5486d7f5d3SJohn Marino /* {ap,q*n+t} -> {rp,n+1} {rm,n+1} , with {ws, n+1}*/
5586d7f5d3SJohn Marino ASSERT (n >= t);
5686d7f5d3SJohn Marino ASSERT (s != 0); /* or _eval_pm1 should be used */
5786d7f5d3SJohn Marino ASSERT (q > 1);
5886d7f5d3SJohn Marino ASSERT (s*q < GMP_NUMB_BITS);
5986d7f5d3SJohn Marino rp[n] = mpn_lshift(rp, ap, n, s*q);
6086d7f5d3SJohn Marino ws[n] = mpn_lshift(ws, ap+n, n, s*(q-1));
6186d7f5d3SJohn Marino if( (q & 1) != 0) {
6286d7f5d3SJohn Marino ASSERT_NOCARRY(mpn_add(ws,ws,n+1,ap+n*q,t));
6386d7f5d3SJohn Marino rp[n] += DO_mpn_addlsh_n(rp, ap+n*(q-1), n, s, rm);
6486d7f5d3SJohn Marino } else {
6586d7f5d3SJohn Marino ASSERT_NOCARRY(mpn_add(rp,rp,n+1,ap+n*q,t));
6686d7f5d3SJohn Marino }
6786d7f5d3SJohn Marino for(i=2; i<q-1; i++)
6886d7f5d3SJohn Marino {
6986d7f5d3SJohn Marino rp[n] += DO_mpn_addlsh_n(rp, ap+n*i, n, s*(q-i), rm);
7086d7f5d3SJohn Marino i++;
7186d7f5d3SJohn Marino ws[n] += DO_mpn_addlsh_n(ws, ap+n*i, n, s*(q-i), rm);
7286d7f5d3SJohn Marino };
7386d7f5d3SJohn Marino
7486d7f5d3SJohn Marino neg = (mpn_cmp (rp, ws, n + 1) < 0) ? ~0 : 0;
7586d7f5d3SJohn Marino
7686d7f5d3SJohn Marino #if HAVE_NATIVE_mpn_add_n_sub_n
7786d7f5d3SJohn Marino if (neg)
7886d7f5d3SJohn Marino mpn_add_n_sub_n (rp, rm, ws, rp, n + 1);
7986d7f5d3SJohn Marino else
8086d7f5d3SJohn Marino mpn_add_n_sub_n (rp, rm, rp, ws, n + 1);
8186d7f5d3SJohn Marino #else /* !HAVE_NATIVE_mpn_add_n_sub_n */
8286d7f5d3SJohn Marino if (neg)
8386d7f5d3SJohn Marino mpn_sub_n (rm, ws, rp, n + 1);
8486d7f5d3SJohn Marino else
8586d7f5d3SJohn Marino mpn_sub_n (rm, rp, ws, n + 1);
8686d7f5d3SJohn Marino
8786d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_add_n (rp, rp, ws, n + 1));
8886d7f5d3SJohn Marino #endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
8986d7f5d3SJohn Marino
9086d7f5d3SJohn Marino return neg;
9186d7f5d3SJohn Marino }
92