(root)/
gmp-6.3.0/
mpn/
cray/
sub_n.c
       1  /* Cray PVP mpn_sub_n -- subtract two limb vectors and store their difference
       2     in a third 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-impl.h"
      36  
      37  mp_limb_t
      38  mpn_sub_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
      39  {
      40    mp_limb_t cy[n];
      41    mp_limb_t a, b, r, s0, c0, c1;
      42    mp_size_t i;
      43    int more_carries;
      44  
      45    /* Main subtract loop.  Generate a raw output difference in rp[] and a
      46       borrow vector in cy[].  */
      47  #pragma _CRI ivdep
      48    for (i = 0; i < n; i++)
      49      {
      50        a = up[i];
      51        b = vp[i];
      52        s0 = a - b;		/* a = s0 + b */
      53        rp[i] = s0;
      54        c0 = ((s0 & b) | ((s0 | b) & ~a)) >> 63;
      55        cy[i] = c0;
      56      }
      57    /* Borrow subtract loop.  Subtract the borrow vector cy[] from the raw
      58       difference rp[] and store the new difference back to rp[0].  If this
      59       generates further borrow, set more_carries.  */
      60    more_carries = 0;
      61  #pragma _CRI ivdep
      62    for (i = 1; i < n; i++)
      63      {
      64        r = rp[i];
      65        c0 = cy[i - 1];
      66        s0 = r - c0;		/* r = s0 + c0 */
      67        rp[i] = s0;
      68        c0 = (s0 & ~r) >> 63;
      69        more_carries += c0;
      70      }
      71    /* If that second loop generated borrow, handle that in scalar loop.  */
      72    if (more_carries)
      73      {
      74        mp_limb_t cyrec = 0;
      75        /* Look for places where rp[k] contains just ones and cy[k-1] is
      76  	 non-zero.  These are where we got a recurrency borrow.  */
      77        for (i = 1; i < n; i++)
      78  	{
      79  	  r = rp[i];
      80  	  c0 = (~r == 0 && cy[i - 1] != 0);
      81  	  s0 = r - cyrec;
      82  	  rp[i] = s0;
      83  	  c1 = (s0 & ~r) >> 63;
      84  	  cyrec = c0 | c1;
      85  	}
      86        return cyrec | cy[n - 1];
      87      }
      88  
      89    return cy[n - 1];
      90  }