xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/toom_couple_handling.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Helper function for high degree Toom-Cook algorithms.
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, 2010 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-impl.h"
39 
40 /* Gets {pp,n} and (sign?-1:1)*{np,n}. Computes at once:
41      {pp,n} <- ({pp,n}+{np,n})/2^{ps+1}
42      {pn,n} <- ({pp,n}-{np,n})/2^{ns+1}
43    Finally recompose them obtaining:
44      {pp,n+off} <- {pp,n}+{np,n}*2^{off*GMP_NUMB_BITS}
45 */
46 void
mpn_toom_couple_handling(mp_ptr pp,mp_size_t n,mp_ptr np,int nsign,mp_size_t off,int ps,int ns)47 mpn_toom_couple_handling (mp_ptr pp, mp_size_t n, mp_ptr np,
48 			  int nsign, mp_size_t off, int ps, int ns)
49 {
50   if (nsign) {
51 #ifdef HAVE_NATIVE_mpn_rsh1sub_n
52     mpn_rsh1sub_n (np, pp, np, n);
53 #else
54     mpn_sub_n (np, pp, np, n);
55     mpn_rshift (np, np, n, 1);
56 #endif
57   } else {
58 #ifdef HAVE_NATIVE_mpn_rsh1add_n
59     mpn_rsh1add_n (np, pp, np, n);
60 #else
61     mpn_add_n (np, pp, np, n);
62     mpn_rshift (np, np, n, 1);
63 #endif
64   }
65 
66 #ifdef HAVE_NATIVE_mpn_rsh1sub_n
67   if (ps == 1)
68     mpn_rsh1sub_n (pp, pp, np, n);
69   else
70 #endif
71   {
72     mpn_sub_n (pp, pp, np, n);
73     if (ps > 0)
74       mpn_rshift (pp, pp, n, ps);
75   }
76   if (ns > 0)
77     mpn_rshift (np, np, n, ns);
78   pp[n] = mpn_add_n (pp+off, pp+off, np, n-off);
79   ASSERT_NOCARRY (mpn_add_1(pp+n, np+n-off, off, pp[n]) );
80 }
81