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