Upload Tizen:Base source
[external/gmp.git] / mpn / cray / add_n.c
1 /* Cray PVP mpn_add_n -- add two limb vectors and store their sum in a third
2    limb vector.
3
4 Copyright 1996, 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 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 4 cycles/limb.  It may be possible to bring it down
22    to 3 cycles/limb.  */
23
24 #include "gmp.h"
25 #include "gmp-impl.h"
26
27 mp_limb_t
28 mpn_add_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
29 {
30   mp_limb_t cy[n];
31   mp_limb_t a, b, r, s0, c0, c1;
32   mp_size_t i;
33   int more_carries;
34
35   /* Main add loop.  Generate a raw output sum in rp[] and a carry vector
36      in cy[].  */
37 #pragma _CRI ivdep
38   for (i = 0; i < n; i++)
39     {
40       a = up[i];
41       b = vp[i];
42       s0 = a + b;
43       rp[i] = s0;
44       c0 = ((a & b) | ((a | b) & ~s0)) >> 63;
45       cy[i] = c0;
46     }
47   /* Carry add loop.  Add the carry vector cy[] to the raw sum rp[] and
48      store the new sum back to rp[0].  If this generates further carry, set
49      more_carries.  */
50   more_carries = 0;
51 #pragma _CRI ivdep
52   for (i = 1; i < n; i++)
53     {
54       r = rp[i];
55       c0 = cy[i - 1];
56       s0 = r + c0;
57       rp[i] = s0;
58       c0 = (r & ~s0) >> 63;
59       more_carries += c0;
60     }
61   /* If that second loop generated carry, handle that in scalar loop.  */
62   if (more_carries)
63     {
64       mp_limb_t cyrec = 0;
65       /* Look for places where rp[k] is zero and cy[k-1] is non-zero.
66          These are where we got a recurrency carry.  */
67       for (i = 1; i < n; i++)
68         {
69           r = rp[i];
70           c0 = (r == 0 && cy[i - 1] != 0);
71           s0 = r + cyrec;
72           rp[i] = s0;
73           c1 = (r & ~s0) >> 63;
74           cyrec = c0 | c1;
75         }
76       return cyrec | cy[n - 1];
77     }
78
79   return cy[n - 1];
80 }