xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/add_err1_n.c (revision 230b95665bbd3a9d1a53658a36b1053f8382a519)
1 /* mpn_add_err1_n -- add_n with one error term
2 
3    Contributed by David Harvey.
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'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
8 
9 Copyright 2011 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 #include "gmp.h"
27 #include "gmp-impl.h"
28 
29 /*
30   Computes:
31 
32   (1) {rp,n} := {up,n} + {vp,n} (just like mpn_add_n) with incoming carry cy,
33   return value is carry out.
34 
35   (2) Let c[i+1] = carry from i-th limb addition (c[0] = cy).
36   Computes c[1]*yp[n-1] + ... + c[n]*yp[0], stores two-limb result at ep.
37 
38   Requires n >= 1.
39 
40   None of the outputs may overlap each other or any of the inputs, except
41   that {rp,n} may be equal to {up,n} or {vp,n}.
42 */
43 mp_limb_t
44 mpn_add_err1_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp,
45 		mp_ptr ep, mp_srcptr yp,
46                 mp_size_t n, mp_limb_t cy)
47 {
48   mp_limb_t el, eh, ul, vl, yl, zl, rl, sl, cy1, cy2;
49 
50   ASSERT (n >= 1);
51   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
52   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
53   ASSERT (! MPN_OVERLAP_P (rp, n, yp, n));
54   ASSERT (! MPN_OVERLAP_P (ep, 2, up, n));
55   ASSERT (! MPN_OVERLAP_P (ep, 2, vp, n));
56   ASSERT (! MPN_OVERLAP_P (ep, 2, yp, n));
57   ASSERT (! MPN_OVERLAP_P (ep, 2, rp, n));
58 
59   yp += n - 1;
60   el = eh = 0;
61 
62   do
63     {
64       yl = *yp--;
65       ul = *up++;
66       vl = *vp++;
67 
68       /* ordinary add_n */
69       ADDC_LIMB (cy1, sl, ul, vl);
70       ADDC_LIMB (cy2, rl, sl, cy);
71       cy = cy1 | cy2;
72       *rp++ = rl;
73 
74       /* update (eh:el) */
75       zl = (-cy) & yl;
76       el += zl;
77       eh += el < zl;
78     }
79   while (--n);
80 
81 #if GMP_NAIL_BITS != 0
82   eh = (eh << GMP_NAIL_BITS) + (el >> GMP_NUMB_BITS);
83   el &= GMP_NUMB_MASK;
84 #endif
85 
86   ep[0] = el;
87   ep[1] = eh;
88 
89   return cy;
90 }
91