1 /* Cray PVP/IEEE mpn_mul_1 -- multiply a limb vector with a limb and store the
2    result in a second limb vector.
3 
4 Copyright 2000, 2001 Free Software Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10 
11   * the GNU Lesser General Public License as published by the Free
12     Software Foundation; either version 3 of the License, or (at your
13     option) any later version.
14 
15 or
16 
17   * the GNU General Public License as published by the Free Software
18     Foundation; either version 2 of the License, or (at your option) any
19     later version.
20 
21 or both in parallel, as here.
22 
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26 for more details.
27 
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library.  If not,
30 see https://www.gnu.org/licenses/.  */
31 
32 /* This code runs at 5 cycles/limb on a T90.  That would probably
33    be hard to improve upon, even with assembly code.  */
34 
35 #include <intrinsics.h>
36 #include "gmp-impl.h"
37 
38 mp_limb_t
mpn_mul_1(mp_ptr rp,mp_srcptr up,mp_size_t n,mp_limb_t vl)39 mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
40 {
41   mp_limb_t cy[n];
42   mp_limb_t a, b, r, s0, s1, c0, c1;
43   mp_size_t i;
44   int more_carries;
45 
46   if (up == rp)
47     {
48       /* The algorithm used below cannot handle overlap.  Handle it here by
49 	 making a temporary copy of the source vector, then call ourselves.  */
50       mp_limb_t xp[n];
51       MPN_COPY (xp, up, n);
52       return mpn_mul_1 (rp, xp, n, vl);
53     }
54 
55   a = up[0] * vl;
56   rp[0] = a;
57   cy[0] = 0;
58 
59   /* Main multiply loop.  Generate a raw accumulated output product in rp[]
60      and a carry vector in cy[].  */
61 #pragma _CRI ivdep
62   for (i = 1; i < n; i++)
63     {
64       a = up[i] * vl;
65       b = _int_mult_upper (up[i - 1], vl);
66       s0 = a + b;
67       c0 = ((a & b) | ((a | b) & ~s0)) >> 63;
68       rp[i] = s0;
69       cy[i] = c0;
70     }
71   /* Carry add loop.  Add the carry vector cy[] to the raw sum rp[] and
72      store the new sum back to rp[0].  */
73   more_carries = 0;
74 #pragma _CRI ivdep
75   for (i = 2; i < n; i++)
76     {
77       r = rp[i];
78       c0 = cy[i - 1];
79       s0 = r + c0;
80       rp[i] = s0;
81       c0 = (r & ~s0) >> 63;
82       more_carries += c0;
83     }
84   /* If that second loop generated carry, handle that in scalar loop.  */
85   if (more_carries)
86     {
87       mp_limb_t cyrec = 0;
88       /* Look for places where rp[k] is zero and cy[k-1] is non-zero.
89 	 These are where we got a recurrency carry.  */
90       for (i = 2; i < n; i++)
91 	{
92 	  r = rp[i];
93 	  c0 = (r == 0 && cy[i - 1] != 0);
94 	  s0 = r + cyrec;
95 	  rp[i] = s0;
96 	  c1 = (r & ~s0) >> 63;
97 	  cyrec = c0 | c1;
98 	}
99       return _int_mult_upper (up[n - 1], vl) + cyrec + cy[n - 1];
100     }
101 
102   return _int_mult_upper (up[n - 1], vl) + cy[n - 1];
103 }
104