xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/cray/ieee/addmul_1.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Cray PVP/IEEE mpn_addmul_1 -- multiply a limb vector with a limb and add the
2    result to a second limb vector.
3 
4 Copyright 2000-2002 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 just under 9 cycles/limb on a T90.  That is not perfect,
33    mainly due to vector register shortage in the main loop.  Assembly code
34    should bring it down to perhaps 7 cycles/limb.  */
35 
36 #include <intrinsics.h>
37 #include "gmp-impl.h"
38 
39 mp_limb_t
mpn_addmul_1(mp_ptr rp,mp_srcptr up,mp_size_t n,mp_limb_t vl)40 mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
41 {
42   mp_limb_t cy[n];
43   mp_limb_t a, b, r, s0, s1, c0, c1;
44   mp_size_t i;
45   int more_carries;
46 
47   if (up == rp)
48     {
49       /* The algorithm used below cannot handle overlap.  Handle it here by
50 	 making a temporary copy of the source vector, then call ourselves.  */
51       mp_limb_t xp[n];
52       MPN_COPY (xp, up, n);
53       return mpn_addmul_1 (rp, xp, n, vl);
54     }
55 
56   a = up[0] * vl;
57   r = rp[0];
58   s0 = a + r;
59   rp[0] = s0;
60   c0 = ((a & r) | ((a | r) & ~s0)) >> 63;
61   cy[0] = c0;
62 
63   /* Main multiply loop.  Generate a raw accumulated output product in rp[]
64      and a carry vector in cy[].  */
65 #pragma _CRI ivdep
66   for (i = 1; i < n; i++)
67     {
68       a = up[i] * vl;
69       b = _int_mult_upper (up[i - 1], vl);
70       s0 = a + b;
71       c0 = ((a & b) | ((a | b) & ~s0)) >> 63;
72       r = rp[i];
73       s1 = s0 + r;
74       rp[i] = s1;
75       c1 = ((s0 & r) | ((s0 | r) & ~s1)) >> 63;
76       cy[i] = c0 + c1;
77     }
78   /* Carry add loop.  Add the carry vector cy[] to the raw result rp[] and
79      store the new result back to rp[].  */
80   more_carries = 0;
81 #pragma _CRI ivdep
82   for (i = 1; i < n; i++)
83     {
84       r = rp[i];
85       c0 = cy[i - 1];
86       s0 = r + c0;
87       rp[i] = s0;
88       c0 = (r & ~s0) >> 63;
89       more_carries += c0;
90     }
91   /* If that second loop generated carry, handle that in scalar loop.  */
92   if (more_carries)
93     {
94       mp_limb_t cyrec = 0;
95       /* Look for places where rp[k] == 0 and cy[k-1] == 1 or
96 	 rp[k] == 1 and cy[k-1] == 2.
97 	 These are where we got a recurrency carry.  */
98       for (i = 1; i < n; i++)
99 	{
100 	  r = rp[i];
101 	  c0 = r < cy[i - 1];
102 	  s0 = r + cyrec;
103 	  rp[i] = s0;
104 	  c1 = (r & ~s0) >> 63;
105 	  cyrec = c0 | c1;
106 	}
107       return _int_mult_upper (up[n - 1], vl) + cyrec + cy[n - 1];
108     }
109 
110   return _int_mult_upper (up[n - 1], vl) + cy[n - 1];
111 }
112