Upgrade GMP from 4.3.2 to 5.0.2 on the vendor branch
[dragonfly.git] / contrib / gmp / mpn / generic / toom_eval_pm2rexp.c
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 the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
17
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21 License for more details.
22
23 You should have received a copy of the GNU Lesser General Public License
24 along 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 #if HAVE_NATIVE_mpn_addlsh_n
31 #define DO_mpn_addlsh_n(dst,src,n,s,ws) mpn_addlsh_n(dst,dst,src,n,s)
32 #else
33 static mp_limb_t
34 DO_mpn_addlsh_n(mp_ptr dst, mp_srcptr src, mp_size_t n, unsigned int s, mp_ptr ws)
35 {
36 #if USE_MUL_1 && 0
37   return mpn_addmul_1(dst,src,n,CNST_LIMB(1) <<(s));
38 #else
39   mp_limb_t __cy;
40   __cy = mpn_lshift(ws,src,n,s);
41   return    __cy + mpn_add_n(dst,dst,ws,n);
42 #endif
43 }
44 #endif
45
46 /* Evaluates a polynomial of degree k >= 3. */
47 int
48 mpn_toom_eval_pm2rexp (mp_ptr rp, mp_ptr rm,
49                       unsigned int q, mp_srcptr ap, mp_size_t n, mp_size_t t,
50                       unsigned int s, mp_ptr ws)
51 {
52   unsigned int i;
53   int neg;
54   /* {ap,q*n+t} -> {rp,n+1} {rm,n+1} , with {ws, n+1}*/
55   ASSERT (n >= t);
56   ASSERT (s != 0); /* or _eval_pm1 should be used */
57   ASSERT (q > 1);
58   ASSERT (s*q < GMP_NUMB_BITS);
59   rp[n] = mpn_lshift(rp, ap, n, s*q);
60   ws[n] = mpn_lshift(ws, ap+n, n, s*(q-1));
61   if( (q & 1) != 0) {
62     ASSERT_NOCARRY(mpn_add(ws,ws,n+1,ap+n*q,t));
63     rp[n] += DO_mpn_addlsh_n(rp, ap+n*(q-1), n, s, rm);
64   } else {
65     ASSERT_NOCARRY(mpn_add(rp,rp,n+1,ap+n*q,t));
66   }
67   for(i=2; i<q-1; i++)
68   {
69     rp[n] += DO_mpn_addlsh_n(rp, ap+n*i, n, s*(q-i), rm);
70     i++;
71     ws[n] += DO_mpn_addlsh_n(ws, ap+n*i, n, s*(q-i), rm);
72   };
73
74   neg = (mpn_cmp (rp, ws, n + 1) < 0) ? ~0 : 0;
75
76 #if HAVE_NATIVE_mpn_add_n_sub_n
77   if (neg)
78     mpn_add_n_sub_n (rp, rm, ws, rp, n + 1);
79   else
80     mpn_add_n_sub_n (rp, rm, rp, ws, n + 1);
81 #else /* !HAVE_NATIVE_mpn_add_n_sub_n */
82   if (neg)
83     mpn_sub_n (rm, ws, rp, n + 1);
84   else
85     mpn_sub_n (rm, rp, ws, n + 1);
86
87   ASSERT_NOCARRY (mpn_add_n (rp, rp, ws, n + 1));
88 #endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
89
90   return neg;
91 }