xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/add_err2_n.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* mpn_add_err2_n -- add_n with two error terms
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]*yp1[n-1] + ... + c[n]*yp1[0],
37            c[1]*yp2[n-1] + ... + c[n]*yp2[0],
38   stores two-limb results at {ep,2} and {ep+2,2} respectively.
39 
40   Requires n >= 1.
41 
42   None of the outputs may overlap each other or any of the inputs, except
43   that {rp,n} may be equal to {up,n} or {vp,n}.
44 */
45 mp_limb_t
46 mpn_add_err2_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp,
47                 mp_ptr ep, mp_srcptr yp1, mp_srcptr yp2,
48                 mp_size_t n, mp_limb_t cy)
49 {
50   mp_limb_t el1, eh1, el2, eh2, ul, vl, yl1, yl2, zl1, zl2, rl, sl, cy1, cy2;
51 
52   ASSERT (n >= 1);
53   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
54   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
55   ASSERT (! MPN_OVERLAP_P (rp, n, yp1, n));
56   ASSERT (! MPN_OVERLAP_P (rp, n, yp2, n));
57   ASSERT (! MPN_OVERLAP_P (ep, 4, up, n));
58   ASSERT (! MPN_OVERLAP_P (ep, 4, vp, n));
59   ASSERT (! MPN_OVERLAP_P (ep, 4, yp1, n));
60   ASSERT (! MPN_OVERLAP_P (ep, 4, yp2, n));
61   ASSERT (! MPN_OVERLAP_P (ep, 4, rp, n));
62 
63   yp1 += n - 1;
64   yp2 += n - 1;
65   el1 = eh1 = 0;
66   el2 = eh2 = 0;
67 
68   do
69     {
70       yl1 = *yp1--;
71       yl2 = *yp2--;
72       ul = *up++;
73       vl = *vp++;
74 
75       /* ordinary add_n */
76       ADDC_LIMB (cy1, sl, ul, vl);
77       ADDC_LIMB (cy2, rl, sl, cy);
78       cy = cy1 | cy2;
79       *rp++ = rl;
80 
81       /* update (eh1:el1) */
82       zl1 = (-cy) & yl1;
83       el1 += zl1;
84       eh1 += el1 < zl1;
85 
86       /* update (eh2:el2) */
87       zl2 = (-cy) & yl2;
88       el2 += zl2;
89       eh2 += el2 < zl2;
90     }
91   while (--n);
92 
93 #if GMP_NAIL_BITS != 0
94   eh1 = (eh1 << GMP_NAIL_BITS) + (el1 >> GMP_NUMB_BITS);
95   el1 &= GMP_NUMB_MASK;
96   eh2 = (eh2 << GMP_NAIL_BITS) + (el2 >> GMP_NUMB_BITS);
97   el2 &= GMP_NUMB_MASK;
98 #endif
99 
100   ep[0] = el1;
101   ep[1] = eh1;
102   ep[2] = el2;
103   ep[3] = eh2;
104 
105   return cy;
106 }
107