beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / add_err3_n.c
blob28cd7facf9337b6acfb9ac5bbdc7d58549d26d06
1 /* mpn_add_err3_n -- add_n with three error terms
3 Contributed by David Harvey.
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.
9 Copyright 2011 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of either:
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.
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.
26 or both in parallel, as here.
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.
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/. */
37 #include "gmp.h"
38 #include "gmp-impl.h"
41 Computes:
43 (1) {rp,n} := {up,n} + {vp,n} (just like mpn_add_n) with incoming carry cy,
44 return value is carry out.
46 (2) Let c[i+1] = carry from i-th limb addition (c[0] = cy).
47 Computes c[1]*yp1[n-1] + ... + c[n]*yp1[0],
48 c[1]*yp2[n-1] + ... + c[n]*yp2[0],
49 c[1]*yp3[n-1] + ... + c[n]*yp3[0],
50 stores two-limb results at {ep,2}, {ep+2,2} and {ep+4,2} respectively.
52 Requires n >= 1.
54 None of the outputs may overlap each other or any of the inputs, except
55 that {rp,n} may be equal to {up,n} or {vp,n}.
57 mp_limb_t
58 mpn_add_err3_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp,
59 mp_ptr ep, mp_srcptr yp1, mp_srcptr yp2, mp_srcptr yp3,
60 mp_size_t n, mp_limb_t cy)
62 mp_limb_t el1, eh1, el2, eh2, el3, eh3, ul, vl, yl1, yl2, yl3, zl1, zl2, zl3, rl, sl, cy1, cy2;
64 ASSERT (n >= 1);
65 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
66 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
67 ASSERT (! MPN_OVERLAP_P (rp, n, yp1, n));
68 ASSERT (! MPN_OVERLAP_P (rp, n, yp2, n));
69 ASSERT (! MPN_OVERLAP_P (rp, n, yp3, n));
70 ASSERT (! MPN_OVERLAP_P (ep, 6, up, n));
71 ASSERT (! MPN_OVERLAP_P (ep, 6, vp, n));
72 ASSERT (! MPN_OVERLAP_P (ep, 6, yp1, n));
73 ASSERT (! MPN_OVERLAP_P (ep, 6, yp2, n));
74 ASSERT (! MPN_OVERLAP_P (ep, 6, yp3, n));
75 ASSERT (! MPN_OVERLAP_P (ep, 6, rp, n));
77 yp1 += n - 1;
78 yp2 += n - 1;
79 yp3 += n - 1;
80 el1 = eh1 = 0;
81 el2 = eh2 = 0;
82 el3 = eh3 = 0;
86 yl1 = *yp1--;
87 yl2 = *yp2--;
88 yl3 = *yp3--;
89 ul = *up++;
90 vl = *vp++;
92 /* ordinary add_n */
93 ADDC_LIMB (cy1, sl, ul, vl);
94 ADDC_LIMB (cy2, rl, sl, cy);
95 cy = cy1 | cy2;
96 *rp++ = rl;
98 /* update (eh1:el1) */
99 zl1 = (-cy) & yl1;
100 el1 += zl1;
101 eh1 += el1 < zl1;
103 /* update (eh2:el2) */
104 zl2 = (-cy) & yl2;
105 el2 += zl2;
106 eh2 += el2 < zl2;
108 /* update (eh3:el3) */
109 zl3 = (-cy) & yl3;
110 el3 += zl3;
111 eh3 += el3 < zl3;
113 while (--n);
115 #if GMP_NAIL_BITS != 0
116 eh1 = (eh1 << GMP_NAIL_BITS) + (el1 >> GMP_NUMB_BITS);
117 el1 &= GMP_NUMB_MASK;
118 eh2 = (eh2 << GMP_NAIL_BITS) + (el2 >> GMP_NUMB_BITS);
119 el2 &= GMP_NUMB_MASK;
120 eh3 = (eh3 << GMP_NAIL_BITS) + (el3 >> GMP_NUMB_BITS);
121 el3 &= GMP_NUMB_MASK;
122 #endif
124 ep[0] = el1;
125 ep[1] = eh1;
126 ep[2] = el2;
127 ep[3] = eh2;
128 ep[4] = el3;
129 ep[5] = eh3;
131 return cy;