1 /* mpn_sub_err2_n -- sub_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 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 #include "gmp-impl.h"
38 
39 /*
40   Computes:
41 
42   (1) {rp,n} := {up,n} - {vp,n} (just like mpn_sub_n) with incoming borrow cy,
43   return value is borrow out.
44 
45   (2) Let c[i+1] = borrow from i-th limb subtraction (c[0] = cy).
46   Computes c[1]*yp1[n-1] + ... + c[n]*yp1[0],
47            c[1]*yp2[n-1] + ... + c[n]*yp2[0],
48   stores two-limb results at {ep,2} and {ep+2,2} respectively.
49 
50   Requires n >= 1.
51 
52   None of the outputs may overlap each other or any of the inputs, except
53   that {rp,n} may be equal to {up,n} or {vp,n}.
54 */
55 mp_limb_t
mpn_sub_err2_n(mp_ptr rp,mp_srcptr up,mp_srcptr vp,mp_ptr ep,mp_srcptr yp1,mp_srcptr yp2,mp_size_t n,mp_limb_t cy)56 mpn_sub_err2_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp,
57                 mp_ptr ep, mp_srcptr yp1, mp_srcptr yp2,
58                 mp_size_t n, mp_limb_t cy)
59 {
60   mp_limb_t el1, eh1, el2, eh2, ul, vl, yl1, yl2, zl1, zl2, rl, sl, cy1, cy2;
61 
62   ASSERT (n >= 1);
63   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
64   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
65   ASSERT (! MPN_OVERLAP_P (rp, n, yp1, n));
66   ASSERT (! MPN_OVERLAP_P (rp, n, yp2, n));
67   ASSERT (! MPN_OVERLAP_P (ep, 4, up, n));
68   ASSERT (! MPN_OVERLAP_P (ep, 4, vp, n));
69   ASSERT (! MPN_OVERLAP_P (ep, 4, yp1, n));
70   ASSERT (! MPN_OVERLAP_P (ep, 4, yp2, n));
71   ASSERT (! MPN_OVERLAP_P (ep, 4, rp, n));
72 
73   yp1 += n - 1;
74   yp2 += n - 1;
75   el1 = eh1 = 0;
76   el2 = eh2 = 0;
77 
78   do
79     {
80       yl1 = *yp1--;
81       yl2 = *yp2--;
82       ul = *up++;
83       vl = *vp++;
84 
85       /* ordinary sub_n */
86       SUBC_LIMB (cy1, sl, ul, vl);
87       SUBC_LIMB (cy2, rl, sl, cy);
88       cy = cy1 | cy2;
89       *rp++ = rl;
90 
91       /* update (eh1:el1) */
92       zl1 = (-cy) & yl1;
93       el1 += zl1;
94       eh1 += el1 < zl1;
95 
96       /* update (eh2:el2) */
97       zl2 = (-cy) & yl2;
98       el2 += zl2;
99       eh2 += el2 < zl2;
100     }
101   while (--n);
102 
103 #if GMP_NAIL_BITS != 0
104   eh1 = (eh1 << GMP_NAIL_BITS) + (el1 >> GMP_NUMB_BITS);
105   el1 &= GMP_NUMB_MASK;
106   eh2 = (eh2 << GMP_NAIL_BITS) + (el2 >> GMP_NUMB_BITS);
107   el2 &= GMP_NUMB_MASK;
108 #endif
109 
110   ep[0] = el1;
111   ep[1] = eh1;
112   ep[2] = el2;
113   ep[3] = eh2;
114 
115   return cy;
116 }
117