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