Blame mpn/cray/add_n.c

Packit 5c3484
/* Cray PVP mpn_add_n -- add two limb vectors and store their sum in a third
Packit 5c3484
   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_add_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 add loop.  Generate a raw output sum in rp[] and a carry vector
Packit 5c3484
     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;
Packit 5c3484
      rp[i] = s0;
Packit 5c3484
      c0 = ((a & b) | ((a | b) & ~s0)) >> 63;
Packit 5c3484
      cy[i] = c0;
Packit 5c3484
    }
Packit 5c3484
  /* Carry add loop.  Add the carry vector cy[] to the raw sum rp[] and
Packit 5c3484
     store the new sum back to rp[0].  If this generates further carry, set
Packit 5c3484
     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;
Packit 5c3484
      rp[i] = s0;
Packit 5c3484
      c0 = (r & ~s0) >> 63;
Packit 5c3484
      more_carries += c0;
Packit 5c3484
    }
Packit 5c3484
  /* If that second loop generated carry, 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] is zero and cy[k-1] is non-zero.
Packit 5c3484
	 These are where we got a recurrency carry.  */
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 = (r & ~s0) >> 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
}