Blame mpn/cray/sub_n.c

Packit 5c3484
/* Cray PVP mpn_sub_n -- subtract two limb vectors and store their difference
Packit 5c3484
   in a third limb vector.
Packit 5c3484
Packit 5c3484
Copyright 1996, 2000, 2001 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
/* This code runs at 4 cycles/limb.  It may be possible to bring it down
Packit 5c3484
   to 3 cycles/limb.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
mp_limb_t
Packit 5c3484
mpn_sub_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
Packit 5c3484
{
Packit 5c3484
  mp_limb_t cy[n];
Packit 5c3484
  mp_limb_t a, b, r, s0, c0, c1;
Packit 5c3484
  mp_size_t i;
Packit 5c3484
  int more_carries;
Packit 5c3484
Packit 5c3484
  /* Main subtract loop.  Generate a raw output difference in rp[] and a
Packit 5c3484
     borrow vector in cy[].  */
Packit 5c3484
#pragma _CRI ivdep
Packit 5c3484
  for (i = 0; i < n; i++)
Packit 5c3484
    {
Packit 5c3484
      a = up[i];
Packit 5c3484
      b = vp[i];
Packit 5c3484
      s0 = a - b;		/* a = s0 + b */
Packit 5c3484
      rp[i] = s0;
Packit 5c3484
      c0 = ((s0 & b) | ((s0 | b) & ~a)) >> 63;
Packit 5c3484
      cy[i] = c0;
Packit 5c3484
    }
Packit 5c3484
  /* Borrow subtract loop.  Subtract the borrow vector cy[] from the raw
Packit 5c3484
     difference rp[] and store the new difference back to rp[0].  If this
Packit 5c3484
     generates further borrow, set more_carries.  */
Packit 5c3484
  more_carries = 0;
Packit 5c3484
#pragma _CRI ivdep
Packit 5c3484
  for (i = 1; i < n; i++)
Packit 5c3484
    {
Packit 5c3484
      r = rp[i];
Packit 5c3484
      c0 = cy[i - 1];
Packit 5c3484
      s0 = r - c0;		/* r = s0 + c0 */
Packit 5c3484
      rp[i] = s0;
Packit 5c3484
      c0 = (s0 & ~r) >> 63;
Packit 5c3484
      more_carries += c0;
Packit 5c3484
    }
Packit 5c3484
  /* If that second loop generated borrow, handle that in scalar loop.  */
Packit 5c3484
  if (more_carries)
Packit 5c3484
    {
Packit 5c3484
      mp_limb_t cyrec = 0;
Packit 5c3484
      /* Look for places where rp[k] contains just ones and cy[k-1] is
Packit 5c3484
	 non-zero.  These are where we got a recurrency borrow.  */
Packit 5c3484
      for (i = 1; i < n; i++)
Packit 5c3484
	{
Packit 5c3484
	  r = rp[i];
Packit 5c3484
	  c0 = (~r == 0 && cy[i - 1] != 0);
Packit 5c3484
	  s0 = r - cyrec;
Packit 5c3484
	  rp[i] = s0;
Packit 5c3484
	  c1 = (s0 & ~r) >> 63;
Packit 5c3484
	  cyrec = c0 | c1;
Packit 5c3484
	}
Packit 5c3484
      return cyrec | cy[n - 1];
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  return cy[n - 1];
Packit 5c3484
}