github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/cray/add_n.c (about)

     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 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 4 cycles/limb.  It may be possible to bring it down
    33     to 3 cycles/limb.  */
    34  
    35  #include "gmp.h"
    36  #include "gmp-impl.h"
    37  
    38  mp_limb_t
    39  mpn_add_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
    40  {
    41    mp_limb_t cy[n];
    42    mp_limb_t a, b, r, s0, c0, c1;
    43    mp_size_t i;
    44    int more_carries;
    45  
    46    /* Main add loop.  Generate a raw output sum in rp[] and a carry vector
    47       in cy[].  */
    48  #pragma _CRI ivdep
    49    for (i = 0; i < n; i++)
    50      {
    51        a = up[i];
    52        b = vp[i];
    53        s0 = a + b;
    54        rp[i] = s0;
    55        c0 = ((a & b) | ((a | b) & ~s0)) >> 63;
    56        cy[i] = c0;
    57      }
    58    /* Carry add loop.  Add the carry vector cy[] to the raw sum rp[] and
    59       store the new sum back to rp[0].  If this generates further carry, set
    60       more_carries.  */
    61    more_carries = 0;
    62  #pragma _CRI ivdep
    63    for (i = 1; i < n; i++)
    64      {
    65        r = rp[i];
    66        c0 = cy[i - 1];
    67        s0 = r + c0;
    68        rp[i] = s0;
    69        c0 = (r & ~s0) >> 63;
    70        more_carries += c0;
    71      }
    72    /* If that second loop generated carry, handle that in scalar loop.  */
    73    if (more_carries)
    74      {
    75        mp_limb_t cyrec = 0;
    76        /* Look for places where rp[k] is zero and cy[k-1] is non-zero.
    77  	 These are where we got a recurrency carry.  */
    78        for (i = 1; i < n; i++)
    79  	{
    80  	  r = rp[i];
    81  	  c0 = (r == 0 && cy[i - 1] != 0);
    82  	  s0 = r + cyrec;
    83  	  rp[i] = s0;
    84  	  c1 = (r & ~s0) >> 63;
    85  	  cyrec = c0 | c1;
    86  	}
    87        return cyrec | cy[n - 1];
    88      }
    89  
    90    return cy[n - 1];
    91  }