1 /* mpn_addmul_1 -- multiply the N long limb vector pointed to by UP by VL,
2    add the N least significant limbs of the product to the limb vector
3    pointed to by RP.  Return the most significant limb of the product,
4    adjusted for carry-out from the addition.
5 
6 Copyright 1992-1994, 1996, 2000, 2002, 2004 Free Software Foundation, Inc.
7 
8 This file is part of the GNU MP Library.
9 
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of either:
12 
13   * the GNU Lesser General Public License as published by the Free
14     Software Foundation; either version 3 of the License, or (at your
15     option) any later version.
16 
17 or
18 
19   * the GNU General Public License as published by the Free Software
20     Foundation; either version 2 of the License, or (at your option) any
21     later version.
22 
23 or both in parallel, as here.
24 
25 The GNU MP Library is distributed in the hope that it will be useful, but
26 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28 for more details.
29 
30 You should have received copies of the GNU General Public License and the
31 GNU Lesser General Public License along with the GNU MP Library.  If not,
32 see https://www.gnu.org/licenses/.  */
33 
34 #include "gmp.h"
35 #include "gmp-impl.h"
36 #include "longlong.h"
37 
38 
39 #if GMP_NAIL_BITS == 0
40 
41 mp_limb_t
mpn_addmul_1(mp_ptr rp,mp_srcptr up,mp_size_t n,mp_limb_t vl)42 mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
43 {
44   mp_limb_t ul, cl, hpl, lpl, rl;
45 
46   ASSERT (n >= 1);
47   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
48 
49   cl = 0;
50   do
51     {
52       ul = *up++;
53       umul_ppmm (hpl, lpl, ul, vl);
54 
55       lpl += cl;
56       cl = (lpl < cl) + hpl;
57 
58       rl = *rp;
59       lpl = rl + lpl;
60       cl += lpl < rl;
61       *rp++ = lpl;
62     }
63   while (--n != 0);
64 
65   return cl;
66 }
67 
68 #endif
69 
70 #if GMP_NAIL_BITS == 1
71 
72 mp_limb_t
mpn_addmul_1(mp_ptr rp,mp_srcptr up,mp_size_t n,mp_limb_t vl)73 mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
74 {
75   mp_limb_t shifted_vl, ul, rl, lpl, hpl, prev_hpl, cl, xl, c1, c2, c3;
76 
77   ASSERT (n >= 1);
78   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
79   ASSERT_MPN (rp, n);
80   ASSERT_MPN (up, n);
81   ASSERT_LIMB (vl);
82 
83   shifted_vl = vl << GMP_NAIL_BITS;
84   cl = 0;
85   prev_hpl = 0;
86   do
87     {
88       ul = *up++;
89       rl = *rp;
90       umul_ppmm (hpl, lpl, ul, shifted_vl);
91       lpl >>= GMP_NAIL_BITS;
92       ADDC_LIMB (c1, xl, prev_hpl, lpl);
93       ADDC_LIMB (c2, xl, xl, rl);
94       ADDC_LIMB (c3, xl, xl, cl);
95       cl = c1 + c2 + c3;
96       *rp++ = xl;
97       prev_hpl = hpl;
98     }
99   while (--n != 0);
100 
101   return prev_hpl + cl;
102 }
103 
104 #endif
105 
106 #if GMP_NAIL_BITS >= 2
107 
108 mp_limb_t
mpn_addmul_1(mp_ptr rp,mp_srcptr up,mp_size_t n,mp_limb_t vl)109 mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
110 {
111   mp_limb_t shifted_vl, ul, rl, lpl, hpl, prev_hpl, xw, cl, xl;
112 
113   ASSERT (n >= 1);
114   ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
115   ASSERT_MPN (rp, n);
116   ASSERT_MPN (up, n);
117   ASSERT_LIMB (vl);
118 
119   shifted_vl = vl << GMP_NAIL_BITS;
120   cl = 0;
121   prev_hpl = 0;
122   do
123     {
124       ul = *up++;
125       rl = *rp;
126       umul_ppmm (hpl, lpl, ul, shifted_vl);
127       lpl >>= GMP_NAIL_BITS;
128       xw = prev_hpl + lpl + rl + cl;
129       cl = xw >> GMP_NUMB_BITS;
130       xl = xw & GMP_NUMB_MASK;
131       *rp++ = xl;
132       prev_hpl = hpl;
133     }
134   while (--n != 0);
135 
136   return prev_hpl + cl;
137 }
138 
139 #endif
140